MCPcopy Create free account
hub / github.com/nodejs/node / httpRedirectFetch

Function httpRedirectFetch

deps/undici/src/lib/web/fetch/index.js:1252–1393  ·  view source on GitHub ↗
(fetchParams, response)

Source from the content-addressed store, hash-verified

1250
1251// https://fetch.spec.whatwg.org/#http-redirect-fetch
1252function httpRedirectFetch (fetchParams, response) {
1253 // 1. Let request be fetchParams’s request.
1254 const request = fetchParams.request
1255
1256 // 2. Let actualResponse be response, if response is not a filtered response,
1257 // and response’s internal response otherwise.
1258 const actualResponse = response.internalResponse
1259 ? response.internalResponse
1260 : response
1261
1262 // 3. Let locationURL be actualResponse’s location URL given request’s current
1263 // URL’s fragment.
1264 let locationURL
1265
1266 try {
1267 locationURL = responseLocationURL(
1268 actualResponse,
1269 requestCurrentURL(request).hash
1270 )
1271
1272 // 4. If locationURL is null, then return response.
1273 if (locationURL == null) {
1274 return response
1275 }
1276 } catch (err) {
1277 // 5. If locationURL is failure, then return a network error.
1278 return Promise.resolve(makeNetworkError(err))
1279 }
1280
1281 // 6. If locationURL’s scheme is not an HTTP(S) scheme, then return a network
1282 // error.
1283 if (!urlIsHttpHttpsScheme(locationURL)) {
1284 return Promise.resolve(makeNetworkError('URL scheme must be a HTTP(S) scheme'))
1285 }
1286
1287 // 7. If request’s redirect count is 20, then return a network error.
1288 if (request.redirectCount === 20) {
1289 return Promise.resolve(makeNetworkError('redirect count exceeded'))
1290 }
1291
1292 // 8. Increase request’s redirect count by 1.
1293 request.redirectCount += 1
1294
1295 // 9. If request’s mode is "cors", locationURL includes credentials, and
1296 // request’s origin is not same origin with locationURL’s origin, then return
1297 // a network error.
1298 if (
1299 request.mode === 'cors' &&
1300 (locationURL.username || locationURL.password) &&
1301 !sameOrigin(request, locationURL)
1302 ) {
1303 return Promise.resolve(makeNetworkError('cross origin not allowed for request mode "cors"'))
1304 }
1305
1306 // 10. If request’s response tainting is "cors" and locationURL includes
1307 // credentials, then return a network error.
1308 if (
1309 request.responseTainting === 'cors' &&

Callers 1

httpFetchFunction · 0.70

Calls 14

includesMethod · 0.80
responseLocationURLFunction · 0.70
requestCurrentURLFunction · 0.70
makeNetworkErrorFunction · 0.70
urlIsHttpHttpsSchemeFunction · 0.70
sameOriginFunction · 0.70
safelyExtractBodyFunction · 0.70
mainFetchFunction · 0.70
deleteMethod · 0.65
assertFunction · 0.50

Tested by

no test coverage detected