{"record":{"id":"af993ef826b82072","repo":"can1357/oh-my-pi","slug":"invalid-content-length-header-value","errorCode":null,"errorMessage":"Invalid Content-Length header: ${value}","messagePattern":"Invalid Content-Length header: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/cli/claude-trace-cli.ts","lineNumber":184,"sourceCode":"\treturn undefined;\n}\n\nfunction hasChunkedTransfer(headers: readonly HeaderEntry[]): boolean {\n\tconst value = headerValue(headers, \"transfer-encoding\");\n\treturn (\n\t\tvalue\n\t\t\t?.toLowerCase()\n\t\t\t.split(\",\")\n\t\t\t.some(part => part.trim() === \"chunked\") === true\n\t);\n}\n\nfunction contentLength(headers: readonly HeaderEntry[]): number {\n\tconst value = headerValue(headers, \"content-length\");\n\tif (!value) return 0;\n\tconst parsed = Number.parseInt(value, 10);\n\tif (!Number.isSafeInteger(parsed) || parsed < 0) {\n\t\tthrow new Error(`Invalid Content-Length header: ${value}`);\n\t}\n\treturn parsed;\n}\ninterface PendingCapturedRequest {\n\ttarget: string;\n\trequest: CapturedRequest;\n}\n\nfunction parseHeaders(headText: string): { startLine: string; headers: HeaderEntry[] } {\n\tconst lines = headText.split(\"\\r\\n\");\n\tconst startLine = lines[0] ?? \"\";\n\tconst headers: HeaderEntry[] = [];\n\tfor (let i = 1; i < lines.length; i++) {\n\t\tconst line = lines[i]!;\n\t\tconst colon = line.indexOf(\":\");\n\t\tif (colon <= 0) continue;\n\t\theaders.push({ name: line.slice(0, colon), value: line.slice(colon + 1).trim() });\n\t}","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/cli/claude-trace-cli.ts#L166-L202","documentation":"contentLength parses the Content-Length HTTP header from captured trace headers. It returns 0 when the header is absent, but if present the value must parse as a non-negative safe integer; anything else (negative, non-numeric, overflow) throws this error rather than returning a corrupt length.","triggerScenarios":"Replaying/analyzing a Claude trace whose captured request/response headers contain a malformed Content-Length, e.g. \"abc\", \"-5\", or a value above Number.MAX_SAFE_INTEGER.","commonSituations":"Hand-edited or corrupted trace files; proxies injecting chunked-transfer responses with a bogus Content-Length; traces recorded by non-conforming tools; extremely large bodies exceeding safe-integer range.","solutions":["Open the trace file and fix or remove the malformed content-length header value","Re-capture the trace from the source request/response","If the response was chunked, remove the bogus Content-Length header so contentLength() returns 0 and framing falls back to chunk handling"],"exampleFix":"// before (trace header)\ncontent-length: -1\n// after\ncontent-length: 1024   // or delete the header entirely","handlingStrategy":"type-guard","validationCode":"const raw = headerValue(headers, \"content-length\");\nif (raw !== undefined && (!/^\\d+$/.test(raw) || !Number.isSafeInteger(Number(raw))))\n  throw new Error(`Trace has malformed Content-Length: ${raw}`);","typeGuard":"function isValidContentLength(value: string | undefined): value is string {\n  return value !== undefined && /^\\d+$/.test(value) && Number.isSafeInteger(Number(value));\n}","tryCatchPattern":"try {\n  const len = contentLength(headers);\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith(\"Invalid Content-Length\")) {\n    // fall back to chunked framing or skip this captured entry\n    const len = 0;\n  } else throw err;\n}","preventionTips":["Sanitize trace files before replay: validate header values are numeric","Remove Content-Length from chunked-transfer entries when re-capturing","Re-capture traces rather than hand-editing header values"],"tags":["http","header-parsing","trace-replay"],"backgroundTag":"invalid-content-length-header","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}