1
1
/*
2
- * angular-http-batcher - v1.11.3 - 2015-08-27
2
+ * angular-http-batcher - v1.12.0 - 2015-10-12
3
3
* https://github.com/jonsamwell/angular-http-batcher
4
4
* Copyright (c) 2015 Jon Samwell
5
5
*/
@@ -29,7 +29,8 @@ function HttpBatchConfigFn() {
29
29
ignoredVerbs : [ 'head' ] ,
30
30
sendCookies : false ,
31
31
enabled : true ,
32
- adapter : defaultBatchAdapter
32
+ adapter : defaultBatchAdapter ,
33
+ uniqueRequestName : null
33
34
} ;
34
35
35
36
/**
@@ -203,7 +204,9 @@ function HttpBatchAdapter($document, $window, httpBatchConfig) {
203
204
singleSpace : ' ' ,
204
205
forwardSlash : '/' ,
205
206
doubleDash : '--' ,
206
- colon : ':'
207
+ colon : ':' ,
208
+ semiColon : ';' ,
209
+ requestName : 'name='
207
210
} ;
208
211
209
212
self . key = 'httpBatchAdapter' ;
@@ -231,7 +234,7 @@ function HttpBatchAdapter($document, $window, httpBatchConfig) {
231
234
headers : config . batchRequestHeaders || { }
232
235
} ,
233
236
batchBody = [ ] ,
234
- urlInfo , i , request , header ;
237
+ urlInfo , i , request , header , relativeUrlParts , encodedRelativeUrl ;
235
238
236
239
httpConfig . headers [ constants . contentType ] = 'multipart/mixed; boundary=' + boundary ;
237
240
@@ -242,13 +245,24 @@ function HttpBatchAdapter($document, $window, httpBatchConfig) {
242
245
batchBody . push ( constants . doubleDash + boundary ) ;
243
246
if ( config . batchPartRequestHeaders ) {
244
247
for ( header in config . batchPartRequestHeaders ) {
245
- batchBody . push ( header + constants . colon + constants . singleSpace + config . batchPartRequestHeaders [ header ] ) ;
248
+ if ( config . batchPartRequestHeaders . hasOwnProperty ( header ) ) {
249
+ var currHeader = header + constants . colon + constants . singleSpace + config . batchPartRequestHeaders [ header ] ;
250
+ if ( header . toLowerCase ( ) === "content-disposition" && config . uniqueRequestName !== null && config . uniqueRequestName !== undefined ) {
251
+ currHeader += constants . semiColon + constants . singleSpace + constants . requestName + config . uniqueRequestName + i ;
252
+ }
253
+ batchBody . push ( currHeader ) ;
254
+ }
246
255
}
247
256
}
248
257
249
258
batchBody . push ( 'Content-Type: application/http; msgtype=request' , constants . emptyString ) ;
250
259
251
- batchBody . push ( request . method + ' ' + encodeURI ( urlInfo . relativeUrl ) + ' ' + constants . httpVersion ) ;
260
+ // angular would have already encoded the parameters *if* the dev passed them in via the params parameter to $http
261
+ // so we only need to url encode the url not the query string part
262
+ relativeUrlParts = urlInfo . relativeUrl . split ( '?' ) ;
263
+ encodedRelativeUrl = encodeURI ( relativeUrlParts [ 0 ] ) + ( relativeUrlParts . length > 1 ? '?' + relativeUrlParts [ 1 ] : '' ) ;
264
+
265
+ batchBody . push ( request . method + ' ' + encodedRelativeUrl + ' ' + constants . httpVersion ) ;
252
266
batchBody . push ( 'Host: ' + urlInfo . host ) ;
253
267
254
268
for ( header in request . headers ) {
@@ -365,18 +379,30 @@ function HttpBatchAdapter($document, $window, httpBatchConfig) {
365
379
function findResponseBoundary ( contentType ) {
366
380
var boundaryText = 'boundary=' ,
367
381
startIndex = contentType . indexOf ( boundaryText ) ,
368
- boundary = contentType . substring ( startIndex + boundaryText . length ) ;
382
+ endIndex = contentType . indexOf ( ';' , startIndex ) ,
383
+ boundary = contentType . substring ( startIndex + boundaryText . length , endIndex > 0 ? endIndex : contentType . length ) ;
369
384
370
385
// the boundary might be quoted so remove the quotes
371
386
boundary = boundary . replace ( / " / g, constants . emptyString ) ;
372
387
return boundary ;
373
388
}
374
389
390
+ /**
391
+ * see https://docs.angularjs.org/api/ng/service/$http#json-vulnerability-protection
392
+ * @param data
393
+ * @returns {*|void|string }
394
+ */
395
+ function trimJsonProtectionVulnerability ( data ) {
396
+ return typeof ( data ) === 'string' ? data . replace ( ')]}\',\n' , '' ) : data ;
397
+ }
398
+
375
399
function convertDataToCorrectType ( contentType , dataStr ) {
376
400
var data = dataStr ;
377
401
contentType = contentType . toLowerCase ( ) ;
378
402
379
403
if ( contentType . indexOf ( 'json' ) > - 1 ) {
404
+ // only remove json vulnerability prefix if we're parsing json
405
+ dataStr = trimJsonProtectionVulnerability ( dataStr ) ;
380
406
data = angular . fromJson ( dataStr ) ;
381
407
}
382
408
@@ -559,27 +585,14 @@ function addRequestFn(request) {
559
585
return true ;
560
586
}
561
587
562
- /**
563
- * see https://docs.angularjs.org/api/ng/service/$http#json-vulnerability-protection
564
- * @param data
565
- * @returns {*|void|string }
566
- */
567
- function trimJsonProtectionVulnerability ( data ) {
568
- return typeof ( data ) === 'string' ? data . replace ( ')]}\',\n' , '' ) : data ;
569
- }
570
-
571
588
function sendFn ( ) {
572
589
var self = this ,
573
590
adapter = self . getAdapter ( ) ,
574
591
httpBatchConfig = adapter . buildRequest ( self . requests , self . config ) ;
575
592
576
593
self . sendCallback ( ) ;
577
594
self . $injector . get ( '$http' ) ( httpBatchConfig ) . then ( function ( response ) {
578
- var batchResponses ;
579
-
580
- response . data = trimJsonProtectionVulnerability ( response . data ) ;
581
-
582
- batchResponses = adapter . parseResponse ( self . requests , response , self . config ) ;
595
+ var batchResponses = adapter . parseResponse ( self . requests , response , self . config ) ;
583
596
584
597
angular . forEach ( batchResponses , function ( batchResponse ) {
585
598
batchResponse . request . callback (
0 commit comments