move the HTTP handler to a separate method on the listener
@@ -81,15 +81,28 @@ func newListener(laddr ma.Multiaddr, transport tpt.Transport, noise *noise.Trans
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, world!"))
})
- mux.HandleFunc(webtransportHTTPEndpoint, func(w http.ResponseWriter, r *http.Request) {
+ mux.HandleFunc(webtransportHTTPEndpoint, ln.httpHandler)
+ ln.server.H3.Handler = mux
+ go func() {
+ defer close(ln.serverClosed)
+ defer func() { udpConn.Close() }()
+ if err := ln.server.Serve(udpConn); err != nil {
+ // TODO: only output if the server hasn't been closed
+ log.Debugw("serving failed", "addr", udpConn.LocalAddr(), "error", err)
+ }
+ }()
+ return ln, nil
+}
+
+func (l *listener) httpHandler(w http.ResponseWriter, r *http.Request) {
// TODO: check ?type=multistream URL param
- c, err := ln.server.Upgrade(w, r)
+ c, err := l.server.Upgrade(w, r)
if err != nil {
w.WriteHeader(500)
return
}
- ctx, cancel := context.WithTimeout(ln.ctx, handshakeTimeout)
- conn, err := ln.handshake(ctx, c)
+ ctx, cancel := context.WithTimeout(l.ctx, handshakeTimeout)
+ conn, err := l.handshake(ctx, c)
if err != nil {
cancel()
log.Debugw("handshake failed", "error", err)
@@ -98,18 +111,7 @@ func newListener(laddr ma.Multiaddr, transport tpt.Transport, noise *noise.Trans
}
cancel()
// TODO: think about what happens when this channel fills up
- ln.queue <- conn
- })
- ln.server.H3.Handler = mux
- go func() {
- defer close(ln.serverClosed)
- defer func() { udpConn.Close() }()
- if err := ln.server.Serve(udpConn); err != nil {
- // TODO: only output if the server hasn't been closed
- log.Debugw("serving failed", "addr", udpConn.LocalAddr(), "error", err)
- }
- }()
- return ln, nil
+ l.queue <- conn
}
func (l *listener) Accept() (tpt.CapableConn, error) {
