Skip to content

Commit e246cf6

Browse files
committed
net/http: accept HEAD requests with a body
RFC 7231 permits HEAD requests to contain a body, although it does state there are no defined semantics for payloads of HEAD requests and that some servers may reject HEAD requests with a payload. Accept HEAD requests with a body. Fix a bug where a HEAD request with a chunked body would interpret the body as the headers for the next request on the connection. For #53960. Change-Id: I83f7112fdedabd6d6291cd956151d718ee6942cd Reviewed-on: https://go-review.googlesource.com/c/go/+/418614 Run-TryBot: Damien Neil <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Cherry Mui <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 493ebc3 commit e246cf6

File tree

4 files changed

+84
-22
lines changed

4 files changed

+84
-22
lines changed

src/net/http/readrequest_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,12 @@ Content-Length: 3
450450
Content-Length: 4
451451
452452
abc`)},
453-
{"smuggle_content_len_head", reqBytes(`HEAD / HTTP/1.1
453+
{"smuggle_two_content_len_head", reqBytes(`HEAD / HTTP/1.1
454454
Host: foo
455-
Content-Length: 5`)},
455+
Content-Length: 4
456+
Content-Length: 5
457+
458+
1234`)},
456459

457460
// golang.org/issue/22464
458461
{"leading_space_in_header", reqBytes(`GET / HTTP/1.1

src/net/http/request_test.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -485,43 +485,39 @@ var readRequestErrorTests = []struct {
485485
1: {"GET / HTTP/1.1\r\nheader:foo\r\n", io.ErrUnexpectedEOF.Error(), nil},
486486
2: {"", io.EOF.Error(), nil},
487487
3: {
488-
in: "HEAD / HTTP/1.1\r\nContent-Length:4\r\n\r\n",
489-
err: "http: method cannot contain a Content-Length",
490-
},
491-
4: {
492488
in: "HEAD / HTTP/1.1\r\n\r\n",
493489
header: Header{},
494490
},
495491

496492
// Multiple Content-Length values should either be
497493
// deduplicated if same or reject otherwise
498494
// See Issue 16490.
499-
5: {
495+
4: {
500496
in: "POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 0\r\n\r\nGopher hey\r\n",
501497
err: "cannot contain multiple Content-Length headers",
502498
},
503-
6: {
499+
5: {
504500
in: "POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 6\r\n\r\nGopher\r\n",
505501
err: "cannot contain multiple Content-Length headers",
506502
},
507-
7: {
503+
6: {
508504
in: "PUT / HTTP/1.1\r\nContent-Length: 6 \r\nContent-Length: 6\r\nContent-Length:6\r\n\r\nGopher\r\n",
509505
err: "",
510506
header: Header{"Content-Length": {"6"}},
511507
},
512-
8: {
508+
7: {
513509
in: "PUT / HTTP/1.1\r\nContent-Length: 1\r\nContent-Length: 6 \r\n\r\n",
514510
err: "cannot contain multiple Content-Length headers",
515511
},
516-
9: {
512+
8: {
517513
in: "POST / HTTP/1.1\r\nContent-Length:\r\nContent-Length: 3\r\n\r\n",
518514
err: "cannot contain multiple Content-Length headers",
519515
},
520-
10: {
516+
9: {
521517
in: "HEAD / HTTP/1.1\r\nContent-Length:0\r\nContent-Length: 0\r\n\r\n",
522518
header: Header{"Content-Length": {"0"}},
523519
},
524-
11: {
520+
10: {
525521
in: "HEAD / HTTP/1.1\r\nHost: foo\r\nHost: bar\r\n\r\n\r\n\r\n",
526522
err: "too many Host headers",
527523
},

src/net/http/serve_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6907,3 +6907,73 @@ func testParseFormCleanup(t *testing.T, h2 bool) {
69076907
t.Errorf("file %q exists after HTTP handler returned", string(fname))
69086908
}
69096909
}
6910+
6911+
func TestHeadBody(t *testing.T) {
6912+
const identityMode = false
6913+
const chunkedMode = true
6914+
t.Run("h1", func(t *testing.T) {
6915+
t.Run("identity", func(t *testing.T) { testHeadBody(t, h1Mode, identityMode, "HEAD") })
6916+
t.Run("chunked", func(t *testing.T) { testHeadBody(t, h1Mode, chunkedMode, "HEAD") })
6917+
})
6918+
t.Run("h2", func(t *testing.T) {
6919+
t.Run("identity", func(t *testing.T) { testHeadBody(t, h2Mode, identityMode, "HEAD") })
6920+
t.Run("chunked", func(t *testing.T) { testHeadBody(t, h2Mode, chunkedMode, "HEAD") })
6921+
})
6922+
}
6923+
6924+
func TestGetBody(t *testing.T) {
6925+
const identityMode = false
6926+
const chunkedMode = true
6927+
t.Run("h1", func(t *testing.T) {
6928+
t.Run("identity", func(t *testing.T) { testHeadBody(t, h1Mode, identityMode, "GET") })
6929+
t.Run("chunked", func(t *testing.T) { testHeadBody(t, h1Mode, chunkedMode, "GET") })
6930+
})
6931+
t.Run("h2", func(t *testing.T) {
6932+
t.Run("identity", func(t *testing.T) { testHeadBody(t, h2Mode, identityMode, "GET") })
6933+
t.Run("chunked", func(t *testing.T) { testHeadBody(t, h2Mode, chunkedMode, "GET") })
6934+
})
6935+
}
6936+
6937+
func testHeadBody(t *testing.T, h2, chunked bool, method string) {
6938+
setParallel(t)
6939+
defer afterTest(t)
6940+
cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
6941+
b, err := io.ReadAll(r.Body)
6942+
if err != nil {
6943+
t.Errorf("server reading body: %v", err)
6944+
return
6945+
}
6946+
w.Header().Set("X-Request-Body", string(b))
6947+
w.Header().Set("Content-Length", "0")
6948+
}))
6949+
defer cst.close()
6950+
for _, reqBody := range []string{
6951+
"",
6952+
"",
6953+
"request_body",
6954+
"",
6955+
} {
6956+
var bodyReader io.Reader
6957+
if reqBody != "" {
6958+
bodyReader = strings.NewReader(reqBody)
6959+
if chunked {
6960+
bodyReader = bufio.NewReader(bodyReader)
6961+
}
6962+
}
6963+
req, err := NewRequest(method, cst.ts.URL, bodyReader)
6964+
if err != nil {
6965+
t.Fatal(err)
6966+
}
6967+
res, err := cst.c.Do(req)
6968+
if err != nil {
6969+
t.Fatal(err)
6970+
}
6971+
res.Body.Close()
6972+
if got, want := res.StatusCode, 200; got != want {
6973+
t.Errorf("%v request with %d-byte body: StatusCode = %v, want %v", method, len(reqBody), got, want)
6974+
}
6975+
if got, want := res.Header.Get("X-Request-Body"), reqBody; got != want {
6976+
t.Errorf("%v request with %d-byte body: handler read body %q, want %q", method, len(reqBody), got, want)
6977+
}
6978+
}
6979+
}

src/net/http/transfer.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ func readTransfer(msg any, r *bufio.Reader) (err error) {
557557
// or close connection when finished, since multipart is not supported yet
558558
switch {
559559
case t.Chunked:
560-
if noResponseBodyExpected(t.RequestMethod) || !bodyAllowedForStatus(t.StatusCode) {
560+
if isResponse && (noResponseBodyExpected(t.RequestMethod) || !bodyAllowedForStatus(t.StatusCode)) {
561561
t.Body = NoBody
562562
} else {
563563
t.Body = &body{src: internal.NewChunkedReader(r), hdr: msg, r: r, closing: t.Close}
@@ -691,14 +691,7 @@ func fixLength(isResponse bool, status int, requestMethod string, header Header,
691691
}
692692

693693
// Logic based on response type or status
694-
if noResponseBodyExpected(requestMethod) {
695-
// For HTTP requests, as part of hardening against request
696-
// smuggling (RFC 7230), don't allow a Content-Length header for
697-
// methods which don't permit bodies. As an exception, allow
698-
// exactly one Content-Length header if its value is "0".
699-
if isRequest && len(contentLens) > 0 && !(len(contentLens) == 1 && contentLens[0] == "0") {
700-
return 0, fmt.Errorf("http: method cannot contain a Content-Length; got %q", contentLens)
701-
}
694+
if isResponse && noResponseBodyExpected(requestMethod) {
702695
return 0, nil
703696
}
704697
if status/100 == 1 {

0 commit comments

Comments
 (0)