Skip to content

Commit b64f1fa

Browse files
committed
internal/http3: add server support for "Trailer:" magic prefix
Similar to our HTTP/1 and HTTP/2 implementations, allow HTTP/3 server handler to send trailer headers that have not been declared prior to writing response body by using the "Trailer:" magic prefix. To support this, also modify transport to accept all trailers, even those that have not been declared. This is consistent with the behavior of our HTTP/1 and HTTP/2 implementations. Note the following, unfortunately somewhat convoluted, behavior: 1. Transport will never send undeclared trailers. 2. Transport will accept undeclared trailers. 3. Server will never accept undeclared trailers. 4. Server will never send undeclared trailers, unless they use the "Trailer:" magic prefix. For golang/go#70914 Change-Id: Ifa5e026eb6ce68af9a8364845e4f2def6a6a6964 Reviewed-on: https://go-review.googlesource.com/c/net/+/785860 Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Viacheslav Danilin <[email protected]> LUCI-TryBot-Result: [email protected] <[email protected]> Reviewed-by: Nicholas Husin <[email protected]>
1 parent 2707ee2 commit b64f1fa

4 files changed

Lines changed: 66 additions & 23 deletions

File tree

‎internal/http3/body.go‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,12 @@ type bodyReader struct {
127127
send100Continue func()
128128
// A map where the key represents the trailer header names we expect. If
129129
// there is a HEADERS frame after reading DATA frames to EOF, the value of
130-
// the headers will be written here, provided that the name of the header
131-
// exists in the map already.
132-
trailer http.Header
130+
// the headers will be written here. Keys in the map are assumed to be
131+
// canonicalized.
132+
// If filterTrailer is true, headers that are not already in the map will
133+
// be ignored; otherwise, all headers will be added to the map.
134+
trailer http.Header
135+
filterTrailer bool
133136
}
134137

135138
func (r *bodyReader) Read(p []byte) (n int, err error) {
@@ -188,8 +191,16 @@ func (r *bodyReader) Read(p []byte) (n int, err error) {
188191
}
189192
var dec qpackDecoder
190193
if err := dec.decode(r.st, func(_ indexType, name, value string) error {
194+
if r.trailer == nil {
195+
return nil
196+
}
197+
if !validWireHeaderFieldName(name) || !httpguts.ValidHeaderFieldValue(name) {
198+
return nil
199+
}
191200
name = textproto.CanonicalMIMEHeaderKey(textproto.TrimString(name))
192-
if _, ok := r.trailer[name]; ok {
201+
if !r.filterTrailer {
202+
r.trailer.Add(name, value)
203+
} else if _, ok := r.trailer[name]; ok {
193204
r.trailer.Add(name, value)
194205
}
195206
return nil

‎internal/http3/roundtrip_test.go‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -557,13 +557,15 @@ func TestRoundTripWriteTrailer(t *testing.T) {
557557
req, _ = http.NewRequest("POST", "https://example.tld/", io.MultiReader(
558558
testReader{readFunc: func(_ []byte) (int, error) {
559559
req.Trailer["Client-Trailer-A"] = []string{"valuea"}
560-
req.Trailer["Undeclared-Trailer"] = []string{"undeclared"} // Should be ignored.
560+
// Transport should not send undeclared trailer.
561+
req.Trailer["Undeclared-Trailer"] = []string{"undeclared"}
561562
return 0, io.EOF
562563
}},
563564
strings.NewReader("a body"),
564565
testReader{readFunc: func(_ []byte) (int, error) {
565566
req.Trailer["Client-Trailer-B"] = []string{"valueb"}
566-
req.Trailer["Undeclared-Trailer"] = []string{"undeclared"} // Should be ignored.
567+
// Transport should not send undeclared trailer.
568+
req.Trailer["Undeclared-Trailer"] = []string{"undeclared"}
567569
return 0, io.EOF
568570
}},
569571
))
@@ -592,12 +594,14 @@ func TestRoundTripWriteTrailerNoBody(t *testing.T) {
592594
req, _ = http.NewRequest("POST", "https://example.tld/", io.MultiReader(
593595
testReader{readFunc: func(_ []byte) (int, error) {
594596
req.Trailer["Client-Trailer-A"] = []string{"valuea"}
595-
req.Trailer["Undeclared-Trailer"] = []string{"undeclared"} // Should be ignored.
597+
// Transport should not send undeclared trailer.
598+
req.Trailer["Undeclared-Trailer"] = []string{"undeclared"}
596599
return 0, io.EOF
597600
}},
598601
testReader{readFunc: func(_ []byte) (int, error) {
599602
req.Trailer["Client-Trailer-B"] = []string{"valueb"}
600-
req.Trailer["Undeclared-Trailer"] = []string{"undeclared"} // Should be ignored.
603+
// Transport should not send undeclared trailer.
604+
req.Trailer["Undeclared-Trailer"] = []string{"undeclared"}
601605
return 0, io.EOF
602606
}},
603607
))
@@ -637,7 +641,7 @@ func TestRoundTripReadTrailer(t *testing.T) {
637641
"server-trailer-a": {"valuea"},
638642
// Note that Server-Trailer-B is skipped.
639643
"server-trailer-c": {"valuec"},
640-
"undeclared-trailer": {"undeclared"}, // Should be ignored.
644+
"undeclared-trailer": {"undeclared"},
641645
})
642646

643647
rt.wantStatus(200)
@@ -655,6 +659,8 @@ func TestRoundTripReadTrailer(t *testing.T) {
655659
"Server-Trailer-A": {"valuea"},
656660
"Server-Trailer-B": nil,
657661
"Server-Trailer-C": {"valuec"},
662+
// Transport should accept undeclared trailers.
663+
"Undeclared-Trailer": {"undeclared"},
658664
})
659665
st.wantClosed("request is complete")
660666
})
@@ -680,7 +686,7 @@ func TestRoundTripReadTrailerNoBody(t *testing.T) {
680686
"server-trailer-a": {"valuea"},
681687
// Note that Server-Trailer-B is skipped.
682688
"server-trailer-c": {"valuec"},
683-
"undeclared-trailer": {"undeclared"}, // Should be ignored.
689+
"undeclared-trailer": {"undeclared"},
684690
})
685691

686692
rt.wantStatus(200)
@@ -698,6 +704,8 @@ func TestRoundTripReadTrailerNoBody(t *testing.T) {
698704
"Server-Trailer-A": {"valuea"},
699705
"Server-Trailer-B": nil,
700706
"Server-Trailer-C": {"valuec"},
707+
// Transport should accept undeclared trailers.
708+
"Undeclared-Trailer": {"undeclared"},
701709
})
702710
st.wantClosed("request is complete")
703711
})

‎internal/http3/server.go‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"io"
1212
"maps"
1313
"net/http"
14+
"net/textproto"
1415
"slices"
1516
"strconv"
1617
"strings"
@@ -503,9 +504,10 @@ func (sc *serverConn) handleRequestStream(st *stream) error {
503504
}
504505
if contentLength != 0 || len(reqInfo.Trailer) != 0 {
505506
body = &bodyReader{
506-
st: st,
507-
remain: contentLength,
508-
trailer: reqInfo.Trailer,
507+
st: st,
508+
remain: contentLength,
509+
trailer: reqInfo.Trailer,
510+
filterTrailer: true,
509511
}
510512
} else {
511513
body = http.NoBody
@@ -578,6 +580,12 @@ func responseCanHaveBody(status int) bool {
578580
return true
579581
}
580582

583+
// trailerPrefix is a magic prefix for [responseWriter.Header] map keys that,
584+
// if present, signals that the map entry is actually for the response
585+
// trailers, and not the response headers. See [net/http.TrailerPrefix] for
586+
// details.
587+
const trailerPrefix = "Trailer:"
588+
581589
type responseWriter struct {
582590
st *stream
583591
bw *bodyWriter
@@ -608,6 +616,12 @@ func (rw *responseWriter) prepareTrailerForWriteLocked() {
608616
delete(rw.trailer, name)
609617
}
610618
}
619+
for name, vals := range rw.headers {
620+
if name, found := strings.CutPrefix(name, trailerPrefix); found {
621+
name = textproto.CanonicalMIMEHeaderKey(textproto.TrimString(name))
622+
rw.trailer[name] = vals
623+
}
624+
}
611625
if len(rw.trailer) > 0 {
612626
rw.bw.trailer = rw.trailer
613627
}

‎internal/http3/server_test.go‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -798,9 +798,10 @@ func TestServerHandlerReadTrailer(t *testing.T) {
798798
}))
799799
reqStream.writeData(body)
800800
reqStream.writeHeaders(http.Header{
801-
"Client-Trailer-A": {"valuea"},
802-
"Client-Trailer-B": {"valueb"},
803-
"Undeclared-Trailer": {"undeclared"}, // Undeclared trailer should be ignored.
801+
"Client-Trailer-A": {"valuea"},
802+
"Client-Trailer-B": {"valueb"},
803+
// Server should not accept undeclared trailers.
804+
"Undeclared-Trailer": {"undeclared"},
804805
})
805806
reqStream.wantHeaders(nil)
806807
reqStream.wantClosed("request is complete")
@@ -838,9 +839,10 @@ func TestServerHandlerReadTrailerNoBody(t *testing.T) {
838839
"content-length": {"0"},
839840
}))
840841
reqStream.writeHeaders(http.Header{
841-
"Client-Trailer-A": {"valuea"},
842-
"Client-Trailer-B": {"valueb"},
843-
"Undeclared-Trailer": {"undeclared"}, // Undeclared trailer should be ignored.
842+
"Client-Trailer-A": {"valuea"},
843+
"Client-Trailer-B": {"valueb"},
844+
// Server should not accept undeclared trailers.
845+
"Undeclared-Trailer": {"undeclared"},
844846
})
845847
reqStream.wantHeaders(nil)
846848
reqStream.wantClosed("request is complete")
@@ -858,7 +860,10 @@ func TestServerHandlerWriteTrailer(t *testing.T) {
858860

859861
w.Header().Set("server-trailer-a", "valuea") // Trailer header will be canonicalized.
860862
w.Header().Set("Server-Trailer-C", "valuec") // skipping B
863+
// Server should not send undeclared trailers, unless it has the
864+
// magic "Trailer:" prefix.
861865
w.Header().Set("Server-Trailer-Not-Declared", "should be omitted")
866+
w.Header().Set("Trailer:Undeclared-Trailer-Exception", "should be sent")
862867
}))
863868
tc := ts.connect()
864869
tc.greet()
@@ -871,8 +876,9 @@ func TestServerHandlerWriteTrailer(t *testing.T) {
871876
})
872877
reqStream.wantData(body)
873878
reqStream.wantSomeHeaders(http.Header{
874-
"Server-Trailer-A": {"valuea"},
875-
"Server-Trailer-C": {"valuec"},
879+
"Server-Trailer-A": {"valuea"},
880+
"Server-Trailer-C": {"valuec"},
881+
"Undeclared-Trailer-Exception": {"should be sent"},
876882
})
877883
reqStream.wantClosed("request is complete")
878884
})
@@ -888,7 +894,10 @@ func TestServerHandlerWriteTrailerNoBody(t *testing.T) {
888894

889895
w.Header().Set("server-trailer-a", "valuea") // Trailer header will be canonicalized.
890896
w.Header().Set("Server-Trailer-C", "valuec") // skipping B
897+
// Server should not send undeclared trailers without "Trailer:"
898+
// prefix.
891899
w.Header().Set("Server-Trailer-Not-Declared", "should be omitted")
900+
w.Header().Set("Trailer:undeclared-trailer-exception", "should be sent")
892901
}))
893902
tc := ts.connect()
894903
tc.greet()
@@ -900,8 +909,9 @@ func TestServerHandlerWriteTrailerNoBody(t *testing.T) {
900909
"Trailer": {"Server-Trailer-A, Server-Trailer-B, Server-Trailer-C"},
901910
})
902911
reqStream.wantSomeHeaders(http.Header{
903-
"Server-Trailer-A": {"valuea"},
904-
"Server-Trailer-C": {"valuec"},
912+
"Server-Trailer-A": {"valuea"},
913+
"Server-Trailer-C": {"valuec"},
914+
"Undeclared-Trailer-Exception": {"should be sent"},
905915
})
906916
reqStream.wantClosed("request is complete")
907917
})

0 commit comments

Comments
 (0)