@@ -242,7 +242,7 @@ HttpProxy.prototype.close = function () {
242
242
// #### @buffer {Object} **Optional** Result from `httpProxy.buffer(req)`
243
243
//
244
244
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 ;
246
246
247
247
//
248
248
// 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) {
301
301
res . end ( ) ;
302
302
}
303
303
304
- // Open new HTTP request to internal resource with will act as a reverse proxy pass
305
- reverseProxy = http . request ( {
304
+ var opts = {
306
305
host : host ,
307
306
port : port ,
308
307
agent : _getAgent ( host , port ) ,
309
308
method : req . method ,
310
309
path : req . url ,
311
310
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 ) {
313
319
314
320
// Process the `reverseProxy` `response` when it's received.
315
321
if ( response . headers . connection ) {
@@ -362,17 +368,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) {
362
368
// request unless we have entered an error state.
363
369
//
364
370
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 ) {
376
372
reverseProxy . end ( ) ;
377
373
}
378
374
} ) ;
@@ -390,20 +386,26 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) {
390
386
// by `this.options.forward` ignoring errors and the subsequent response.
391
387
//
392
388
HttpProxy . prototype . _forwardRequest = function ( req ) {
393
- var self = this , port , host , forwardProxy ;
389
+ var self = this , port , host , forwardProxy , opts ;
394
390
395
391
port = this . options . forward . port ;
396
392
host = this . options . forward . host ;
397
393
398
- // Open new HTTP request to internal resource with will act as a reverse proxy pass
399
- forwardProxy = http . request ( {
394
+ opts = {
400
395
host : host ,
401
396
port : port ,
402
397
agent : _getAgent ( host , port ) ,
403
398
method : req . method ,
404
399
path : req . url ,
405
400
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 ) {
407
409
//
408
410
// Ignore the response from the forward proxy since this is a 'fire-and-forget' proxy.
409
411
// Remark (indexzero): We will eventually emit a 'forward' event here for performance tuning.
@@ -415,7 +417,7 @@ HttpProxy.prototype._forwardRequest = function (req) {
415
417
// Remark: Ignoring this error in the event
416
418
// forward target doesn't exist.
417
419
//
418
- forwardProxy . on ( 'error' , function ( err ) { } ) ;
420
+ forwardProxy . once ( 'error' , function ( err ) { } ) ;
419
421
420
422
// Chunk the client request body as chunks from the proxied request come in
421
423
req . on ( 'data' , function ( chunk ) {
0 commit comments