accept.ts
This file
Path
src/utils/accept.ts
268 lines · 7.2 KiB · TypeScript
Last changed by
fix(accept): clamp a negative q to 0, not 1 (#5357)monembefore adoption9b4e9c2
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–164why, one step back1export interface Accept {2 type: string3 params: Record<string, string>4 q: number5}67const isWhitespace = (char: number): boolean =>8 char === 32 || char === 9 || char === 10 || char === 13910const consumeWhitespace = (acceptHeader: string, startIndex: number): number => {11 while (startIndex < acceptHeader.length) {12 if (!isWhitespace(acceptHeader.charCodeAt(startIndex))) {13 break14 }15 startIndex++16 }17 return startIndex18}1920const ignoreTrailingWhitespace = (acceptHeader: string, startIndex: number): number => {21 while (startIndex > 0) {22 if (!isWhitespace(acceptHeader.charCodeAt(startIndex - 1))) {23 break24 }25 startIndex--26 }27 return startIndex28}2930const skipInvalidParam = (acceptHeader: string, startIndex: number): [number, boolean] => {31 while (startIndex < acceptHeader.length) {32 const char = acceptHeader.charCodeAt(startIndex)33 if (char === 59) {34 // ';' => next param35 return [startIndex + 1, true]36 }37 if (char === 44) {38 // ',' => next accept value39 return [startIndex + 1, false]40 }41 startIndex++42 }43 return [startIndex, false]44}4546const skipInvalidAcceptValue = (acceptHeader: string, startIndex: number): number => {47 let i = startIndex48 let inQuotes = false49 while (i < acceptHeader.length) {50 const char = acceptHeader.charCodeAt(i)51 if (inQuotes && char === 92) {52 // '\' => escape53 i++54 } else if (char === 34) {55 // '"' => toggle quotes56 inQuotes = !inQuotes57 } else if (!inQuotes && char === 44) {58 // ',' => next accept value59 return i + 160 }61 i++62 }63 return i64}6566const getNextParam = (67 acceptHeader: string,68 startIndex: number69): [number, string | undefined, string | undefined, boolean] => {70 startIndex = consumeWhitespace(acceptHeader, startIndex)71 let i = startIndex72 let key: string | undefined73 let value: string | undefined74 let hasNext = false75 while (i < acceptHeader.length) {76 const char = acceptHeader.charCodeAt(i)77 if (char === 61) {78 // '=' => end of key79 key = acceptHeader.slice(startIndex, ignoreTrailingWhitespace(acceptHeader, i))80 i++81 break82 }83 if (char === 59) {84 // ';' => invalid empty param, continue parsing params85 return [i + 1, undefined, undefined, true]86 }87 if (char === 44) {88 // ',' => invalid empty param, move to next accept value89 return [i + 1, undefined, undefined, false]90 }91 i++92 }93 if (key === undefined) {94 return [i, undefined, undefined, false]95 }9697 i = consumeWhitespace(acceptHeader, i)98 if (acceptHeader.charCodeAt(i) === 61) {99 // '=' is invalid as a value, so return undefined100 const skipResult = skipInvalidParam(acceptHeader, i + 1)101 return [skipResult[0], key, undefined, skipResult[1]]102 }103104 let inQuotes = false105 const paramStartIndex = i106 while (i < acceptHeader.length) {107 const char = acceptHeader.charCodeAt(i)108109 if (inQuotes && char === 92) {110 // '\' => escape111 i++112 } else if (char === 34) {113 // '"' => start of quotes114 if (inQuotes) {115 let nextIndex = consumeWhitespace(acceptHeader, i + 1)116 const nextChar = acceptHeader.charCodeAt(nextIndex)117 if (nextIndex < acceptHeader.length && !(nextChar === 59 || nextChar === 44)) {118 // not ';' or ',' => invalid trailing chars119 const skipResult = skipInvalidParam(acceptHeader, nextIndex)120 return [skipResult[0], key, undefined, skipResult[1]]121 }122 value = acceptHeader.slice(paramStartIndex + 1, i)123 if (value.includes('\\')) {124 value = value.replace(/\\(.)/g, '$1')125 }126 if (nextChar === 44) {127 // ',' => end of accept value128 return [nextIndex + 1, key, value, false]129 }130 if (nextChar === 59) {131 // ';' => has next param132 hasNext = true133 nextIndex++134 }135 i = nextIndex136 break137 }138 inQuotes = true139 } else if (!inQuotes && (char === 59 || char === 44)) {140 // ';' or ',' => end of value141 value = acceptHeader.slice(paramStartIndex, ignoreTrailingWhitespace(acceptHeader, i))142 if (char === 59) {143 // ';' => has next param144 hasNext = true145 }146 i++147 break148 }149 i++150 }151 return [152 i,153 key,154 value ?? acceptHeader.slice(paramStartIndex, ignoreTrailingWhitespace(acceptHeader, i)),155 hasNext,156 ]157}158159const getNextAcceptValue = (160 acceptHeader: string,161 startIndex: number162): [number, Accept | undefined] => {163 const accept: Accept = {164 type: '',fix: use `Object.create(null)` when parsing query, headers, and params (#5161)Yusuke Wadabefore adoptionc85aead
line 165why, one step back165 params: Object.create(null),Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 166–223why, one step back166 q: 1,167 }168 startIndex = consumeWhitespace(acceptHeader, startIndex)169 let i = startIndex170 while (i < acceptHeader.length) {171 const char = acceptHeader.charCodeAt(i)172 if (char === 59 || char === 44) {173 // ';' or ',' => end of type174 accept.type = acceptHeader.slice(startIndex, ignoreTrailingWhitespace(acceptHeader, i))175 i++176 if (char === 44) {177 // ',' => end of value178 return [i, accept.type ? accept : undefined]179 }180 if (!accept.type) {181 return [skipInvalidAcceptValue(acceptHeader, i), undefined]182 }183 break // parse params184 }185 i++186 }187 if (!accept.type) {188 accept.type = acceptHeader.slice(189 startIndex,190 ignoreTrailingWhitespace(acceptHeader, acceptHeader.length)191 )192 return [acceptHeader.length, accept.type ? accept : undefined]193 }194195 let param: string | undefined196 let value: string | undefined197 let hasNext: boolean198 while (i < acceptHeader.length) {199 ;[i, param, value, hasNext] = getNextParam(acceptHeader, i)200 if (param && value) {201 accept.params[param] = value202 }203 if (!hasNext) {204 break205 }206 }207208 return [i, accept] as [number, Accept]209}210211export const parseAccept = (acceptHeader: string): Accept[] => {212 if (!acceptHeader) {213 return []214 }215216 const values: Accept[] = []217 let i = 0218 let accept: Accept | undefined219 let requiresSort = false // in many cases, accept values are already sorted by quality (e.g. "text/html, application/json;q=0.9, */*;q=0.8")220 let lastAccept: Accept | undefined221 while (i < acceptHeader.length) {222 ;[i, accept] = getNextAcceptValue(acceptHeader, i)223 if (accept) {fix(accept): treat the q parameter name as case-insensitive (#5349)Mochammad Fadhlan Al-Ghiffaribefore adoption65cff90
lines 224–226why, one step back224 // The "q" parameter is case-insensitive (RFC 9110 §12.4.2). `params` keeps225 // the name as it was sent, so both spellings are checked here.226 accept.q = parseQuality(accept.params.q ?? accept.params.Q)Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 227–253why, one step back227 values.push(accept)228 if (lastAccept && lastAccept.q < accept.q) {229 // find higher quality accept value, so we need to sort230 requiresSort = true231 }232 lastAccept = accept233 }234 }235 if (requiresSort) {236 values.sort((a, b) => b.q - a.q)237 }238239 return values240}241242const parseQuality = (qVal?: string): number => {243 if (qVal === undefined) {244 return 1245 }246 if (qVal === '') {247 return 1248 }249 if (qVal === 'NaN') {250 return 0251 }252253 const num = Number(qVal)fix(accept): clamp a negative q to 0, not 1 (#5357)monembefore adoption9b4e9c2
line 254why, one step back254 if (Number.isNaN(num)) {Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 255–256why, one step back255 return 1256 }fix(accept): clamp a negative q to 0, not 1 (#5357)monembefore adoption9b4e9c2
lines 257–260why, one step back257 // A qvalue is 0-1 (RFC 9110 12.4.2). Clamp rather than reject, but clamp each side to the258 // bound it is past: sending a negative q below every real one instead of above them, which259 // is what `-Infinity` already did.260 if (num < 0) {Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 261–262why, one step back261 return 0262 }fix(accept): clamp a negative q to 0, not 1 (#5357)monembefore adoption9b4e9c2
line 263why, one step back263 if (num > 1) {Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 264–268why, one step back264 return 1265 }266267 return num268}