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
6 changes: 6 additions & 0 deletions common/httpx/httpx.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ func New(options *Options) (*HTTPX, error) {
CheckRedirect: redirectFunc,
}, retryablehttpOptions)

// When HTTP/1.1 is explicitly requested, prevent retryablehttp from
// falling back to HTTP/2 on malformed response retries.
if httpx.Options.Protocol == "http11" && httpx.client != nil {
httpx.client.HTTPClient2 = httpx.client.HTTPClient
}
Comment on lines +186 to +190
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

Redundant httpx.client != nil guard

retryablehttp.Client holds both HTTPClient and HTTPClient2 as exported fields; NewWithHTTPClient always returns a fully-initialised, non-nil pointer. The httpx.client != nil branch condition is therefore always true and adds noise without a safety benefit. The protocol check alone is sufficient:

🛠️ Proposed simplification
-	if httpx.Options.Protocol == "http11" && httpx.client != nil {
+	if httpx.Options.Protocol == "http11" {
 		httpx.client.HTTPClient2 = httpx.client.HTTPClient
 	}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@common/httpx/httpx.go` around lines 186 - 190, The guard checking
httpx.client != nil before assigning HTTPClient2 is redundant because
NewWithHTTPClient always returns a non-nil *retryablehttp.Client; remove the nil
check and simplify the block to only test httpx.Options.Protocol == "http11" and
then set httpx.client.HTTPClient2 = httpx.client.HTTPClient (referencing
httpx.Options.Protocol, httpx.client, HTTPClient and HTTPClient2) so the intent
(prevent falling back to HTTP/2) is preserved without the unnecessary nil
condition.


transport2 := &http2.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
Expand Down
25 changes: 25 additions & 0 deletions common/httpx/httpx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httpx
import (
"net/http"
"testing"
"time"

"github.com/projectdiscovery/retryablehttp-go"
"github.com/stretchr/testify/require"
Expand All @@ -28,3 +29,27 @@ func TestDo(t *testing.T) {
require.Greater(t, len(resp.Raw), 800)
})
}

func TestHTTP11DisablesRetryHTTP2Fallback(t *testing.T) {
opts := DefaultOptions
opts.Timeout = 2 * time.Second
opts.Protocol = "http11"

ht, err := New(&opts)
require.NoError(t, err)
require.NotNil(t, ht.client)
require.NotNil(t, ht.client.HTTPClient)
require.Same(t, ht.client.HTTPClient, ht.client.HTTPClient2)
}

func TestDefaultProtocolKeepsDedicatedHTTP2Client(t *testing.T) {
opts := DefaultOptions
opts.Timeout = 2 * time.Second

ht, err := New(&opts)
require.NoError(t, err)
require.NotNil(t, ht.client)
require.NotNil(t, ht.client.HTTPClient)
require.NotNil(t, ht.client.HTTPClient2)
require.NotSame(t, ht.client.HTTPClient, ht.client.HTTPClient2)
}