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

http3: add ConnContext to the server #4230

Merged
merged 6 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions http3/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ type Server struct {
// In that case, the stream type will not be set.
UniStreamHijacker func(StreamType, quic.Connection, quic.ReceiveStream, error) (hijacked bool)

// ConnContext optionally specifies a function that modifies
// the context used for a new connection c. The provided ctx
// has a ServerContextKey value.
ConnContext func(ctx context.Context, c quic.Connection) context.Context

mutex sync.RWMutex
listeners map[*QUICEarlyListener]listenerInfo

Expand Down Expand Up @@ -610,6 +615,9 @@ func (s *Server) handleRequest(conn quic.Connection, str quic.Stream, decoder *q
ctx = context.WithValue(ctx, ServerContextKey, s)
ctx = context.WithValue(ctx, http.LocalAddrContextKey, conn.LocalAddr())
ctx = context.WithValue(ctx, RemoteAddrContextKey, conn.RemoteAddr())
if s.ConnContext != nil {
ctx = s.ConnContext(ctx, conn)
marten-seemann marked this conversation as resolved.
Show resolved Hide resolved
}
req = req.WithContext(ctx)
r := newResponseWriter(str, conn, s.logger)
if req.Method == http.MethodHead {
Expand Down
5 changes: 5 additions & 0 deletions http3/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,15 @@ var _ = Describe("Server", func() {
s *Server
origQuicListenAddr = quicListenAddr
)
type testConnContextKey string

BeforeEach(func() {
s = &Server{
TLSConfig: testdata.GetTLSConfig(),
logger: utils.DefaultLogger,
ConnContext: func(ctx context.Context, c quic.Connection) context.Context {
return context.WithValue(ctx, testConnContextKey("test"), c)
},
}
origQuicListenAddr = quicListenAddr
})
Expand Down Expand Up @@ -163,6 +167,7 @@ var _ = Describe("Server", func() {
Expect(req.Host).To(Equal("www.example.com"))
Expect(req.RemoteAddr).To(Equal("127.0.0.1:1337"))
Expect(req.Context().Value(ServerContextKey)).To(Equal(s))
Expect(req.Context().Value(testConnContextKey("test"))).ToNot(Equal(nil))
})

It("returns 200 with an empty handler", func() {
Expand Down