@Test public void testHttp() throws Exception { assertExpectedProxyConfig(proxyConfigFromEnv("http", ImmutableMap.of( "http_proxy", PROXY_URL, "HTTP_PROXY", "http: "https_proxy", "http: "HTTPS_PROXY", "http: assertExpectedProxyConfig(proxyConfigFromEnv("http", ImmutableMap.of( "HTTP_PROXY", PROXY_URL, "https_proxy", "http: "HTTPS_PROXY", "http: assertThat(proxyConfigFromEnv("http", ImmutableMap.of( "https_proxy", PROXY_URL, "HTTPS_PROXY", "http: assertThat(proxyConfigFromEnv("http", ImmutableMap.of( "HTTPS_PROXY", PROXY_URL)), is(Optional.absent())); }
@VisibleForTesting public static Optional<ProxyConfig> proxyConfigFromEnv(String scheme, Map<String, String> env) { List<String> preference; switch (scheme) { case "http": preference = ImmutableList.of("http_proxy", "HTTP_PROXY"); break; case "https": preference = ImmutableList.of("https_proxy", "HTTPS_PROXY", "http_proxy", "HTTP_PROXY"); break; default: throw new IllegalArgumentException("Unsupported scheme: " + scheme); } String proxyEnvVarName = null; String var = null; for (String name : preference) { var = env.getOrDefault(name, "").trim(); if (!var.isEmpty()) { proxyEnvVarName = name; break; } } if (proxyEnvVarName == null) { return Optional.absent(); } URI uri; try { uri = new URI(var); } catch (URISyntaxException e) { logger.warn("Failed to parse environment variable, ignoring: {}={}", proxyEnvVarName, var, e); return Optional.absent(); } ProxyConfig.ProxyConfigBuilder builder = new ProxyConfig.ProxyConfigBuilder(); builder.setHost(uri.getHost()); if (uri.getPort() != -1) { builder.setPort(uri.getPort()); } if (uri.getRawUserInfo() != null) { String userInfo = uri.getRawUserInfo(); int colonIndex = userInfo.indexOf(':'); if (colonIndex == -1) { builder.setUser(decode(userInfo)); } else { String user = userInfo.substring(0, colonIndex); String pass = userInfo.substring(colonIndex + 1, userInfo.length()); builder.setUser(decode(user)); builder.setPassword(decode(pass)); } } if ("https".equals(uri.getScheme())) { builder.useSSL(true); } return Optional.of(builder.createProxyConfig()); }
Proxies { @VisibleForTesting public static Optional<ProxyConfig> proxyConfigFromEnv(String scheme, Map<String, String> env) { List<String> preference; switch (scheme) { case "http": preference = ImmutableList.of("http_proxy", "HTTP_PROXY"); break; case "https": preference = ImmutableList.of("https_proxy", "HTTPS_PROXY", "http_proxy", "HTTP_PROXY"); break; default: throw new IllegalArgumentException("Unsupported scheme: " + scheme); } String proxyEnvVarName = null; String var = null; for (String name : preference) { var = env.getOrDefault(name, "").trim(); if (!var.isEmpty()) { proxyEnvVarName = name; break; } } if (proxyEnvVarName == null) { return Optional.absent(); } URI uri; try { uri = new URI(var); } catch (URISyntaxException e) { logger.warn("Failed to parse environment variable, ignoring: {}={}", proxyEnvVarName, var, e); return Optional.absent(); } ProxyConfig.ProxyConfigBuilder builder = new ProxyConfig.ProxyConfigBuilder(); builder.setHost(uri.getHost()); if (uri.getPort() != -1) { builder.setPort(uri.getPort()); } if (uri.getRawUserInfo() != null) { String userInfo = uri.getRawUserInfo(); int colonIndex = userInfo.indexOf(':'); if (colonIndex == -1) { builder.setUser(decode(userInfo)); } else { String user = userInfo.substring(0, colonIndex); String pass = userInfo.substring(colonIndex + 1, userInfo.length()); builder.setUser(decode(user)); builder.setPassword(decode(pass)); } } if ("https".equals(uri.getScheme())) { builder.useSSL(true); } return Optional.of(builder.createProxyConfig()); } }
Proxies { @VisibleForTesting public static Optional<ProxyConfig> proxyConfigFromEnv(String scheme, Map<String, String> env) { List<String> preference; switch (scheme) { case "http": preference = ImmutableList.of("http_proxy", "HTTP_PROXY"); break; case "https": preference = ImmutableList.of("https_proxy", "HTTPS_PROXY", "http_proxy", "HTTP_PROXY"); break; default: throw new IllegalArgumentException("Unsupported scheme: " + scheme); } String proxyEnvVarName = null; String var = null; for (String name : preference) { var = env.getOrDefault(name, "").trim(); if (!var.isEmpty()) { proxyEnvVarName = name; break; } } if (proxyEnvVarName == null) { return Optional.absent(); } URI uri; try { uri = new URI(var); } catch (URISyntaxException e) { logger.warn("Failed to parse environment variable, ignoring: {}={}", proxyEnvVarName, var, e); return Optional.absent(); } ProxyConfig.ProxyConfigBuilder builder = new ProxyConfig.ProxyConfigBuilder(); builder.setHost(uri.getHost()); if (uri.getPort() != -1) { builder.setPort(uri.getPort()); } if (uri.getRawUserInfo() != null) { String userInfo = uri.getRawUserInfo(); int colonIndex = userInfo.indexOf(':'); if (colonIndex == -1) { builder.setUser(decode(userInfo)); } else { String user = userInfo.substring(0, colonIndex); String pass = userInfo.substring(colonIndex + 1, userInfo.length()); builder.setUser(decode(user)); builder.setPassword(decode(pass)); } } if ("https".equals(uri.getScheme())) { builder.useSSL(true); } return Optional.of(builder.createProxyConfig()); }  }
Proxies { @VisibleForTesting public static Optional<ProxyConfig> proxyConfigFromEnv(String scheme, Map<String, String> env) { List<String> preference; switch (scheme) { case "http": preference = ImmutableList.of("http_proxy", "HTTP_PROXY"); break; case "https": preference = ImmutableList.of("https_proxy", "HTTPS_PROXY", "http_proxy", "HTTP_PROXY"); break; default: throw new IllegalArgumentException("Unsupported scheme: " + scheme); } String proxyEnvVarName = null; String var = null; for (String name : preference) { var = env.getOrDefault(name, "").trim(); if (!var.isEmpty()) { proxyEnvVarName = name; break; } } if (proxyEnvVarName == null) { return Optional.absent(); } URI uri; try { uri = new URI(var); } catch (URISyntaxException e) { logger.warn("Failed to parse environment variable, ignoring: {}={}", proxyEnvVarName, var, e); return Optional.absent(); } ProxyConfig.ProxyConfigBuilder builder = new ProxyConfig.ProxyConfigBuilder(); builder.setHost(uri.getHost()); if (uri.getPort() != -1) { builder.setPort(uri.getPort()); } if (uri.getRawUserInfo() != null) { String userInfo = uri.getRawUserInfo(); int colonIndex = userInfo.indexOf(':'); if (colonIndex == -1) { builder.setUser(decode(userInfo)); } else { String user = userInfo.substring(0, colonIndex); String pass = userInfo.substring(colonIndex + 1, userInfo.length()); builder.setUser(decode(user)); builder.setPassword(decode(pass)); } } if ("https".equals(uri.getScheme())) { builder.useSSL(true); } return Optional.of(builder.createProxyConfig()); }  @VisibleForTesting static Optional<ProxyConfig> proxyConfigFromEnv(String scheme, Map<String, String> env); }
Proxies { @VisibleForTesting public static Optional<ProxyConfig> proxyConfigFromEnv(String scheme, Map<String, String> env) { List<String> preference; switch (scheme) { case "http": preference = ImmutableList.of("http_proxy", "HTTP_PROXY"); break; case "https": preference = ImmutableList.of("https_proxy", "HTTPS_PROXY", "http_proxy", "HTTP_PROXY"); break; default: throw new IllegalArgumentException("Unsupported scheme: " + scheme); } String proxyEnvVarName = null; String var = null; for (String name : preference) { var = env.getOrDefault(name, "").trim(); if (!var.isEmpty()) { proxyEnvVarName = name; break; } } if (proxyEnvVarName == null) { return Optional.absent(); } URI uri; try { uri = new URI(var); } catch (URISyntaxException e) { logger.warn("Failed to parse environment variable, ignoring: {}={}", proxyEnvVarName, var, e); return Optional.absent(); } ProxyConfig.ProxyConfigBuilder builder = new ProxyConfig.ProxyConfigBuilder(); builder.setHost(uri.getHost()); if (uri.getPort() != -1) { builder.setPort(uri.getPort()); } if (uri.getRawUserInfo() != null) { String userInfo = uri.getRawUserInfo(); int colonIndex = userInfo.indexOf(':'); if (colonIndex == -1) { builder.setUser(decode(userInfo)); } else { String user = userInfo.substring(0, colonIndex); String pass = userInfo.substring(colonIndex + 1, userInfo.length()); builder.setUser(decode(user)); builder.setPassword(decode(pass)); } } if ("https".equals(uri.getScheme())) { builder.useSSL(true); } return Optional.of(builder.createProxyConfig()); }  @VisibleForTesting static Optional<ProxyConfig> proxyConfigFromEnv(String scheme, Map<String, String> env);  }