From 0d36e8de5e4a4396a05d881c8b744ac2f60d3b93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Fri, 13 Nov 2020 19:38:03 +0100 Subject: [PATCH] net/http: 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. If the status code is in the 1xx range, 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. --- src/net/http/serve_test.go | 33 +++++++++++++++++++++++++++++++++ src/net/http/server.go | 30 +++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go index 404cca0825a85a..2a9daeffcf55d1 100644 --- a/src/net/http/serve_test.go +++ b/src/net/http/serve_test.go @@ -6725,3 +6725,36 @@ func testMaxBytesHandler(t *testing.T, maxSize, requestSize int64) { t.Errorf("expected echo of size %d; got %d", handlerN, buf.Len()) } } + +func TestEarlyHints(t *testing.T) { + ht := newHandlerTest(HandlerFunc(func(w ResponseWriter, r *Request) { + h := w.Header() + h.Add("Link", "; rel=preload; as=style") + h.Add("Link", "; rel=preload; as=script") + w.WriteHeader(StatusEarlyHints) + + h.Add("Link", "; rel=preload; as=script") + w.WriteHeader(StatusEarlyHints) + + w.Write([]byte("stuff")) + })) + + got := ht.rawResponse("GET / HTTP/1.1\nHost: golang.org") + expected := "HTTP/1.1 103 Early Hints\r\nLink: ; rel=preload; as=style\r\nLink: ; rel=preload; as=script\r\n\r\nHTTP/1.1 103 Early Hints\r\nLink: ; rel=preload; as=style\r\nLink: ; rel=preload; as=script\r\nLink: ; rel=preload; as=script\r\n\r\nHTTP/1.1 200 OK\r\nLink: ; rel=preload; as=style\r\nLink: ; rel=preload; as=script\r\nLink: ; rel=preload; as=script\r\nDate: " // dynamic content expected + if !strings.Contains(got, expected) { + t.Errorf("unexpected response; got %q; should start by %q", got, expected) + } +} + +func TestProcessing(t *testing.T) { + ht := newHandlerTest(HandlerFunc(func(w ResponseWriter, r *Request) { + w.WriteHeader(StatusProcessing) + w.Write([]byte("stuff")) + })) + + got := ht.rawResponse("GET / HTTP/1.1\nHost: golang.org") + expected := "HTTP/1.1 102 Processing\r\n\r\nHTTP/1.1 200 OK\r\nDate: " // dynamic content expected + if !strings.Contains(got, expected) { + t.Errorf("unexpected response; got %q; should start by %q", got, expected) + } +} diff --git a/src/net/http/server.go b/src/net/http/server.go index d44b0fb256577b..cc8bc3ec795a82 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -144,13 +144,20 @@ type ResponseWriter interface { // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). // Thus explicit calls to WriteHeader are mainly used to - // send error codes. + // send error codes or informational responses. // // The provided code must be a valid HTTP 1xx-5xx status code. - // Only one header may be written. Go does not currently - // support sending user-defined 1xx informational headers, - // with the exception of 100-continue response header that the - // Server sends automatically when the Request.Body is read. + // Only one header with a status code not in the 1xx range may + // be written. + // + // Informational headers (status in the 1xx range) can be sent multiple + // times before sending the final header. + // + // If the passed status code is 103, the current content of the header + // map will be sent as early hints for the client. + // + // The server automatically sends the 100-continue response header + // when the Request.Body is read. WriteHeader(statusCode int) } @@ -1144,6 +1151,19 @@ func (w *response) WriteHeader(code int) { return } checkWriteHeaderCode(code) + + // Handle provisional headers, except 100 (continue) which is handled automatically + if code > 100 && code < 200 { + writeStatusLine(w.conn.bufw, w.req.ProtoAtLeast(1, 1), code, w.statusBuf[:]) + if code == 103 { + // Per RFC 8297 we must not clear the current header map + w.handlerHeader.Write(w.conn.bufw) + } + w.conn.bufw.Write(crlf) + + return + } + w.wroteHeader = true w.status = code