From 0f243516e1c6737b95fba220a5028439264b5de6 Mon Sep 17 00:00:00 2001 From: Damian Kaczmarek Date: Tue, 10 Jun 2014 01:35:53 +0200 Subject: [PATCH 1/4] Added targetTimeout option and two tests for timeout --- lib/http-proxy/passes/web-incoming.js | 8 ++ ...lib-http-proxy-passes-web-incoming-test.js | 85 ++++++++++++++++++- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index b127bc7..8d0fc47 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -109,6 +109,14 @@ web_o = Object.keys(web_o).map(function(pass) { common.setupOutgoing(options.ssl || {}, options, req) ); + // allow outgoing socket to timeout so that we could + // show an error page at the initial request + if(options.targetTimeout) { + proxyReq.setTimeout(options.targetTimeout, function() { + proxyReq.abort(); + }); + } + // Ensure we abort proxy if request is aborted req.on('aborted', function () { proxyReq.abort(); diff --git a/test/lib-http-proxy-passes-web-incoming-test.js b/test/lib-http-proxy-passes-web-incoming-test.js index 49ffa9e..8d2adfe 100644 --- a/test/lib-http-proxy-passes-web-incoming-test.js +++ b/test/lib-http-proxy-passes-web-incoming-test.js @@ -128,6 +128,87 @@ describe('#createProxyServer.web() using own http server', function () { }, function() {}).end(); }); + it('should proxy the request and handle timeout error (targetTimeout)', function(done) { + var proxy = httpProxy.createProxyServer({ + target: 'http://127.0.0.1:45000', + targetTimeout: 100 + }); + + require('net').createServer().listen(45000); + + var proxyServer = http.createServer(requestHandler); + + var started = new Date().getTime(); + function requestHandler(req, res) { + proxy.once('error', function (err, errReq, errRes) { + proxyServer.close(); + expect(err).to.be.an(Error); + expect(errReq).to.be.equal(req); + expect(errRes).to.be.equal(res); + expect(new Date().getTime() - started).to.be.greaterThan(99); + expect(err.code).to.be('ECONNRESET'); + done(); + }); + + proxy.web(req, res); + } + + proxyServer.listen('8084'); + + http.request({ + hostname: '127.0.0.1', + port: '8084', + method: 'GET', + }, function() {}).end(); + }); + + it('should proxy the request and handle timeout error', function(done) { + var proxy = httpProxy.createProxyServer({ + target: 'http://127.0.0.1:45001', + timeout: 100 + }); + + require('net').createServer().listen(45001); + + var proxyServer = http.createServer(requestHandler); + + var cnt = 0; + var doneOne = function() { + cnt += 1; + if(cnt === 2) done(); + } + + var started = new Date().getTime(); + function requestHandler(req, res) { + proxy.once('error', function (err, errReq, errRes) { + proxyServer.close(); + expect(err).to.be.an(Error); + expect(errReq).to.be.equal(req); + expect(errRes).to.be.equal(res); + expect(err.code).to.be('ECONNRESET'); + doneOne(); + }); + + proxy.web(req, res); + } + + proxyServer.listen('8085'); + + var req = http.request({ + hostname: '127.0.0.1', + port: '8085', + method: 'GET', + }, function() {}); + + req.on('error', function(err) { + expect(err).to.be.an(Error); + expect(err.code).to.be('ECONNRESET'); + expect(new Date().getTime() - started).to.be.greaterThan(99); + doneOne(); + }); + req.end(); + }); + it('should proxy the request and provide a proxyRes event with the request and response parameters', function(done) { var proxy = httpProxy.createProxyServer({ target: 'http://127.0.0.1:8080' @@ -151,8 +232,8 @@ describe('#createProxyServer.web() using own http server', function () { res.end('Response'); }); - proxyServer.listen('8084'); + proxyServer.listen('8086'); source.listen('8080'); - http.request('http://127.0.0.1:8084', function() {}).end(); + http.request('http://127.0.0.1:8086', function() {}).end(); }); }); \ No newline at end of file From 159ca83652c1f9f42b469bd42ec1cce595f009b1 Mon Sep 17 00:00:00 2001 From: Damian Kaczmarek Date: Tue, 10 Jun 2014 01:49:16 +0200 Subject: [PATCH 2/4] Fix #657 --- test/lib-http-proxy-test.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test/lib-http-proxy-test.js b/test/lib-http-proxy-test.js index d6d5b31..220d2f7 100644 --- a/test/lib-http-proxy-test.js +++ b/test/lib-http-proxy-test.js @@ -294,9 +294,11 @@ describe('lib/http-proxy.js', function() { var proxy = httpProxy.createProxyServer({ target: 'ws://127.0.0.1:' + ports.source, ws: true - }), - proxyServer = proxy.listen(ports.proxy), - destiny = io.listen(ports.source, function () { + }); + proxyServer = proxy.listen(ports.proxy); + var server = http.createServer(); + destiny = io.listen(server); + function startSocketIo() { var client = ioClient.connect('ws://127.0.0.1:' + ports.proxy); client.on('connect', function () { @@ -306,10 +308,12 @@ describe('lib/http-proxy.js', function() { client.on('outgoing', function (data) { expect(data).to.be('Hello over websockets'); proxyServer._server.close(); - destiny.server.close(); + server.close(); done(); }); - }); + } + server.listen(ports.source); + server.on('listening', startSocketIo); destiny.sockets.on('connection', function (socket) { socket.on('incoming', function (msg) { From 4193d3bd74e5ec02341707587b9832650d49a004 Mon Sep 17 00:00:00 2001 From: Damian Kaczmarek Date: Tue, 10 Jun 2014 01:49:16 +0200 Subject: [PATCH 3/4] Fix #657 --- test/lib-http-proxy-test.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test/lib-http-proxy-test.js b/test/lib-http-proxy-test.js index d6d5b31..220d2f7 100644 --- a/test/lib-http-proxy-test.js +++ b/test/lib-http-proxy-test.js @@ -294,9 +294,11 @@ describe('lib/http-proxy.js', function() { var proxy = httpProxy.createProxyServer({ target: 'ws://127.0.0.1:' + ports.source, ws: true - }), - proxyServer = proxy.listen(ports.proxy), - destiny = io.listen(ports.source, function () { + }); + proxyServer = proxy.listen(ports.proxy); + var server = http.createServer(); + destiny = io.listen(server); + function startSocketIo() { var client = ioClient.connect('ws://127.0.0.1:' + ports.proxy); client.on('connect', function () { @@ -306,10 +308,12 @@ describe('lib/http-proxy.js', function() { client.on('outgoing', function (data) { expect(data).to.be('Hello over websockets'); proxyServer._server.close(); - destiny.server.close(); + server.close(); done(); }); - }); + } + server.listen(ports.source); + server.on('listening', startSocketIo); destiny.sockets.on('connection', function (socket) { socket.on('incoming', function (msg) { From 7b79a7409ade7a8c79b2ae5761abc4843529063a Mon Sep 17 00:00:00 2001 From: Damian Kaczmarek Date: Tue, 10 Jun 2014 19:04:12 +0200 Subject: [PATCH 4/4] Change name targetTimeout to proxyTimeout --- lib/http-proxy/passes/web-incoming.js | 4 ++-- test/lib-http-proxy-passes-web-incoming-test.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index 8d0fc47..056236e 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -111,8 +111,8 @@ web_o = Object.keys(web_o).map(function(pass) { // allow outgoing socket to timeout so that we could // show an error page at the initial request - if(options.targetTimeout) { - proxyReq.setTimeout(options.targetTimeout, function() { + if(options.proxyTimeout) { + proxyReq.setTimeout(options.proxyTimeout, function() { proxyReq.abort(); }); } diff --git a/test/lib-http-proxy-passes-web-incoming-test.js b/test/lib-http-proxy-passes-web-incoming-test.js index 8d2adfe..93c76ca 100644 --- a/test/lib-http-proxy-passes-web-incoming-test.js +++ b/test/lib-http-proxy-passes-web-incoming-test.js @@ -128,10 +128,10 @@ describe('#createProxyServer.web() using own http server', function () { }, function() {}).end(); }); - it('should proxy the request and handle timeout error (targetTimeout)', function(done) { + it('should proxy the request and handle timeout error (proxyTimeout)', function(done) { var proxy = httpProxy.createProxyServer({ target: 'http://127.0.0.1:45000', - targetTimeout: 100 + proxyTimeout: 100 }); require('net').createServer().listen(45000);