Skip to content

Commit

Permalink
Support nonProxyHosts
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed Jun 22, 2022
1 parent 9feb9f4 commit ea85d10
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;

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 @@ -282,12 +283,13 @@ class ApacheConnector implements Connector {

URI proxyUri = HttpUrlConnector.getProxyUri(config);
if (proxyUri != null) {
// TODO No proxy hosts?
HttpHost proxy = new HttpHost(proxyUri.getHost(), proxyUri.getPort(), proxyUri.getScheme());
String userName = ClientProperties.getValue(config.getProperties(),
ClientProperties.PROXY_USERNAME, "http.proxyUser");
ClientProperties.PROXY_USERNAME, ExternalProperties.HTTP_PROXY_USER);
if (userName != null) {
String password = ClientProperties.getValue(config.getProperties(),
ClientProperties.PROXY_PASSWORD, "http.proxyPassword");
ClientProperties.PROXY_PASSWORD, ExternalProperties.HTTP_PROXY_PASSWORD);
if (password != null) {
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import org.apache.hc.core5.util.TextUtils;
import org.apache.hc.core5.util.Timeout;
import org.apache.hc.core5.util.VersionInfo;
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 @@ -283,12 +284,13 @@ class Apache5Connector implements Connector {

URI proxyUri = HttpUrlConnector.getProxyUri(config);
if (proxyUri != null) {
// TODO No proxy hosts?
HttpHost proxy = new HttpHost(proxyUri.getScheme(), proxyUri.getHost(), proxyUri.getPort());
String userName = ClientProperties.getValue(config.getProperties(),
ClientProperties.PROXY_USERNAME, "http.proxyUser");
ClientProperties.PROXY_USERNAME, ExternalProperties.HTTP_PROXY_USER);
if (userName != null) {
String password = ClientProperties.getValue(config.getProperties(),
ClientProperties.PROXY_PASSWORD, "http.proxyPassword");
ClientProperties.PROXY_PASSWORD, ExternalProperties.HTTP_PROXY_PASSWORD);

if (password != null) {
final CredentialsStore credsProvider = new BasicCredentialsProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.eclipse.jetty.util.Jetty;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
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 @@ -186,13 +187,14 @@ class JettyConnector implements Connector {

URI proxyUri = HttpUrlConnector.getProxyUri(config);
if (proxyUri != null) {
// TODO No proxy hosts?
final ProxyConfiguration proxyConfig = client.getProxyConfiguration();
proxyConfig.getProxies().add(new HttpProxy(proxyUri.getHost(), proxyUri.getPort()));
String proxyUsername = ClientProperties.getValue(config.getProperties(),
ClientProperties.PROXY_USERNAME, "http.proxyUser");
ClientProperties.PROXY_USERNAME, ExternalProperties.HTTP_PROXY_USER);
if (proxyUsername != null) {
String proxyPassword = ClientProperties.getValue(config.getProperties(),
ClientProperties.PROXY_PASSWORD, "http.proxyPassword");
ClientProperties.PROXY_PASSWORD, ExternalProperties.HTTP_PROXY_PASSWORD);
auth.addAuthentication(new BasicAuthentication(proxyUri, "<<ANY_REALM>>",
proxyUsername, proxyPassword));
}
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, ExternalProperties.HTTP_PROXY_USER);
String password = ClientProperties.getValue(request.getConfiguration().getProperties(),
ClientProperties.PROXY_PASSWORD, ExternalProperties.HTTP_PROXY_PASSWORD);
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 Expand Up @@ -32,11 +32,21 @@ public final class ExternalProperties {
public static final String HTTP_PROXY_PORT = "http.proxyPort";

/**
* Property used to indicates the hosts that should be accessed
* Property used to indicate the hosts that should be accessed
* without going through the proxy.
*/
public static final String HTTP_NON_PROXY_HOSTS = "http.nonProxyHosts";

/**
* Property used to specify the user name to authenticate with the proxy.
*/
public static final String HTTP_PROXY_USER = "http.proxyUser";

/**
* Property used to specify the password to authenticate with the proxy.
*/
public static final String HTTP_PROXY_PASSWORD = "http.proxyPassword";

/**
* Prevent instantiation.
*/
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 ea85d10

Please sign in to comment.