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 <gobot@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
Run-TryBot: Damien Neil <dneil@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
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 @@
 		// 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 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)
@@ -4437,7 +4438,7 @@
 			{"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) {