|
| 1 | +import { StatusError } from '@httptoolkit/util'; |
| 2 | +import { HttpEndpoint, HttpRequest } from '../http-index.js'; |
| 3 | +import { httpRedirects } from '../groups.js'; |
| 4 | +import { TLSSocket } from 'tls'; |
| 5 | + |
| 6 | +const MAX_REDIRECTS = 100; |
| 7 | + |
| 8 | +const getBaseUrl = (req: HttpRequest): string => { |
| 9 | + const isHTTPS = req.socket instanceof TLSSocket; |
| 10 | + const host = req.headers[':authority'] || req.headers.host; |
| 11 | + return `${isHTTPS ? 'https' : 'http'}://${host}`; |
| 12 | +}; |
| 13 | + |
| 14 | +const parseN = (path: string, prefix: string): number => { |
| 15 | + const n = parseInt(path.slice(prefix.length), 10); |
| 16 | + if (isNaN(n) || n < 1) throw new StatusError(400, `Invalid redirect count in ${path}`); |
| 17 | + if (n > MAX_REDIRECTS) throw new StatusError(400, `Redirect count exceeds maximum of ${MAX_REDIRECTS}`); |
| 18 | + return n; |
| 19 | +}; |
| 20 | + |
| 21 | +export const redirectTo: HttpEndpoint = { |
| 22 | + matchPath: (path) => path === '/redirect-to', |
| 23 | + handle: (req, res, { query }) => { |
| 24 | + const url = query.get('url'); |
| 25 | + if (!url) { |
| 26 | + res.writeHead(400).end('Missing "url" query parameter'); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + const statusCode = parseInt(query.get('status_code') || '302', 10); |
| 31 | + res.writeHead(statusCode, { 'location': url }).end(); |
| 32 | + }, |
| 33 | + meta: { |
| 34 | + path: '/redirect-to', |
| 35 | + description: 'Redirects to the URL specified in the "url" query parameter, with an optional "status_code" (default 302).', |
| 36 | + examples: ['/redirect-to?url=/get', '/redirect-to?url=/get&status_code=301'], |
| 37 | + group: httpRedirects |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +export const redirectN: HttpEndpoint = { |
| 42 | + matchPath: (path) => { |
| 43 | + if (!path.match(/^\/redirect\/\d+$/)) return false; |
| 44 | + parseN(path, '/redirect/'); |
| 45 | + return true; |
| 46 | + }, |
| 47 | + handle: (_req, res, { path }) => { |
| 48 | + const n = parseN(path, '/redirect/'); |
| 49 | + const location = n <= 1 ? '/get' : `/relative-redirect/${n - 1}`; |
| 50 | + res.writeHead(302, { 'location': location }).end(); |
| 51 | + }, |
| 52 | + meta: { |
| 53 | + path: '/redirect/{n}', |
| 54 | + description: 'Redirects n times using relative URLs (via /relative-redirect), then returns /get.', |
| 55 | + examples: ['/redirect/3'], |
| 56 | + group: httpRedirects |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +export const relativeRedirectN: HttpEndpoint = { |
| 61 | + matchPath: (path) => { |
| 62 | + if (!path.match(/^\/relative-redirect\/\d+$/)) return false; |
| 63 | + parseN(path, '/relative-redirect/'); |
| 64 | + return true; |
| 65 | + }, |
| 66 | + handle: (req, res, { path }) => { |
| 67 | + const n = parseN(path, '/relative-redirect/'); |
| 68 | + const location = n <= 1 ? '/get' : `/relative-redirect/${n - 1}`; |
| 69 | + res.writeHead(302, { 'location': location }).end(); |
| 70 | + }, |
| 71 | + meta: { |
| 72 | + path: '/relative-redirect/{n}', |
| 73 | + description: 'Redirects n times using relative URLs, then returns /get.', |
| 74 | + examples: ['/relative-redirect/3'], |
| 75 | + group: httpRedirects |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +export const absoluteRedirectN: HttpEndpoint = { |
| 80 | + matchPath: (path) => { |
| 81 | + if (!path.match(/^\/absolute-redirect\/\d+$/)) return false; |
| 82 | + parseN(path, '/absolute-redirect/'); |
| 83 | + return true; |
| 84 | + }, |
| 85 | + handle: (req, res, { path }) => { |
| 86 | + const n = parseN(path, '/absolute-redirect/'); |
| 87 | + const base = getBaseUrl(req); |
| 88 | + const location = n <= 1 ? `${base}/get` : `${base}/absolute-redirect/${n - 1}`; |
| 89 | + res.writeHead(302, { 'location': location }).end(); |
| 90 | + }, |
| 91 | + meta: { |
| 92 | + path: '/absolute-redirect/{n}', |
| 93 | + description: 'Redirects n times using absolute URLs, then returns /get.', |
| 94 | + examples: ['/absolute-redirect/3'], |
| 95 | + group: httpRedirects |
| 96 | + } |
| 97 | +}; |
0 commit comments