From a3d02196c5e62cd58bc0ebe8a66afcdb905d96b3 Mon Sep 17 00:00:00 2001 From: John Catron Date: Wed, 13 Aug 2014 20:22:38 +0000 Subject: [PATCH 1/2] Added close method to proxy server. Ensured server exists before closing. Updated tests to use new close function. Added documentation to README. --- README.md | 16 ++++++++++++++++ lib/http-proxy/index.js | 7 +++++++ test/lib-http-proxy-test.js | 18 +++++++++--------- test/lib-https-proxy-test.js | 8 ++++---- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 4281dcf..2ea3d02 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ An object will be returned with four values: * web `req, res, [options]` (used for proxying regular HTTP(S) requests) * ws `req, socket, head, [options]` (used for proxying WS(S) requests) * listen `port` (a function that wraps the object in a webserver, for your convenience) +* close `[callback]` (a function that closes the inner webserver and stops listening on given port) Is it then possible to proxy requests by calling these functions @@ -322,6 +323,21 @@ If you are using the `proxyServer.listen` method, the following options are also * **xfwd**: true/false, adds x-forward headers * **toProxy**: passes the absolute URL as the `path` (useful for proxying to proxies) +### Shutdown + +* When testing or running server within another program it may be necessary to close the proxy. +* This will stop the proxy from accepting new connections. + +```js +var proxy = new httpProxy.createProxyServer({ + target: { + host: 'localhost', + port: 1337 + } +}); + +proxy.close(); +``` ### Test diff --git a/lib/http-proxy/index.js b/lib/http-proxy/index.js index 2e8d212..a33084f 100644 --- a/lib/http-proxy/index.js +++ b/lib/http-proxy/index.js @@ -132,6 +132,13 @@ ProxyServer.prototype.listen = function(port, hostname) { return this; }; +ProxyServer.prototype.close = function(callback) { + if (this._server) { + this._server.close(callback); + this._server = null; + } +}; + ProxyServer.prototype.before = function(type, passName, callback) { if (type !== 'ws' && type !== 'web') { throw new Error('type must be `web` or `ws`'); diff --git a/test/lib-http-proxy-test.js b/test/lib-http-proxy-test.js index 220d2f7..e0b4eca 100644 --- a/test/lib-http-proxy-test.js +++ b/test/lib-http-proxy-test.js @@ -52,7 +52,7 @@ describe('lib/http-proxy.js', function() { expect(req.method).to.eql('GET'); expect(req.headers.host.split(':')[1]).to.eql(ports.proxy); source.close(); - proxy._server.close(); + proxy.close(); done(); }); @@ -73,7 +73,7 @@ describe('lib/http-proxy.js', function() { expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1'); expect(req.headers.host.split(':')[1]).to.eql(ports.proxy); source.close(); - proxy._server.close(); + proxy.close(); done(); }); @@ -119,7 +119,7 @@ describe('lib/http-proxy.js', function() { res.on('end', function () { source.close(); - proxy._server.close(); + proxy.close(); done(); }); }).end(); @@ -136,7 +136,7 @@ describe('lib/http-proxy.js', function() { proxy.on('error', function (err) { expect(err).to.be.an(Error); expect(err.code).to.be('ECONNREFUSED'); - proxy._server.close(); + proxy.close(); done(); }) @@ -181,7 +181,7 @@ describe('lib/http-proxy.js', function() { testReq.on('error', function (e) { expect(e).to.be.an(Error); expect(e.code).to.be.eql('ECONNRESET'); - proxy._server.close(); + proxy.close(); source.close(); done(); }); @@ -228,7 +228,7 @@ describe('lib/http-proxy.js', function() { // expect(events).to.contain('http-proxy:outgoing:web:begin'); // expect(events).to.contain('http-proxy:outgoing:web:end'); // source.close(); - // proxyServer._server.close(); + // proxyServer.close(); // done(); // }); // }).end(); @@ -253,7 +253,7 @@ describe('lib/http-proxy.js', function() { client.on('message', function (msg) { expect(msg).to.be('Hello over websockets'); client.close(); - proxyServer._server.close(); + proxyServer.close(); destiny.close(); done(); }); @@ -284,7 +284,7 @@ describe('lib/http-proxy.js', function() { proxy.on('error', function (err) { expect(err).to.be.an(Error); expect(err.code).to.be('ECONNREFUSED'); - proxyServer._server.close(); + proxyServer.close(); done(); }); }); @@ -307,7 +307,7 @@ describe('lib/http-proxy.js', function() { client.on('outgoing', function (data) { expect(data).to.be('Hello over websockets'); - proxyServer._server.close(); + proxyServer.close(); server.close(); done(); }); diff --git a/test/lib-https-proxy-test.js b/test/lib-https-proxy-test.js index 1925049..7a966f4 100644 --- a/test/lib-https-proxy-test.js +++ b/test/lib-https-proxy-test.js @@ -53,7 +53,7 @@ describe('lib/http-proxy.js', function() { res.on('end', function () { source.close(); - proxy._server.close(); + proxy.close(); done(); }) }).end(); @@ -93,7 +93,7 @@ describe('lib/http-proxy.js', function() { res.on('end', function () { source.close(); - proxy._server.close(); + proxy.close(); done(); }); }).end(); @@ -138,7 +138,7 @@ describe('lib/http-proxy.js', function() { res.on('end', function () { source.close(); - proxy._server.close(); + proxy.close(); done(); }) }).end(); @@ -219,4 +219,4 @@ describe('lib/http-proxy.js', function() { }) }) }); -}); \ No newline at end of file +}); From 8be9d945d03169056bbf84d702292b5763b015dc Mon Sep 17 00:00:00 2001 From: John Catron Date: Thu, 14 Aug 2014 20:12:20 +0000 Subject: [PATCH 2/2] updated close function for safety --- lib/http-proxy/index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/http-proxy/index.js b/lib/http-proxy/index.js index a33084f..070c6db 100644 --- a/lib/http-proxy/index.js +++ b/lib/http-proxy/index.js @@ -134,8 +134,16 @@ ProxyServer.prototype.listen = function(port, hostname) { ProxyServer.prototype.close = function(callback) { if (this._server) { - this._server.close(callback); - this._server = null; + // Wrap callback to nullify server after all open connections are closed. + var callback_wrapper = function() { + this._server = null; + + if (callback) { + callback(arguments); + } + }; + + this._server.close(callback_wrapper); } };