Skip to content

net/http: add new error type to be returned on unsuccessful CONNECT request #44123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
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
20 changes: 16 additions & 4 deletions src/net/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,20 @@ type erringRoundTripper interface {
RoundTripErr() error
}

// ProxyConnectError holds the response to an unsuccessful proxy CONNECT request.
type ProxyConnectError struct {
Response *Response
}

func (p *ProxyConnectError) Error() string {
// Don't return full Response.Status for backwards compatibility
f := strings.SplitN(p.Response.Status, " ", 2)
if len(f) < 2 {
return "unknown status code"
}
return f[1]
}

func (t *Transport) dialConn(ctx context.Context, cm connectMethod) (pconn *persistConn, err error) {
pconn = &persistConn{
t: t,
Expand Down Expand Up @@ -1711,12 +1725,10 @@ func (t *Transport) dialConn(ctx context.Context, cm connectMethod) (pconn *pers
return nil, err
}
if resp.StatusCode != 200 {
f := strings.SplitN(resp.Status, " ", 2)
conn.Close()
if len(f) < 2 {
return nil, errors.New("unknown status code")
return nil, &ProxyConnectError{
Response: resp,
}
return nil, errors.New(f[1])
}
}

Expand Down
35 changes: 35 additions & 0 deletions src/net/http/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3774,6 +3774,41 @@ func TestRoundTripReturnsProxyError(t *testing.T) {
}
}

// Test for issue 38143
// Return an error containing the proxy CONNECT request if status is not 200
func TestRoundTripReturnsProxyConnectError(t *testing.T) {
s := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
w.WriteHeader(StatusServiceUnavailable)
}))
defer s.Close()

proxyURL, err := url.Parse(s.URL)
if err != nil {
t.Fatal(err)
}
tr := &Transport{
Proxy: ProxyURL(proxyURL),
}

req, err := NewRequest("GET", "https://example.com", nil)
if err != nil {
t.Fatal(err)
}

_, got := tr.RoundTrip(req)

switch errType := got.(type) {
case *ProxyConnectError:
default:
t.Errorf("Wrong error type returned, Got: %T, Want: *ProxyConnectError", errType)
}

// Test backwards compatibility
if got.Error() != "Service Unavailable" {
t.Error("Non-backwards compatible error message from *ProxyConnectError")
}
}

// tests that putting an idle conn after a call to CloseIdleConns does return it
func TestTransportCloseIdleConnsThenReturn(t *testing.T) {
tr := &Transport{}
Expand Down