Skip to content

Commit 9564170

Browse files
dunglasneild
authored andcommitted
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. 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 golang/go#26089. Fixes golang/go#36734. Updates golang/go#26088. Change-Id: Iadb7be92844a3588ef4ce044f887ef5c1792f431 GitHub-Last-Rev: eee95b5 GitHub-Pull-Request: #96 Reviewed-on: https://go-review.googlesource.com/c/net/+/291029 Run-TryBot: Damien Neil <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Damien Neil <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 2871e0c commit 9564170

File tree

2 files changed

+116
-8
lines changed

2 files changed

+116
-8
lines changed

http2/server.go

+27-8
Original file line numberDiff line numberDiff line change
@@ -2645,8 +2645,7 @@ func checkWriteHeaderCode(code int) {
26452645
// Issue 22880: require valid WriteHeader status codes.
26462646
// For now we only enforce that it's three digits.
26472647
// In the future we might block things over 599 (600 and above aren't defined
2648-
// at http://httpwg.org/specs/rfc7231.html#status.codes)
2649-
// and we might block under 200 (once we have more mature 1xx support).
2648+
// at http://httpwg.org/specs/rfc7231.html#status.codes).
26502649
// But for now any three digits.
26512650
//
26522651
// We used to send "HTTP/1.1 000 0" on the wire in responses but there's
@@ -2667,13 +2666,33 @@ func (w *responseWriter) WriteHeader(code int) {
26672666
}
26682667

26692668
func (rws *responseWriterState) writeHeader(code int) {
2670-
if !rws.wroteHeader {
2671-
checkWriteHeaderCode(code)
2672-
rws.wroteHeader = true
2673-
rws.status = code
2674-
if len(rws.handlerHeader) > 0 {
2675-
rws.snapHeader = cloneHeader(rws.handlerHeader)
2669+
if rws.wroteHeader {
2670+
return
2671+
}
2672+
2673+
checkWriteHeaderCode(code)
2674+
2675+
// Handle informational headers
2676+
if code >= 100 && code <= 199 {
2677+
// Per RFC 8297 we must not clear the current header map
2678+
h := rws.handlerHeader
2679+
2680+
if rws.conn.writeHeaders(rws.stream, &writeResHeaders{
2681+
streamID: rws.stream.id,
2682+
httpResCode: code,
2683+
h: h,
2684+
endStream: rws.handlerDone && !rws.hasTrailers(),
2685+
}) != nil {
2686+
rws.dirty = true
26762687
}
2688+
2689+
return
2690+
}
2691+
2692+
rws.wroteHeader = true
2693+
rws.status = code
2694+
if len(rws.handlerHeader) > 0 {
2695+
rws.snapHeader = cloneHeader(rws.handlerHeader)
26772696
}
26782697
}
26792698

http2/server_test.go

+89
Original file line numberDiff line numberDiff line change
@@ -4356,3 +4356,92 @@ func TestNoErrorLoggedOnPostAfterGOAWAY(t *testing.T) {
43564356
t.Error("got protocol error")
43574357
}
43584358
}
4359+
4360+
func TestServerSendsProcessing(t *testing.T) {
4361+
testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error {
4362+
w.WriteHeader(http.StatusProcessing)
4363+
w.Write([]byte("stuff"))
4364+
4365+
return nil
4366+
}, func(st *serverTester) {
4367+
getSlash(st)
4368+
hf := st.wantHeaders()
4369+
goth := st.decodeHeader(hf.HeaderBlockFragment())
4370+
wanth := [][2]string{
4371+
{":status", "102"},
4372+
}
4373+
4374+
if !reflect.DeepEqual(goth, wanth) {
4375+
t.Errorf("Got = %q; want %q", goth, wanth)
4376+
}
4377+
4378+
hf = st.wantHeaders()
4379+
goth = st.decodeHeader(hf.HeaderBlockFragment())
4380+
wanth = [][2]string{
4381+
{":status", "200"},
4382+
{"content-type", "text/plain; charset=utf-8"},
4383+
{"content-length", "5"},
4384+
}
4385+
4386+
if !reflect.DeepEqual(goth, wanth) {
4387+
t.Errorf("Got = %q; want %q", goth, wanth)
4388+
}
4389+
})
4390+
}
4391+
4392+
func TestServerSendsEarlyHints(t *testing.T) {
4393+
testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error {
4394+
h := w.Header()
4395+
h.Add("Link", "</style.css>; rel=preload; as=style")
4396+
h.Add("Link", "</script.js>; rel=preload; as=script")
4397+
w.WriteHeader(http.StatusEarlyHints)
4398+
4399+
h.Add("Link", "</foo.js>; rel=preload; as=script")
4400+
w.WriteHeader(http.StatusEarlyHints)
4401+
4402+
w.Write([]byte("stuff"))
4403+
4404+
return nil
4405+
}, func(st *serverTester) {
4406+
getSlash(st)
4407+
hf := st.wantHeaders()
4408+
goth := st.decodeHeader(hf.HeaderBlockFragment())
4409+
wanth := [][2]string{
4410+
{":status", "103"},
4411+
{"link", "</style.css>; rel=preload; as=style"},
4412+
{"link", "</script.js>; rel=preload; as=script"},
4413+
}
4414+
4415+
if !reflect.DeepEqual(goth, wanth) {
4416+
t.Errorf("Got = %q; want %q", goth, wanth)
4417+
}
4418+
4419+
hf = st.wantHeaders()
4420+
goth = st.decodeHeader(hf.HeaderBlockFragment())
4421+
wanth = [][2]string{
4422+
{":status", "103"},
4423+
{"link", "</style.css>; rel=preload; as=style"},
4424+
{"link", "</script.js>; rel=preload; as=script"},
4425+
{"link", "</foo.js>; rel=preload; as=script"},
4426+
}
4427+
4428+
if !reflect.DeepEqual(goth, wanth) {
4429+
t.Errorf("Got = %q; want %q", goth, wanth)
4430+
}
4431+
4432+
hf = st.wantHeaders()
4433+
goth = st.decodeHeader(hf.HeaderBlockFragment())
4434+
wanth = [][2]string{
4435+
{":status", "200"},
4436+
{"link", "</style.css>; rel=preload; as=style"},
4437+
{"link", "</script.js>; rel=preload; as=script"},
4438+
{"link", "</foo.js>; rel=preload; as=script"},
4439+
{"content-type", "text/plain; charset=utf-8"},
4440+
{"content-length", "5"},
4441+
}
4442+
4443+
if !reflect.DeepEqual(goth, wanth) {
4444+
t.Errorf("Got = %q; want %q", goth, wanth)
4445+
}
4446+
})
4447+
}

0 commit comments

Comments
 (0)