Skip to content

Commit

Permalink
Added support for the Angular Json protection vulnerability when pars…
Browse files Browse the repository at this point in the history
…ing json (see https://docs.angularjs.org/api/ng/service/$http#json-vulnerability-protection)thanks to @riann (https://github.com/riaann) for this!!Added documentation for the new 'adapter' config property and tests around the node js multifetch adapter.
  • Loading branch information
jonsamwell committed Aug 11, 2015
1 parent d6b22a5 commit 1c824b6
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 16 deletions.
68 changes: 63 additions & 5 deletions README.md
Expand Up @@ -2,10 +2,11 @@ Angular Http Batcher - enabling transparent HTTP batch request with AngularJS
====================

The biggest performance boost you will get with modern single page style apps is to reduce the number of HTTP request you
send. This module has been designed to batch http requests to the same endpoint following the http 1.1 batch spec. All
you need to do is configure the batch endpoint with the library and the rest is taken care of.
send. This module has been designed to batch http requests to the same endpoint following the http 1.1 batch spec and after the
1.11.0 update it can now support multiple any number of batch formats and I'm planning to implement that Facebook batch protocol
very soon. All you need to do is configure the batch endpoint with the library and the rest is taken care of!

See my original blog blog for a detailed overview - http://jonsamwell.com/batching-http-requests-in-angular/
See my original blog post for a detailed overview - http://jonsamwell.com/batching-http-requests-in-angular/

<h3 id="angular-http-batcher-getting-started">Getting Started</h3>

Expand Down Expand Up @@ -51,7 +52,7 @@ angular.module('myApp', ['jcs.angular-http-batch']);

The root endpoint url is simply the base address of your api and the endpoint batch address is the url of the method that can accept the batch request (usually just /batch or /$batch). You are able to pass some optional configuration paramaters to this call in the third argument (see below)

The setAllowedBatchEndpoint has some options that can be passed in as a third paramter to the call which are explained below.
The setAllowedBatchEndpoint has some options that can be passed in as a third parameter to the call which are explained below.

```language-javascript
{
Expand All @@ -60,10 +61,67 @@ The setAllowedBatchEndpoint has some options that can be passed in as a third pa
batchRequestCollectionDelay: 100,
ignoredVerbs: ['head'],
sendCookies: false,
enabled: true
enabled: true,
adapter: 'httpBatchAdapter' //defaults to this value we currently also support a node js multifetch format as well
}
```

####adapter
The key for the adapter to use. Defaults to the HTTP 1.1 adapter 'httpBatchAdapter'.
Current adapters are:
'httpBatchAdapter' - supports the HTTP 1.1 spec and used by .Net (WebAPI) and JAVA servers.
'nodeJsMultiFetchAdapter' - supports batching GET requests to a node server that uses the multifetch library.
Coming soon:
'facebookAdapter' - will support the facebook batching protocol.

Please request adapters that are not present.

Adapters convert http requests into a single batch request and parse the batch response. They consist of two methods defined below.

This adapter parameter can also be an object with the two below functions if you need to be more specific about the way
requests and responses are handled.

```javascript
/**
* Builds the single batch request from the given batch of pending requests.
* Returns a standard angular httpConfig object that will be use to invoke the $http service.
* See:
* https://developers.google.com/storage/docs/json_api/v1/how-tos/batch
* http://blogs.msdn.com/b/webdev/archive/2013/11/01/introducing-batch-support-in-web-api-and-web-api-odata.aspx
*
* @param requests - the collection of pending http request to build into a single http batch request.
* @param config - the http batch config.
* @returns {object} - a http config object.
*/
function buildRequestFn(requests, config) {
var httpConfig = {
method: 'POST',
url: config.batchEndpointUrl,
cache: false,
headers: config.batchRequestHeaders || {}
};

// do processing...

return httpConfig;
}

/**
* Parses the raw response into an array of HttpBatchResponseData objects. If is this methods job
* to parse the response and match it up with the orginal request object.
* @param rawResponse
* @param config
* @returns {Array.HttpBatchResponseData[]}
*/
function parseResponseFn(requests, rawResponse, config) {
var batchResponses = []; // array of HttpBatchResponseData

//do processing..

return batchResponses;
}
```

####maxBatchedRequestPerCall
The maximum number of single http request that are allow to be sent in one http batch request. If this limit is reached the call will be split up into multiple batch requests. This option defaults to 10 request per batch but it is probably worth playing around with this number to see the optimal batch size between total request size and response speed.

Expand Down
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "angular-http-batcher",
"version": "1.11.0",
"version": "1.11.1",
"description": "Enables transparent HTTP batch requests with Angular",
"main": "dist/angular-http-batch.js",
"keywords": [
Expand Down
5 changes: 5 additions & 0 deletions dist/ChangeLog.txt
@@ -1,3 +1,8 @@
11/08/2015 V1.11.1
Added support for the Angular Json protection vulnerability when parsing json (see https://docs.angularjs.org/api/ng/service/$http#json-vulnerability-protection)
thanks to @riann (https://github.com/riaann) for this!!
Added documentation for the new 'adapter' config property and tests around the node js multifetch adapter.

10/08/2015 V1.11.0
HUGE refactor of the library geared towards supporting multiple different formats of batch request and response i.e.
http 1.1 batch, NodeJS, Facebook etc. The library now has the concept of batch adapters which are able to transform raw
Expand Down
17 changes: 15 additions & 2 deletions dist/angular-http-batch.js
@@ -1,5 +1,5 @@
/*
* angular-http-batcher - v1.11.0 - 2015-08-10
* angular-http-batcher - v1.11.1 - 2015-08-11
* https://github.com/jonsamwell/angular-http-batcher
* Copyright (c) 2015 Jon Samwell
*/
Expand Down Expand Up @@ -210,14 +210,27 @@ function addRequestFn(request) {
return true;
}

/**
* see https://docs.angularjs.org/api/ng/service/$http#json-vulnerability-protection
* @param data
* @returns {*|void|string}
*/
function trimJsonProtectionVulnerability(data) {
return data !== undefined ? data.replace(')]}\',\n', '') : data;
}

function sendFn() {
var self = this,
adapter = self.getAdapter(),
httpBatchConfig = adapter.buildRequest(self.requests, self.config);

self.sendCallback();
self.$injector.get('$http')(httpBatchConfig).then(function (response) {
var batchResponses = adapter.parseResponse(self.requests, response, self.config);
var batchResponses;

response.data = trimJsonProtectionVulnerability(response.data);

batchResponses = adapter.parseResponse(self.requests, response, self.config);

angular.forEach(batchResponses, function (batchResponse) {
batchResponse.request.callback(
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
@@ -1,6 +1,6 @@
{
"name": "angular-http-batcher",
"version": "1.11.0",
"version": "1.11.1",
"description": "Enables transparent HTTP batch requests with Angular",
"main": "angular-http-batcher.min.js",
"scripts": {
Expand Down
6 changes: 2 additions & 4 deletions src/services/adapters/nodeJsMultiFetchAdapter.js
Expand Up @@ -50,10 +50,9 @@ function NodeJsMultiFetchAdapter() {
* Parses the raw response into an array of HttpBatchResponseData objects. If is this methods job
* to parse the response and match it up with the orginal request object.
* @param rawResponse
* @param config
* @returns {Array.HttpBatchResponseData[]}
*/
function parseResponseFn(requests, rawResponse, config) {
function parseResponseFn(requests, rawResponse) {
var batchResponses = [],
i, request,
responseData = rawResponse.data,
Expand All @@ -77,10 +76,9 @@ function NodeJsMultiFetchAdapter() {
/**
* Gaurd method to ensure the adapter supports this given request.
* @param request
* @param config
* @returns {boolean} false to indicate the request type is not supported.
*/
function canBatchRequestFn(request, config) {
function canBatchRequestFn(request) {
return request.method === 'GET';
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/services/httpBatcher.js
Expand Up @@ -34,14 +34,27 @@ function addRequestFn(request) {
return true;
}

/**
* see https://docs.angularjs.org/api/ng/service/$http#json-vulnerability-protection
* @param data
* @returns {*|void|string}
*/
function trimJsonProtectionVulnerability(data) {
return data !== undefined ? data.replace(')]}\',\n', '') : data;
}

function sendFn() {
var self = this,
adapter = self.getAdapter(),
httpBatchConfig = adapter.buildRequest(self.requests, self.config);

self.sendCallback();
self.$injector.get('$http')(httpBatchConfig).then(function (response) {
var batchResponses = adapter.parseResponse(self.requests, response, self.config);
var batchResponses;

response.data = trimJsonProtectionVulnerability(response.data);

batchResponses = adapter.parseResponse(self.requests, response, self.config);

angular.forEach(batchResponses, function (batchResponse) {
batchResponse.request.callback(
Expand Down

0 comments on commit 1c824b6

Please sign in to comment.