MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / parseChallenges

Function parseChallenges

packages/core/sdk/src/www-authenticate.ts:34–166  ·  view source on GitHub ↗
(header: string)

Source from the content-addressed store, hash-verified

32
33/** Parse authentication challenges without joining parameters across schemes. */
34export const parseChallenges = (header: string): readonly Challenge[] | null => {
35 const len = header.length;
36 const challenges: Challenge[] = [];
37 let current: Challenge | null = null;
38 let state: "boundary" | "scheme" | "token68" | "params" = "boundary";
39 let sawComma = true; // header start counts as a list boundary
40 let i = 0;
41
42 const readWord = (): string => {
43 const start = i;
44 while (i < len && WORD_RE.test(header[i]!)) i += 1;
45 return header.slice(start, i);
46 };
47 // Returns null on an unterminated quote or a quote run into the next token.
48 const readQuoted = (): string | null => {
49 let value = "";
50 i += 1; // opening quote
51 while (i < len) {
52 const ch = header[i]!;
53 if (ch === '"') {
54 i += 1;
55 return i >= len || /[\s,]/.test(header[i]!) ? value : null;
56 }
57 if (ch === "\\" && i + 1 < len) {
58 value += header[i + 1];
59 i += 2;
60 continue;
61 }
62 value += ch;
63 i += 1;
64 }
65 return null; // unterminated
66 };
67
68 while (i < len) {
69 while (i < len && /\s/.test(header[i]!)) i += 1;
70 if (i >= len) break;
71 if (header[i] === ",") {
72 sawComma = true;
73 i += 1;
74 continue;
75 }
76 if (!WORD_RE.test(header[i]!)) return null; // stray quote/byte: malformed
77 const word = readWord();
78 // Look ahead through BWS for `=` to classify the word.
79 let j = i;
80 while (j < len && /[ \t]/.test(header[j]!)) j += 1;
81 const isPaddingRun = (() => {
82 // An `=`-run directly on the word (no BWS) that is followed (after
83 // optional whitespace) by a comma or the end of input is token68
84 // padding. An `=` followed by a value — even across BWS — is an
85 // auth-param (RFC 7230 allows BWS around `=`).
86 if (header[i] !== "=") return false;
87 let k = i;
88 while (k < len && header[k] === "=") k += 1;
89 while (k < len && /[ \t]/.test(header[k]!)) k += 1;
90 return k >= len || header[k] === ",";
91 })();

Callers 3

detectFromChallengeFunction · 0.90

Calls 7

readWordFunction · 0.85
isToken68Function · 0.85
isTokenFunction · 0.85
readQuotedFunction · 0.85
isUrlishFunction · 0.85
pushMethod · 0.80
setMethod · 0.65

Tested by

no test coverage detected