Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 8b6f8a0

Browse files
fix(): properly access request headers with mixed case
Fixes #10881
1 parent 507ee2d commit 8b6f8a0

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/ng/http.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,24 @@ function isJsonLike(str) {
3737
* @returns {Object} Parsed headers as key value object
3838
*/
3939
function parseHeaders(headers) {
40-
var parsed = createMap(), key, val, i;
41-
42-
if (!headers) return parsed;
43-
44-
forEach(headers.split('\n'), function(line) {
45-
i = line.indexOf(':');
46-
key = lowercase(trim(line.substr(0, i)));
47-
val = trim(line.substr(i + 1));
40+
var parsed = createMap(), i;
4841

42+
function fillInParsed(key, val) {
4943
if (key) {
5044
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
5145
}
52-
});
46+
}
47+
48+
if (isString(headers)) {
49+
forEach(headers.split('\n'), function(line) {
50+
i = line.indexOf(':');
51+
fillInParsed(lowercase(trim(line.substr(0, i))), trim(line.substr(i + 1)));
52+
});
53+
} else if (isObject(headers)) {
54+
forEach(headers, function(headerVal, headerKey) {
55+
fillInParsed(lowercase(headerKey), trim(headerVal));
56+
});
57+
}
5358

5459
return parsed;
5560
}
@@ -68,7 +73,7 @@ function parseHeaders(headers) {
6873
* - if called with no arguments returns an object containing all headers.
6974
*/
7075
function headersGetter(headers) {
71-
var headersObj = isObject(headers) ? headers : undefined;
76+
var headersObj;
7277

7378
return function(name) {
7479
if (!headersObj) headersObj = parseHeaders(headers);

test/ng/httpSpec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,18 @@ describe('$http', function() {
10381038
expect(callback).toHaveBeenCalledOnce();
10391039
});
10401040

1041+
it('should have access to request headers with mixed case', function() {
1042+
$httpBackend.expect('POST', '/url', 'header1').respond(200);
1043+
$http.post('/url', 'req', {
1044+
headers: {H1: 'header1'},
1045+
transformRequest: function(data, headers) {
1046+
return headers('H1');
1047+
}
1048+
}).success(callback);
1049+
$httpBackend.flush();
1050+
1051+
expect(callback).toHaveBeenCalledOnce();
1052+
});
10411053

10421054
it('should pipeline more functions', function() {
10431055
function first(d, h) {return d + '-first' + ':' + h('h1');}

0 commit comments

Comments
 (0)