Skip to content

Commit

Permalink
fix: ignore body should not set content-length of streaming (#1406)
Browse files Browse the repository at this point in the history
* fix: ignore body should not set content-length of streaming #1022

* fix: add commit
  • Loading branch information
byene0923 committed Oct 29, 2022
1 parent 128e9b3 commit e214137
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
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() {
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

0 comments on commit e214137

Please sign in to comment.