Skip to content

Commit 1817990

Browse files
Merge branch 'keep-alive' into remove-use-named-socket
2 parents d75238b + 8673fe9 commit 1817990

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ __Arguments__
116116
* port - The port or named socket to listen on (default: 8080).
117117
* sslCaDir - Path to the certificates cache directory (default: process.cwd() + '/.http-mitm-proxy')
118118
* silent - if set to true, nothing will be written to console (default: false)
119+
* keepAlive - enable [HTTP persistent connection](https://en.wikipedia.org/wiki/HTTP_persistent_connection)
119120
* timeout - The number of milliseconds of inactivity before a socket is presumed to have timed out. Defaults to no timeout.
120121
* httpAgent - The [http.Agent](https://nodejs.org/api/http.html#http_class_http_agent) to use when making http requests. Useful for chaining proxys. (default: internal Agent)
121122
* httpsAgent - The [https.Agent](https://nodejs.org/api/https.html#https_class_https_agent) to use when making https requests. Useful for chaining proxys. (default: internal Agent)

lib/proxy.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ Proxy.prototype.listen = function(options, callback) {
4747
this.silent = !!options.silent;
4848
this.httpPort = options.port || 8080;
4949
this.timeout = options.timeout || 0;
50-
this.httpAgent = options.httpAgent || new http.Agent();
51-
this.httpsAgent = options.httpsAgent || new https.Agent();
50+
this.keepAlive = !!options.keepAlive;
51+
this.httpAgent = typeof(options.httpAgent) !== "undefined" ? options.httpAgent : new http.Agent({ keepAlive: this.keepAlive });
52+
this.httpsAgent = typeof(options.httpsAgent) !== "undefined" ? options.httpsAgent : new https.Agent({ keepAlive: this.keepAlive });
5253
this.forceSNI = !!options.forceSNI;
5354
if (this.forceSNI && !this.silent) {
5455
console.log('SNI enabled. Clients not supporting SNI may fail');
@@ -702,7 +703,14 @@ Proxy.prototype._onHttpServerRequest = function(isSSL, clientToProxyRequest, pro
702703
}
703704
ctx.serverToProxyResponse.headers['transfer-encoding'] = 'chunked';
704705
delete ctx.serverToProxyResponse.headers['content-length'];
705-
ctx.serverToProxyResponse.headers['connection'] = 'close';
706+
if (self.keepAlive) {
707+
if (!ctx.serverToProxyResponse.headers['connection']) {
708+
var keepAlive = ctx.clientToProxyRequest.headers && ctx.clientToProxyRequest.headers['connection'] && ctx.clientToProxyRequest.headers['connection'].trim().toLowerCase() === 'keep-alive';
709+
ctx.serverToProxyResponse.headers['connection'] = keepAlive ? 'keep-alive' : 'close';
710+
}
711+
} else {
712+
ctx.serverToProxyResponse.headers['connection'] = 'close';
713+
}
706714
return self._onResponseHeaders(ctx, function (err) {
707715
if (err) {
708716
return self._onError('ON_RESPONSEHEADERS_ERROR', ctx, err);

0 commit comments

Comments
 (0)