{"record":{"id":"e12297db1903156b","repo":"valyala/fasthttp","slug":"cannot-parse-content-length-w","errorCode":null,"errorMessage":"cannot parse content-length: %w","messagePattern":"cannot parse content-length: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"header.go","lineNumber":3345,"sourceCode":"\tfor i, n := 0, len(h.h); i < n; i++ {\n\t\tkv := &h.h[i]\n\t\tif caseInsensitiveCompare(kv.key, strCookie) {\n\t\t\th.cookies = parseRequestCookies(h.cookies, kv.value)\n\t\t\ttmp := *kv\n\t\t\tcopy(h.h[i:], h.h[i+1:])\n\t\t\tn--\n\t\t\ti--\n\t\t\th.h[n] = tmp\n\t\t\th.h = h.h[:n]\n\t\t}\n\t}\n\th.cookiesCollected = true\n}\n\nfunc parseContentLength(b []byte) (int, error) {\n\tv, n, err := parseUintBuf(b)\n\tif err != nil {\n\t\treturn -1, fmt.Errorf(\"cannot parse content-length: %w\", err)\n\t}\n\tif n != len(b) {\n\t\treturn -1, fmt.Errorf(\"cannot parse content-length: %w\", ErrNonNumericChars)\n\t}\n\treturn v, nil\n}\n\ntype headerValueScanner struct {\n\tb     []byte\n\tvalue []byte\n}\n\nfunc (s *headerValueScanner) next() bool {\n\tb := s.b\n\tif len(b) == 0 {\n\t\treturn false\n\t}\n\tbefore, after, ok := bytes.Cut(b, []byte{','})","sourceCodeStart":3327,"sourceCodeEnd":3363,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/header.go#L3327-L3363","documentation":"parseContentLength failed because parseUintBuf could not read any valid unsigned integer from the Content-Length bytes (e.g. empty or starting with a non-digit). The underlying parse error is wrapped with this message.","triggerScenarios":"A Content-Length header whose value is empty or begins with a non-numeric character, e.g. 'Content-Length: ' or 'Content-Length: abc' or 'Content-Length: +12'.","commonSituations":"Hand-crafted or scripted HTTP requests with bad framing; proxy rewriting bugs truncating the value; fuzzing/scanning tools sending garbage framing headers.","solutions":["Send a plain decimal integer as Content-Length (e.g. 'Content-Length: 42').","Use chunked transfer-encoding instead if the length is unknown.","Fix the client/proxy that mangles the Content-Length value.","Log the raw header at an early middleware to identify the offending producer."],"exampleFix":"// before\nreq.Header.Set(\"Content-Length\", \"12 bytes\")\n// after\nreq.Header.Set(\"Content-Length\", strconv.Itoa(len(body)))","handlingStrategy":"validation","validationCode":"func validContentLength(cl string) bool {\n    if cl == \"\" { return false }\n    for i := 0; i < len(cl); i++ { if cl[i] < '0' || cl[i] > '9' { return false } }\n    return true\n}","typeGuard":null,"tryCatchPattern":"_, err := parseContentLength(b)\nif err != nil && strings.Contains(err.Error(), \"cannot parse content-length\") {\n    // return 400 Bad Request with framing error\n}","preventionTips":["Set Content-Length programmatically via strconv.Itoa(len(body))","Never hard-code Content-Length strings","Prefer chunked encoding when length is unknown"],"tags":["http","fasthttp","content-length","parsing"],"backgroundTag":"invalid-content-length","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}