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) {