Skip to content

Commit

Permalink
http2: exclude some header from 1xx responses
Browse files Browse the repository at this point in the history
Content-Length and Transfer-Encoding must not be sent when the response
has no body.

Necessary to fix the tests of golang/go#42597.

Change-Id: Ibe90048bb122cc3ad1e04f8ebf9aa966b40489ae
GitHub-Last-Rev: 2605919859d0b6b4b5d41da68c8bc3efddea4cbf
GitHub-Pull-Request: golang/net#134
Reviewed-on: https://go-review.googlesource.com/c/net/+/406494
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
Run-TryBot: Damien Neil <dneil@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
  • Loading branch information
WeiminShang authored and neild committed May 17, 2022
1 parent 314f518 commit 13d9946
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions http2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2677,6 +2677,14 @@ func (rws *responseWriterState) writeHeader(code int) {
// Per RFC 8297 we must not clear the current header map
h := rws.handlerHeader

_, cl := h["Content-Length"]
_, te := h["Transfer-Encoding"]
if cl || te {
h = h.Clone()
h.Del("Content-Length")
h.Del("Transfer-Encoding")
}

if rws.conn.writeHeaders(rws.stream, &writeResHeaders{
streamID: rws.stream.id,
httpResCode: code,
Expand Down
3 changes: 2 additions & 1 deletion http2/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4392,6 +4392,7 @@ func TestServerSendsProcessing(t *testing.T) {
func TestServerSendsEarlyHints(t *testing.T) {
testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error {
h := w.Header()
h.Add("Content-Length", "123")
h.Add("Link", "</style.css>; rel=preload; as=style")
h.Add("Link", "</script.js>; rel=preload; as=script")
w.WriteHeader(http.StatusEarlyHints)
Expand Down Expand Up @@ -4437,7 +4438,7 @@ func TestServerSendsEarlyHints(t *testing.T) {
{"link", "</script.js>; rel=preload; as=script"},
{"link", "</foo.js>; rel=preload; as=script"},
{"content-type", "text/plain; charset=utf-8"},
{"content-length", "5"},
{"content-length", "123"},
}

if !reflect.DeepEqual(goth, wanth) {
Expand Down

0 comments on commit 13d9946

Please sign in to comment.