Skip to content

Commit

Permalink
Add followRedirects option
Browse files Browse the repository at this point in the history
  • Loading branch information
n30n0v authored and jcrugzz committed Apr 19, 2018
1 parent c0a5022 commit 2399f85
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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:

Expand Down
13 changes: 10 additions & 3 deletions 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.
*
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down
37 changes: 37 additions & 0 deletions 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() {
Expand Down Expand Up @@ -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();
});
});

0 comments on commit 2399f85

Please sign in to comment.