Skip to content

Commit f92f7ae

Browse files
committed
Merge pull request #679 from RackspaceEmailAndApps/close_proxy
Added functionality to close proxy.
2 parents 0a6b424 + 8be9d94 commit f92f7ae

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

Diff for: README.md

+16
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ An object will be returned with four values:
3636
* web `req, res, [options]` (used for proxying regular HTTP(S) requests)
3737
* ws `req, socket, head, [options]` (used for proxying WS(S) requests)
3838
* listen `port` (a function that wraps the object in a webserver, for your convenience)
39+
* close `[callback]` (a function that closes the inner webserver and stops listening on given port)
3940

4041
Is it then possible to proxy requests by calling these functions
4142

@@ -322,6 +323,21 @@ If you are using the `proxyServer.listen` method, the following options are also
322323
* **xfwd**: true/false, adds x-forward headers
323324
* **toProxy**: passes the absolute URL as the `path` (useful for proxying to proxies)
324325

326+
### Shutdown
327+
328+
* When testing or running server within another program it may be necessary to close the proxy.
329+
* This will stop the proxy from accepting new connections.
330+
331+
```js
332+
var proxy = new httpProxy.createProxyServer({
333+
target: {
334+
host: 'localhost',
335+
port: 1337
336+
}
337+
});
338+
339+
proxy.close();
340+
```
325341

326342
### Test
327343

Diff for: lib/http-proxy/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ ProxyServer.prototype.listen = function(port, hostname) {
134134
return this;
135135
};
136136

137+
ProxyServer.prototype.close = function(callback) {
138+
if (this._server) {
139+
// Wrap callback to nullify server after all open connections are closed.
140+
var callback_wrapper = function() {
141+
this._server = null;
142+
143+
if (callback) {
144+
callback(arguments);
145+
}
146+
};
147+
148+
this._server.close(callback_wrapper);
149+
}
150+
};
151+
137152
ProxyServer.prototype.before = function(type, passName, callback) {
138153
if (type !== 'ws' && type !== 'web') {
139154
throw new Error('type must be `web` or `ws`');

Diff for: test/lib-http-proxy-test.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('lib/http-proxy.js', function() {
5252
expect(req.method).to.eql('GET');
5353
expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
5454
source.close();
55-
proxy._server.close();
55+
proxy.close();
5656
done();
5757
});
5858

@@ -73,7 +73,7 @@ describe('lib/http-proxy.js', function() {
7373
expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1');
7474
expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
7575
source.close();
76-
proxy._server.close();
76+
proxy.close();
7777
done();
7878
});
7979

@@ -119,7 +119,7 @@ describe('lib/http-proxy.js', function() {
119119

120120
res.on('end', function () {
121121
source.close();
122-
proxy._server.close();
122+
proxy.close();
123123
done();
124124
});
125125
}).end();
@@ -136,7 +136,7 @@ describe('lib/http-proxy.js', function() {
136136
proxy.on('error', function (err) {
137137
expect(err).to.be.an(Error);
138138
expect(err.code).to.be('ECONNREFUSED');
139-
proxy._server.close();
139+
proxy.close();
140140
done();
141141
})
142142

@@ -181,7 +181,7 @@ describe('lib/http-proxy.js', function() {
181181
testReq.on('error', function (e) {
182182
expect(e).to.be.an(Error);
183183
expect(e.code).to.be.eql('ECONNRESET');
184-
proxy._server.close();
184+
proxy.close();
185185
source.close();
186186
done();
187187
});
@@ -228,7 +228,7 @@ describe('lib/http-proxy.js', function() {
228228
// expect(events).to.contain('http-proxy:outgoing:web:begin');
229229
// expect(events).to.contain('http-proxy:outgoing:web:end');
230230
// source.close();
231-
// proxyServer._server.close();
231+
// proxyServer.close();
232232
// done();
233233
// });
234234
// }).end();
@@ -253,7 +253,7 @@ describe('lib/http-proxy.js', function() {
253253
client.on('message', function (msg) {
254254
expect(msg).to.be('Hello over websockets');
255255
client.close();
256-
proxyServer._server.close();
256+
proxyServer.close();
257257
destiny.close();
258258
done();
259259
});
@@ -284,7 +284,7 @@ describe('lib/http-proxy.js', function() {
284284
proxy.on('error', function (err) {
285285
expect(err).to.be.an(Error);
286286
expect(err.code).to.be('ECONNREFUSED');
287-
proxyServer._server.close();
287+
proxyServer.close();
288288
done();
289289
});
290290
});
@@ -307,7 +307,7 @@ describe('lib/http-proxy.js', function() {
307307

308308
client.on('outgoing', function (data) {
309309
expect(data).to.be('Hello over websockets');
310-
proxyServer._server.close();
310+
proxyServer.close();
311311
server.close();
312312
done();
313313
});

Diff for: test/lib-https-proxy-test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('lib/http-proxy.js', function() {
5353

5454
res.on('end', function () {
5555
source.close();
56-
proxy._server.close();
56+
proxy.close();
5757
done();
5858
})
5959
}).end();
@@ -93,7 +93,7 @@ describe('lib/http-proxy.js', function() {
9393

9494
res.on('end', function () {
9595
source.close();
96-
proxy._server.close();
96+
proxy.close();
9797
done();
9898
});
9999
}).end();
@@ -138,7 +138,7 @@ describe('lib/http-proxy.js', function() {
138138

139139
res.on('end', function () {
140140
source.close();
141-
proxy._server.close();
141+
proxy.close();
142142
done();
143143
})
144144
}).end();
@@ -219,4 +219,4 @@ describe('lib/http-proxy.js', function() {
219219
})
220220
})
221221
});
222-
});
222+
});

0 commit comments

Comments
 (0)