Configure advertised port using h3 server option

Co-authored-by: Romain <rtribotte@users.noreply.github.com>
This commit is contained in:
Kevin Pollet 2022-02-15 16:04:09 +01:00 committed by GitHub
parent 9297055ad8
commit aaf5aa4506
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 33 deletions

View file

@ -30,9 +30,13 @@ func newHTTP3Server(ctx context.Context, configuration *static.EntryPoint, https
return nil, nil
}
if configuration.HTTP3.AdvertisedPort < 0 {
return nil, errors.New("advertised port must be greater than or equal to zero")
}
conn, err := net.ListenPacket("udp", configuration.GetAddress())
if err != nil {
return nil, fmt.Errorf("error while starting http3 listener: %w", err)
return nil, fmt.Errorf("starting listener: %w", err)
}
h3 := &http3server{
@ -43,6 +47,7 @@ func newHTTP3Server(ctx context.Context, configuration *static.EntryPoint, https
}
h3.Server = &http3.Server{
Port: uint32(configuration.HTTP3.AdvertisedPort),
Server: &http.Server{
Addr: configuration.GetAddress(),
Handler: httpsServer.Server.(*http.Server).Handler,
@ -56,10 +61,8 @@ func newHTTP3Server(ctx context.Context, configuration *static.EntryPoint, https
previousHandler := httpsServer.Server.(*http.Server).Handler
setQuicHeaders := getQuicHeadersSetter(configuration)
httpsServer.Server.(*http.Server).Handler = http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
err := setQuicHeaders(rw.Header())
err := h3.Server.SetQuicHeaders(rw.Header())
if err != nil {
log.FromContext(ctx).Errorf("failed to set HTTP3 headers: %v", err)
}
@ -70,25 +73,6 @@ func newHTTP3Server(ctx context.Context, configuration *static.EntryPoint, https
return h3, nil
}
// TODO: rewrite if at some point `port` become an exported field of http3.Server.
func getQuicHeadersSetter(configuration *static.EntryPoint) func(header http.Header) error {
advertisedAddress := configuration.GetAddress()
if configuration.HTTP3.AdvertisedPort != 0 {
advertisedAddress = fmt.Sprintf(`:%d`, configuration.HTTP3.AdvertisedPort)
}
// if `QuickConfig` of h3.server happens to be configured,
// it should also be configured identically in the headerServer
headerServer := &http3.Server{
Server: &http.Server{
Addr: advertisedAddress,
},
}
// set quic headers with the "header" http3 server instance
return headerServer.SetQuicHeaders
}
func (e *http3server) Start() error {
return e.Serve(e.http3conn)
}