Skip to content

Commit c9a556c

Browse files
n30n0vindexzero
n30n0v
authored andcommitted
Add followRedirects option
1 parent 6f88caf commit c9a556c

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ proxyServer.listen(8015);
375375
* **headers**: object with extra headers to be added to target requests.
376376
* **proxyTimeout**: timeout (in millis) for outgoing proxy requests
377377
* **timeout**: timeout (in millis) for incoming requests
378+
* **followRedirects**: true/false, Default: false - specify whether you want to follow redirects
378379
* **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
379380
* **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:
380381

Diff for: lib/http-proxy/passes/web-incoming.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
var http = require('http'),
2-
https = require('https'),
1+
var httpNative = require('http'),
2+
httpsNative = require('https'),
33
web_o = require('./web-outgoing'),
4-
common = require('../common');
4+
common = require('../common'),
5+
followRedirects = require('follow-redirects');
56

67
web_o = Object.keys(web_o).map(function(pass) {
78
return web_o[pass];
89
});
910

11+
var nativeAgents = { http: httpNative, https: httpsNative };
12+
1013
/*!
1114
* Array of passes.
1215
*
@@ -99,6 +102,10 @@ module.exports = {
99102
// And we begin!
100103
server.emit('start', req, res, options.target || options.forward);
101104

105+
var agents = options.followRedirects ? followRedirects : nativeAgents;
106+
var http = agents.http;
107+
var https = agents.https;
108+
102109
if(options.forward) {
103110
// If forward enable, so just pipe the request
104111
var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"main": "index.js",
1414
"dependencies": {
1515
"eventemitter3": "^3.0.0",
16-
"requires-port": "^1.0.0"
16+
"requires-port": "^1.0.0",
17+
"follow-redirects": "^1.0.0"
1718
},
1819
"devDependencies": {
1920
"async": "^2.0.0",

Diff for: test/lib-http-proxy-passes-web-incoming-test.js

+37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var webPasses = require('../lib/http-proxy/passes/web-incoming'),
22
httpProxy = require('../lib/http-proxy'),
33
expect = require('expect.js'),
4+
url = require('url'),
45
http = require('http');
56

67
describe('lib/http-proxy/passes/web.js', function() {
@@ -413,3 +414,39 @@ describe('#createProxyServer.web() using own http server', function () {
413414
http.request('http://127.0.0.1:8080/test2', function() {}).end();
414415
});
415416
});
417+
418+
describe('#followRedirects', function () {
419+
it('should proxy the request follow redirects', function (done) {
420+
var proxy = httpProxy.createProxyServer({
421+
target: 'http://127.0.0.1:8080',
422+
followRedirects: true
423+
});
424+
425+
function requestHandler(req, res) {
426+
proxy.web(req, res);
427+
}
428+
429+
var proxyServer = http.createServer(requestHandler);
430+
431+
var source = http.createServer(function(req, res) {
432+
433+
if (url.parse(req.url).pathname === '/redirect') {
434+
res.writeHead(200, { 'Content-Type': 'text/plain' });
435+
res.end('ok');
436+
}
437+
438+
res.writeHead(301, { 'Location': '/redirect' });
439+
res.end();
440+
});
441+
442+
proxyServer.listen('8081');
443+
source.listen('8080');
444+
445+
http.request('http://127.0.0.1:8081', function(res) {
446+
source.close();
447+
proxyServer.close();
448+
expect(res.statusCode).to.eql(200);
449+
done();
450+
}).end();
451+
});
452+
});

0 commit comments

Comments
 (0)