diff --git a/common/httpx/httpx.go b/common/httpx/httpx.go index 039f4c4c..f174da2d 100644 --- a/common/httpx/httpx.go +++ b/common/httpx/httpx.go @@ -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{} @@ -183,6 +183,13 @@ func New(options *Options) (*HTTPX, error) { CheckRedirect: redirectFunc, }, retryablehttpOptions) + // When http11 is forced, override HTTPClient2 to also use the HTTP/1.1-only + // transport. Without this, retryablehttp silently falls back to HTTPClient2 + // (which has HTTP/2 enabled) on certain transport errors, bypassing -pr http11. + if httpx.Options.Protocol == HTTP11 { + httpx.client.HTTPClient2 = httpx.client.HTTPClient + } + transport2 := &http2.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, diff --git a/common/httpx/httpx_test.go b/common/httpx/httpx_test.go index 7da6ad12..7ee589a6 100644 --- a/common/httpx/httpx_test.go +++ b/common/httpx/httpx_test.go @@ -8,6 +8,22 @@ import ( "github.com/stretchr/testify/require" ) +func TestHTTP11ProtocolDisablesHTTP2Fallback(t *testing.T) { + opts := DefaultOptions + opts.Protocol = HTTP11 + ht, err := New(&opts) + require.Nil(t, err) + require.Same(t, ht.client.HTTPClient, ht.client.HTTPClient2, + "HTTPClient2 should equal HTTPClient when Protocol is HTTP11") +} + +func TestDefaultProtocolKeepsHTTP2Fallback(t *testing.T) { + ht, err := New(&DefaultOptions) + require.Nil(t, err) + require.NotSame(t, ht.client.HTTPClient, ht.client.HTTPClient2, + "HTTPClient2 should differ from HTTPClient with default protocol") +} + func TestDo(t *testing.T) { ht, err := New(&DefaultOptions) require.Nil(t, err)