@@ -593,12 +593,7 @@ function request_call(uri, options, counter) {
593593 options . evt = null ;
594594 } ) ;
595595
596- req . on ( 'response' , function ( response ) {
597- response . req = req ;
598- options . length = + response . headers [ 'content-length' ] || 0 ;
599- options . evt && options . evt . emit ( 'begin' , options . length ) ;
600- } ) ;
601-
596+ req . on ( 'response' , ( response ) => response . req = req ) ;
602597 req . end ( options . data ) ;
603598}
604599
@@ -647,6 +642,9 @@ function request_response(res, uri, options) {
647642 return ;
648643 }
649644
645+ options . length = + res . headers [ 'content-length' ] || 0 ;
646+ options . evt . emit ( 'begin' , options . length ) ;
647+
650648 res . on ( 'data' , function ( chunk ) {
651649 var self = this ;
652650 if ( options . max && self . _bufferlength > options . max )
@@ -663,11 +661,18 @@ function request_response(res, uri, options) {
663661 var self = this ;
664662 var str = self . _buffer ? self . _buffer . toString ( options . encoding ) : '' ;
665663 delete self . _buffer ;
666- options . evt && options . evt . emit ( 'end' , str , self . statusCode , self . headers , uri . host ) ;
667- options . callback && options . callback ( null , uri . method === 'HEAD' ? self . headers : str , self . statusCode , self . headers , uri . host ) ;
668- options . callback = null ;
669- options . evt . removeAllListeners ( ) ;
670- options . evt = null ;
664+
665+ if ( options . evt ) {
666+ options . evt . emit ( 'end' , str , self . statusCode , self . headers , uri . host ) ;
667+ options . evt . removeAllListeners ( ) ;
668+ options . evt = null ;
669+ }
670+
671+ if ( options . callback ) {
672+ options . callback ( null , uri . method === 'HEAD' ? self . headers : str , self . statusCode , self . headers , uri . host ) ;
673+ options . callback = null ;
674+ }
675+
671676 res . req && res . req . removeAllListeners ( ) ;
672677 res . removeAllListeners ( ) ;
673678 } ) ;
0 commit comments