Skip to content

Commit

Permalink
Fixed - Number of items in a request is one larger than maxBatchedReq…
Browse files Browse the repository at this point in the history
…uestPerCallAdded the ability to send non-HTTPOnly cookies in each request segment in a batch request via the sendCookies flag on theconfig object.The batcher can now be disabled by setting the enabled config flag to false
  • Loading branch information
jonsamwell committed May 28, 2015
1 parent 5538a6e commit b0f21f8
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 34 deletions.
46 changes: 44 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ The setAllowedBatchEndpoint has some options that can be passed in as a third pa
maxBatchedRequestPerCall: 10,
minimumBatchSize: 2,
batchRequestCollectionDelay: 100,
ignoredVerbs: ['head']
ignoredVerbs: ['head'],
sendCookies: false,
enabled: true
}
```

Expand All @@ -71,8 +73,48 @@ The smallest number of individual calls allowed in a batch request. This has a
####ignoredVerbs
This is a string array of the HTTP verbs that are **not** allowed to form part of a batch request. By default HEAD requests will not be batched. If for instance you did not want to batch HEAD and DELETE calls you would pass in this array as an option <code>['head', 'delete']</code>

####enabled
True by default. If this is set to false the batcher will ignore all request and them will be send as normal single HTTP requests.

####sendCookies
False by default to reduce requesst size. If this is set to true cookies availiable on the document.cookie property will be set
in each segment of a batch request. Note that only non HTTPOnly cookie will be sent as HTTPOnly cookies cannot be access by JavaScript
because of security limitations.

Note that if you are sending CORS request you will have to enable withCredentials on $http to allow cookies to be sent on the XHR request.

```language-javascript
angular.module('myApp').config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.withCredentials = true;
}]);
```

Also ensure the server responses to the OPTIONS call with the below header:

```language-csharp
Access-Control-Allow-Credentials: true
[EnableCors("*", "*", "*", SupportsCredentials=true)]
or
config.EnableCors();
var defaultPolicyProvider = new EnableCorsAttribute("*", "*", "*");
defaultPolicyProvider.SupportsCredentials = true; //important if you are sending cookies
AttributeBasedPolicyProviderFactory policyProviderFactory = new AttributeBasedPolicyProviderFactory();
policyProviderFactory.DefaultPolicyProvider = defaultPolicyProvider;
config.SetCorsPolicyProviderFactory(policyProviderFactory);
config.Routes.MapHttpRoute(
name: "BatchApi",
routeTemplate: "api/batch",
defaults: null,
constraints: null,
handler: new CorsMessageHandler(config) { InnerHandler = new DefaultHttpBatchHandler(GlobalConfiguration.DefaultServer) });
```

####batchRequestCollectionDelay
This is undoubtaly the most important option. As this module tries to be as transparent as possible to the user.
This is undoubtedly the most important option. As this module tries to be as transparent as possible to the user.

The default time in milliseconds the http batcher should wait to collection all request to this domain after the first http call that can be batched has been collect. This defaults to 100ms. Therefore if you send a HTTP GET call that can be batched the HTTP batcher will receive this call and wait a further 100ms before sending the call in order to wait for other calls to the same domain in order to add them to the current batch request. If no other calls are collected the initial HTTP call will be allowed to continue as normal and will not be batched unless the config property - **minimumBatchSize** is set to one.

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-http-batcher",
"version": "1.7.0",
"version": "1.8.0",
"description": "Enables transparent HTTP batch requests with Angular",
"main": "dist/angular-http-batch.js",
"keywords": [
Expand Down
8 changes: 7 additions & 1 deletion dist/ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
29/05/2015 V1.8.0
Fixed - Number of items in a request is one larger than maxBatchedRequestPerCall
Added the ability to send non-HTTPOnly cookies in each request segment in a batch request via the sendCookies flag on the
config object.
The batcher can now be disabled by setting the enabled config flag to false

16/03/2015 V1.7.0
Fixed issue with parsing the response string into a json object with having more than two dashes '--' in the returned data.

Expand All @@ -23,4 +29,4 @@ be a string.

27/08/2014 V1.1.0
Fixed bug when a request hit the batch collection timeout and the request have not reached the min batch size the pending
request is not moved which forces other request to have sending delays.
request is not moved which forces other request to have sending delays.
22 changes: 17 additions & 5 deletions dist/angular-http-batch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* angular-http-batcher - v1.7.0 - 2015-03-18
* angular-http-batcher - v1.8.0 - 2015-05-29
* https://github.com/jonsamwell/angular-http-batcher
* Copyright (c) 2015 Jon Samwell
*/
Expand Down Expand Up @@ -29,7 +29,9 @@ angular.module(window.ahb.name).provider('httpBatchConfig', [
maxBatchedRequestPerCall: 10,
minimumBatchSize: 2,
batchRequestCollectionDelay: 100,
ignoredVerbs: ['head']
ignoredVerbs: ['head'],
sendCookies: false,
enabled: true
};

/**
Expand Down Expand Up @@ -63,6 +65,11 @@ angular.module(window.ahb.name).provider('httpBatchConfig', [
* request. If no other calls are collected the initial HTTP call will be allowed to continue as normal and will
* not be batched unless the config property - **minimumBatchSize** is set to one.
* - **ignoredVerbs** - The HTTP verbs that are ignored and not included in a batch request. By default only HEAD request are ignored.
* - **sendCookies** - True indicates that cookies will be send within each request segment in the batch request. Note
* only non HTTPOnly cookies can be sent as Javascript cannot access HTTPOnly cookies for security reasons. This
* property is false by default to reduce request size.
* - **enabled** True indicates batching is enabled. The default is true. If the property is false the batcher will
* send request down the normal $http pipeline and request will not be batched.
*/
this.setAllowedBatchEndpoint = function (serviceUrl, batchEndpointUrl, config) {
var mergedConfiguration = angular.copy(defaultConfiguration);
Expand Down Expand Up @@ -125,6 +132,7 @@ angular.module(window.ahb.name).provider('httpBatchConfig', [
this.canBatchCall = function (url, method) {
var config = this.getBatchConfig(url);
return config !== undefined &&
config.enabled === true &&
config.batchEndpointUrl !== url &&
config.ignoredVerbs.indexOf(method.toLowerCase()) === -1;
};
Expand All @@ -142,7 +150,6 @@ angular.module(window.ahb.name).provider('httpBatchConfig', [
};

this.$get = [

function () {
return this;
}
Expand All @@ -152,10 +159,11 @@ angular.module(window.ahb.name).provider('httpBatchConfig', [

angular.module(window.ahb.name).factory('httpBatcher', [
'$injector',
'$document',
'$window',
'$timeout',
'httpBatchConfig',
function ($injector, $window, $timeout, httpBatchConfig) {
function ($injector, $document, $window, $timeout, httpBatchConfig) {
'use strict';

var constants = {
Expand Down Expand Up @@ -344,6 +352,10 @@ angular.module(window.ahb.name).factory('httpBatcher', [
batchBody.push(header + ': ' + request.headers[header]);
}

if (config.sendCookies === true && $document[0].cookie && $document[0].cookie.length > 0) {
batchBody.push('Cookie: ' + $document[0].cookie);
}

batchBody.push(constants.emptyString);

if (request.data) {
Expand Down Expand Up @@ -387,7 +399,7 @@ angular.module(window.ahb.name).factory('httpBatcher', [
addRequest = function (request) {
this.requests.push(request);

if (this.requests.length > this.config.maxBatchedRequestPerCall) {
if (this.requests.length >= this.config.maxBatchedRequestPerCall) {
this.flush();
}

Expand Down
4 changes: 2 additions & 2 deletions dist/angular-http-batch.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-http-batcher",
"version": "1.7.0",
"version": "1.8.0",
"description": "Enables transparent HTTP batch requests with Angular",
"main": "angular-http-batcher.min.js",
"scripts": {
Expand Down
Binary file modified servers/WebApi2/WebApiHttpBatchServer.v12.suo
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//config.EnableCors();
config.EnableCors();
// Web API routes
config.EnableSystemDiagnosticsTracing().IsVerbose = true;
config.MapHttpAttributeRoutes();

var defaultPolicyProvider = new EnableCorsAttribute("*", "*", "*");
defaultPolicyProvider.SupportsCredentials = true; //important if you are sending cookies
AttributeBasedPolicyProviderFactory policyProviderFactory = new AttributeBasedPolicyProviderFactory();
policyProviderFactory.DefaultPolicyProvider = defaultPolicyProvider;
config.SetCorsPolicyProviderFactory(policyProviderFactory);
Expand Down
Loading

0 comments on commit b0f21f8

Please sign in to comment.