Skip to content

Preserve header case for incoming requests. #1108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 24 additions & 7 deletions lib/http-proxy/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ common.isSSL = isSSL;
* @param {Object} Options Config object passed to the proxy
* @param {ClientRequest} Req Request Object
* @param {String} Forward String to select forward or target
* 
*
* @return {Object} Outgoing Object with all required properties set
*
* @api private
Expand All @@ -41,7 +41,24 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
);

outgoing.method = req.method;
outgoing.headers = extend({}, req.headers);

var connectionHeaderName = 'connection';
var hostHeaderName = 'host';
if (options.preserveHeaderKeyCase && req.rawHeaders != undefined) {
var rawHeaders = {};
for (var i = 0; i < req.rawHeaders.length - 1; i += 2) {
var key = req.rawHeaders[i];
rawHeaders[key] = req.rawHeaders[i + 1];
Copy link
Contributor

@pachirel pachirel Dec 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Learning from #1104, I prefer to use req.headers[key] instead.
I didn't check the behavior of incoming, though.


if(key.toLowerCase() === connectionHeaderName)
connectionHeaderName = key;
else if(key.toLowerCase() === hostHeaderName)
hostHeaderName = key;
}
outgoing.headers = extend({}, rawHeaders);
} else {
outgoing.headers = extend({}, req.headers);
}

if (options.headers){
extend(outgoing.headers, options.headers);
Expand Down Expand Up @@ -69,9 +86,9 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
//
if (!outgoing.agent) {
outgoing.headers = outgoing.headers || {};
if (typeof outgoing.headers.connection !== 'string'
|| !upgradeHeader.test(outgoing.headers.connection)
) { outgoing.headers.connection = 'close'; }
if (typeof outgoing.headers[connectionHeaderName] !== 'string'
|| !upgradeHeader.test(outgoing.headers[connectionHeaderName])
) { outgoing.headers[connectionHeaderName] = 'close'; }
}


Expand All @@ -98,7 +115,7 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
outgoing.path = common.urlJoin(targetPath, outgoingPath);

if (options.changeOrigin) {
outgoing.headers.host =
outgoing.headers[hostHeaderName] =
required(outgoing.port, options[forward || 'target'].protocol) && !hasPort(outgoing.host)
? outgoing.host + ':' + outgoing.port
: outgoing.host;
Expand All @@ -117,7 +134,7 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
* // => Socket
*
* @param {Socket} Socket instance to setup
* 
*
* @return {Socket} Return the configured socket.
*
* @api private
Expand Down