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 #321

Merged
merged 7 commits into from
Nov 3, 2022
12 changes: 10 additions & 2 deletions pkg/protocol/http1/req/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,11 @@ func ContinueReadBodyStream(req *protocol.Request, zr network.Reader, maxBodySiz
// 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 Expand Up @@ -339,7 +343,11 @@ func ContinueReadBody(req *protocol.Request, r network.Reader, maxBodySize int,
// 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
84 changes: 84 additions & 0 deletions pkg/protocol/http1/req/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,90 @@ func TestRequestContinueReadBody(t *testing.T) {
}
}

func TestRequestReadNoBody(t *testing.T) {
t.Parallel()

var r protocol.Request

s := "GET / HTTP/1.1\r\n\r\n"

zr := mock.NewZeroCopyReader(s)
if err := Read(&r, zr); err != nil {
t.Fatalf("unexpected error: %s", err)
}
r.SetHost("foobar")
headerStr := r.Header.String()
if strings.Contains(headerStr, "Content-Length: ") {
t.Fatalf("unexpected Content-Length")
}
}

func TestRequestRead(t *testing.T) {
t.Parallel()

var r protocol.Request

s := "POST / HTTP/1.1\r\n\r\n"

zr := mock.NewZeroCopyReader(s)
if err := Read(&r, zr); err != nil {
t.Fatalf("unexpected error: %s", err)
}
r.SetHost("foobar")
headerStr := r.Header.String()
if !strings.Contains(headerStr, "Content-Length: ") {
t.Fatalf("should contain Content-Length")
}
cLen := r.Header.Peek(consts.HeaderContentLength)
if string(cLen) != "0" {
t.Fatalf("unexpected Content-Length: %s, Expecting 0", string(cLen))
}
}

func TestRequestReadNoBodyStreaming(t *testing.T) {
t.Parallel()

var r protocol.Request
r.Header.SetContentLength(-2)
r.Header.SetMethod("GET")

s := ""

zr := mock.NewZeroCopyReader(s)
if err := ContinueReadBodyStream(&r, zr, 2048, true); err != nil {
t.Fatalf("unexpected error: %s", err)
}
r.SetHost("foobar")
headerStr := r.Header.String()
if strings.Contains(headerStr, "Content-Length: ") {
t.Fatalf("unexpected Content-Length")
byene0923 marked this conversation as resolved.
Show resolved Hide resolved
}
}

func TestRequestReadStreaming(t *testing.T) {
t.Parallel()

var r protocol.Request
r.Header.SetContentLength(-2)
r.Header.SetMethod("POST")

s := ""

zr := mock.NewZeroCopyReader(s)
if err := ContinueReadBodyStream(&r, zr, 2048, true); err != nil {
t.Fatalf("unexpected error: %s", err)
}
r.SetHost("foobar")
headerStr := r.Header.String()
if !strings.Contains(headerStr, "Content-Length: ") {
t.Fatalf("should contain Content-Length")
}
cLen := r.Header.Peek(consts.HeaderContentLength)
if string(cLen) != "0" {
t.Fatalf("unexpected Content-Length: %s, Expecting 0", string(cLen))
}
}

func TestMethodAndPathAndQueryString(t *testing.T) {
s := "PUT /foo/bar?query=1 HTTP/1.1\r\nExpect: 100-continue\r\nContent-Length: 5\r\nContent-Type: foo/bar\r\n\r\nabcdef4343"
zr := mock.NewZeroCopyReader(s)
Expand Down