Skip to content

Commit 3fd3c96

Browse files
committed
[api] Force connection header to be close until keep-alive is replemented
1 parent 3588687 commit 3fd3c96

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

lib/node-http-proxy.js

+22-20
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ HttpProxy.prototype.close = function () {
242242
// #### @buffer {Object} **Optional** Result from `httpProxy.buffer(req)`
243243
//
244244
HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) {
245-
var self = this, reverseProxy, location, errState = false;
245+
var self = this, reverseProxy, location, errState = false, opts;
246246

247247
//
248248
// Check the proxy table for this instance to see if we need
@@ -301,15 +301,21 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) {
301301
res.end();
302302
}
303303

304-
// Open new HTTP request to internal resource with will act as a reverse proxy pass
305-
reverseProxy = http.request({
304+
var opts = {
306305
host: host,
307306
port: port,
308307
agent: _getAgent(host, port),
309308
method: req.method,
310309
path: req.url,
311310
headers: req.headers
312-
}, function (response) {
311+
};
312+
313+
// Force the `connection` header to be 'close' until
314+
// node.js core re-implements 'keep-alive'.
315+
opts.headers['connection'] = 'close';
316+
317+
// Open new HTTP request to internal resource with will act as a reverse proxy pass
318+
reverseProxy = http.request(opts, function (response) {
313319

314320
// Process the `reverseProxy` `response` when it's received.
315321
if (response.headers.connection) {
@@ -362,17 +368,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) {
362368
// request unless we have entered an error state.
363369
//
364370
req.on('end', function () {
365-
//
366-
// __Remark__ *(indexzero | 3/10/2011)*: This is a short-term workaround for a suspect error from net.js when
367-
// `http.ClientRequest.end()` is called in reproducable, but uninvestigated scenarios
368-
//
369-
// net.js:313
370-
// throw new Error('Socket.end() called already; cannot write.');
371-
// ^
372-
// Error: Socket.end() called already; cannot write.
373-
// at Socket.write (net.js:313:13)
374-
//
375-
if (!errState && (!reverseProxy.socket || reverseProxy.socket._writeQueueLast() !== 42)) {
371+
if (!errState) {
376372
reverseProxy.end();
377373
}
378374
});
@@ -390,20 +386,26 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) {
390386
// by `this.options.forward` ignoring errors and the subsequent response.
391387
//
392388
HttpProxy.prototype._forwardRequest = function (req) {
393-
var self = this, port, host, forwardProxy;
389+
var self = this, port, host, forwardProxy, opts;
394390

395391
port = this.options.forward.port;
396392
host = this.options.forward.host;
397393

398-
// Open new HTTP request to internal resource with will act as a reverse proxy pass
399-
forwardProxy = http.request({
394+
opts = {
400395
host: host,
401396
port: port,
402397
agent: _getAgent(host, port),
403398
method: req.method,
404399
path: req.url,
405400
headers: req.headers
406-
}, function (response) {
401+
};
402+
403+
// Force the `connection` header to be 'close' until
404+
// node.js core re-implements 'keep-alive'.
405+
opts.headers['connection'] = 'close';
406+
407+
// Open new HTTP request to internal resource with will act as a reverse proxy pass
408+
forwardProxy = http.request(opts, function (response) {
407409
//
408410
// Ignore the response from the forward proxy since this is a 'fire-and-forget' proxy.
409411
// Remark (indexzero): We will eventually emit a 'forward' event here for performance tuning.
@@ -415,7 +417,7 @@ HttpProxy.prototype._forwardRequest = function (req) {
415417
// Remark: Ignoring this error in the event
416418
// forward target doesn't exist.
417419
//
418-
forwardProxy.on('error', function (err) { });
420+
forwardProxy.once('error', function (err) { });
419421

420422
// Chunk the client request body as chunks from the proxied request come in
421423
req.on('data', function (chunk) {

0 commit comments

Comments
 (0)