Skip to content

Commit c1701af

Browse files
committed
extract http.Server from http.Request and use it in http2.ServeConnOpts
1 parent a158d28 commit c1701af

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

Diff for: http2/h2c/h2c.go

+11
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ func NewHandler(h http.Handler, s *http2.Server) http.Handler {
7070
}
7171
}
7272

73+
// extractServer extracts existing http.Server instance from http.Request or create an empty http.Server
74+
func extractServer(r *http.Request) *http.Server {
75+
server, ok := r.Context().Value(http.ServerContextKey).(*http.Server)
76+
if ok {
77+
return server
78+
}
79+
return new(http.Server)
80+
}
81+
7382
// ServeHTTP implement the h2c support that is enabled by h2c.GetH2CHandler.
7483
func (s h2cHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7584
// Handle h2c with prior knowledge (RFC 7540 Section 3.4)
@@ -87,6 +96,7 @@ func (s h2cHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
8796
defer conn.Close()
8897
s.s.ServeConn(conn, &http2.ServeConnOpts{
8998
Context: r.Context(),
99+
BaseConfig: extractServer(r),
90100
Handler: s.Handler,
91101
SawClientPreface: true,
92102
})
@@ -104,6 +114,7 @@ func (s h2cHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
104114
defer conn.Close()
105115
s.s.ServeConn(conn, &http2.ServeConnOpts{
106116
Context: r.Context(),
117+
BaseConfig: extractServer(r),
107118
Handler: s.Handler,
108119
UpgradeRequest: r,
109120
Settings: settings,

0 commit comments

Comments
 (0)