Skip to content

Commit 97f4d42

Browse files
committed
clean up proxy and remove buffered reader - don't actually need it
1 parent 332be40 commit 97f4d42

1 file changed

Lines changed: 16 additions & 40 deletions

File tree

internal/api/proxy.go

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,10 @@ import (
99
"net"
1010
"net/http"
1111
"net/url"
12-
"sync"
1312

1413
"github.com/sourcegraph/sourcegraph/lib/errors"
1514
)
1615

17-
type connWithBufferedReader struct {
18-
net.Conn
19-
r *bufio.Reader
20-
mu sync.Mutex
21-
}
22-
23-
func (c *connWithBufferedReader) Read(p []byte) (int, error) {
24-
c.mu.Lock()
25-
defer c.mu.Unlock()
26-
return c.r.Read(p)
27-
}
28-
2916
// proxyDialAddr returns proxyURL.Host with a default port appended if one is
3017
// not already present (443 for https, 80 for http).
3118
func proxyDialAddr(proxyURL *url.URL) string {
@@ -93,39 +80,29 @@ func withProxyTransport(baseTransport *http.Transport, proxyURL *url.URL, proxyP
9380
baseTransport.Proxy = http.ProxyURL(proxyURL)
9481
case "https":
9582
dial := func(ctx context.Context, network, addr string) (net.Conn, error) {
96-
// Dial the proxy. For https:// proxies, we TLS-connect to the
83+
// Dial the proxy. For TLS-enabled proxies, we TLS-connect to the
9784
// proxy itself and force ALPN to HTTP/1.1 to prevent Go from
9885
// negotiating HTTP/2 for the CONNECT tunnel. Many proxy servers
9986
// don't support HTTP/2 CONNECT, and Go's default Transport.Proxy
100-
// would negotiate h2 via ALPN when TLS-connecting to an https://
101-
// proxy, causing "bogus greeting" errors. For http:// proxies,
87+
// would negotiate h2 via ALPN when TLS-connecting to a TLS-enabled
88+
// proxy, causing "bogus greeting" errors. For plain HTTP proxies,
10289
// CONNECT is always HTTP/1.1 over plain TCP so this isn't needed.
10390
// The target connection (e.g. to sourcegraph.com) still negotiates
10491
// HTTP/2 normally through the established tunnel.
10592
proxyAddr := proxyDialAddr(proxyURL)
10693

107-
var conn net.Conn
108-
var err error
109-
if proxyURL.Scheme == "https" {
110-
raw, dialErr := (&net.Dialer{}).DialContext(ctx, "tcp", proxyAddr)
111-
if dialErr != nil {
112-
return nil, dialErr
113-
}
114-
cfg := baseTransport.TLSClientConfig.Clone()
115-
cfg.NextProtos = []string{"http/1.1"}
116-
if cfg.ServerName == "" {
117-
cfg.ServerName = proxyURL.Hostname()
118-
}
119-
tlsConn := tls.Client(raw, cfg)
120-
if err := tlsConn.HandshakeContext(ctx); err != nil {
121-
raw.Close()
122-
return nil, err
123-
}
124-
conn = tlsConn
125-
} else {
126-
conn, err = (&net.Dialer{}).DialContext(ctx, "tcp", proxyAddr)
94+
raw, dialErr := (&net.Dialer{}).DialContext(ctx, "tcp", proxyAddr)
95+
if dialErr != nil {
96+
return nil, dialErr
12797
}
128-
if err != nil {
98+
cfg := baseTransport.TLSClientConfig.Clone()
99+
cfg.NextProtos = []string{"http/1.1"}
100+
if cfg.ServerName == "" {
101+
cfg.ServerName = proxyURL.Hostname()
102+
}
103+
conn := tls.Client(raw, cfg)
104+
if err := conn.HandshakeContext(ctx); err != nil {
105+
raw.Close()
129106
return nil, err
130107
}
131108

@@ -145,8 +122,7 @@ func withProxyTransport(baseTransport *http.Transport, proxyURL *url.URL, proxyP
145122
return nil, err
146123
}
147124

148-
br := bufio.NewReader(conn)
149-
resp, err := http.ReadResponse(br, nil)
125+
resp, err := http.ReadResponse(bufio.NewReader(conn), nil)
150126
if err != nil {
151127
conn.Close()
152128
return nil, err
@@ -160,7 +136,7 @@ func withProxyTransport(baseTransport *http.Transport, proxyURL *url.URL, proxyP
160136
return nil, errors.Newf("failed to connect to proxy %s: %s: %q", proxyURL.Redacted(), resp.Status, b)
161137
}
162138
// 200 CONNECT: do NOT resp.Body.Close(); it would interfere with the tunnel.
163-
return &connWithBufferedReader{Conn: conn, r: br}, nil
139+
return conn, nil
164140
}
165141
dialTLS := func(ctx context.Context, network, addr string) (net.Conn, error) {
166142
// Dial the underlying connection through the proxy

0 commit comments

Comments
 (0)