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

fix($http): properly access request headers with mixed case #10883

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,24 @@ function isJsonLike(str) {
* @returns {Object} Parsed headers as key value object
*/
function parseHeaders(headers) {
var parsed = createMap(), key, val, i;

if (!headers) return parsed;

forEach(headers.split('\n'), function(line) {
i = line.indexOf(':');
key = lowercase(trim(line.substr(0, i)));
val = trim(line.substr(i + 1));
var parsed = createMap(), i;

function fillInParsed(key, val) {
if (key) {
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
}
});
}

if (isString(headers)) {
forEach(headers.split('\n'), function(line) {
i = line.indexOf(':');
fillInParsed(lowercase(trim(line.substr(0, i))), trim(line.substr(i + 1)));
});
} else if (isObject(headers)) {
forEach(headers, function(headerVal, headerKey) {
fillInParsed(lowercase(headerKey), trim(headerVal));
});
}

return parsed;
}
Expand All @@ -68,7 +73,7 @@ function parseHeaders(headers) {
* - if called with no arguments returns an object containing all headers.
*/
function headersGetter(headers) {
var headersObj = isObject(headers) ? headers : undefined;
var headersObj;

return function(name) {
if (!headersObj) headersObj = parseHeaders(headers);
Expand Down
12 changes: 12 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,18 @@ describe('$http', function() {
expect(callback).toHaveBeenCalledOnce();
});

it('should have access to request headers with mixed case', function() {
$httpBackend.expect('POST', '/url', 'header1').respond(200);
$http.post('/url', 'req', {
headers: {H1: 'header1'},
transformRequest: function(data, headers) {
return headers('H1');
}
}).success(callback);
$httpBackend.flush();

expect(callback).toHaveBeenCalledOnce();
});

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