From 05616a2f4ceca7e86f35a921ab26fe571db33cc9 Mon Sep 17 00:00:00 2001 From: Christian Budde Christensen Date: Mon, 21 Mar 2016 18:04:41 +0100 Subject: [PATCH] fix(proxy): Port mixup and infinite loop Derive port from proxy-protocol if present. Closes #1987 --- lib/middleware/proxy.js | 11 ++++++++--- test/unit/middleware/proxy.spec.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lib/middleware/proxy.js b/lib/middleware/proxy.js index d84c7e4af..1a4f31e93 100644 --- a/lib/middleware/proxy.js +++ b/lib/middleware/proxy.js @@ -40,10 +40,15 @@ var parseProxyConfig = function (proxies, config) { var hostname = proxyDetails.hostname || config.hostname var protocol = proxyDetails.protocol || config.protocol - var port = proxyDetails.port || config.port || - (proxyDetails.protocol === 'https:' ? '443' : '80') var https = proxyDetails.protocol === 'https:' - + var port + if (proxyDetails.port) { + port = proxyDetails.port + } else if (proxyDetails.protocol) { + port = proxyDetails.protocol === 'https:' ? '443' : '80' + } else { + port = config.port + } var changeOrigin = 'changeOrigin' in proxyConfiguration ? proxyConfiguration.changeOrigin : false var proxy = httpProxy.createProxyServer({ target: { diff --git a/test/unit/middleware/proxy.spec.js b/test/unit/middleware/proxy.spec.js index a3eaad4b5..ba5b8dd70 100644 --- a/test/unit/middleware/proxy.spec.js +++ b/test/unit/middleware/proxy.spec.js @@ -253,6 +253,36 @@ describe('middleware.proxy', () => { expect(parsedProxyConfig[0].proxy).to.exist }) + it('should parse right port of proxy target', () => { + var proxy = { '/w': 'http://krinkle.dev/w' } + var config = {port: 9877, hostname: 'localhost'} + var parsedProxyConfig = m.parseProxyConfig(proxy, config) + expect(parsedProxyConfig).to.have.length(1) + expect(parsedProxyConfig[0]).to.containSubset({ + host: 'krinkle.dev', + port: '80', + baseUrl: '/w', + path: '/w', + https: false + }) + expect(parsedProxyConfig[0].proxy).to.exist + }) + + it('should parse right port of proxy target w. https', () => { + var proxy = { '/w': 'https://krinkle.dev/w' } + var config = {port: 9877, hostname: 'localhost'} + var parsedProxyConfig = m.parseProxyConfig(proxy, config) + expect(parsedProxyConfig).to.have.length(1) + expect(parsedProxyConfig[0]).to.containSubset({ + host: 'krinkle.dev', + port: '443', + baseUrl: '/w', + path: '/w', + https: true + }) + expect(parsedProxyConfig[0].proxy).to.exist + }) + it('should normalize proxy url', () => { var proxy = {'/base/': 'http://localhost:8000/proxy/test'} var parsedProxyConfig = m.parseProxyConfig(proxy, {})