Skip to content

Commit 183a9ca

Browse files
dunglasneild
authored andcommitted
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: 2605919 GitHub-Pull-Request: #134 Reviewed-on: https://go-review.googlesource.com/c/net/+/406494 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Damien Neil <[email protected]> Run-TryBot: Damien Neil <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent 20f9603 commit 183a9ca

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

http2/server.go

+8
Original file line numberDiff line numberDiff line change
@@ -2677,6 +2677,14 @@ func (rws *responseWriterState) writeHeader(code int) {
26772677
// Per RFC 8297 we must not clear the current header map
26782678
h := rws.handlerHeader
26792679

2680+
_, cl := h["Content-Length"]
2681+
_, te := h["Transfer-Encoding"]
2682+
if cl || te {
2683+
h = h.Clone()
2684+
h.Del("Content-Length")
2685+
h.Del("Transfer-Encoding")
2686+
}
2687+
26802688
if rws.conn.writeHeaders(rws.stream, &writeResHeaders{
26812689
streamID: rws.stream.id,
26822690
httpResCode: code,

http2/server_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4392,6 +4392,7 @@ func TestServerSendsProcessing(t *testing.T) {
43924392
func TestServerSendsEarlyHints(t *testing.T) {
43934393
testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error {
43944394
h := w.Header()
4395+
h.Add("Content-Length", "123")
43954396
h.Add("Link", "</style.css>; rel=preload; as=style")
43964397
h.Add("Link", "</script.js>; rel=preload; as=script")
43974398
w.WriteHeader(http.StatusEarlyHints)
@@ -4437,7 +4438,7 @@ func TestServerSendsEarlyHints(t *testing.T) {
44374438
{"link", "</script.js>; rel=preload; as=script"},
44384439
{"link", "</foo.js>; rel=preload; as=script"},
44394440
{"content-type", "text/plain; charset=utf-8"},
4440-
{"content-length", "5"},
4441+
{"content-length", "123"},
44414442
}
44424443

44434444
if !reflect.DeepEqual(goth, wanth) {

0 commit comments

Comments
 (0)