/
http2.go
95 lines (74 loc) · 2.05 KB
/
http2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package httpserver
import (
"context"
"net"
"net/http"
"time"
"golang.org/x/net/http2"
"darvaza.org/core"
)
// NewHTTPServer creates a new [http.Server].
func (srv *Server) NewHTTPServer(proto string, addr net.Addr) *http.Server {
return &http.Server{
Addr: addr.String(),
ReadTimeout: srv.cfg.ReadTimeout,
ReadHeaderTimeout: srv.cfg.ReadHeaderTimeout,
WriteTimeout: srv.cfg.WriteTimeout,
IdleTimeout: srv.cfg.IdleTimeout,
ErrorLog: srv.NewHTTPServerErrorLogger(proto, addr),
}
}
// NewH2Server creates a new HTTP/2 capable [http.Server].
func (srv *Server) NewH2Server(h http.Handler, addr net.Addr) (*http.Server, error) {
h1s := srv.NewHTTPServer("h2", addr)
h1s.TLSConfig = srv.NewTLSConfig()
h1s.Handler = h
h2s := &http2.Server{}
if err := http2.ConfigureServer(h1s, h2s); err != nil {
return nil, err
}
return h1s, nil
}
// NewH2Handler returns the [http.Handler] to use on the H2 server.
func (srv *Server) NewH2Handler(h http.Handler) http.Handler {
if h == nil {
// no handler implies 404.
h = http.NotFoundHandler()
}
// ACME-HTTP-01 handler or 404 for /.well-known/acme-challenge
h = AcmeHTTP01Middleware(h, srv.cfg.AcmeHTTP01)
// Advertise Quic
h = srv.QuicHeadersMiddleware(h)
return h
}
func (srv *Server) spawnH2(h http.Handler, listeners []net.Listener, graceful time.Duration) error {
// wrap
h = srv.NewH2Handler(h)
for _, lsn := range listeners {
s, err := srv.NewH2Server(h, lsn.Addr())
if err != nil {
return err
}
srv.spawnTCP(s, "h2", lsn, graceful)
}
return nil
}
func (srv *Server) spawnTCP(s *http.Server, proto string, lsn net.Listener, graceful time.Duration) {
addr, ok := core.AddrPort(lsn.Addr())
if !ok {
core.Panic("unreachable")
}
srv.eg.Go(func(_ context.Context) error {
srv.logListening(proto, addr)
return s.Serve(lsn)
}, func() error {
srv.logShuttingDown(proto, addr)
ctx := context.Background()
if graceful > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, graceful)
defer cancel()
}
return s.Shutdown(ctx)
})
}