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

A response without a body can't have trailers #1825

Merged
merged 1 commit into from
Aug 20, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1365,9 +1365,7 @@ func (c *HostClient) do(req *Request, resp *Response) (bool, error) {
defer ReleaseResponse(resp)
}

ok, err := c.doNonNilReqResp(req, resp)

return ok, err
return c.doNonNilReqResp(req, resp)
}

func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error) {
Expand Down
49 changes: 49 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3484,3 +3484,52 @@ func TestDialTimeout(t *testing.T) {
})
}
}

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

ln := fasthttputil.NewInmemoryListener()
defer ln.Close()

go func() {
c, err := ln.Accept()
if err != nil {
t.Error(err)
}
c.Write([]byte("HTTP/1.1 200 OK\r\n" + //nolint:errcheck
"content-type: text/plain\r\n" +
"transfer-encoding: chunked\r\n\r\n" +
"24\r\nThis is the data in the first chunk \r\n" +
"1B\r\nand this is the second one \r\n" +
"0\r\n\r\n",
))
}()

c := &Client{
Dial: func(addr string) (net.Conn, error) {
return ln.Dial()
},
ReadTimeout: time.Millisecond * 10,
MaxIdemponentCallAttempts: 1,
}

req := AcquireRequest()
req.SetRequestURI("http://127.0.0.1:7070")
req.Header.SetMethod(MethodHead)

resp := AcquireResponse()

err := c.Do(req, resp)
if err != nil {
t.Fatal(err)
}

// The second request on the same connection is going to give a timeout as it can't find
// a proper request in what is left on the connection.
err = c.Do(req, resp)
if err == nil {
t.Error("expected timeout error")
} else if err != ErrTimeout {
t.Error(err)
}
}
2 changes: 2 additions & 0 deletions header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2961,6 +2961,8 @@ func verifyRequestHeader(t *testing.T, h *RequestHeader, expectedContentLength i
}

func verifyResponseTrailer(t *testing.T, h *ResponseHeader, expectedTrailers map[string]string) {
t.Helper()

for k, v := range expectedTrailers {
got := h.Peek(k)
if !bytes.Equal(got, []byte(v)) {
Expand Down
3 changes: 2 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,8 @@ func (resp *Response) ReadLimitBody(r *bufio.Reader, maxBodySize int) error {
}
}

if resp.Header.ContentLength() == -1 && !resp.StreamBody {
// A response without a body can't have trailers.
if resp.Header.ContentLength() == -1 && !resp.StreamBody && !resp.mustSkipBody() {
err = resp.Header.ReadTrailer(r)
if err != nil && err != io.EOF {
if isConnectionReset(err) {
Expand Down
17 changes: 9 additions & 8 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2021,25 +2021,27 @@ func TestResponseReadWithoutBody(t *testing.T) {
var resp Response

testResponseReadWithoutBody(t, &resp, "HTTP/1.1 304 Not Modified\r\nContent-Type: aa\r\nContent-Length: 1235\r\n\r\n", false,
304, 1235, "aa", nil)
304, 1235, "aa")

testResponseReadWithoutBody(t, &resp, "HTTP/1.1 204 Foo Bar\r\nContent-Type: aab\r\nTransfer-Encoding: chunked\r\n\r\n0\r\nFoo: bar\r\n\r\n", false,
204, -1, "aab", map[string]string{"Foo": "bar"})
testResponseReadWithoutBody(t, &resp, "HTTP/1.1 204 Foo Bar\r\nContent-Type: aab\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n\r\n", false,
204, -1, "aab")

testResponseReadWithoutBody(t, &resp, "HTTP/1.1 123 AAA\r\nContent-Type: xxx\r\nContent-Length: 3434\r\n\r\n", false,
123, 3434, "xxx", nil)
123, 3434, "xxx")

testResponseReadWithoutBody(t, &resp, "HTTP 200 OK\r\nContent-Type: text/xml\r\nContent-Length: 123\r\n\r\nfoobar\r\n", true,
200, 123, "text/xml", nil)
200, 123, "text/xml")

// '100 Continue' must be skipped.
testResponseReadWithoutBody(t, &resp, "HTTP/1.1 100 Continue\r\nFoo-bar: baz\r\n\r\nHTTP/1.1 329 aaa\r\nContent-Type: qwe\r\nContent-Length: 894\r\n\r\n", true,
329, 894, "qwe", nil)
329, 894, "qwe")
}

func testResponseReadWithoutBody(t *testing.T, resp *Response, s string, skipBody bool,
expectedStatusCode, expectedContentLength int, expectedContentType string, expectedTrailer map[string]string,
expectedStatusCode, expectedContentLength int, expectedContentType string,
) {
t.Helper()

r := bytes.NewBufferString(s)
rb := bufio.NewReader(r)
resp.SkipBody = skipBody
Expand All @@ -2051,7 +2053,6 @@ func testResponseReadWithoutBody(t *testing.T, resp *Response, s string, skipBod
t.Fatalf("Unexpected response body %q. Expected %q. response=%q", resp.Body(), "", s)
}
verifyResponseHeader(t, &resp.Header, expectedStatusCode, expectedContentLength, expectedContentType, "")
verifyResponseTrailer(t, &resp.Header, expectedTrailer)

// verify that ordinal response is read after null-body response
resp.SkipBody = false
Expand Down