From e8f4d3b7a3ebe50a894dfc8c8422fad15fec78eb Mon Sep 17 00:00:00 2001 From: Patricio Whittingslow Date: Tue, 28 Jul 2026 20:45:35 -0300 Subject: [PATCH] update documentation on ContentLength methods and fix bug in Form reset on empty body --- http/httphi/exchange.go | 7 ++++--- http/httpraw/header.go | 7 ++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/http/httphi/exchange.go b/http/httphi/exchange.go index 4ed1fd9..e300705 100644 --- a/http/httphi/exchange.go +++ b/http/httphi/exchange.go @@ -404,10 +404,9 @@ func (exch *Exchange) RequestContentType() []byte { } // RequestContentLength returns the body length declared by the request's -// Content-Length field. An absent field is not a client error: such a request -// has no body at all, RFC 9112 6.3. Check for the error to answer 411 instead. +// Content-Length field. An absent field is signalled with present=false and no error. // See [httpraw.Header.ContentLength]. -func (exch *Exchange) RequestContentLength() (int64, bool, error) { +func (exch *Exchange) RequestContentLength() (_ int64, present bool, _ error) { return exch.RequestHeaderRaw().ContentLength() } @@ -432,8 +431,10 @@ func (exch *Exchange) RequestParseForm(dst *httpraw.Form, buf []byte) error { // wire would parse chunk sizes as form data. httpraw does not decode them. return errUnsupportedTransferCoding } + length, present, err := exch.RequestContentLength() if !present { + dst.Reset(nil) return nil // No length is no body, RFC 9112 6.3. } else if err != nil { return err diff --git a/http/httpraw/header.go b/http/httpraw/header.go index 9150481..ba474ee 100644 --- a/http/httpraw/header.go +++ b/http/httpraw/header.go @@ -395,11 +395,8 @@ func (h *Header) NormalizeKeys() { } // ContentLength returns the body length declared by the Content-Length field. -// Fails with an error if the field is absent, which for a request means the -// message has no body at all unless a transfer coding applies, RFC 9112 6.3. -// The value must be digits only, so a negative or list-valued field is rejected -// rather than guessed at. -func (h *Header) ContentLength() (int64, bool, error) { +// If the field is not present then the returned bool is false. Will return error for invalid or non-integer value. +func (h *Header) ContentLength() (_ int64, present bool, _ error) { value := h.GetFold(headerContentLength) if value == nil { return 0, false, nil