Skip to content

Commit

Permalink
Maintain request header casing.
Browse files Browse the repository at this point in the history
While headers are meant to be case sensitive, many servers do not
respect this.  Node maintains the casing of the request headers in order
to work with this and http-browserify should do likewise in order to be
consistent with the node implementation.

This patch maintains the casing with which the header was last set, e.g.
setting header "fOo", then header "Foo" will still only send one header,
but will use the final casing "Foo".  My understanding is that this
matches the node implementation (as described in
https://groups.google.com/forum/#!topic/nodejs/XIU55IYtfJo).
  • Loading branch information
Liam O'Boyle committed Mar 22, 2016
1 parent 17b2990 commit 59bf2a7
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var Request = module.exports = function (xhr, params) {
};

self._headers = {};
self._headerNames = {};

if (params.headers) {
var keys = objectKeys(params.headers);
Expand Down Expand Up @@ -78,6 +79,7 @@ inherits(Request, Stream);

Request.prototype.setHeader = function (key, value) {
this._headers[key.toLowerCase()] = value
this._headerNames[key.toLowerCase()] = key;
};

Request.prototype.getHeader = function (key) {
Expand All @@ -86,6 +88,7 @@ Request.prototype.getHeader = function (key) {

Request.prototype.removeHeader = function (key) {
delete this._headers[key.toLowerCase()]
delete this._headerNames[key.toLowerCase()]
};

Request.prototype.write = function (s) {
Expand All @@ -105,12 +108,13 @@ Request.prototype.end = function (s) {
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = this._headers[key];
var name = this._headerNames[key];
if (isArray(value)) {
for (var j = 0; j < value.length; j++) {
this.xhr.setRequestHeader(key, value[j]);
this.xhr.setRequestHeader(name, value[j]);
}
}
else this.xhr.setRequestHeader(key, value)
else this.xhr.setRequestHeader(name, value)
}

if (this.body.length === 0) {
Expand Down

0 comments on commit 59bf2a7

Please sign in to comment.