Commit e6f21be3 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http: support https_proxy in ProxyFromEnvironment

Fixes #6181

LGTM=adg
R=adg
CC=golang-codereviews
https://golang.org/cl/148980043
parent 6077f0fc
...@@ -66,6 +66,7 @@ func NewTestTimeoutHandler(handler Handler, ch <-chan time.Time) Handler { ...@@ -66,6 +66,7 @@ func NewTestTimeoutHandler(handler Handler, ch <-chan time.Time) Handler {
func ResetCachedEnvironment() { func ResetCachedEnvironment() {
httpProxyEnv.reset() httpProxyEnv.reset()
httpsProxyEnv.reset()
noProxyEnv.reset() noProxyEnv.reset()
} }
......
...@@ -124,7 +124,13 @@ type Transport struct { ...@@ -124,7 +124,13 @@ type Transport struct {
// As a special case, if req.URL.Host is "localhost" (with or without // As a special case, if req.URL.Host is "localhost" (with or without
// a port number), then a nil URL and nil error will be returned. // a port number), then a nil URL and nil error will be returned.
func ProxyFromEnvironment(req *Request) (*url.URL, error) { func ProxyFromEnvironment(req *Request) (*url.URL, error) {
proxy := httpProxyEnv.Get() var proxy string
if req.URL.Scheme == "https" {
proxy = httpsProxyEnv.Get()
}
if proxy == "" {
proxy = httpProxyEnv.Get()
}
if proxy == "" { if proxy == "" {
return nil, nil return nil, nil
} }
...@@ -276,6 +282,9 @@ var ( ...@@ -276,6 +282,9 @@ var (
httpProxyEnv = &envOnce{ httpProxyEnv = &envOnce{
names: []string{"HTTP_PROXY", "http_proxy"}, names: []string{"HTTP_PROXY", "http_proxy"},
} }
httpsProxyEnv = &envOnce{
names: []string{"HTTPS_PROXY", "https_proxy"},
}
noProxyEnv = &envOnce{ noProxyEnv = &envOnce{
names: []string{"NO_PROXY", "no_proxy"}, names: []string{"NO_PROXY", "no_proxy"},
} }
......
...@@ -1701,26 +1701,40 @@ Content-Length: %d ...@@ -1701,26 +1701,40 @@ Content-Length: %d
} }
type proxyFromEnvTest struct { type proxyFromEnvTest struct {
req string // URL to fetch; blank means "http://example.com" req string // URL to fetch; blank means "http://example.com"
env string
noenv string env string // HTTP_PROXY
httpsenv string // HTTPS_PROXY
noenv string // NO_RPXY
want string want string
wanterr error wanterr error
} }
func (t proxyFromEnvTest) String() string { func (t proxyFromEnvTest) String() string {
var buf bytes.Buffer var buf bytes.Buffer
space := func() {
if buf.Len() > 0 {
buf.WriteByte(' ')
}
}
if t.env != "" { if t.env != "" {
fmt.Fprintf(&buf, "http_proxy=%q", t.env) fmt.Fprintf(&buf, "http_proxy=%q", t.env)
} }
if t.httpsenv != "" {
space()
fmt.Fprintf(&buf, "https_proxy=%q", t.httpsenv)
}
if t.noenv != "" { if t.noenv != "" {
fmt.Fprintf(&buf, " no_proxy=%q", t.noenv) space()
fmt.Fprintf(&buf, "no_proxy=%q", t.noenv)
} }
req := "http://example.com" req := "http://example.com"
if t.req != "" { if t.req != "" {
req = t.req req = t.req
} }
fmt.Fprintf(&buf, " req=%q", req) space()
fmt.Fprintf(&buf, "req=%q", req)
return strings.TrimSpace(buf.String()) return strings.TrimSpace(buf.String())
} }
...@@ -1731,7 +1745,15 @@ var proxyFromEnvTests = []proxyFromEnvTest{ ...@@ -1731,7 +1745,15 @@ var proxyFromEnvTests = []proxyFromEnvTest{
{env: "https://cache.corp.example.com", want: "https://cache.corp.example.com"}, {env: "https://cache.corp.example.com", want: "https://cache.corp.example.com"},
{env: "http://127.0.0.1:8080", want: "http://127.0.0.1:8080"}, {env: "http://127.0.0.1:8080", want: "http://127.0.0.1:8080"},
{env: "https://127.0.0.1:8080", want: "https://127.0.0.1:8080"}, {env: "https://127.0.0.1:8080", want: "https://127.0.0.1:8080"},
// Don't use secure for http
{req: "http://insecure.tld/", env: "http.proxy.tld", httpsenv: "secure.proxy.tld", want: "http://http.proxy.tld"},
// Use secure for https.
{req: "https://secure.tld/", env: "http.proxy.tld", httpsenv: "secure.proxy.tld", want: "http://secure.proxy.tld"},
{req: "https://secure.tld/", env: "http.proxy.tld", httpsenv: "https://secure.proxy.tld", want: "https://secure.proxy.tld"},
{want: "<nil>"}, {want: "<nil>"},
{noenv: "example.com", req: "http://example.com/", env: "proxy", want: "<nil>"}, {noenv: "example.com", req: "http://example.com/", env: "proxy", want: "<nil>"},
{noenv: ".example.com", req: "http://example.com/", env: "proxy", want: "<nil>"}, {noenv: ".example.com", req: "http://example.com/", env: "proxy", want: "<nil>"},
{noenv: "ample.com", req: "http://example.com/", env: "proxy", want: "http://proxy"}, {noenv: "ample.com", req: "http://example.com/", env: "proxy", want: "http://proxy"},
...@@ -1743,6 +1765,7 @@ func TestProxyFromEnvironment(t *testing.T) { ...@@ -1743,6 +1765,7 @@ func TestProxyFromEnvironment(t *testing.T) {
ResetProxyEnv() ResetProxyEnv()
for _, tt := range proxyFromEnvTests { for _, tt := range proxyFromEnvTests {
os.Setenv("HTTP_PROXY", tt.env) os.Setenv("HTTP_PROXY", tt.env)
os.Setenv("HTTPS_PROXY", tt.httpsenv)
os.Setenv("NO_PROXY", tt.noenv) os.Setenv("NO_PROXY", tt.noenv)
ResetCachedEnvironment() ResetCachedEnvironment()
reqURL := tt.req reqURL := tt.req
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment