@@ -83,7 +83,6 @@ class RedirectHandler {
8383 }
8484
8585 onRequestStart ( controller , context ) {
86- this . location = null
8786 this . handler . onRequestStart ?. ( controller , { ...context , history : this . history } )
8887 }
8988
@@ -136,9 +135,7 @@ class RedirectHandler {
136135 // Remove headers referring to the original URL.
137136 // By default it is Host only, unless it's a 303 (see below), which removes also all Content-* headers.
138137 // https://tools.ietf.org/html/rfc7231#section-6.4
139- this . opts . headers = this . opts . headers
140- ? cleanRequestHeaders ( this . opts . headers , statusCode === 303 , this . opts . origin !== origin )
141- : this . opts . headers
138+ this . opts . headers = cleanRequestHeaders ( this . opts . headers , statusCode === 303 , this . opts . origin !== origin )
142139 this . opts . path = path
143140 this . opts . origin = origin
144141 this . opts . maxRedirections = 0
@@ -191,26 +188,38 @@ class RedirectHandler {
191188}
192189
193190// https://tools.ietf.org/html/rfc7231#section-6.4.4
194- function shouldRemoveHeader ( name , removeContent , unknownOrigin ) {
195- if ( name . length === 4 ) {
196- return name === 'host'
191+ function shouldRemoveHeader ( header , removeContent , unknownOrigin ) {
192+ if ( header . length === 4 ) {
193+ return util . headerNameToString ( header ) === 'host'
197194 }
198- if ( removeContent && name . startsWith ( 'content-' ) ) {
195+ if ( removeContent && util . headerNameToString ( header ) . startsWith ( 'content-' ) ) {
199196 return true
200197 }
201- if ( unknownOrigin && ( name . length === 13 || name . length === 6 || name . length === 19 ) ) {
198+ if ( unknownOrigin && ( header . length === 13 || header . length === 6 || header . length === 19 ) ) {
199+ const name = util . headerNameToString ( header )
202200 return name === 'authorization' || name === 'cookie' || name === 'proxy-authorization'
203201 }
204202 return false
205203}
206204
207205// https://tools.ietf.org/html/rfc7231#section-6.4
208206function cleanRequestHeaders ( headers , removeContent , unknownOrigin ) {
209- const ret = util . normalizeHeaders ( headers )
210- for ( const name of Object . keys ( ret ) ) {
211- if ( shouldRemoveHeader ( name , removeContent , unknownOrigin ) ) {
212- delete ret [ name ]
207+ const ret = [ ]
208+ if ( Array . isArray ( headers ) ) {
209+ for ( let i = 0 ; i < headers . length ; i += 2 ) {
210+ if ( ! shouldRemoveHeader ( headers [ i ] , removeContent , unknownOrigin ) ) {
211+ ret . push ( headers [ i ] , headers [ i + 1 ] )
212+ }
213+ }
214+ } else if ( headers && typeof headers === 'object' ) {
215+ const entries = typeof headers [ Symbol . iterator ] === 'function' ? headers : Object . entries ( headers )
216+ for ( const [ key , value ] of entries ) {
217+ if ( ! shouldRemoveHeader ( key , removeContent , unknownOrigin ) ) {
218+ ret . push ( key , value )
219+ }
213220 }
221+ } else {
222+ assert ( headers == null , 'headers must be an object or an array' )
214223 }
215224 return ret
216225}
0 commit comments