|
| 1 | +// Pure helpers for HTTP Accept media-range parsing. |
| 2 | + |
| 3 | +const HTTP_TOKEN_PATTERN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/u; |
| 4 | +const HTTP_QVALUE_PATTERN = /^(?:0(?:\.\d{0,3})?|1(?:\.0{0,3})?)$/u; |
| 5 | +const HTTP_OPTIONAL_WHITESPACE_PATTERN = /^[\t ]+|[\t ]+$/gu; |
| 6 | + |
| 7 | +function trimHttpOptionalWhitespace(value: string): string { |
| 8 | + return value.replace(HTTP_OPTIONAL_WHITESPACE_PATTERN, ""); |
| 9 | +} |
| 10 | + |
| 11 | +function splitOutsideQuotedStrings(value: string, delimiter: string): string[] | null { |
| 12 | + const parts: string[] = []; |
| 13 | + let start = 0; |
| 14 | + let quoted = false; |
| 15 | + let escaped = false; |
| 16 | + |
| 17 | + for (let index = 0; index < value.length; index += 1) { |
| 18 | + const character = value[index]; |
| 19 | + if (quoted) { |
| 20 | + if (escaped) { |
| 21 | + escaped = false; |
| 22 | + continue; |
| 23 | + } |
| 24 | + if (character === "\\") { |
| 25 | + escaped = true; |
| 26 | + continue; |
| 27 | + } |
| 28 | + if (character === '"') { |
| 29 | + quoted = false; |
| 30 | + } |
| 31 | + continue; |
| 32 | + } |
| 33 | + if (character === '"') { |
| 34 | + quoted = true; |
| 35 | + continue; |
| 36 | + } |
| 37 | + if (character === delimiter) { |
| 38 | + parts.push(value.slice(start, index)); |
| 39 | + start = index + 1; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + if (quoted || escaped) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + parts.push(value.slice(start)); |
| 47 | + return parts; |
| 48 | +} |
| 49 | + |
| 50 | +function parseParameterValue(value: string): string | null { |
| 51 | + if (HTTP_TOKEN_PATTERN.test(value)) { |
| 52 | + return value; |
| 53 | + } |
| 54 | + if (value.length < 2 || value[0] !== '"' || value.at(-1) !== '"') { |
| 55 | + return null; |
| 56 | + } |
| 57 | + let parsed = ""; |
| 58 | + let escaped = false; |
| 59 | + for (let index = 1; index < value.length - 1; index += 1) { |
| 60 | + const character = value[index]; |
| 61 | + if (escaped) { |
| 62 | + parsed += character; |
| 63 | + escaped = false; |
| 64 | + continue; |
| 65 | + } |
| 66 | + if (character === "\\") { |
| 67 | + escaped = true; |
| 68 | + continue; |
| 69 | + } |
| 70 | + if (character === '"') { |
| 71 | + return null; |
| 72 | + } |
| 73 | + parsed += character; |
| 74 | + } |
| 75 | + return escaped ? null : parsed; |
| 76 | +} |
| 77 | + |
| 78 | +function normalizeParameterValue(name: string, value: string): string { |
| 79 | + return name === "charset" ? value.toLowerCase() : value; |
| 80 | +} |
| 81 | + |
| 82 | +type ParsedMediaType = { |
| 83 | + type: string; |
| 84 | + subtype: string; |
| 85 | + parameters: Map<string, string>; |
| 86 | + quality: number; |
| 87 | +}; |
| 88 | + |
| 89 | +function parseMediaType(value: string, allowQuality: boolean): ParsedMediaType | null { |
| 90 | + const segments = splitOutsideQuotedStrings(value, ";"); |
| 91 | + if (!segments) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + const mediaType = trimHttpOptionalWhitespace(segments.shift() ?? "").toLowerCase(); |
| 95 | + const [type, subtype, ...extra] = mediaType.split("/"); |
| 96 | + if ( |
| 97 | + extra.length > 0 || |
| 98 | + !type || |
| 99 | + !subtype || |
| 100 | + !HTTP_TOKEN_PATTERN.test(type) || |
| 101 | + !HTTP_TOKEN_PATTERN.test(subtype) |
| 102 | + ) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + let quality = 1; |
| 107 | + let qualitySeen = false; |
| 108 | + const parameters = new Map<string, string>(); |
| 109 | + for (const rawParameter of segments) { |
| 110 | + const parameter = trimHttpOptionalWhitespace(rawParameter); |
| 111 | + // RFC 9110 permits a semicolon whose optional parameter is omitted. |
| 112 | + if (!parameter) { |
| 113 | + continue; |
| 114 | + } |
| 115 | + const separator = parameter.indexOf("="); |
| 116 | + const name = (separator < 0 ? parameter : parameter.slice(0, separator)).toLowerCase(); |
| 117 | + if (!HTTP_TOKEN_PATTERN.test(name)) { |
| 118 | + return null; |
| 119 | + } |
| 120 | + if (separator <= 0) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + // Parameter grammar has no whitespace around `=`; accepting it can route |
| 124 | + // a malformed negotiation request into a persistent SSE response. |
| 125 | + const rawValue = parameter.slice(separator + 1); |
| 126 | + if (!rawValue) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + if (name === "q") { |
| 130 | + // RFC 9110 recognizes q as the weight regardless of parameter order; |
| 131 | + // every other parameter still participates in representation matching. |
| 132 | + if (!allowQuality || qualitySeen || !HTTP_QVALUE_PATTERN.test(rawValue)) { |
| 133 | + return null; |
| 134 | + } |
| 135 | + qualitySeen = true; |
| 136 | + quality = Number(rawValue); |
| 137 | + continue; |
| 138 | + } |
| 139 | + const parameterValue = parseParameterValue(rawValue); |
| 140 | + if (parameterValue === null || parameters.has(name)) { |
| 141 | + return null; |
| 142 | + } |
| 143 | + parameters.set(name, normalizeParameterValue(name, parameterValue)); |
| 144 | + } |
| 145 | + return { type, subtype, parameters, quality }; |
| 146 | +} |
| 147 | + |
| 148 | +function matchesRepresentation(range: ParsedMediaType, representation: ParsedMediaType): boolean { |
| 149 | + if (range.type !== representation.type || range.subtype !== representation.subtype) { |
| 150 | + return false; |
| 151 | + } |
| 152 | + for (const [name, value] of range.parameters) { |
| 153 | + if (representation.parameters.get(name) !== value) { |
| 154 | + return false; |
| 155 | + } |
| 156 | + } |
| 157 | + return true; |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * Checks for an explicit, positive-quality media range in an Accept field. |
| 162 | + * Wildcards intentionally do not opt callers into long-lived streaming responses. |
| 163 | + */ |
| 164 | +export function hasExplicitAcceptableMediaRange( |
| 165 | + accept: string | undefined, |
| 166 | + expectedRepresentation: string, |
| 167 | +): boolean { |
| 168 | + if (!accept) { |
| 169 | + return false; |
| 170 | + } |
| 171 | + const representation = parseMediaType(expectedRepresentation, false); |
| 172 | + if (!representation) { |
| 173 | + return false; |
| 174 | + } |
| 175 | + |
| 176 | + const ranges = splitOutsideQuotedStrings(accept, ","); |
| 177 | + if (!ranges) { |
| 178 | + return false; |
| 179 | + } |
| 180 | + let bestSpecificity = -1; |
| 181 | + let bestExplicitQuality = 0; |
| 182 | + for (const range of ranges) { |
| 183 | + if (!trimHttpOptionalWhitespace(range)) { |
| 184 | + continue; |
| 185 | + } |
| 186 | + const parsedRange = parseMediaType(range, true); |
| 187 | + if (!parsedRange || !matchesRepresentation(parsedRange, representation)) { |
| 188 | + continue; |
| 189 | + } |
| 190 | + const specificity = parsedRange.parameters.size; |
| 191 | + if (specificity > bestSpecificity) { |
| 192 | + bestSpecificity = specificity; |
| 193 | + bestExplicitQuality = parsedRange.quality; |
| 194 | + } else if (specificity === bestSpecificity) { |
| 195 | + bestExplicitQuality = Math.max(bestExplicitQuality, parsedRange.quality); |
| 196 | + } |
| 197 | + } |
| 198 | + return bestSpecificity >= 0 && bestExplicitQuality > 0; |
| 199 | +} |
0 commit comments