Skip to content

Commit 16b745b

Browse files
committed
refactor(h2c): h2c use net/http
1 parent 2b4d57a commit 16b745b

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ require (
2727
go.etcd.io/bbolt v1.4.0
2828
golang.org/x/crypto/x509roots/fallback v0.0.0-20240916204253-42ee18b96377
2929
golang.org/x/image v0.24.0
30-
golang.org/x/net v0.35.0
3130
)
3231

3332
require (
@@ -45,6 +44,7 @@ require (
4544
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
4645
github.com/samber/lo v1.47.0 // indirect
4746
github.com/samber/slog-common v0.18.1 // indirect
47+
golang.org/x/net v0.35.0 // indirect
4848
golang.org/x/sys v0.30.0 // indirect
4949
golang.org/x/text v0.22.0 // indirect
5050
)

web/listener.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package web
22

33
import (
44
"context"
5-
"crypto/tls"
65
"errors"
76
"fmt"
87
"log/slog"
@@ -12,8 +11,6 @@ import (
1211
"github.com/coreos/go-systemd/v22/activation"
1312
"github.com/librespeed/speedtest/config"
1413
"github.com/pires/go-proxyproto"
15-
"golang.org/x/net/http2"
16-
"golang.org/x/net/http2/h2c"
1714
)
1815

1916
func startListener(ctx context.Context, conf *config.Config, r http.Handler) error {
@@ -65,27 +62,24 @@ func startListener(ctx context.Context, conf *config.Config, r http.Handler) err
6562
srv := &http.Server{
6663
Handler: r,
6764
}
65+
srv.Protocols = new(http.Protocols)
66+
srv.Protocols.SetHTTP1(true)
67+
6868
var listenFn func() error
6969
// TLS and HTTP/2.
7070
if conf.EnableTLS {
7171
slog.Info("Use TLS connection")
72-
73-
if !conf.EnableHTTP2 {
74-
// If TLSNextProto is not nil, HTTP/2 support is not enabled automatically.
75-
srv.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
72+
if conf.EnableHTTP2 {
73+
slog.Info("Use HTTP2 connection.")
74+
srv.Protocols.SetHTTP2(conf.EnableHTTP2)
7675
}
7776
listenFn = func() error {
7877
return srv.ServeTLS(listener, conf.TLSCertFile, conf.TLSKeyFile)
7978
}
8079
} else {
8180
if conf.EnableHTTP2 {
8281
slog.Info("Use HTTP2 connection.")
83-
h2s := &http2.Server{}
84-
srv.Handler = h2c.NewHandler(r, h2s)
85-
err = http2.ConfigureServer(srv, h2s)
86-
if err != nil {
87-
return fmt.Errorf("http2.ConfigureServer: %s", err)
88-
}
82+
srv.Protocols.SetUnencryptedHTTP2(conf.EnableHTTP2)
8983
}
9084
listenFn = func() error {
9185
return srv.Serve(listener)

0 commit comments

Comments
 (0)