Skip to content

Commit

Permalink
[tailscale1.18] net/http: add Transport.OnProxyConnectResponse for de…
Browse files Browse the repository at this point in the history
…bugging

Change-Id: I114abf13920e3e91bfe8603e787aae219045137d
  • Loading branch information
josharian committed Feb 7, 2022
1 parent 212a90c commit 485a201
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
1 change: 1 addition & 0 deletions api/go1.tailscale.txt
@@ -0,0 +1 @@
pkg net/http, type Transport struct, OnProxyConnectResponse func(context.Context, *url.URL, *Request, *Response) error
11 changes: 11 additions & 0 deletions src/net/http/transport.go
Expand Up @@ -252,6 +252,11 @@ type Transport struct {
// ignored.
GetProxyConnectHeader func(ctx context.Context, proxyURL *url.URL, target string) (Header, error)

// OnProxyConnectResponse is called when the Transport gets an HTTP response from
// a proxy for a CONNECT request. It's called before the check for a 200 OK response.
// If it returns an error, the request fails with that error.
OnProxyConnectResponse func(ctx context.Context, proxyURL *url.URL, connectReq *Request, connectRes *Response) error

// MaxResponseHeaderBytes specifies a limit on how many
// response bytes are allowed in the server's response
// header.
Expand Down Expand Up @@ -324,6 +329,7 @@ func (t *Transport) Clone() *Transport {
ExpectContinueTimeout: t.ExpectContinueTimeout,
ProxyConnectHeader: t.ProxyConnectHeader.Clone(),
GetProxyConnectHeader: t.GetProxyConnectHeader,
OnProxyConnectResponse: t.OnProxyConnectResponse,
MaxResponseHeaderBytes: t.MaxResponseHeaderBytes,
ForceAttemptHTTP2: t.ForceAttemptHTTP2,
WriteBufferSize: t.WriteBufferSize,
Expand Down Expand Up @@ -1714,6 +1720,11 @@ func (t *Transport) dialConn(ctx context.Context, cm connectMethod) (pconn *pers
conn.Close()
return nil, err
}
if f := t.OnProxyConnectResponse; f != nil {
if err := f(ctx, cm.proxyURL, connectReq, resp); err != nil {
return nil, err
}
}
if resp.StatusCode != 200 {
_, text, ok := strings.Cut(resp.Status, " ")
conn.Close()
Expand Down
37 changes: 20 additions & 17 deletions src/net/http/transport_test.go
Expand Up @@ -5878,23 +5878,26 @@ func TestTransportRequestWriteRoundTrip(t *testing.T) {

func TestTransportClone(t *testing.T) {
tr := &Transport{
Proxy: func(*Request) (*url.URL, error) { panic("") },
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { panic("") },
Dial: func(network, addr string) (net.Conn, error) { panic("") },
DialTLS: func(network, addr string) (net.Conn, error) { panic("") },
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) { panic("") },
TLSClientConfig: new(tls.Config),
TLSHandshakeTimeout: time.Second,
DisableKeepAlives: true,
DisableCompression: true,
MaxIdleConns: 1,
MaxIdleConnsPerHost: 1,
MaxConnsPerHost: 1,
IdleConnTimeout: time.Second,
ResponseHeaderTimeout: time.Second,
ExpectContinueTimeout: time.Second,
ProxyConnectHeader: Header{},
GetProxyConnectHeader: func(context.Context, *url.URL, string) (Header, error) { return nil, nil },
Proxy: func(*Request) (*url.URL, error) { panic("") },
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { panic("") },
Dial: func(network, addr string) (net.Conn, error) { panic("") },
DialTLS: func(network, addr string) (net.Conn, error) { panic("") },
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) { panic("") },
TLSClientConfig: new(tls.Config),
TLSHandshakeTimeout: time.Second,
DisableKeepAlives: true,
DisableCompression: true,
MaxIdleConns: 1,
MaxIdleConnsPerHost: 1,
MaxConnsPerHost: 1,
IdleConnTimeout: time.Second,
ResponseHeaderTimeout: time.Second,
ExpectContinueTimeout: time.Second,
ProxyConnectHeader: Header{},
GetProxyConnectHeader: func(context.Context, *url.URL, string) (Header, error) { return nil, nil },
OnProxyConnectResponse: func(ctx context.Context, proxyURL *url.URL, connectReq *Request, connectRes *Response) error {
return nil
},
MaxResponseHeaderBytes: 1,
ForceAttemptHTTP2: true,
TLSNextProto: map[string]func(authority string, c *tls.Conn) RoundTripper{
Expand Down

0 comments on commit 485a201

Please sign in to comment.