@@ -37,7 +37,10 @@ const {
37
37
isErrorLike,
38
38
fullyReadBody,
39
39
readableStreamClose,
40
- isomorphicEncode
40
+ isomorphicEncode,
41
+ urlIsLocal,
42
+ urlIsHttpHttpsScheme,
43
+ urlHasHttpsScheme
41
44
} = require ( './util' )
42
45
const { kState, kHeaders, kGuard, kRealm, kHeadersCaseInsensitive } = require ( './symbols' )
43
46
const assert = require ( 'assert' )
@@ -272,7 +275,7 @@ function finalizeAndReportTiming (response, initiatorType = 'other') {
272
275
let cacheState = response . cacheState
273
276
274
277
// 6. If originalURL’s scheme is not an HTTP(S) scheme, then return.
275
- if ( ! / ^ h t t p s ? : / . test ( originalURL . protocol ) ) {
278
+ if ( ! urlIsHttpHttpsScheme ( originalURL ) ) {
276
279
return
277
280
}
278
281
@@ -530,10 +533,7 @@ async function mainFetch (fetchParams, recursive = false) {
530
533
531
534
// 3. If request’s local-URLs-only flag is set and request’s current URL is
532
535
// not local, then set response to a network error.
533
- if (
534
- request . localURLsOnly &&
535
- ! / ^ ( a b o u t | b l o b | d a t a ) : / . test ( requestCurrentURL ( request ) . protocol )
536
- ) {
536
+ if ( request . localURLsOnly && ! urlIsLocal ( requestCurrentURL ( request ) ) ) {
537
537
response = makeNetworkError ( 'local URLs only' )
538
538
}
539
539
@@ -623,7 +623,7 @@ async function mainFetch (fetchParams, recursive = false) {
623
623
}
624
624
625
625
// request’s current URL’s scheme is not an HTTP(S) scheme
626
- if ( ! / ^ h t t p s ? : / . test ( requestCurrentURL ( request ) . protocol ) ) {
626
+ if ( ! urlIsHttpHttpsScheme ( requestCurrentURL ( request ) ) ) {
627
627
// Return a network error.
628
628
return makeNetworkError ( 'URL scheme must be a HTTP(S) scheme' )
629
629
}
@@ -1130,7 +1130,7 @@ async function httpRedirectFetch (fetchParams, response) {
1130
1130
1131
1131
// 6. If locationURL’s scheme is not an HTTP(S) scheme, then return a network
1132
1132
// error.
1133
- if ( ! / ^ h t t p s ? : / . test ( locationURL . protocol ) ) {
1133
+ if ( ! urlIsHttpHttpsScheme ( locationURL ) ) {
1134
1134
return makeNetworkError ( 'URL scheme must be a HTTP(S) scheme' )
1135
1135
}
1136
1136
@@ -1205,7 +1205,7 @@ async function httpRedirectFetch (fetchParams, response) {
1205
1205
// 14. If request’s body is non-null, then set request’s body to the first return
1206
1206
// value of safely extracting request’s body’s source.
1207
1207
if ( request . body != null ) {
1208
- assert ( request . body . source )
1208
+ assert ( request . body . source != null )
1209
1209
request . body = safelyExtractBody ( request . body . source ) [ 0 ]
1210
1210
}
1211
1211
@@ -1399,7 +1399,7 @@ async function httpNetworkOrCacheFetch (
1399
1399
// header if httpRequest’s header list contains that header’s name.
1400
1400
// TODO: https://github.com/whatwg/fetch/issues/1285#issuecomment-896560129
1401
1401
if ( ! httpRequest . headersList . contains ( 'accept-encoding' ) ) {
1402
- if ( / ^ h t t p s : / . test ( requestCurrentURL ( httpRequest ) . protocol ) ) {
1402
+ if ( urlHasHttpsScheme ( requestCurrentURL ( httpRequest ) ) ) {
1403
1403
httpRequest . headersList . append ( 'accept-encoding' , 'br, gzip, deflate' )
1404
1404
} else {
1405
1405
httpRequest . headersList . append ( 'accept-encoding' , 'gzip, deflate' )
@@ -1845,6 +1845,7 @@ async function httpNetworkFetch (
1845
1845
// 4. Set bytes to the result of handling content codings given
1846
1846
// codings and bytes.
1847
1847
let bytes
1848
+ let isFailure
1848
1849
try {
1849
1850
const { done, value } = await fetchParams . controller . next ( )
1850
1851
@@ -1859,6 +1860,10 @@ async function httpNetworkFetch (
1859
1860
bytes = undefined
1860
1861
} else {
1861
1862
bytes = err
1863
+
1864
+ // err may be propagated from the result of calling readablestream.cancel,
1865
+ // which might not be an error. https://github.com/nodejs/undici/issues/2009
1866
+ isFailure = true
1862
1867
}
1863
1868
}
1864
1869
@@ -1878,7 +1883,7 @@ async function httpNetworkFetch (
1878
1883
timingInfo . decodedBodySize += bytes ?. byteLength ?? 0
1879
1884
1880
1885
// 6. If bytes is failure, then terminate fetchParams’s controller.
1881
- if ( isErrorLike ( bytes ) ) {
1886
+ if ( isFailure ) {
1882
1887
fetchParams . controller . terminate ( bytes )
1883
1888
return
1884
1889
}
@@ -1979,7 +1984,9 @@ async function httpNetworkFetch (
1979
1984
const val = headersList [ n + 1 ] . toString ( 'latin1' )
1980
1985
1981
1986
if ( key . toLowerCase ( ) === 'content-encoding' ) {
1982
- codings = val . split ( ',' ) . map ( ( x ) => x . trim ( ) )
1987
+ // https://www.rfc-editor.org/rfc/rfc7231#section-3.1.2.1
1988
+ // "All content-coding values are case-insensitive..."
1989
+ codings = val . toLowerCase ( ) . split ( ',' ) . map ( ( x ) => x . trim ( ) )
1983
1990
} else if ( key . toLowerCase ( ) === 'location' ) {
1984
1991
location = val
1985
1992
}
@@ -1998,9 +2005,10 @@ async function httpNetworkFetch (
1998
2005
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
1999
2006
if ( request . method !== 'HEAD' && request . method !== 'CONNECT' && ! nullBodyStatus . includes ( status ) && ! willFollow ) {
2000
2007
for ( const coding of codings ) {
2001
- if ( / ( x - ) ? g z i p / . test ( coding ) ) {
2008
+ // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.2
2009
+ if ( coding === 'x-gzip' || coding === 'gzip' ) {
2002
2010
decoders . push ( zlib . createGunzip ( ) )
2003
- } else if ( / ( x - ) ? d e f l a t e / . test ( coding ) ) {
2011
+ } else if ( coding === 'deflate' ) {
2004
2012
decoders . push ( zlib . createInflate ( ) )
2005
2013
} else if ( coding === 'br' ) {
2006
2014
decoders . push ( zlib . createBrotliDecompress ( ) )
0 commit comments