Skip to content

Commit 1474eea

Browse files
add keepAlive option to toggle Keep Alive
1 parent 107ea73 commit 1474eea

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ __Arguments__
114114
* port - The port or named socket to listen on (default: 8080).
115115
* sslCaDir - Path to the certificates cache directory (default: process.cwd() + '/.http-mitm-proxy')
116116
* silent - if set to true, nothing will be written to console (default: false)
117+
* keepAlive - enable [HTTP persistent connection](https://en.wikipedia.org/wiki/HTTP_persistent_connection)
117118
* timeout - The number of milliseconds of inactivity before a socket is presumed to have timed out. Defaults to no timeout.
118119
* 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)
119120
* 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: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ Proxy.prototype.listen = function(options, callback) {
4848
this.silent = !!options.silent;
4949
this.httpPort = options.port || 8080;
5050
this.timeout = options.timeout || 0;
51-
this.httpAgent = options.httpAgent || new http.Agent({ keepAlive: true });
52-
this.httpsAgent = options.httpsAgent || new https.Agent({ keepAlive: true });
51+
this.keepAlive = !!options.keepAlive;
52+
this.httpAgent = options.httpAgent || new http.Agent({ keepAlive: this.keepAlive });
53+
this.httpsAgent = options.httpsAgent || new https.Agent({ keepAlive: this.keepAlive });
5354
this.forceSNI = !!options.forceSNI;
5455
if (this.forceSNI && !this.silent) {
5556
console.log('SNI enabled. Clients not supporting SNI may fail');
@@ -703,9 +704,13 @@ Proxy.prototype._onHttpServerRequest = function(isSSL, clientToProxyRequest, pro
703704
}
704705
ctx.serverToProxyResponse.headers['transfer-encoding'] = 'chunked';
705706
delete ctx.serverToProxyResponse.headers['content-length'];
706-
if (!ctx.serverToProxyResponse.headers['connection']) {
707-
var keepAlive = ctx.clientToProxyRequest.headers && ctx.clientToProxyRequest.headers['connection'] && ctx.clientToProxyRequest.headers['connection'].trim().toLowerCase() === 'keep-alive';
708-
ctx.serverToProxyResponse.headers['connection'] = keepAlive ? 'keep-alive' : 'close';
707+
if (self.keepAlive) {
708+
if (!ctx.serverToProxyResponse.headers['connection']) {
709+
var keepAlive = ctx.clientToProxyRequest.headers && ctx.clientToProxyRequest.headers['connection'] && ctx.clientToProxyRequest.headers['connection'].trim().toLowerCase() === 'keep-alive';
710+
ctx.serverToProxyResponse.headers['connection'] = keepAlive ? 'keep-alive' : 'close';
711+
}
712+
} else {
713+
ctx.serverToProxyResponse.headers['connection'] = 'close';
709714
}
710715
return self._onResponseHeaders(ctx, function (err) {
711716
if (err) {

0 commit comments

Comments
 (0)