@@ -36,13 +36,15 @@ function argumentsArray(args) {
36
36
function fail ( req , res , code , msg ) {
37
37
// log a failure, and finish the HTTP request with an error code
38
38
msg = msg || "" ;
39
+ res . _logMsg = msg ;
40
+
41
+ if ( res . writableEnded ) return ; // response already done
39
42
if ( res . writeHead ) res . writeHead ( code ) ;
40
43
if ( res . write ) {
41
44
if ( ! msg ) {
42
45
msg = http . STATUS_CODES [ code ] ;
43
46
}
44
47
res . write ( msg ) ;
45
- res . _logMsg = msg ;
46
48
}
47
49
if ( res . end ) res . end ( ) ;
48
50
}
@@ -410,6 +412,7 @@ class ConfigurableProxy extends EventEmitter {
410
412
_handleProxyErrorDefault ( code , kind , req , res ) {
411
413
// called when no custom error handler is registered,
412
414
// or is registered and doesn't work
415
+ if ( res . writableEnded ) return ; // response already done
413
416
if ( ! res . headersSent && res . writeHead ) res . writeHead ( code ) ;
414
417
if ( res . write ) res . write ( http . STATUS_CODES [ code ] ) ;
415
418
if ( res . end ) res . end ( ) ;
@@ -468,13 +471,14 @@ class ConfigurableProxy extends EventEmitter {
468
471
}
469
472
470
473
var errorRequest = ( secure ? https : http ) . request ( target , function ( upstream ) {
474
+ if ( res . writableEnded ) return ; // response already done
471
475
[ "content-type" , "content-encoding" ] . map ( function ( key ) {
472
476
if ( ! upstream . headers [ key ] ) return ;
473
477
if ( res . setHeader ) res . setHeader ( key , upstream . headers [ key ] ) ;
474
478
} ) ;
475
479
if ( res . writeHead ) res . writeHead ( code ) ;
476
480
upstream . on ( "data" , ( data ) => {
477
- if ( res . write ) res . write ( data ) ;
481
+ if ( res . write && ! res . writableEnded ) res . write ( data ) ;
478
482
} ) ;
479
483
upstream . on ( "end" , ( ) => {
480
484
if ( res . end ) res . end ( ) ;
@@ -503,6 +507,7 @@ class ConfigurableProxy extends EventEmitter {
503
507
this . _handleProxyErrorDefault ( code , kind , req , res ) ;
504
508
return ;
505
509
}
510
+ if ( res . writableEnded ) return ; // response already done
506
511
if ( res . writeHead ) res . writeHead ( code , { "Content-Type" : "text/html" } ) ;
507
512
if ( res . write ) res . write ( data ) ;
508
513
if ( res . end ) res . end ( ) ;
0 commit comments