From 5b0a6c6080b64b05334553455c35161fed35eb10 Mon Sep 17 00:00:00 2001 From: Alex Cabello Date: Tue, 13 Dec 2016 00:18:53 -1000 Subject: [PATCH] Preserve header case for incoming requests. --- lib/http-proxy/common.js | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/http-proxy/common.js b/lib/http-proxy/common.js index aa9700234..9c0a63b25 100644 --- a/lib/http-proxy/common.js +++ b/lib/http-proxy/common.js @@ -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 @@ -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]; + + 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); @@ -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'; } } @@ -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; @@ -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