use of java.net.Proxy in project bazel by bazelbuild.
the class ProxyHelperTest method testTrailingSlash.
@Test
public void testTrailingSlash() throws Exception {
Proxy proxy = ProxyHelper.createProxy("http://foo:bar@example.com:8000/");
assertEquals(Proxy.Type.HTTP, proxy.type());
assertThat(proxy.toString()).endsWith(":8000");
assertEquals(System.getProperty("http.proxyHost"), "example.com");
assertEquals(System.getProperty("http.proxyPort"), "8000");
assertEquals(System.getProperty("http.proxyUser"), "foo");
assertEquals(System.getProperty("http.proxyPassword"), "bar");
}
use of java.net.Proxy in project bazel by bazelbuild.
the class ProxyHelperTest method testNoProxy.
@Test
public void testNoProxy() throws Exception {
// Empty address.
Proxy proxy = ProxyHelper.createProxy(null);
assertEquals(Proxy.NO_PROXY, proxy);
proxy = ProxyHelper.createProxy("");
assertEquals(Proxy.NO_PROXY, proxy);
Map<String, String> env = ImmutableMap.of();
ProxyHelper helper = new ProxyHelper(env);
proxy = helper.createProxyIfNeeded(new URL("https://www.something.com"));
assertEquals(Proxy.NO_PROXY, proxy);
}
use of java.net.Proxy in project bazel by bazelbuild.
the class ProxyHelperTest method testCreateIfNeededHttpsLowerCase.
@Test
public void testCreateIfNeededHttpsLowerCase() throws Exception {
ProxyHelper helper = new ProxyHelper(ImmutableMap.of("https_proxy", "https://my.example.com"));
Proxy proxy = helper.createProxyIfNeeded(new URL("https://www.something.com"));
assertThat(proxy.toString()).endsWith("my.example.com:443");
}
use of java.net.Proxy in project bazel by bazelbuild.
the class ProxyHelperTest method testProxyExplicitPort.
@Test
public void testProxyExplicitPort() throws Exception {
Proxy proxy = ProxyHelper.createProxy("http://my.example.com:12345");
assertThat(proxy.toString()).endsWith(":12345");
assertEquals(System.getProperty("http.proxyHost"), "my.example.com");
assertEquals(System.getProperty("http.proxyPort"), "12345");
proxy = ProxyHelper.createProxy("https://my.example.com:12345");
assertThat(proxy.toString()).endsWith(":12345");
assertEquals(System.getProperty("https.proxyHost"), "my.example.com");
assertEquals(System.getProperty("https.proxyPort"), "12345");
}
use of java.net.Proxy in project bazel by bazelbuild.
the class ProxyHelper method createProxy.
/**
* This method takes a proxyAddress as a String (ex.
* http://userId:password@proxyhost.domain.com:8000) and sets JVM arguments for http and https
* proxy as well as returns a java.net.Proxy object for optional use.
*
* @param proxyAddress The fully qualified address of the proxy server
* @return Proxy
* @throws IOException
*/
public static Proxy createProxy(@Nullable String proxyAddress) throws IOException {
if (Strings.isNullOrEmpty(proxyAddress)) {
return Proxy.NO_PROXY;
}
// Here there be dragons.
Pattern urlPattern = Pattern.compile("^(https?)://(([^:@]+?)(?::([^@]+?))?@)?([^:]+)(?::(\\d+))?/?$");
Matcher matcher = urlPattern.matcher(proxyAddress);
if (!matcher.matches()) {
throw new IOException("Proxy address " + proxyAddress + " is not a valid URL");
}
final String protocol = matcher.group(1);
final String idAndPassword = matcher.group(2);
final String username = matcher.group(3);
final String password = matcher.group(4);
final String hostname = matcher.group(5);
final String portRaw = matcher.group(6);
String cleanProxyAddress = proxyAddress;
if (idAndPassword != null) {
cleanProxyAddress = // Used to remove id+pwd from logging
proxyAddress.replace(idAndPassword, "");
}
boolean https;
switch(protocol) {
case "https":
https = true;
break;
case "http":
https = false;
break;
default:
throw new IOException("Invalid proxy protocol for " + cleanProxyAddress);
}
// Default port numbers
int port = https ? 443 : 80;
if (portRaw != null) {
try {
port = Integer.parseInt(portRaw);
} catch (NumberFormatException e) {
throw new IOException("Error parsing proxy port: " + cleanProxyAddress);
}
}
// We need to set both of these because jgit uses whichever the resource dictates
System.setProperty("https.proxyHost", hostname);
System.setProperty("https.proxyPort", Integer.toString(port));
System.setProperty("http.proxyHost", hostname);
System.setProperty("http.proxyPort", Integer.toString(port));
if (username != null) {
if (password == null) {
throw new IOException("No password given for proxy " + cleanProxyAddress);
}
// We need to make sure the proxy password is not url encoded; some special characters in
// proxy passwords require url encoding for shells and other tools to properly consume.
final String decodedPassword = URLDecoder.decode(password, "UTF-8");
System.setProperty("http.proxyUser", username);
System.setProperty("http.proxyPassword", decodedPassword);
System.setProperty("https.proxyUser", username);
System.setProperty("https.proxyPassword", decodedPassword);
Authenticator.setDefault(new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, decodedPassword.toCharArray());
}
});
}
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(hostname, port));
}
Aggregations