Skip to content

Commit

Permalink
Fix proxy parsing when HTTP_PROXY doesn't have a scheme (bazelbuild#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheister committed Mar 30, 2021
1 parent fa73b1a commit 31114ef
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
18 changes: 10 additions & 8 deletions private/proxy.bzl
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
def _get_proxy_user(url):
netloc = url.split("://", 1)[1]
if "@" in netloc:
userinfo = netloc.rsplit("@", 1)[0]
no_scheme_url = url.split("://", 1)[1] if "://" in url else url
if "@" in no_scheme_url:
userinfo = no_scheme_url.rsplit("@", 1)[0]
if ":" in userinfo:
userinfo = userinfo.split(":", 1)[0]
return userinfo
return None

def _get_proxy_password(url):
netloc = url.split("://", 1)[1]
if "@" in netloc:
userinfo = netloc.rsplit("@", 1)[0]
no_scheme_url = url.split("://", 1)[1] if "://" in url else url
if "@" in no_scheme_url:
userinfo = no_scheme_url.rsplit("@", 1)[0]
if ":" in userinfo:
userinfo = userinfo.split(":", 1)[1]
return userinfo
return None

def _get_proxy_hostname(url):
netloc = url.split("://", 1)[1].split("@")[-1]
no_scheme_url = url.split("://", 1)[1] if "://" in url else url
netloc = no_scheme_url.split("@")[-1]
if ":" in netloc:
return netloc.split(":")[0]
else:
return netloc

def _get_proxy_port(url):
netloc = url.split("://", 1)[1].split("/")[0].split("@")[-1]
no_scheme_url = url.split("://", 1)[1] if "://" in url else url
netloc = no_scheme_url.split("/")[0].split("@")[-1]
if ":" in netloc:
return netloc.split(":")[1]
else:
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/proxy_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ def _java_proxy_parsing_empty_test_impl(ctx):

java_proxy_parsing_empty_test = unittest.make(_java_proxy_parsing_empty_test_impl)

def _java_proxy_parsing_no_scheme_test_impl(ctx):
env = unittest.begin(ctx)
asserts.equals(
env,
[
"-Dhttp.proxyHost=localhost",
"-Dhttp.proxyPort=8888",
"-Dhttps.proxyHost=localhost",
"-Dhttps.proxyPort=8843",
"-Dhttp.nonProxyHosts=google.com",
],
get_java_proxy_args("localhost:8888", "localhost:8843", "google.com"),
)
return unittest.end(env)

java_proxy_parsing_no_scheme_test = unittest.make(_java_proxy_parsing_no_scheme_test_impl)

def _java_proxy_parsing_no_user_test_impl(ctx):
env = unittest.begin(ctx)
asserts.equals(
Expand Down Expand Up @@ -83,6 +100,7 @@ def proxy_test_suite():
unittest.suite(
"proxy_tests",
java_proxy_parsing_empty_test,
java_proxy_parsing_no_scheme_test,
java_proxy_parsing_no_user_test,
java_proxy_parsing_no_port_test,
java_proxy_parsing_trailing_slash_test,
Expand Down

0 comments on commit 31114ef

Please sign in to comment.