Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions common/httpx/httpx.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func New(options *Options) (*HTTPX, error) {
DisableKeepAlives: true,
}

if httpx.Options.Protocol == "http11" {
if httpx.Options.Protocol == HTTP11 {
// disable http2
_ = os.Setenv("GODEBUG", "http2client=0")
transport.TLSNextProto = map[string]func(string, *tls.Conn) http.RoundTripper{}
Expand Down Expand Up @@ -183,19 +183,26 @@ func New(options *Options) (*HTTPX, error) {
CheckRedirect: redirectFunc,
}, retryablehttpOptions)

transport2 := &http2.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
MinVersion: tls.VersionTLS10,
},
AllowHTTP: true,
}
if httpx.Options.SniName != "" {
transport2.TLSClientConfig.ServerName = httpx.Options.SniName
}
httpx.client2 = &http.Client{
Transport: transport2,
Timeout: httpx.Options.Timeout,
// honor explicit HTTP/1.1 mode by disabling retryablehttp-go's internal
// HTTP/2 fallback client and HTTPX's own HTTP/2 probing client
if httpx.Options.Protocol == HTTP11 {
httpx.client.HTTPClient2 = httpx.client.HTTPClient
httpx.client2 = httpx.client.HTTPClient
} else {
transport2 := &http2.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
MinVersion: tls.VersionTLS10,
},
AllowHTTP: true,
}
if httpx.Options.SniName != "" {
transport2.TLSClientConfig.ServerName = httpx.Options.SniName
}
httpx.client2 = &http.Client{
Transport: transport2,
Timeout: httpx.Options.Timeout,
}
}

httpx.htmlPolicy = bluemonday.NewPolicy()
Expand Down
51 changes: 51 additions & 0 deletions common/httpx/httpx_protocol_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package httpx

import (
"os"
"testing"

"golang.org/x/net/http2"

"github.com/stretchr/testify/require"
)

// TestNew_HTTP11DisablesRetryableHTTP2Fallback verifies that forcing HTTP/1.1 disables retryablehttp-go's HTTP/2 fallback and prevents HTTP/2 probing client creation.
func TestNew_HTTP11DisablesRetryableHTTP2Fallback(t *testing.T) {
opts := DefaultOptions
opts.Protocol = HTTP11

originalGODEBUG, hadGODEBUG := os.LookupEnv("GODEBUG")
t.Cleanup(func() {
if hadGODEBUG {
_ = os.Setenv("GODEBUG", originalGODEBUG)
} else {
_ = os.Unsetenv("GODEBUG")
}
})

ht, err := New(&opts)
require.NoError(t, err)
require.NotNil(t, ht)
t.Cleanup(func() { ht.Dialer.Close() })
require.NotNil(t, ht.client)
require.Same(t, ht.client.HTTPClient, ht.client.HTTPClient2)
require.Same(t, ht.client.HTTPClient, ht.client2)
_, isHTTP2 := ht.client2.Transport.(*http2.Transport)
require.False(t, isHTTP2)
}

// TestNew_NonHTTP11KeepsRetryableHTTP2FallbackClient verifies that non-HTTP/1.1 mode keeps a dedicated HTTP/2 client2 transport and allows retryable fallback behavior.
func TestNew_NonHTTP11KeepsRetryableHTTP2FallbackClient(t *testing.T) {
opts := DefaultOptions
opts.Protocol = HTTP2

ht, err := New(&opts)
require.NoError(t, err)
require.NotNil(t, ht)
t.Cleanup(func() { ht.Dialer.Close() })
require.NotNil(t, ht.client)
require.NotSame(t, ht.client.HTTPClient, ht.client.HTTPClient2)
require.NotSame(t, ht.client.HTTPClient, ht.client2)
_, isHTTP2 := ht.client2.Transport.(*http2.Transport)
require.True(t, isHTTP2)
}