MCPcopy Create free account
hub / github.com/Effect-TS/effect / parseHeader

Function parseHeader

packages/effect/src/unstable/http/Cookies.ts:925–962  ·  view source on GitHub ↗
(header: string)

Source from the content-addressed store, hash-verified

923 * @since 4.0.0
924 */
925export function parseHeader(header: string): Record<string, string> {
926 const result: Record<string, string> = {}
927
928 const strLen = header.length
929 let pos = 0
930 let terminatorPos = 0
931
932 while (true) {
933 if (terminatorPos === strLen) break
934 terminatorPos = header.indexOf(";", pos)
935 if (terminatorPos === -1) terminatorPos = strLen // This is the last pair
936
937 let eqIdx = header.indexOf("=", pos)
938 if (eqIdx === -1) break // No key-value pairs left
939 if (eqIdx > terminatorPos) {
940 // Malformed key-value pair
941 pos = terminatorPos + 1
942 continue
943 }
944
945 const key = header.substring(pos, eqIdx++).trim()
946 if (!Object.hasOwn(result, key)) {
947 const val = header.charCodeAt(eqIdx) === 0x22
948 ? header.substring(eqIdx + 1, terminatorPos - 1).trim()
949 : header.substring(eqIdx, terminatorPos).trim()
950
951 InternalRecord.assignProperty(
952 result,
953 key,
954 !(val.indexOf("%") === -1) ? tryDecodeURIComponent(val) : val
955 )
956 }
957
958 pos = terminatorPos + 1
959 }
960
961 return result
962}
963
964const tryDecodeURIComponent = (str: string): string => {
965 try {

Callers

nothing calls this directly

Calls 1

tryDecodeURIComponentFunction · 0.85

Tested by

no test coverage detected