Skip to content
Closed
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)
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

// 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)
Comment on lines +48 to +49
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Add a nil guard for ht.client2 before accessing .Transport.

require.NotSame at line 48 passes silently when ht.client2 is nil (nil address ≠ non-nil address), so execution reaches line 49 and panics with a nil-pointer dereference instead of producing a clean test-failure message. Test 1 is safe because require.Same would fail if client2 were nil, but require.NotSame provides no such protection here.

🛡️ Proposed fix
 	require.NotSame(t, ht.client.HTTPClient, ht.client2)
+	require.NotNil(t, ht.client2)
 	_, isHTTP2 := ht.client2.Transport.(*http2.Transport)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
require.NotSame(t, ht.client.HTTPClient, ht.client2)
_, isHTTP2 := ht.client2.Transport.(*http2.Transport)
require.NotSame(t, ht.client.HTTPClient, ht.client2)
require.NotNil(t, ht.client2)
_, isHTTP2 := ht.client2.Transport.(*http2.Transport)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@common/httpx/httpx_protocol_test.go` around lines 48 - 49, Add a nil-guard
before dereferencing ht.client2: ensure ht.client2 is non-nil (e.g., call
require.NotNil(t, ht.client2)) before doing the type assertion on
ht.client2.Transport; this prevents a nil-pointer panic when require.NotSame
passes and lets the test fail cleanly if client2 is nil.

require.True(t, isHTTP2)
}