Skip to content

Commit 8c07e20

Browse files
huww98gopherbot
authored andcommitted
httpproxy: allow any scheme
currently only http/https/socks5 scheme are allowed. However, any scheme could be possible if user provides their own implementation. Specifically, the widely used "socks5h://localhost" is parsed as Scheme="http" Host="socks5h:", which does not make sense because host name cannot contain ":". This patch allows any scheme to appear in the proxy config. And only fallback to http scheme if parsed scheme or host is empty. url.Parse() result of fallback cases: localhost => Scheme="localhost" localhost:1234 => Scheme="localhost" Opaque="1234" example.com => Path="example.com" Updates golang/go#24135 Change-Id: Ia2c041e37e2ac61be16220fd41d6cb6fabeeca3d Reviewed-on: https://go-review.googlesource.com/c/net/+/525257 LUCI-TryBot-Result: Go LUCI <[email protected]> Run-TryBot: Damien Neil <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Damien Neil <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Damien Neil <[email protected]>
1 parent ab271c3 commit 8c07e20

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

http/httpproxy/proxy.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,7 @@ func parseProxy(proxy string) (*url.URL, error) {
149149
}
150150

151151
proxyURL, err := url.Parse(proxy)
152-
if err != nil ||
153-
(proxyURL.Scheme != "http" &&
154-
proxyURL.Scheme != "https" &&
155-
proxyURL.Scheme != "socks5") {
152+
if err != nil || proxyURL.Scheme == "" || proxyURL.Host == "" {
156153
// proxy was bogus. Try prepending "http://" to it and
157154
// see if that parses correctly. If not, we fall
158155
// through and complain about the original one.

http/httpproxy/proxy_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ var proxyForURLTests = []proxyForURLTest{{
6868
HTTPProxy: "cache.corp.example.com",
6969
},
7070
want: "http://cache.corp.example.com",
71+
}, {
72+
// single label domain is recognized as scheme by url.Parse
73+
cfg: httpproxy.Config{
74+
HTTPProxy: "localhost",
75+
},
76+
want: "http://localhost",
7177
}, {
7278
cfg: httpproxy.Config{
7379
HTTPProxy: "https://cache.corp.example.com",
@@ -88,6 +94,12 @@ var proxyForURLTests = []proxyForURLTest{{
8894
HTTPProxy: "socks5://127.0.0.1",
8995
},
9096
want: "socks5://127.0.0.1",
97+
}, {
98+
// Preserve unknown schemes.
99+
cfg: httpproxy.Config{
100+
HTTPProxy: "foo://host",
101+
},
102+
want: "foo://host",
91103
}, {
92104
// Don't use secure for http
93105
cfg: httpproxy.Config{

0 commit comments

Comments
 (0)