@@ -42,7 +42,6 @@ class RedirectHandler {
4242
4343 this . dispatch = dispatch
4444 this . location = null
45- this . abort = null
4645 this . opts = { ...opts , maxRedirections : 0 } // opts must be a copy
4746 this . maxRedirections = maxRedirections
4847 this . handler = handler
@@ -83,20 +82,16 @@ class RedirectHandler {
8382 }
8483 }
8584
86- onConnect ( abort ) {
87- this . abort = abort
88- this . handler . onConnect ( abort , { history : this . history } )
89- }
90-
91- onUpgrade ( statusCode , headers , socket ) {
92- this . handler . onUpgrade ( statusCode , headers , socket )
85+ onRequestStart ( controller , context ) {
86+ this . location = null
87+ this . handler . onRequestStart ?. ( controller , { ...context , history : this . history } )
9388 }
9489
95- onError ( error ) {
96- this . handler . onError ( error )
90+ onRequestUpgrade ( controller , statusCode , headers , socket ) {
91+ this . handler . onRequestUpgrade ?. ( controller , statusCode , headers , socket )
9792 }
9893
99- onHeaders ( statusCode , rawHeaders , resume , statusText ) {
94+ onResponseStart ( controller , statusCode , statusMessage , headers ) {
10095 if ( this . opts . throwOnMaxRedirect && this . history . length >= this . maxRedirections ) {
10196 throw new Error ( 'max redirects' )
10297 }
@@ -122,16 +117,17 @@ class RedirectHandler {
122117 this . opts . body = null
123118 }
124119
125- this . location = this . history . length >= this . maxRedirections || util . isDisturbed ( this . opts . body )
120+ this . location = this . history . length >= this . maxRedirections || util . isDisturbed ( this . opts . body ) || redirectableStatusCodes . indexOf ( statusCode ) === - 1
126121 ? null
127- : parseLocation ( statusCode , rawHeaders )
122+ : headers . location
128123
129124 if ( this . opts . origin ) {
130125 this . history . push ( new URL ( this . opts . path , this . opts . origin ) )
131126 }
132127
133128 if ( ! this . location ) {
134- return this . handler . onHeaders ( statusCode , rawHeaders , resume , statusText )
129+ this . handler . onResponseStart ?. ( controller , statusCode , statusMessage , headers )
130+ return
135131 }
136132
137133 const { origin, pathname, search } = util . parseURL ( new URL ( this . location , this . opts . origin && new URL ( this . opts . path , this . opts . origin ) ) )
@@ -140,14 +136,16 @@ class RedirectHandler {
140136 // Remove headers referring to the original URL.
141137 // By default it is Host only, unless it's a 303 (see below), which removes also all Content-* headers.
142138 // https://tools.ietf.org/html/rfc7231#section-6.4
143- this . opts . headers = cleanRequestHeaders ( this . opts . headers , statusCode === 303 , this . opts . origin !== origin )
139+ this . opts . headers = this . opts . headers
140+ ? cleanRequestHeaders ( this . opts . headers , statusCode === 303 , this . opts . origin !== origin )
141+ : this . opts . headers
144142 this . opts . path = path
145143 this . opts . origin = origin
146144 this . opts . maxRedirections = 0
147145 this . opts . query = null
148146 }
149147
150- onData ( chunk ) {
148+ onResponseData ( controller , chunk ) {
151149 if ( this . location ) {
152150 /*
153151 https://tools.ietf.org/html/rfc7231#section-6.4
@@ -167,11 +165,11 @@ class RedirectHandler {
167165 servers and browsers implementors, we ignore the body as there is no specified way to eventually parse it.
168166 */
169167 } else {
170- return this . handler . onData ( chunk )
168+ this . handler . onResponseData ?. ( controller , chunk )
171169 }
172170 }
173171
174- onComplete ( trailers ) {
172+ onResponseEnd ( controller , trailers ) {
175173 if ( this . location ) {
176174 /*
177175 https://tools.ietf.org/html/rfc7231#section-6.4
@@ -181,68 +179,38 @@ class RedirectHandler {
181179
182180 See comment on onData method above for more detailed information.
183181 */
184-
185- this . location = null
186- this . abort = null
187-
188182 this . dispatch ( this . opts , this )
189183 } else {
190- this . handler . onComplete ( trailers )
184+ this . handler . onResponseEnd ( controller , trailers )
191185 }
192186 }
193187
194- onBodySent ( chunk ) {
195- if ( this . handler . onBodySent ) {
196- this . handler . onBodySent ( chunk )
197- }
198- }
199- }
200-
201- function parseLocation ( statusCode , rawHeaders ) {
202- if ( redirectableStatusCodes . indexOf ( statusCode ) === - 1 ) {
203- return null
204- }
205-
206- for ( let i = 0 ; i < rawHeaders . length ; i += 2 ) {
207- if ( rawHeaders [ i ] . length === 8 && util . headerNameToString ( rawHeaders [ i ] ) === 'location' ) {
208- return rawHeaders [ i + 1 ]
209- }
188+ onResponseError ( controller , error ) {
189+ this . handler . onResponseError ?. ( controller , error )
210190 }
211191}
212192
213193// https://tools.ietf.org/html/rfc7231#section-6.4.4
214- function shouldRemoveHeader ( header , removeContent , unknownOrigin ) {
215- if ( header . length === 4 ) {
216- return util . headerNameToString ( header ) === 'host'
194+ function shouldRemoveHeader ( name , removeContent , unknownOrigin ) {
195+ if ( name . length === 4 ) {
196+ return name === 'host'
217197 }
218- if ( removeContent && util . headerNameToString ( header ) . startsWith ( 'content-' ) ) {
198+ if ( removeContent && name . startsWith ( 'content-' ) ) {
219199 return true
220200 }
221- if ( unknownOrigin && ( header . length === 13 || header . length === 6 || header . length === 19 ) ) {
222- const name = util . headerNameToString ( header )
201+ if ( unknownOrigin && ( name . length === 13 || name . length === 6 || name . length === 19 ) ) {
223202 return name === 'authorization' || name === 'cookie' || name === 'proxy-authorization'
224203 }
225204 return false
226205}
227206
228207// https://tools.ietf.org/html/rfc7231#section-6.4
229208function cleanRequestHeaders ( headers , removeContent , unknownOrigin ) {
230- const ret = [ ]
231- if ( Array . isArray ( headers ) ) {
232- for ( let i = 0 ; i < headers . length ; i += 2 ) {
233- if ( ! shouldRemoveHeader ( headers [ i ] , removeContent , unknownOrigin ) ) {
234- ret . push ( headers [ i ] , headers [ i + 1 ] )
235- }
236- }
237- } else if ( headers && typeof headers === 'object' ) {
238- const entries = typeof headers [ Symbol . iterator ] === 'function' ? headers : Object . entries ( headers )
239- for ( const [ key , value ] of entries ) {
240- if ( ! shouldRemoveHeader ( key , removeContent , unknownOrigin ) ) {
241- ret . push ( key , value )
242- }
209+ const ret = util . normalizeHeaders ( headers )
210+ for ( const name of Object . keys ( ret ) ) {
211+ if ( shouldRemoveHeader ( name , removeContent , unknownOrigin ) ) {
212+ delete ret [ name ]
243213 }
244- } else {
245- assert ( headers == null , 'headers must be an object or an array' )
246214 }
247215 return ret
248216}
0 commit comments