-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fetch): support following redirect responses (#627)
- Loading branch information
1 parent
c4b68ba
commit 7410d45
Showing
6 changed files
with
391 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export function createNetworkError(cause?: unknown) { | ||
return Object.assign(new TypeError('Failed to fetch'), { | ||
cause, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { createNetworkError } from './createNetworkError' | ||
|
||
const REQUEST_BODY_HEADERS = [ | ||
'content-encoding', | ||
'content-language', | ||
'content-location', | ||
'content-type', | ||
'content-length', | ||
] | ||
|
||
const kRedirectCount = Symbol('kRedirectCount') | ||
|
||
/** | ||
* @see https://github.com/nodejs/undici/blob/a6dac3149c505b58d2e6d068b97f4dc993da55f0/lib/web/fetch/index.js#L1210 | ||
*/ | ||
export async function followFetchRedirect( | ||
request: Request, | ||
response: Response | ||
): Promise<Response> { | ||
if (response.status !== 303 && request.body != null) { | ||
return Promise.reject(createNetworkError()) | ||
} | ||
|
||
const requestUrl = new URL(request.url) | ||
|
||
let locationUrl: URL | ||
try { | ||
locationUrl = new URL(response.headers.get('location')!) | ||
} catch (error) { | ||
return Promise.reject(createNetworkError(error)) | ||
} | ||
|
||
if ( | ||
!(locationUrl.protocol === 'http:' || locationUrl.protocol === 'https:') | ||
) { | ||
return Promise.reject( | ||
createNetworkError('URL scheme must be a HTTP(S) scheme') | ||
) | ||
} | ||
|
||
if (Reflect.get(request, kRedirectCount) > 20) { | ||
return Promise.reject(createNetworkError('redirect count exceeded')) | ||
} | ||
|
||
Object.defineProperty(request, kRedirectCount, { | ||
value: (Reflect.get(request, kRedirectCount) || 0) + 1, | ||
}) | ||
|
||
if ( | ||
request.mode === 'cors' && | ||
(locationUrl.username || locationUrl.password) && | ||
!sameOrigin(requestUrl, locationUrl) | ||
) { | ||
return Promise.reject( | ||
createNetworkError('cross origin not allowed for request mode "cors"') | ||
) | ||
} | ||
|
||
const requestInit: RequestInit = {} | ||
|
||
if ( | ||
([301, 302].includes(response.status) && request.method === 'POST') || | ||
(response.status === 303 && !['HEAD', 'GET'].includes(request.method)) | ||
) { | ||
requestInit.method = 'GET' | ||
requestInit.body = null | ||
|
||
REQUEST_BODY_HEADERS.forEach((headerName) => { | ||
request.headers.delete(headerName) | ||
}) | ||
} | ||
|
||
if (!sameOrigin(requestUrl, locationUrl)) { | ||
request.headers.delete('authorization') | ||
request.headers.delete('proxy-authorization') | ||
request.headers.delete('cookie') | ||
request.headers.delete('host') | ||
} | ||
|
||
/** | ||
* @note Undici "safely" extracts the request body. | ||
* I suspect we cannot dispatch this request again | ||
* since its body has been read and the stream is locked. | ||
*/ | ||
|
||
requestInit.headers = request.headers | ||
return fetch(new Request(locationUrl, requestInit)) | ||
} | ||
|
||
/** | ||
* @see https://github.com/nodejs/undici/blob/a6dac3149c505b58d2e6d068b97f4dc993da55f0/lib/web/fetch/util.js#L761 | ||
*/ | ||
function sameOrigin(left: URL, right: URL): boolean { | ||
if (left.origin === right.origin && left.origin === 'null') { | ||
return true | ||
} | ||
|
||
if ( | ||
left.protocol === right.protocol && | ||
left.hostname === right.hostname && | ||
left.port === right.port | ||
) { | ||
return true | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.