@@ -8,15 +8,14 @@ import (
88 "log"
99 "net"
1010 "net/http"
11+ "slices"
1112 "sync"
1213 "time"
1314
1415 "github.com/hamba/logger/v2"
1516 lctx "github.com/hamba/logger/v2/ctx"
1617 "github.com/hamba/pkg/v2/http/healthz"
1718 "github.com/hamba/statter/v2"
18- "golang.org/x/net/http2"
19- "golang.org/x/net/http2/h2c"
2019)
2120
2221var testHookServerServe func (net.Listener )
@@ -128,20 +127,25 @@ func (s *GenericServer[T]) runServer(
128127 if err != nil {
129128 return nil , nil , err
130129 }
130+ var tlsCfg * tls.Config
131131 if s .TLSConfig != nil {
132- ln = tls .NewListener (ln , s .TLSConfig )
132+ tlsCfg = s .TLSConfig .Clone ()
133+ if ! slices .Contains (tlsCfg .NextProtos , "h2" ) {
134+ tlsCfg .NextProtos = append ([]string {"h2" }, tlsCfg .NextProtos ... )
135+ }
136+ ln = tls .NewListener (ln , tlsCfg )
133137 }
134138
135139 if testHookServerServe != nil {
136140 testHookServerServe (ln )
137141 }
138142
139- // If there is no TLS, setup h2c.
143+ protos := & http.Protocols {}
144+ protos .SetHTTP1 (true )
145+ protos .SetHTTP2 (true )
140146 if s .TLSConfig == nil {
141- h2s := & http2.Server {
142- IdleTimeout : s .IdleTimeout ,
143- }
144- h = h2c .NewHandler (h , h2s )
147+ // If there is no TLS, setup unencrypted HTTP/2 (h2c).
148+ protos .SetUnencryptedHTTP2 (true )
145149 }
146150
147151 srv := & http.Server {
@@ -150,12 +154,13 @@ func (s *GenericServer[T]) runServer(
150154 return ctx
151155 },
152156 Handler : h ,
153- TLSConfig : s . TLSConfig ,
157+ TLSConfig : tlsCfg ,
154158 ReadHeaderTimeout : withDefault (s .ReadHeaderTimeout , time .Second ),
155159 ReadTimeout : withDefault (s .ReadTimeout , 10 * time .Second ),
156160 WriteTimeout : withDefault (s .WriteTimeout , 10 * time .Second ),
157161 IdleTimeout : withDefault (s .IdleTimeout , 120 * time .Second ),
158162 ErrorLog : log .New (s .Log .Writer (logger .Error ), "" , 0 ),
163+ Protocols : protos ,
159164 }
160165
161166 serverShutdownCh := make (chan struct {})
@@ -197,7 +202,7 @@ func (s *GenericServer[T]) shutdownTimeout() time.Duration {
197202func withDefault [T comparable ](val , def T ) T {
198203 var defT T
199204 if val == defT {
200- return val
205+ return def
201206 }
202- return def
207+ return val
203208}
0 commit comments