From 2399f8523f67a016518f4253322f3ac7c7042024 Mon Sep 17 00:00:00 2001 From: n30n0v Date: Thu, 17 Aug 2017 18:22:44 +0300 Subject: [PATCH] Add followRedirects option --- README.md | 1 + lib/http-proxy/passes/web-incoming.js | 13 +++++-- package.json | 3 +- ...lib-http-proxy-passes-web-incoming-test.js | 37 +++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5971e5ab3..f290c4caf 100644 --- a/README.md +++ b/README.md @@ -375,6 +375,7 @@ proxyServer.listen(8015); * **headers**: object with extra headers to be added to target requests. * **proxyTimeout**: timeout (in millis) for outgoing proxy requests * **timeout**: timeout (in millis) for incoming requests +* **followRedirects**: true/false, Default: false - specify whether you want to follow redirects * **selfHandleRequest** true/false, if set to true, none of the webOutgoing passes are called and its your responsibility ro appropriately return the response by listening and acting on the `proxyRes` event * **buffer**: stream of data to send as the request body. Maybe you have some middleware that consumes the request stream before proxying it on e.g. If you read the body of a request into a field called 'req.rawbody' you could restream this field in the buffer option: diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index 2e72f312a..419a5dfb6 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 8950610ff..2f01cf795 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "main": "index.js", "dependencies": { "eventemitter3": "^3.0.0", - "requires-port": "^1.0.0" + "requires-port": "^1.0.0", + "follow-redirects": "^1.0.0" }, "devDependencies": { "async": "^2.0.0", diff --git a/test/lib-http-proxy-passes-web-incoming-test.js b/test/lib-http-proxy-passes-web-incoming-test.js index 7a34a58bc..cffe1af1e 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() { @@ -413,3 +414,39 @@ describe('#createProxyServer.web() using own http server', function () { http.request('http://127.0.0.1:8080/test2', 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(); + }); +});