Skip to content

Commit

Permalink
support h2c
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Aug 24, 2022
1 parent 9a27463 commit b5ecb93
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
12 changes: 12 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,18 @@ func (c *Client) DisableForceHttpVersion() *Client {
return c
}

// EnableH2C enables HTTP/2 over TCP without TLS.
func (c *Client) EnableH2C() *Client {
c.t.EnableH2C()
return c
}

// DisableH2C disables HTTP/2 over TCP without TLS.
func (c *Client) DisableH2C() *Client {
c.t.DisableH2C()
return c
}

// DisableAllowGetMethodPayload disable sending GET method requests with body.
func (c *Client) DisableAllowGetMethodPayload() *Client {
c.AllowGetMethodPayload = false
Expand Down
12 changes: 12 additions & 0 deletions client_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,18 @@ func DisableForceHttpVersion() *Client {
return defaultClient.DisableForceHttpVersion()
}

// EnableH2C is a global wrapper methods which delegated
// to the default client's EnableH2C.
func EnableH2C() *Client {
return defaultClient.EnableH2C()
}

// DisableH2C is a global wrapper methods which delegated
// to the default client's DisableH2C.
func DisableH2C() *Client {
return defaultClient.DisableH2C()
}

// DisableAllowGetMethodPayload is a global wrapper methods which delegated
// to the default client's DisableAllowGetMethodPayload.
func DisableAllowGetMethodPayload() *Client {
Expand Down
3 changes: 3 additions & 0 deletions internal/transport/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ type Options struct {
// uncompressed.
DisableCompression bool

// EnableH2C, if true, enables http2 over plain http without tls.
EnableH2C bool

// MaxIdleConns controls the maximum number of idle (keep-alive)
// connections across all hosts. Zero means no limit.
MaxIdleConns int
Expand Down
27 changes: 25 additions & 2 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,24 @@ func (t *Transport) EnableForceHTTP2() *Transport {
return t
}

// EnableH2C enables HTTP2 over TCP without TLS.
func (t *Transport) EnableH2C() *Transport {
t.Options.EnableH2C = true
t.t2.AllowHTTP = true
t.t2.DialTLS = func(network, addr string, cfg *tls.Config) (net.Conn, error) {
return net.Dial(network, addr)
}
return t
}

// DisableH2C disables HTTP2 over TCP without TLS.
func (t *Transport) DisableH2C() *Transport {
t.Options.EnableH2C = false
t.t2.AllowHTTP = false
t.t2.DialTLS = nil
return t
}

// EnableForceHTTP3 enable force using HTTP3 for https requests
// (disabled by default).
func (t *Transport) EnableForceHTTP3() *Transport {
Expand Down Expand Up @@ -717,8 +735,13 @@ func (t *Transport) roundTrip(req *http.Request) (resp *http.Response, err error
}
}

if t.forceHttpVersion == h3 {
return t.t3.RoundTrip(req)
if t.forceHttpVersion != "" {
switch t.forceHttpVersion {
case h3:
return t.t3.RoundTrip(req)
case h2:
return t.t2.RoundTrip(req)
}
}

origReq := req
Expand Down

0 comments on commit b5ecb93

Please sign in to comment.