From a548c35b5f5789e3fc76bdfe5ab04cb100494bde Mon Sep 17 00:00:00 2001 From: byene0923 Date: Mon, 24 Oct 2022 22:38:52 +0800 Subject: [PATCH 1/2] fix: ignore body should not set content-length of streaming https://github.com/valyala/fasthttp/pull/1022 --- http.go | 4 +++- http_test.go | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/http.go b/http.go index 97bf5d626c..8a7bc0f007 100644 --- a/http.go +++ b/http.go @@ -1287,7 +1287,9 @@ 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) + 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() From e2b590fc49fdf88f1bdaaaaffc299a1fb0020ca5 Mon Sep 17 00:00:00 2001 From: byene0923 Date: Fri, 28 Oct 2022 01:25:50 +0800 Subject: [PATCH 2/2] fix: add commit --- http.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/http.go b/http.go index 8a7bc0f007..62ba30ffb6 100644 --- a/http.go +++ b/http.go @@ -1287,6 +1287,8 @@ 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. + + // refer to https://tools.ietf.org/html/rfc7230#section-3.3.2 if !req.Header.ignoreBody() { req.Header.SetContentLength(0) }