Skip to content

Commit

Permalink
Support nonProxyHosts
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos committed Jun 22, 2022
1 parent 9feb9f4 commit b258009
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ class ApacheConnector implements Connector {

URI proxyUri = HttpUrlConnector.getProxyUri(config);
if (proxyUri != null) {
// FIXME No proxy hosts?
HttpHost proxy = new HttpHost(proxyUri.getHost(), proxyUri.getPort(), proxyUri.getScheme());
String userName = ClientProperties.getValue(config.getProperties(),
ClientProperties.PROXY_USERNAME, "http.proxyUser");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ class Apache5Connector implements Connector {

URI proxyUri = HttpUrlConnector.getProxyUri(config);
if (proxyUri != null) {
// FIXME No proxy hosts?
HttpHost proxy = new HttpHost(proxyUri.getScheme(), proxyUri.getHost(), proxyUri.getPort());
String userName = ClientProperties.getValue(config.getProperties(),
ClientProperties.PROXY_USERNAME, "http.proxyUser");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class JettyConnector implements Connector {

URI proxyUri = HttpUrlConnector.getProxyUri(config);
if (proxyUri != null) {
// FIXME No proxy hosts?
final ProxyConfiguration proxyConfig = client.getProxyConfiguration();
proxyConfig.getProxies().add(new HttpProxy(proxyUri.getHost(), proxyUri.getPort()));
String proxyUsername = ClientProperties.getValue(config.getProperties(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.net.ssl.HostnameVerifier;
Expand All @@ -53,6 +54,7 @@
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.ExternalProperties;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.ClientRequest;
import org.glassfish.jersey.client.ClientResponse;
Expand Down Expand Up @@ -338,34 +340,55 @@ private static URI getProxyUriValue(Object proxy) {
* @return the URI or null
*/
public static URI getProxyUri(Configuration config) {
URI uri = null;
Object proxyUri = config.getProperty(ClientProperties.PROXY_URI);
if (proxyUri == null) {
String proxyHost = System.getProperty("http.proxyHost");
String proxyPort = System.getProperty("http.proxyPort");
String proxyHost = System.getProperty(ExternalProperties.HTTP_PROXY_HOST);
String proxyPort = System.getProperty(ExternalProperties.HTTP_PROXY_PORT);
if (proxyHost != null && proxyPort != null) {
return URI.create(proxyHost + ":" + proxyPort);
if (proxyHost.startsWith("http://")) {
uri = URI.create(proxyHost + ":" + proxyPort);
} else {
uri = URI.create("http://" + proxyHost + ":" + proxyPort);
}
}
} else {
return getProxyUriValue(proxyUri);
uri = getProxyUriValue(proxyUri);
}
return null;
return uri;
}

private ClientResponse _apply(final ClientRequest request) throws IOException {
final HttpURLConnection uc;
Proxy proxy = null;
URI proxyUri = getProxyUri(request.getConfiguration());
if (proxyUri != null) {
String username = ClientProperties.getValue(request.getConfiguration().getProperties(),
ClientProperties.PROXY_USERNAME, "http.proxyUser");
String password = ClientProperties.getValue(request.getConfiguration().getProperties(),
ClientProperties.PROXY_PASSWORD, "http.proxyPassword");
if (username != null && password != null) {
StringBuilder auth = new StringBuilder().append(username).append(":").append(password);
String encoded = "Basic " + Base64.getEncoder().encodeToString(auth.toString().getBytes());
request.getHeaders().put("Proxy-Authorization", Arrays.asList(encoded));
boolean skipProxy = false;
// Evaluate HTTP_NON_PROXY_HOSTS if HTTP_PROXY_HOST is also set
if (System.getProperty(ExternalProperties.HTTP_PROXY_HOST) != null
&& System.getProperty(ExternalProperties.HTTP_NON_PROXY_HOSTS) != null) {
String[] nonProxyHosts = System.getProperty(ExternalProperties.HTTP_NON_PROXY_HOSTS)
.trim().split("\\|");
String currentHost = request.getUri().getHost();
for (String nonProxyHost : nonProxyHosts) {
if (Pattern.matches(nonProxyHost, currentHost)) {
skipProxy = true;
break;
}
}
}
if (!skipProxy) {
URI proxyUri = getProxyUri(request.getConfiguration());
if (proxyUri != null) {
String username = ClientProperties.getValue(request.getConfiguration().getProperties(),
ClientProperties.PROXY_USERNAME, "http.proxyUser");
String password = ClientProperties.getValue(request.getConfiguration().getProperties(),
ClientProperties.PROXY_PASSWORD, "http.proxyPassword");
if (username != null && password != null) {
StringBuilder auth = new StringBuilder().append(username).append(":").append(password);
String encoded = "Basic " + Base64.getEncoder().encodeToString(auth.toString().getBytes());
request.getHeaders().put("Proxy-Authorization", Arrays.asList(encoded));
}
proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort()));
}
proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort()));
}
uc = this.connectionFactory.getConnection(request.getUri().toURL(), proxy);
uc.setDoInput(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down

0 comments on commit b258009

Please sign in to comment.