Skip to content

Commit b0b28d5

Browse files
WeiminShangneild
authored andcommitted
http2: 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. Test is in net/http CL 418614. For golang/go#53960. Change-Id: I946d3ec796054c3908beb8a69cc78aa51c04c972 Reviewed-on: https://go-review.googlesource.com/c/net/+/418634 Run-TryBot: Damien Neil <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent ae94880 commit b0b28d5

File tree

2 files changed

+1
-21
lines changed

2 files changed

+1
-21
lines changed

http2/server.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,12 +2099,6 @@ func (sc *serverConn) newWriterAndRequest(st *stream, f *MetaHeadersFrame) (*res
20992099
return nil, nil, sc.countError("bad_path_method", streamError(f.StreamID, ErrCodeProtocol))
21002100
}
21012101

2102-
bodyOpen := !f.StreamEnded()
2103-
if rp.method == "HEAD" && bodyOpen {
2104-
// HEAD requests can't have bodies
2105-
return nil, nil, sc.countError("head_body", streamError(f.StreamID, ErrCodeProtocol))
2106-
}
2107-
21082102
rp.header = make(http.Header)
21092103
for _, hf := range f.RegularFields() {
21102104
rp.header.Add(sc.canonicalHeader(hf.Name), hf.Value)
@@ -2117,6 +2111,7 @@ func (sc *serverConn) newWriterAndRequest(st *stream, f *MetaHeadersFrame) (*res
21172111
if err != nil {
21182112
return nil, nil, err
21192113
}
2114+
bodyOpen := !f.StreamEnded()
21202115
if bodyOpen {
21212116
if vv, ok := rp.header["Content-Length"]; ok {
21222117
if cl, err := strconv.ParseUint(vv[0], 10, 63); err == nil {

http2/server_test.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3388,21 +3388,6 @@ func TestConfigureServer(t *testing.T) {
33883388
}
33893389
}
33903390

3391-
func TestServerRejectHeadWithBody(t *testing.T) {
3392-
st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
3393-
// No response body.
3394-
})
3395-
defer st.Close()
3396-
st.greet()
3397-
st.writeHeaders(HeadersFrameParam{
3398-
StreamID: 1, // clients send odd numbers
3399-
BlockFragment: st.encodeHeader(":method", "HEAD"),
3400-
EndStream: false, // what we're testing, a bogus HEAD request with body
3401-
EndHeaders: true,
3402-
})
3403-
st.wantRSTStream(1, ErrCodeProtocol)
3404-
}
3405-
34063391
func TestServerNoAutoContentLengthOnHead(t *testing.T) {
34073392
st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
34083393
// No response body. (or smaller than one frame)

0 commit comments

Comments
 (0)