From 11220d7ab1f36b7321ecf004aa9d9cfa684d2cfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Wed, 17 Feb 2021 14:47:07 +0100 Subject: [PATCH] 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. It follows the patch for HTTP/1 (golang/go#42597) and HTTP/2 (golang/net#96). 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. --- http3/response_writer.go | 5 ++++- http3/response_writer_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/http3/response_writer.go b/http3/response_writer.go index 2a45f5867dc..2b233f776f1 100644 --- a/http3/response_writer.go +++ b/http3/response_writer.go @@ -57,7 +57,10 @@ func (w *responseWriter) WriteHeader(status int) { if w.headerWritten { return } - w.headerWritten = true + + if status < 100 || status >= 200 { + w.headerWritten = true + } w.status = status var headers bytes.Buffer diff --git a/http3/response_writer_test.go b/http3/response_writer_test.go index 5b2cc2dff88..c3801fd071f 100644 --- a/http3/response_writer_test.go +++ b/http3/response_writer_test.go @@ -117,6 +117,30 @@ var _ = Describe("Response Writer", func() { Expect(fields).To(HaveKeyWithValue(":status", []string{"200"})) }) + It("allows calling WriteHeader() several times when using the 103 status code", func() { + rw.Header().Add("Link", "; rel=preload; as=style") + rw.Header().Add("Link", "; rel=preload; as=script") + rw.WriteHeader(http.StatusEarlyHints) + + n, err := rw.Write([]byte("foobar")) + Expect(n).To(Equal(6)) + Expect(err).ToNot(HaveOccurred()) + + // Early Hints must have been received + fields := decodeHeader(strBuf) + Expect(fields).To(HaveLen(2)) + Expect(fields).To(HaveKeyWithValue(":status", []string{"103"})) + Expect(fields).To(HaveKeyWithValue("link", []string{"; rel=preload; as=style", "; rel=preload; as=script"})) + + // According to the spec, headers sent in the informational response must also be included in the final response + fields = decodeHeader(strBuf) + Expect(fields).To(HaveLen(2)) + Expect(fields).To(HaveKeyWithValue(":status", []string{"200"})) + Expect(fields).To(HaveKeyWithValue("link", []string{"; rel=preload; as=style", "; rel=preload; as=script"})) + + Expect(getData(strBuf)).To(Equal([]byte("foobar"))) + }) + It("doesn't allow writes if the status code doesn't allow a body", func() { rw.WriteHeader(304) n, err := rw.Write([]byte("foobar"))