{"record":{"id":"a8a86b58bcb6d6f6","repo":"valyala/fasthttp","slug":"error-when-reading-request-headers-w-n-d-read","errorCode":null,"errorMessage":"error when reading request headers: %w (n=%d, reader buffered=%d)","messagePattern":"error when reading request headers: %w \\(n=(.+?), reader buffered=(.+?)\\)","errorType":"exception","errorClass":"ErrSmallBuffer","httpStatus":null,"severity":"error","filePath":"header.go","lineNumber":2346,"sourceCode":"\t}\n}\n\nfunc (h *RequestHeader) tryRead(r *bufio.Reader, n int) error {\n\th.resetSkipNormalize()\n\tb, err := r.Peek(n)\n\tif len(b) == 0 {\n\t\tif err == io.EOF {\n\t\t\treturn err\n\t\t}\n\n\t\tif err == nil {\n\t\t\tpanic(\"bufio.Reader.Peek() returned nil, nil\")\n\t\t}\n\n\t\t// This is for go 1.6 bug. See https://github.com/golang/go/issues/14121 .\n\t\tif err == bufio.ErrBufferFull {\n\t\t\treturn &ErrSmallBuffer{\n\t\t\t\terror: fmt.Errorf(\"error when reading request headers: %w (n=%d, reader buffered=%d)\", ErrSmallReadBuffer, n, r.Buffered()),\n\t\t\t}\n\t\t}\n\n\t\t// n == 1 on the first read for the request.\n\t\tif n == 1 {\n\t\t\t// We didn't read a single byte.\n\t\t\treturn ErrNothingRead{error: err}\n\t\t}\n\n\t\treturn fmt.Errorf(\"error when reading request headers: %w\", err)\n\t}\n\tb = mustPeekBuffered(r)\n\theadersLen, errParse := h.parse(b)\n\tif errParse != nil {\n\t\treturn headerError(\"request\", err, errParse, b, h.secureErrorLogMessage)\n\t}\n\tif errValidate := h.validate(); errValidate != nil {\n\t\treturn headerError(\"request\", err, errValidate, b, h.secureErrorLogMessage)","sourceCodeStart":2328,"sourceCodeEnd":2364,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/header.go#L2328-L2364","documentation":"fasthttp wraps ErrSmallReadBuffer in an ErrSmallBuffer when reading request headers fills the bufio buffer without reaching the header terminator (\\r\\n\\r\\n). The error reports how many bytes were read and how many the reader had buffered, pinpointing that the configured buffer is too small for the incoming header block.","triggerScenarios":"Server.ReadBufferSize (default 4096) is smaller than the total size of an incoming request's headers, so bufio.Reader.Peek returns bufio.ErrBufferFull inside RequestHeader.Read.","commonSituations":"Clients sending many/large cookies, long authorization JWTs, or custom tracing headers; servers left at default ReadBufferSize behind proxies that append headers (X-Forwarded-*, CDN headers).","solutions":["Increase Server.ReadBufferSize so it exceeds the largest expected request header block","Reduce header size on the client side (trim cookies, compress tokens, shorten tracing headers)","If you control the proxy, strip unnecessary forwarded headers before they reach fasthttp"],"exampleFix":"// before\nsrv := &fasthttp.Server{Handler: h} // ReadBufferSize=4096\n// after\nsrv := &fasthttp.Server{Handler: h, ReadBufferSize: 32768}","handlingStrategy":"validation","validationCode":"// ensure server buffer can hold worst-case headers\nconst maxHeaderBlock = 32 * 1024\nsrv := &fasthttp.Server{ReadBufferSize: maxHeaderBlock}\n// client-side: keep request headers under the server's buffer\nif len(cookieHeader) > 8*1024 {\n    return errors.New(\"cookie header too large\")\n}","typeGuard":"func isSmallBufferErr(err error) bool {\n    var e *fasthttp.ErrSmallBuffer\n    return errors.As(err, &e)\n}","tryCatchPattern":"if err := h.Read(br); err != nil {\n    var sbe *fasthttp.ErrSmallBuffer\n    if errors.As(err, &sbe) {\n        return respondStatusBadRequest(\"headers too large\")\n    }\n    return err\n}","preventionTips":["Raise ReadBufferSize above the largest expected header block","Keep cookies and auth tokens compact","Strip redundant forwarded headers at the proxy","Test with header-heavy clients during load testing"],"tags":["http","buffer-size","fasthttp"],"backgroundTag":"http-read-buffer-too-small","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}