Skip to content

Commit a858a95

Browse files
yin1999RPRX
authored andcommitted
XHTTP server: Switch to native h2c support (#4451)
1 parent 06b4a7c commit a858a95

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

Diff for: transport/internet/splithttp/hub.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
"github.com/xtls/xray-core/transport/internet/reality"
2525
"github.com/xtls/xray-core/transport/internet/stat"
2626
"github.com/xtls/xray-core/transport/internet/tls"
27-
"golang.org/x/net/http2"
28-
"golang.org/x/net/http2/h2c"
2927
)
3028

3129
type requestHandler struct {
@@ -426,11 +424,15 @@ func ListenXH(ctx context.Context, address net.Address, port net.Port, streamSet
426424

427425
handler.localAddr = l.listener.Addr()
428426

429-
// h2cHandler can handle both plaintext HTTP/1.1 and h2c
427+
// server can handle both plaintext HTTP/1.1 and h2c
428+
protocols := new(http.Protocols)
429+
protocols.SetHTTP1(true)
430+
protocols.SetUnencryptedHTTP2(true)
430431
l.server = http.Server{
431-
Handler: h2c.NewHandler(handler, &http2.Server{}),
432+
Handler: handler,
432433
ReadHeaderTimeout: time.Second * 4,
433434
MaxHeaderBytes: 8192,
435+
Protocols: protocols,
434436
}
435437
go func() {
436438
if err := l.server.Serve(l.listener); err != nil {

Diff for: transport/internet/splithttp/splithttp_test.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ package splithttp_test
33
import (
44
"context"
55
"crypto/rand"
6-
gotls "crypto/tls"
76
"fmt"
87
"io"
9-
gonet "net"
108
"net/http"
119
"runtime"
1210
"testing"
@@ -23,7 +21,6 @@ import (
2321
. "github.com/xtls/xray-core/transport/internet/splithttp"
2422
"github.com/xtls/xray-core/transport/internet/stat"
2523
"github.com/xtls/xray-core/transport/internet/tls"
26-
"golang.org/x/net/http2"
2724
)
2825

2926
func Test_ListenXHAndDial(t *testing.T) {
@@ -201,17 +198,11 @@ func Test_ListenXHAndDial_H2C(t *testing.T) {
201198
common.Must(err)
202199
defer listen.Close()
203200

201+
protocols := new(http.Protocols)
202+
protocols.SetUnencryptedHTTP2(true)
204203
client := http.Client{
205-
Transport: &http2.Transport{
206-
// So http2.Transport doesn't complain the URL scheme isn't 'https'
207-
AllowHTTP: true,
208-
// even with AllowHTTP, http2.Transport will attempt to establish
209-
// the connection using DialTLSContext. Disable TLS with custom
210-
// dial context.
211-
DialTLSContext: func(ctx context.Context, network, addr string, cfg *gotls.Config) (gonet.Conn, error) {
212-
var d gonet.Dialer
213-
return d.DialContext(ctx, network, addr)
214-
},
204+
Transport: &http.Transport{
205+
Protocols: protocols,
215206
},
216207
}
217208

0 commit comments

Comments
 (0)