diff --git a/README.md b/README.md index 40367e111fdefce07d4d3b755cc185fc4d04a206..237fbede838544fa9477928666ebce7b6fb0d55a 100644 --- a/README.md +++ b/README.md @@ -352,6 +352,7 @@ proxyServer.listen(8015); ``` * **headers**: object with extra headers to be added to target requests. * **proxyTimeout**: timeout (in millis) when proxy receives no response from target +* **followRedirects**: true/false, Default: false - specify whether you want to follow redirects **NOTE:** `options.ws` and `options.ssl` are optional. diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index 5cb0b03d391261866368bf541ccbf67d18dee199..67792f14dabbb87d52cbe8672b25455f84800fc0 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -1,12 +1,15 @@ -var http = require('http'), - https = require('https'), +var httpNative = require('http'), + httpsNative = require('https'), web_o = require('./web-outgoing'), - common = require('../common'); + common = require('../common'), + followRedirects = require('follow-redirects'); web_o = Object.keys(web_o).map(function(pass) { return web_o[pass]; }); +var nativeAgents = { http: httpNative, https: httpsNative }; + /*! * Array of passes. * @@ -99,6 +102,10 @@ module.exports = { // And we begin! server.emit('start', req, res, options.target || options.forward); + var agents = options.followRedirects ? followRedirects : nativeAgents; + var http = agents.http; + var https = agents.https; + if(options.forward) { // If forward enable, so just pipe the request var forwardReq = (options.forward.protocol === 'https:' ? https : http).request( diff --git a/package.json b/package.json index 3718993c3ed7dba0d34d883255b33dde78532656..40aede29e6aa635a8a14ed279f60cae3ed9059fe 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "main": "index.js", "dependencies": { "eventemitter3": "2.0.x", + "follow-redirects": "1.x.x", "requires-port": "1.x.x" }, "devDependencies": { diff --git a/test/lib-http-proxy-passes-web-incoming-test.js b/test/lib-http-proxy-passes-web-incoming-test.js index 1996276d246a3cafa288300da65a4e26f74a463e..d7a40906a263e3e25a7ad05535c0958cde0c66f4 100644 --- a/test/lib-http-proxy-passes-web-incoming-test.js +++ b/test/lib-http-proxy-passes-web-incoming-test.js @@ -1,6 +1,7 @@ var webPasses = require('../lib/http-proxy/passes/web-incoming'), httpProxy = require('../lib/http-proxy'), expect = require('expect.js'), + url = require('url'), http = require('http'); describe('lib/http-proxy/passes/web.js', function() { @@ -368,3 +369,39 @@ describe('#createProxyServer.web() using own http server', function () { http.request('http://127.0.0.1:8081', function() {}).end(); }); }); + +describe('#followRedirects', function () { + it('should proxy the request follow redirects', function (done) { + var proxy = httpProxy.createProxyServer({ + target: 'http://127.0.0.1:8080', + followRedirects: true + }); + + function requestHandler(req, res) { + proxy.web(req, res); + } + + var proxyServer = http.createServer(requestHandler); + + var source = http.createServer(function(req, res) { + + if (url.parse(req.url).pathname === '/redirect') { + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.end('ok'); + } + + res.writeHead(301, { 'Location': '/redirect' }); + res.end(); + }); + + proxyServer.listen('8081'); + source.listen('8080'); + + http.request('http://127.0.0.1:8081', function(res) { + source.close(); + proxyServer.close(); + expect(res.statusCode).to.eql(200); + done(); + }).end(); + }); +});