From 5ed5e26516d6f8994bb142742dc62a63102f2b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Sat, 14 Nov 2020 17:25:01 +0100 Subject: [PATCH] x/net/http2: allow sending 1xx responses Currently, it's not possible to send informational responses such as 103 Early Hints or 102 Processing. This patch allows calling WriteHeader() multiple times in order to send informational responses before the final one. In conformance with RFC 8297, if the status code is 103 the current content of the header map is also sent. Its content is not removed after the call to WriteHeader() because the headers must also be included in the final response. The Chrome and Fastly teams are starting a large-scale experiment to measure the real-life impact of the 103 status code. Using Early Hints is proposed as a (partial) alternative to Server Push, which are going to be removed from Chrome: https://groups.google.com/a/chromium.org/g/blink-dev/c/K3rYLvmQUBY/m/21anpFhxAQAJ Being able to send this status code from servers implemented using Go would help to see if implementing it in browsers is worth it. Fixes #26089. Fixes #36734. Updates #26088. Updates #42597. --- http2/server.go | 38 +++++++++++++++---- http2/server_test.go | 89 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 8 deletions(-) diff --git a/http2/server.go b/http2/server.go index 2aa859f76f..9716cb4fad 100644 --- a/http2/server.go +++ b/http2/server.go @@ -2611,8 +2611,7 @@ func checkWriteHeaderCode(code int) { // Issue 22880: require valid WriteHeader status codes. // For now we only enforce that it's three digits. // In the future we might block things over 599 (600 and above aren't defined - // at http://httpwg.org/specs/rfc7231.html#status.codes) - // and we might block under 200 (once we have more mature 1xx support). + // at http://httpwg.org/specs/rfc7231.html#status.codes). // But for now any three digits. // // We used to send "HTTP/1.1 000 0" on the wire in responses but there's @@ -2633,13 +2632,36 @@ func (w *responseWriter) WriteHeader(code int) { } func (rws *responseWriterState) writeHeader(code int) { - if !rws.wroteHeader { - checkWriteHeaderCode(code) - rws.wroteHeader = true - rws.status = code - if len(rws.handlerHeader) > 0 { - rws.snapHeader = cloneHeader(rws.handlerHeader) + if rws.wroteHeader { + return + } + + checkWriteHeaderCode(code) + + // Handle informational headers, except 100 (Continue) which is handled automatically + if code > 100 && code < 200 { + var h http.Header + if code == 103 { + // Per RFC 8297 we must not clear the current header map + h = rws.handlerHeader + } + + if rws.conn.writeHeaders(rws.stream, &writeResHeaders{ + streamID: rws.stream.id, + httpResCode: code, + h: h, + endStream: rws.handlerDone && !rws.hasTrailers(), + }) != nil { + rws.dirty = true } + + return + } + + rws.wroteHeader = true + rws.status = code + if len(rws.handlerHeader) > 0 { + rws.snapHeader = cloneHeader(rws.handlerHeader) } } diff --git a/http2/server_test.go b/http2/server_test.go index 7f644d4bbb..d9d71e370c 100644 --- a/http2/server_test.go +++ b/http2/server_test.go @@ -4267,3 +4267,92 @@ func TestServerWindowUpdateOnBodyClose(t *testing.T) { t.Error(err) } } + +func TestServerSendsEarlyHints(t *testing.T) { + testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { + h := w.Header() + h.Add("Link", "; rel=preload; as=style") + h.Add("Link", "; rel=preload; as=script") + w.WriteHeader(http.StatusEarlyHints) + + h.Add("Link", "; rel=preload; as=script") + w.WriteHeader(http.StatusEarlyHints) + + w.Write([]byte("stuff")) + + return nil + }, func(st *serverTester) { + getSlash(st) + hf := st.wantHeaders() + goth := st.decodeHeader(hf.HeaderBlockFragment()) + wanth := [][2]string{ + {":status", "103"}, + {"link", "; rel=preload; as=style"}, + {"link", "; rel=preload; as=script"}, + } + + if !reflect.DeepEqual(goth, wanth) { + t.Errorf("Got = %q; want %q", goth, wanth) + } + + hf = st.wantHeaders() + goth = st.decodeHeader(hf.HeaderBlockFragment()) + wanth = [][2]string{ + {":status", "103"}, + {"link", "; rel=preload; as=style"}, + {"link", "; rel=preload; as=script"}, + {"link", "; rel=preload; as=script"}, + } + + if !reflect.DeepEqual(goth, wanth) { + t.Errorf("Got = %q; want %q", goth, wanth) + } + + hf = st.wantHeaders() + goth = st.decodeHeader(hf.HeaderBlockFragment()) + wanth = [][2]string{ + {":status", "200"}, + {"link", "; rel=preload; as=style"}, + {"link", "; rel=preload; as=script"}, + {"link", "; rel=preload; as=script"}, + {"content-type", "text/plain; charset=utf-8"}, + {"content-length", "5"}, + } + + if !reflect.DeepEqual(goth, wanth) { + t.Errorf("Got = %q; want %q", goth, wanth) + } + }) +} + +func TestServerSendsProcessing(t *testing.T) { + testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { + w.WriteHeader(http.StatusProcessing) + w.Write([]byte("stuff")) + + return nil + }, func(st *serverTester) { + getSlash(st) + hf := st.wantHeaders() + goth := st.decodeHeader(hf.HeaderBlockFragment()) + wanth := [][2]string{ + {":status", "102"}, + } + + if !reflect.DeepEqual(goth, wanth) { + t.Errorf("Got = %q; want %q", goth, wanth) + } + + hf = st.wantHeaders() + goth = st.decodeHeader(hf.HeaderBlockFragment()) + wanth = [][2]string{ + {":status", "200"}, + {"content-type", "text/plain; charset=utf-8"}, + {"content-length", "5"}, + } + + if !reflect.DeepEqual(goth, wanth) { + t.Errorf("Got = %q; want %q", goth, wanth) + } + }) +}