From e2141372b6f9007fe964cf8355852e8843fbbf2c Mon Sep 17 00:00:00 2001 From: byene0923 <34917110+byene0923@users.noreply.github.com> Date: Sun, 30 Oct 2022 00:32:08 +0800 Subject: [PATCH] fix: ignore body should not set content-length of streaming (#1406) * fix: ignore body should not set content-length of streaming https://github.com/valyala/fasthttp/pull/1022 * fix: add commit --- http.go | 6 +++++- http_test.go | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/http.go b/http.go index 97bf5d626c..62ba30ffb6 100644 --- a/http.go +++ b/http.go @@ -1287,7 +1287,11 @@ func (req *Request) ContinueReadBodyStream(r *bufio.Reader, maxBodySize int, pre // the end of body is determined by connection close. // So just ignore request body for requests without // 'Content-Length' and 'Transfer-Encoding' headers. - req.Header.SetContentLength(0) + + // refer to https://tools.ietf.org/html/rfc7230#section-3.3.2 + if !req.Header.ignoreBody() { + req.Header.SetContentLength(0) + } return nil } diff --git a/http_test.go b/http_test.go index 58bc8cb4ff..4e16f05eb9 100644 --- a/http_test.go +++ b/http_test.go @@ -1053,6 +1053,25 @@ func TestRequestReadNoBody(t *testing.T) { } } +func TestRequestReadNoBodyStreaming(t *testing.T) { + t.Parallel() + + var r Request + + r.Header.contentLength = -2 + + br := bufio.NewReader(bytes.NewBufferString("GET / HTTP/1.1\r\n\r\n")) + err := r.ContinueReadBodyStream(br, 0) + r.SetHost("foobar") + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + s := r.String() + if strings.Contains(s, "Content-Length: ") { + t.Fatalf("unexpected Content-Length") + } +} + func TestResponseWriteTo(t *testing.T) { t.Parallel()