(rawHost)
| 35 | * @returns {{ host: string, hostname: string, port: string } | null} |
| 36 | */ |
| 37 | export function parseHttpHostHeader(rawHost) { |
| 38 | if (typeof rawHost !== "string") return null; |
| 39 | const trimmed = rawHost.trim(); |
| 40 | if (!trimmed || !HOST_AUTHORITY_RE.test(trimmed)) return null; |
| 41 | try { |
| 42 | const parsed = new URL(`http://${trimmed}`); |
| 43 | if ( |
| 44 | parsed.username || |
| 45 | parsed.password || |
| 46 | parsed.pathname !== "/" || |
| 47 | parsed.search || |
| 48 | parsed.hash |
| 49 | ) { |
| 50 | return null; |
| 51 | } |
| 52 | const hostname = parsed.hostname.replace(/^\[|\]$/g, "").toLowerCase(); |
| 53 | if (!hostname) return null; |
| 54 | return { |
| 55 | host: parsed.host.toLowerCase(), |
| 56 | hostname, |
| 57 | port: parsed.port, |
| 58 | }; |
| 59 | } catch { |
| 60 | return null; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Parse an HTTP `Origin` header into a lowercase authority and unbracketed |
no outgoing calls
no test coverage detected