Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore body should not set content-length of streaming #1406

Merged
merged 2 commits into from Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion http.go
Expand Up @@ -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() {
byene0923 marked this conversation as resolved.
Show resolved Hide resolved
req.Header.SetContentLength(0)
}
return nil
}

Expand Down
19 changes: 19 additions & 0 deletions http_test.go
Expand Up @@ -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()

Expand Down