From 1d67ea70cc7def976b2695477e566172ea2e8010 Mon Sep 17 00:00:00 2001 From: Eru Penkman Date: Sat, 15 Nov 2014 14:21:15 +1000 Subject: [PATCH 1/4] update readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 1943f3f30..46fc1b050 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@

+### Fork adding redirect following + node-http-proxy ======= From f079ee77bf75d5f9f573683b0400edf371dbad72 Mon Sep 17 00:00:00 2001 From: Eru Penkman Date: Sat, 15 Nov 2014 14:29:15 +1000 Subject: [PATCH 2/4] change --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 46fc1b050..5d5fb472d 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@

-### Fork adding redirect following +### Fork adding redirect following by P.I. Penkman. node-http-proxy ======= From a4b4e9b1dc3592514e924549595333c75f9bef0f Mon Sep 17 00:00:00 2001 From: Eru Penkman Date: Sat, 15 Nov 2014 17:08:53 +1000 Subject: [PATCH 3/4] Added an optional argument to proxyRes so that users can handle sending the response themselves. Added an example of using this to enable following redirects. --- examples/http/redirect.js | 39 +++++++++++++++++++++++++++ lib/http-proxy/passes/web-incoming.js | 22 ++++++++++----- 2 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 examples/http/redirect.js diff --git a/examples/http/redirect.js b/examples/http/redirect.js new file mode 100644 index 000000000..3e566c659 --- /dev/null +++ b/examples/http/redirect.js @@ -0,0 +1,39 @@ +var util = require('util'), + colors = require('colors'), + http = require('http'), + httpProxy = require('../../lib/http-proxy'); + + +// +// Basic Http Proxy Server +// + +var proxy = httpProxy.createProxyServer(); +http.createServer(function(req, res) { + proxy.web(req, res, { + target: 'http://localhost:9003' + }) +}).listen(8003); +proxy.on('proxyRes', function(proxyRes, req, res, done) { + //check for redirects and follow them + if (proxyRes.statusCode <= 399 && proxyRes.statusCode >= 300 && proxyRes.headers.location) { + proxy.web(req, res, { + target: proxyRes.headers.location + }); + done(); + } +}); +// +// Target Http Server +// +http.createServer(function(req, res) { + res.writeHead(302, { + 'Location': 'http://localhost:9004' + }); + res.end(); +}).listen(9003); + +http.createServer(function(req, res) { + res.write('request successfully redirected to localhost:9004: ' + '\n' + JSON.stringify(req.headers, true, 2)); + res.end(); +}).listen(9004); \ No newline at end of file diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index 572c11a8c..c6003ebcc 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -142,20 +142,28 @@ web_o = Object.keys(web_o).map(function(pass) { (options.buffer || req).pipe(proxyReq); proxyReq.on('response', function(proxyRes) { - if(server) { server.emit('proxyRes', proxyRes, req, res); } - for(var i=0; i < web_o.length; i++) { - if(web_o[i](req, res, proxyRes)) { break; } + var isDone = false + + function done() { + isDone = true; + } + if (server) { + server.emit('proxyRes', proxyRes, req, res, done); } // Allow us to listen when the proxy has completed proxyRes.on('end', function () { server.emit('end', req, res, proxyRes); - }) + }); + if (!isDone) { + for (var i = 0; i < web_o.length; i++) { + if (web_o[i](req, res, proxyRes)) { break; } + } + proxyRes.pipe(res); + } - proxyRes.pipe(res); + //proxyReq.end(); }); - - //proxyReq.end(); } ] // <-- From 37f85c4b4fd5e7975380d5eb6857d324efe708d1 Mon Sep 17 00:00:00 2001 From: Eru Penkman Date: Sat, 15 Nov 2014 17:12:33 +1000 Subject: [PATCH 4/4] revert test commit --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 5d5fb472d..1943f3f30 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,6 @@

-### Fork adding redirect following by P.I. Penkman. - node-http-proxy =======