Skip to content

Commit 391127f

Browse files
committed
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.
1 parent e18ecbb commit 391127f

File tree

2 files changed

+119
-8
lines changed

2 files changed

+119
-8
lines changed

http2/server.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2627,8 +2627,7 @@ func checkWriteHeaderCode(code int) {
26272627
// Issue 22880: require valid WriteHeader status codes.
26282628
// For now we only enforce that it's three digits.
26292629
// In the future we might block things over 599 (600 and above aren't defined
2630-
// at http://httpwg.org/specs/rfc7231.html#status.codes)
2631-
// and we might block under 200 (once we have more mature 1xx support).
2630+
// at http://httpwg.org/specs/rfc7231.html#status.codes).
26322631
// But for now any three digits.
26332632
//
26342633
// We used to send "HTTP/1.1 000 0" on the wire in responses but there's
@@ -2649,13 +2648,36 @@ func (w *responseWriter) WriteHeader(code int) {
26492648
}
26502649

26512650
func (rws *responseWriterState) writeHeader(code int) {
2652-
if !rws.wroteHeader {
2653-
checkWriteHeaderCode(code)
2654-
rws.wroteHeader = true
2655-
rws.status = code
2656-
if len(rws.handlerHeader) > 0 {
2657-
rws.snapHeader = cloneHeader(rws.handlerHeader)
2651+
if rws.wroteHeader {
2652+
return
2653+
}
2654+
2655+
checkWriteHeaderCode(code)
2656+
2657+
// Handle informational headers, except 100 (Continue) which is handled automatically
2658+
if code > 100 && code < 200 {
2659+
var h http.Header
2660+
if code == 103 {
2661+
// Per RFC 8297 we must not clear the current header map
2662+
h = rws.handlerHeader
2663+
}
2664+
2665+
if rws.conn.writeHeaders(rws.stream, &writeResHeaders{
2666+
streamID: rws.stream.id,
2667+
httpResCode: code,
2668+
h: h,
2669+
endStream: rws.handlerDone && !rws.hasTrailers(),
2670+
}) != nil {
2671+
rws.dirty = true
26582672
}
2673+
2674+
return
2675+
}
2676+
2677+
rws.wroteHeader = true
2678+
rws.status = code
2679+
if len(rws.handlerHeader) > 0 {
2680+
rws.snapHeader = cloneHeader(rws.handlerHeader)
26592681
}
26602682
}
26612683

http2/server_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4331,3 +4331,92 @@ func TestNoErrorLoggedOnPostAfterGOAWAY(t *testing.T) {
43314331
t.Error("got protocol error")
43324332
}
43334333
}
4334+
4335+
func TestServerSendsProcessing(t *testing.T) {
4336+
testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error {
4337+
w.WriteHeader(http.StatusProcessing)
4338+
w.Write([]byte("stuff"))
4339+
4340+
return nil
4341+
}, func(st *serverTester) {
4342+
getSlash(st)
4343+
hf := st.wantHeaders()
4344+
goth := st.decodeHeader(hf.HeaderBlockFragment())
4345+
wanth := [][2]string{
4346+
{":status", "102"},
4347+
}
4348+
4349+
if !reflect.DeepEqual(goth, wanth) {
4350+
t.Errorf("Got = %q; want %q", goth, wanth)
4351+
}
4352+
4353+
hf = st.wantHeaders()
4354+
goth = st.decodeHeader(hf.HeaderBlockFragment())
4355+
wanth = [][2]string{
4356+
{":status", "200"},
4357+
{"content-type", "text/plain; charset=utf-8"},
4358+
{"content-length", "5"},
4359+
}
4360+
4361+
if !reflect.DeepEqual(goth, wanth) {
4362+
t.Errorf("Got = %q; want %q", goth, wanth)
4363+
}
4364+
})
4365+
}
4366+
4367+
func TestServerSendsEarlyHints(t *testing.T) {
4368+
testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error {
4369+
h := w.Header()
4370+
h.Add("Link", "</style.css>; rel=preload; as=style")
4371+
h.Add("Link", "</script.js>; rel=preload; as=script")
4372+
w.WriteHeader(http.StatusEarlyHints)
4373+
4374+
h.Add("Link", "</foo.js>; rel=preload; as=script")
4375+
w.WriteHeader(http.StatusEarlyHints)
4376+
4377+
w.Write([]byte("stuff"))
4378+
4379+
return nil
4380+
}, func(st *serverTester) {
4381+
getSlash(st)
4382+
hf := st.wantHeaders()
4383+
goth := st.decodeHeader(hf.HeaderBlockFragment())
4384+
wanth := [][2]string{
4385+
{":status", "103"},
4386+
{"link", "</style.css>; rel=preload; as=style"},
4387+
{"link", "</script.js>; rel=preload; as=script"},
4388+
}
4389+
4390+
if !reflect.DeepEqual(goth, wanth) {
4391+
t.Errorf("Got = %q; want %q", goth, wanth)
4392+
}
4393+
4394+
hf = st.wantHeaders()
4395+
goth = st.decodeHeader(hf.HeaderBlockFragment())
4396+
wanth = [][2]string{
4397+
{":status", "103"},
4398+
{"link", "</style.css>; rel=preload; as=style"},
4399+
{"link", "</script.js>; rel=preload; as=script"},
4400+
{"link", "</foo.js>; rel=preload; as=script"},
4401+
}
4402+
4403+
if !reflect.DeepEqual(goth, wanth) {
4404+
t.Errorf("Got = %q; want %q", goth, wanth)
4405+
}
4406+
4407+
hf = st.wantHeaders()
4408+
goth = st.decodeHeader(hf.HeaderBlockFragment())
4409+
wanth = [][2]string{
4410+
{":status", "200"},
4411+
{"link", "</style.css>; rel=preload; as=style"},
4412+
{"link", "</script.js>; rel=preload; as=script"},
4413+
{"link", "</foo.js>; rel=preload; as=script"},
4414+
{"content-type", "text/plain; charset=utf-8"},
4415+
{"content-length", "5"},
4416+
}
4417+
4418+
if !reflect.DeepEqual(goth, wanth) {
4419+
t.Errorf("Got = %q; want %q", goth, wanth)
4420+
}
4421+
})
4422+
}

0 commit comments

Comments
 (0)