Skip to content
Permalink
Browse files

Keep original letter case of response header keys (#1098)

* Keep original letter case of response header keys

* Support node older than v0.11.6

messege.rawHeaders was added in v0.11.6

* Extract duplicated logic to method
  • Loading branch information...
pachirel authored and jcrugzz committed Dec 2, 2016
1 parent 69cf892 commit 4edbb62cc5881b20e7dae5e6240e693b03fd3609
@@ -83,19 +83,34 @@ module.exports = { // <--
* @api private
*/
writeHeaders: function writeHeaders(req, res, proxyRes, options) {
var rewriteCookieDomainConfig = options.cookieDomainRewrite;
var rewriteCookieDomainConfig = options.cookieDomainRewrite,
setHeader = function(key, header) {
if (header != undefined) {
if (rewriteCookieDomainConfig && key.toLowerCase() === 'set-cookie') {
header = common.rewriteCookieDomain(header, rewriteCookieDomainConfig);
}
res.setHeader(String(key).trim(), header);
}
};

if (typeof rewriteCookieDomainConfig === 'string') { //also test for ''
rewriteCookieDomainConfig = { '*': rewriteCookieDomainConfig };
}
Object.keys(proxyRes.headers).forEach(function(key) {
var header = proxyRes.headers[key];
if (header != undefined) {
if (rewriteCookieDomainConfig && key.toLowerCase() === 'set-cookie') {
header = common.rewriteCookieDomain(header, rewriteCookieDomainConfig);
}
res.setHeader(String(key).trim(), header);
}
});

// message.rawHeaders is added in: v0.11.6
// https://nodejs.org/api/http.html#http_message_rawheaders
if (proxyRes.rawHeaders != undefined) {
for (var i = 0; i < proxyRes.rawHeaders.length; i += 2) {
var key = proxyRes.rawHeaders[i];
var header = proxyRes.rawHeaders[i + 1];
setHeader(key, header);
};
} else {
Object.keys(proxyRes.headers).forEach(function(key) {
var header = proxyRes.headers[key];
setHeader(key, header);
});
}
},

/**
@@ -234,11 +234,18 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
hey: 'hello',
how: 'are you?',
'set-cookie': 'hello; domain=my.domain; path=/'
}
},
rawHeaders: [
'Hey', 'hello',
'How', 'are you?',
'Set-Cookie', 'hello; domain=my.domain; path=/'
]
};
this.res = {
setHeader: function(k, v) {
this.headers[k] = v;
// https://nodejs.org/api/http.html#http_message_headers
// Header names are lower-cased
this.headers[k.toLowerCase()] = v;
},
headers: {}
};
@@ -260,7 +267,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {

expect(this.res.headers['set-cookie']).to.eql('hello; domain=my.domain; path=/');
});

it('rewrites domain', function() {
var options = {
cookieDomainRewrite: 'my.new.domain'
@@ -294,6 +301,12 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
'hello-on-my.old.domain; domain=my.old.domain; path=/',
'hello-on-my.special.domain; domain=my.special.domain; path=/'
];
var setCookieValueIndex = this.proxyRes.rawHeaders.indexOf('Set-Cookie') + 1;
this.proxyRes.rawHeaders[setCookieValueIndex] = [
'hello-on-my.domain; domain=my.domain; path=/',
'hello-on-my.old.domain; domain=my.old.domain; path=/',
'hello-on-my.special.domain; domain=my.special.domain; path=/'
];

httpProxy.writeHeaders({}, this.res, this.proxyRes, options);

@@ -148,6 +148,11 @@ describe('lib/http-proxy.js', function() {
method: 'GET'
}, function(res) {
expect(res.statusCode).to.eql(200);
expect(res.headers['content-type']).to.eql('text/plain');
if (res.rawHeaders != undefined) {
expect(res.rawHeaders.indexOf('Content-Type')).not.to.eql(-1);
expect(res.rawHeaders.indexOf('text/plain')).not.to.eql(-1);
}

res.on('data', function (data) {
expect(data.toString()).to.eql('Hello from ' + ports.source);

0 comments on commit 4edbb62

Please sign in to comment.
You can’t perform that action at this time.