|
| 1 | +import { isSameOrigin } from './url.ts'; |
| 2 | + |
| 3 | +const REDIRECT_HEADER = 'X-VoidPages-Redirect'; |
| 4 | +const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]); |
| 5 | + |
| 6 | +/** Opt-in transport: fetch hides redirect Location headers and drops fragments from response.url. */ |
| 7 | +export function pagesRedirectResponse(request: Request, response: Response): Response { |
| 8 | + if (!REDIRECT_STATUSES.has(response.status) || !response.headers.has('Location')) { |
| 9 | + return response; |
| 10 | + } |
| 11 | + const headers = new Headers(response.headers); |
| 12 | + headers.append('Vary', REDIRECT_HEADER); |
| 13 | + if (request.headers.get(REDIRECT_HEADER) !== 'manual') { |
| 14 | + return new Response(response.body, { |
| 15 | + status: response.status, |
| 16 | + statusText: response.statusText, |
| 17 | + headers, |
| 18 | + }); |
| 19 | + } |
| 20 | + headers.set(REDIRECT_HEADER, String(response.status)); |
| 21 | + headers.set('Cache-Control', 'no-store'); |
| 22 | + return new Response(response.body, { status: 200, headers }); |
| 23 | +} |
| 24 | + |
| 25 | +export type PagesFetchResult = { response: Response; redirectedTo?: string }; |
| 26 | +export type PagesFetchOptions = Omit<RequestInit, 'headers'> & { headers: Record<string, string> }; |
| 27 | + |
| 28 | +/** Follow negotiated redirects with HTTP method rules and origin checks. */ |
| 29 | +export async function fetchPages( |
| 30 | + url: string, |
| 31 | + options: PagesFetchOptions, |
| 32 | + followGetRedirects = true, |
| 33 | +): Promise<PagesFetchResult> { |
| 34 | + const headers: Record<string, string> = { ...options.headers, [REDIRECT_HEADER]: 'manual' }; |
| 35 | + const init = { ...options, headers }; |
| 36 | + let redirectedTo: string | undefined; |
| 37 | + for (let redirects = 0; redirects <= 20; redirects++) { |
| 38 | + const response = await fetch(url.split('#')[0], init); |
| 39 | + const status = Number(response.headers.get(REDIRECT_HEADER)); |
| 40 | + const location = response.headers.get('Location'); |
| 41 | + if (response.status !== 200 || !REDIRECT_STATUSES.has(status) || location === null) { |
| 42 | + if (response.redirected) { |
| 43 | + // Older servers still use HTTP redirects. Preserve a fragment already known to us. |
| 44 | + const destination = new URL(response.url); |
| 45 | + const hash = url.indexOf('#'); |
| 46 | + if (hash !== -1) { |
| 47 | + destination.hash = url.slice(hash); |
| 48 | + } |
| 49 | + redirectedTo = destination.href; |
| 50 | + } |
| 51 | + return { response, redirectedTo }; |
| 52 | + } |
| 53 | + await response.body?.cancel(); |
| 54 | + const base = new URL( |
| 55 | + url, |
| 56 | + window.location.origin + window.location.pathname + window.location.search, |
| 57 | + ); |
| 58 | + const destination = new URL(location, response.url || base.href); |
| 59 | + if (!['http:', 'https:', 'capacitor:'].includes(destination.protocol)) { |
| 60 | + throw new Error(`Void Pages: refusing to redirect to a ${destination.protocol} URL.`); |
| 61 | + } |
| 62 | + if (!location.includes('#')) { |
| 63 | + destination.hash = base.hash; |
| 64 | + } |
| 65 | + redirectedTo = destination.href; |
| 66 | + const method = (init.method ?? 'GET').toUpperCase(); |
| 67 | + const get = |
| 68 | + (status === 303 && method !== 'GET' && method !== 'HEAD') || |
| 69 | + ((status === 301 || status === 302) && method === 'POST'); |
| 70 | + if ( |
| 71 | + !isSameOrigin(destination, window.location.origin) || |
| 72 | + (!followGetRedirects && (get || method === 'GET' || method === 'HEAD')) |
| 73 | + ) { |
| 74 | + return { response, redirectedTo }; |
| 75 | + } |
| 76 | + if (get) { |
| 77 | + for (const name of Object.keys(headers)) { |
| 78 | + if (/^content-(?:type|length|encoding|language|location)$/i.test(name)) { |
| 79 | + delete headers[name]; |
| 80 | + } |
| 81 | + } |
| 82 | + init.method = 'GET'; |
| 83 | + init.body = undefined; |
| 84 | + } |
| 85 | + url = destination.href; |
| 86 | + } |
| 87 | + throw new TypeError('Too many Pages redirects.'); |
| 88 | +} |
0 commit comments