net/http: fix bug parsing http_proxy lacking a protocol

Per the curl man page, the http_proxy configuration can be
of the form:

   [protocol://]<host>[:port]

And we had a test that <ip>:<port> worked, but if
the host began with a letter, url.Parse parsed the hostname
as the scheme instead, confusing ProxyFromEnvironment.

R=golang-dev
CC=golang-dev
https://golang.org/cl/6875060
This commit is contained in:
Brad Fitzpatrick 2012-12-05 19:08:42 -08:00
parent 08918ba438
commit a034fc9855
2 changed files with 4 additions and 1 deletions

View File

@ -90,7 +90,7 @@ func ProxyFromEnvironment(req *Request) (*url.URL, error) {
return nil, nil
}
proxyURL, err := url.Parse(proxy)
if err != nil || proxyURL.Scheme == "" {
if err != nil || !strings.HasPrefix(proxyURL.Scheme, "http") {
if u, err := url.Parse("http://" + proxy); err == nil {
proxyURL = u
err = nil

View File

@ -1068,6 +1068,9 @@ var proxyFromEnvTests = []struct {
wanterr error
}{
{"127.0.0.1:8080", "http://127.0.0.1:8080", nil},
{"cache.corp.example.com:1234", "http://cache.corp.example.com:1234", nil},
{"cache.corp.example.com", "http://cache.corp.example.com", nil},
{"https://cache.corp.example.com", "https://cache.corp.example.com", nil},
{"http://127.0.0.1:8080", "http://127.0.0.1:8080", nil},
{"https://127.0.0.1:8080", "https://127.0.0.1:8080", nil},
{"", "<nil>", nil},