@@ -39,7 +39,7 @@ const {
39
39
readableStreamClose,
40
40
isomorphicEncode
41
41
} = require ( './util' )
42
- const { kState, kHeaders, kGuard, kRealm } = require ( './symbols' )
42
+ const { kState, kHeaders, kGuard, kRealm, kHeadersCaseInsensitive } = require ( './symbols' )
43
43
const assert = require ( 'assert' )
44
44
const { safelyExtractBody } = require ( './body' )
45
45
const {
@@ -61,8 +61,7 @@ const { webidl } = require('./webidl')
61
61
62
62
/** @type {import('buffer').resolveObjectURL } */
63
63
let resolveObjectURL
64
- /** @type {globalThis['ReadableStream'] } */
65
- let ReadableStream
64
+ let ReadableStream = globalThis . ReadableStream
66
65
67
66
const nodeVersion = process . versions . node . split ( '.' )
68
67
const nodeMajor = Number ( nodeVersion [ 0 ] )
@@ -781,8 +780,11 @@ async function mainFetch (fetchParams, recursive = false) {
781
780
// https://fetch.spec.whatwg.org/#concept-scheme-fetch
782
781
// given a fetch params fetchParams
783
782
async function schemeFetch ( fetchParams ) {
783
+ // Note: since the connection is destroyed on redirect, which sets fetchParams to a
784
+ // cancelled state, we do not want this condition to trigger *unless* there have been
785
+ // no redirects. See https://github.com/nodejs/undici/issues/1776
784
786
// 1. If fetchParams is canceled, then return the appropriate network error for fetchParams.
785
- if ( isCancelled ( fetchParams ) ) {
787
+ if ( isCancelled ( fetchParams ) && fetchParams . request . redirectCount === 0 ) {
786
788
return makeAppropriateNetworkError ( fetchParams )
787
789
}
788
790
@@ -840,8 +842,8 @@ async function schemeFetch (fetchParams) {
840
842
const response = makeResponse ( {
841
843
statusText : 'OK' ,
842
844
headersList : [
843
- [ 'content-length' , length ] ,
844
- [ 'content-type' , type ]
845
+ [ 'content-length' , { name : 'Content-Length' , value : length } ] ,
846
+ [ 'content-type' , { name : 'Content-Type' , value : type } ]
845
847
]
846
848
} )
847
849
@@ -870,7 +872,7 @@ async function schemeFetch (fetchParams) {
870
872
return makeResponse ( {
871
873
statusText : 'OK' ,
872
874
headersList : [
873
- [ 'content-type' , mimeType ]
875
+ [ 'content-type' , { name : 'Content-Type' , value : mimeType } ]
874
876
] ,
875
877
body : safelyExtractBody ( dataURLStruct . body ) [ 0 ]
876
878
} )
@@ -1135,12 +1137,12 @@ async function httpRedirectFetch (fetchParams, response) {
1135
1137
return makeNetworkError ( 'URL scheme must be a HTTP(S) scheme' )
1136
1138
}
1137
1139
1138
- // 7. If request’s redirect count is twenty, return a network error.
1140
+ // 7. If request’s redirect count is 20, then return a network error.
1139
1141
if ( request . redirectCount === 20 ) {
1140
1142
return makeNetworkError ( 'redirect count exceeded' )
1141
1143
}
1142
1144
1143
- // 8. Increase request’s redirect count by one .
1145
+ // 8. Increase request’s redirect count by 1 .
1144
1146
request . redirectCount += 1
1145
1147
1146
1148
// 9. If request’s mode is "cors", locationURL includes credentials, and
@@ -1195,36 +1197,44 @@ async function httpRedirectFetch (fetchParams, response) {
1195
1197
}
1196
1198
}
1197
1199
1198
- // 13. If request’s body is non-null, then set request’s body to the first return
1200
+ // 13. If request’s current URL’s origin is not same origin with locationURL’s
1201
+ // origin, then for each headerName of CORS non-wildcard request-header name,
1202
+ // delete headerName from request’s header list.
1203
+ if ( ! sameOrigin ( requestCurrentURL ( request ) , locationURL ) ) {
1204
+ // https://fetch.spec.whatwg.org/#cors-non-wildcard-request-header-name
1205
+ request . headersList . delete ( 'authorization' )
1206
+ }
1207
+
1208
+ // 14. If request’s body is non-null, then set request’s body to the first return
1199
1209
// value of safely extracting request’s body’s source.
1200
1210
if ( request . body != null ) {
1201
1211
assert ( request . body . source )
1202
1212
request . body = safelyExtractBody ( request . body . source ) [ 0 ]
1203
1213
}
1204
1214
1205
- // 14 . Let timingInfo be fetchParams’s timing info.
1215
+ // 15 . Let timingInfo be fetchParams’s timing info.
1206
1216
const timingInfo = fetchParams . timingInfo
1207
1217
1208
- // 15 . Set timingInfo’s redirect end time and post-redirect start time to the
1218
+ // 16 . Set timingInfo’s redirect end time and post-redirect start time to the
1209
1219
// coarsened shared current time given fetchParams’s cross-origin isolated
1210
1220
// capability.
1211
1221
timingInfo . redirectEndTime = timingInfo . postRedirectStartTime =
1212
1222
coarsenedSharedCurrentTime ( fetchParams . crossOriginIsolatedCapability )
1213
1223
1214
- // 16 . If timingInfo’s redirect start time is 0, then set timingInfo’s
1224
+ // 17 . If timingInfo’s redirect start time is 0, then set timingInfo’s
1215
1225
// redirect start time to timingInfo’s start time.
1216
1226
if ( timingInfo . redirectStartTime === 0 ) {
1217
1227
timingInfo . redirectStartTime = timingInfo . startTime
1218
1228
}
1219
1229
1220
- // 17 . Append locationURL to request’s URL list.
1230
+ // 18 . Append locationURL to request’s URL list.
1221
1231
request . urlList . push ( locationURL )
1222
1232
1223
- // 18 . Invoke set request’s referrer policy on redirect on request and
1233
+ // 19 . Invoke set request’s referrer policy on redirect on request and
1224
1234
// actualResponse.
1225
1235
setRequestReferrerPolicyOnRedirect ( request , actualResponse )
1226
1236
1227
- // 19 . Return the result of running main fetch given fetchParams and true.
1237
+ // 20 . Return the result of running main fetch given fetchParams and true.
1228
1238
return mainFetch ( fetchParams , true )
1229
1239
}
1230
1240
@@ -1930,7 +1940,7 @@ async function httpNetworkFetch (
1930
1940
origin : url . origin ,
1931
1941
method : request . method ,
1932
1942
body : fetchParams . controller . dispatcher . isMockActive ? request . body && request . body . source : body ,
1933
- headers : [ ... request . headersList ] . flat ( ) ,
1943
+ headers : request . headersList [ kHeadersCaseInsensitive ] ,
1934
1944
maxRedirections : 0 ,
1935
1945
bodyTimeout : 300_000 ,
1936
1946
headersTimeout : 300_000
0 commit comments