Skip to content

Commit c099eea

Browse files
committed
net/http: add Transport.OnProxyConnectResponse for debugging
1 parent 9473dd3 commit c099eea

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

api/go1.tailscale.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pkg net/http, type Transport struct, GetProxyConnectHeader func(context.Context, *url.URL, string) (Header, error)
2+
pkg net/http, type Transport struct, OnProxyConnectResponse func(context.Context, *url.URL, *Request, *Response) error

src/net/http/transport.go

+11
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ type Transport struct {
248248
// If GetProxyConnectHeader is non-nil, ProxyConnectHeader is ignored.
249249
GetProxyConnectHeader func(ctx context.Context, proxyURL *url.URL, target string) (Header, error)
250250

251+
// OnProxyConnectResponse is called when the Transport gets an HTTP response from
252+
// a proxy for a CONNECT request. It's called before the check for a 200 OK response.
253+
// If it returns an error, the request fails with that error.
254+
OnProxyConnectResponse func(ctx context.Context, proxyURL *url.URL, connectReq *Request, connectRes *Response) error
255+
251256
// MaxResponseHeaderBytes specifies a limit on how many
252257
// response bytes are allowed in the server's response
253258
// header.
@@ -320,6 +325,7 @@ func (t *Transport) Clone() *Transport {
320325
ExpectContinueTimeout: t.ExpectContinueTimeout,
321326
ProxyConnectHeader: t.ProxyConnectHeader.Clone(),
322327
GetProxyConnectHeader: t.GetProxyConnectHeader,
328+
OnProxyConnectResponse: t.OnProxyConnectResponse,
323329
MaxResponseHeaderBytes: t.MaxResponseHeaderBytes,
324330
ForceAttemptHTTP2: t.ForceAttemptHTTP2,
325331
WriteBufferSize: t.WriteBufferSize,
@@ -1692,6 +1698,11 @@ func (t *Transport) dialConn(ctx context.Context, cm connectMethod) (pconn *pers
16921698
conn.Close()
16931699
return nil, err
16941700
}
1701+
if f := t.OnProxyConnectResponse; f != nil {
1702+
if err := f(ctx, cm.proxyURL, connectReq, resp); err != nil {
1703+
return nil, err
1704+
}
1705+
}
16951706
if resp.StatusCode != 200 {
16961707
f := strings.SplitN(resp.Status, " ", 2)
16971708
conn.Close()

src/net/http/transport_test.go

+20-17
Original file line numberDiff line numberDiff line change
@@ -5830,23 +5830,26 @@ func TestTransportRequestWriteRoundTrip(t *testing.T) {
58305830

58315831
func TestTransportClone(t *testing.T) {
58325832
tr := &Transport{
5833-
Proxy: func(*Request) (*url.URL, error) { panic("") },
5834-
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { panic("") },
5835-
Dial: func(network, addr string) (net.Conn, error) { panic("") },
5836-
DialTLS: func(network, addr string) (net.Conn, error) { panic("") },
5837-
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) { panic("") },
5838-
TLSClientConfig: new(tls.Config),
5839-
TLSHandshakeTimeout: time.Second,
5840-
DisableKeepAlives: true,
5841-
DisableCompression: true,
5842-
MaxIdleConns: 1,
5843-
MaxIdleConnsPerHost: 1,
5844-
MaxConnsPerHost: 1,
5845-
IdleConnTimeout: time.Second,
5846-
ResponseHeaderTimeout: time.Second,
5847-
ExpectContinueTimeout: time.Second,
5848-
ProxyConnectHeader: Header{},
5849-
GetProxyConnectHeader: func(context.Context, *url.URL, string) (Header, error) { return nil, nil },
5833+
Proxy: func(*Request) (*url.URL, error) { panic("") },
5834+
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { panic("") },
5835+
Dial: func(network, addr string) (net.Conn, error) { panic("") },
5836+
DialTLS: func(network, addr string) (net.Conn, error) { panic("") },
5837+
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) { panic("") },
5838+
TLSClientConfig: new(tls.Config),
5839+
TLSHandshakeTimeout: time.Second,
5840+
DisableKeepAlives: true,
5841+
DisableCompression: true,
5842+
MaxIdleConns: 1,
5843+
MaxIdleConnsPerHost: 1,
5844+
MaxConnsPerHost: 1,
5845+
IdleConnTimeout: time.Second,
5846+
ResponseHeaderTimeout: time.Second,
5847+
ExpectContinueTimeout: time.Second,
5848+
ProxyConnectHeader: Header{},
5849+
GetProxyConnectHeader: func(context.Context, *url.URL, string) (Header, error) { return nil, nil },
5850+
OnProxyConnectResponse: func(ctx context.Context, proxyURL *url.URL, connectReq *Request, connectRes *Response) error {
5851+
return nil
5852+
},
58505853
MaxResponseHeaderBytes: 1,
58515854
ForceAttemptHTTP2: true,
58525855
TLSNextProto: map[string]func(authority string, c *tls.Conn) RoundTripper{

0 commit comments

Comments
 (0)