11/*!
22 * send
33 * Copyright(c) 2012 TJ Holowaychuk
4- * Copyright(c) 2014-2016 Douglas Christopher Wilson
4+ * Copyright(c) 2014-2022 Douglas Christopher Wilson
55 * MIT Licensed
66 */
77
@@ -166,13 +166,11 @@ util.inherits(SendStream, Stream)
166166SendStream . prototype . error = function error ( status , err ) {
167167 // emit if listeners instead of responding
168168 if ( hasListeners ( this , 'error' ) ) {
169- return this . emit ( 'error' , createError ( status , err , {
170- expose : false
171- } ) )
169+ return this . emit ( 'error' , createHttpError ( status , err ) )
172170 }
173171
174172 var res = this . res
175- var msg = statuses [ status ] || String ( status )
173+ var msg = statuses . message [ status ] || String ( status )
176174 var doc = createHtmlDocument ( 'Error' , escapeHtml ( msg ) )
177175
178176 // clear existing headers
@@ -248,21 +246,19 @@ SendStream.prototype.isPreconditionFailure = function isPreconditionFailure () {
248246}
249247
250248/**
251- * Strip content-* header fields.
249+ * Strip various content header fields for a change in entity .
252250 *
253251 * @private
254252 */
255253
256254SendStream . prototype . removeContentHeaderFields = function removeContentHeaderFields ( ) {
257255 var res = this . res
258- var headers = getHeaderNames ( res )
259256
260- for ( var i = 0 ; i < headers . length ; i ++ ) {
261- var header = headers [ i ]
262- if ( header . substr ( 0 , 8 ) === 'content-' && header !== 'content-location' ) {
263- res . removeHeader ( header )
264- }
265- }
257+ res . removeHeader ( 'Content-Encoding' )
258+ res . removeHeader ( 'Content-Language' )
259+ res . removeHeader ( 'Content-Length' )
260+ res . removeHeader ( 'Content-Range' )
261+ res . removeHeader ( 'Content-Type' )
266262}
267263
268264/**
@@ -677,8 +673,6 @@ SendStream.prototype.sendIndex = function sendIndex (path) {
677673 */
678674
679675SendStream . prototype . stream = function stream ( path , options ) {
680- // TODO: this is all lame, refactor meeee
681- var finished = false
682676 var self = this
683677 var res = this . res
684678
@@ -687,20 +681,18 @@ SendStream.prototype.stream = function stream (path, options) {
687681 this . emit ( 'stream' , stream )
688682 stream . pipe ( res )
689683
690- // response finished, done with the fd
691- onFinished ( res , function onfinished ( ) {
692- finished = true
693- destroy ( stream )
694- } )
684+ // cleanup
685+ function cleanup ( ) {
686+ destroy ( stream , true )
687+ }
695688
696- // error handling code-smell
697- stream . on ( 'error' , function onerror ( err ) {
698- // request already finished
699- if ( finished ) return
689+ // response finished, cleanup
690+ onFinished ( res , cleanup )
700691
701- // clean up stream
702- finished = true
703- destroy ( stream )
692+ // error handling
693+ stream . on ( 'error' , function onerror ( err ) {
694+ // clean up stream early
695+ cleanup ( )
704696
705697 // error
706698 self . onStatError ( err )
@@ -858,6 +850,24 @@ function createHtmlDocument (title, body) {
858850 '</html>\n'
859851}
860852
853+ /**
854+ * Create a HttpError object from simple arguments.
855+ *
856+ * @param {number } status
857+ * @param {Error|object } err
858+ * @private
859+ */
860+
861+ function createHttpError ( status , err ) {
862+ if ( ! err ) {
863+ return createError ( status )
864+ }
865+
866+ return err instanceof Error
867+ ? createError ( status , err , { expose : false } )
868+ : createError ( status , err )
869+ }
870+
861871/**
862872 * decodeURIComponent.
863873 *
0 commit comments