From 183a9ca12b87817e0ced91cdd139606cbb193ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 17 May 2022 16:11:44 +0000 Subject: [PATCH] http2: exclude some header from 1xx responses 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 Reviewed-by: Damien Neil Run-TryBot: Damien Neil Reviewed-by: Michael Knyszek --- http2/server.go | 8 ++++++++ http2/server_test.go | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/http2/server.go b/http2/server.go index 5e2583c..2d859af 100644 --- a/http2/server.go +++ b/http2/server.go @@ -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, diff --git a/http2/server_test.go b/http2/server_test.go index ef5ad5b..9caf713 100644 --- a/http2/server_test.go +++ b/http2/server_test.go @@ -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", "; rel=preload; as=style") h.Add("Link", "; rel=preload; as=script") w.WriteHeader(http.StatusEarlyHints) @@ -4437,7 +4438,7 @@ func TestServerSendsEarlyHints(t *testing.T) { {"link", "; rel=preload; as=script"}, {"link", "; rel=preload; as=script"}, {"content-type", "text/plain; charset=utf-8"}, - {"content-length", "5"}, + {"content-length", "123"}, } if !reflect.DeepEqual(goth, wanth) {