From 7b25e2bbdd77ef99623bb41555c542725cfb0013 Mon Sep 17 00:00:00 2001 From: Yuta Shimizu Date: Tue, 6 Dec 2016 15:49:55 +0900 Subject: [PATCH] Add preserveHeaderKeyCase Option --- README.md | 1 + lib/http-proxy.js | 1 + lib/http-proxy/passes/web-outgoing.js | 6 ++++-- test/lib-http-proxy-test.js | 3 ++- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ae8ed8110..49c49a396 100644 --- a/README.md +++ b/README.md @@ -333,6 +333,7 @@ proxyServer.listen(8015); * **ignorePath**: true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request (note: you will have to append / manually if required). * **localAddress**: Local interface string to bind for outgoing connections * **changeOrigin**: true/false, Default: false - changes the origin of the host header to the target URL +* **preserveHeaderKeyCase**: true/false, Default: false - specify whether you want to keep letter case of response header key * **auth**: Basic authentication i.e. 'user:password' to compute an Authorization header. * **hostRewrite**: rewrites the location hostname on (201/301/302/307/308) redirects. * **autoRewrite**: rewrites the location host/port on (201/301/302/307/308) redirects based on requested host/port. Default: false. diff --git a/lib/http-proxy.js b/lib/http-proxy.js index 40f2e4b91..7dab7a48d 100644 --- a/lib/http-proxy.js +++ b/lib/http-proxy.js @@ -35,6 +35,7 @@ function createProxyServer(options) { * ignorePath: * localAddress : * changeOrigin: + * preserveHeaderKeyCase: * auth : Basic authentication i.e. 'user:password' to compute an Authorization header. * hostRewrite: rewrites the location hostname on (301/302/307/308) redirects, Default: null. * autoRewrite: rewrites the location host/port on (301/302/307/308) redirects based on requested host/port. Default: false. diff --git a/lib/http-proxy/passes/web-outgoing.js b/lib/http-proxy/passes/web-outgoing.js index 9806ead07..4b2a75c1e 100644 --- a/lib/http-proxy/passes/web-outgoing.js +++ b/lib/http-proxy/passes/web-outgoing.js @@ -84,7 +84,8 @@ module.exports = { // <-- */ writeHeaders: function writeHeaders(req, res, proxyRes, options) { var rewriteCookieDomainConfig = options.cookieDomainRewrite, - rawHeaderKeyMap = {}, + preserveHeaderKeyCase = options.preserveHeaderKeyCase, + rawHeaderKeyMap, setHeader = function(key, header) { if (header == undefined) return; if (rewriteCookieDomainConfig && key.toLowerCase() === 'set-cookie') { @@ -99,7 +100,8 @@ module.exports = { // <-- // message.rawHeaders is added in: v0.11.6 // https://nodejs.org/api/http.html#http_message_rawheaders - if (proxyRes.rawHeaders != undefined) { + if (preserveHeaderKeyCase && proxyRes.rawHeaders != undefined) { + rawHeaderKeyMap = {}; for (var i = 0; i < proxyRes.rawHeaders.length; i += 2) { var key = proxyRes.rawHeaders[i]; rawHeaderKeyMap[key.toLowerCase()] = key; diff --git a/test/lib-http-proxy-test.js b/test/lib-http-proxy-test.js index 8b024c9f1..06702be05 100644 --- a/test/lib-http-proxy-test.js +++ b/test/lib-http-proxy-test.js @@ -130,7 +130,8 @@ describe('lib/http-proxy.js', function() { it('should make the request, handle response and finish it', function(done) { var ports = { source: gen.port, proxy: gen.port }; var proxy = httpProxy.createProxyServer({ - target: 'http://127.0.0.1:' + ports.source + target: 'http://127.0.0.1:' + ports.source, + preserveHeaderKeyCase: true }).listen(ports.proxy); var source = http.createServer(function(req, res) {