Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/net/http2: allow sending 1xx responses #87

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions http2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}

Expand Down
89 changes: 89 additions & 0 deletions http2/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "</style.css>; rel=preload; as=style")
h.Add("Link", "</script.js>; rel=preload; as=script")
w.WriteHeader(http.StatusEarlyHints)

h.Add("Link", "</foo.js>; 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", "</style.css>; rel=preload; as=style"},
{"link", "</script.js>; 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", "</style.css>; rel=preload; as=style"},
{"link", "</script.js>; rel=preload; as=script"},
{"link", "</foo.js>; 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", "</style.css>; rel=preload; as=style"},
{"link", "</script.js>; rel=preload; as=script"},
{"link", "</foo.js>; 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)
}
})
}