From b2580095d03cac91f11326b56ef94b6d18763e29 Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Wed, 22 Jun 2022 12:15:25 +0200 Subject: [PATCH] Support nonProxyHosts Signed-off-by: Jorge Bescos Gascon --- .../apache/connector/ApacheConnector.java | 1 + .../apache5/connector/Apache5Connector.java | 1 + .../jetty/connector/JettyConnector.java | 1 + .../client/internal/HttpUrlConnector.java | 55 +++++++++++++------ .../externalproperties/HttpProxyTest.java | 2 +- 5 files changed, 43 insertions(+), 17 deletions(-) diff --git a/connectors/apache-connector/src/main/java/org/glassfish/jersey/apache/connector/ApacheConnector.java b/connectors/apache-connector/src/main/java/org/glassfish/jersey/apache/connector/ApacheConnector.java index 3820333642f..2b48621be37 100644 --- a/connectors/apache-connector/src/main/java/org/glassfish/jersey/apache/connector/ApacheConnector.java +++ b/connectors/apache-connector/src/main/java/org/glassfish/jersey/apache/connector/ApacheConnector.java @@ -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"); diff --git a/connectors/apache5-connector/src/main/java/org/glassfish/jersey/apache5/connector/Apache5Connector.java b/connectors/apache5-connector/src/main/java/org/glassfish/jersey/apache5/connector/Apache5Connector.java index c7522101744..287ec517c3b 100644 --- a/connectors/apache5-connector/src/main/java/org/glassfish/jersey/apache5/connector/Apache5Connector.java +++ b/connectors/apache5-connector/src/main/java/org/glassfish/jersey/apache5/connector/Apache5Connector.java @@ -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"); diff --git a/connectors/jetty-connector/src/main/java/org/glassfish/jersey/jetty/connector/JettyConnector.java b/connectors/jetty-connector/src/main/java/org/glassfish/jersey/jetty/connector/JettyConnector.java index 26c962c3c49..c5e0451456e 100644 --- a/connectors/jetty-connector/src/main/java/org/glassfish/jersey/jetty/connector/JettyConnector.java +++ b/connectors/jetty-connector/src/main/java/org/glassfish/jersey/jetty/connector/JettyConnector.java @@ -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(), diff --git a/core-client/src/main/java/org/glassfish/jersey/client/internal/HttpUrlConnector.java b/core-client/src/main/java/org/glassfish/jersey/client/internal/HttpUrlConnector.java index 5f6c425c35b..6454e8c922a 100644 --- a/core-client/src/main/java/org/glassfish/jersey/client/internal/HttpUrlConnector.java +++ b/core-client/src/main/java/org/glassfish/jersey/client/internal/HttpUrlConnector.java @@ -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; @@ -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; @@ -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); diff --git a/tests/integration/externalproperties/src/test/java/org/glassfish/jersey/tests/externalproperties/HttpProxyTest.java b/tests/integration/externalproperties/src/test/java/org/glassfish/jersey/tests/externalproperties/HttpProxyTest.java index 94100297f0a..f74a55da144 100644 --- a/tests/integration/externalproperties/src/test/java/org/glassfish/jersey/tests/externalproperties/HttpProxyTest.java +++ b/tests/integration/externalproperties/src/test/java/org/glassfish/jersey/tests/externalproperties/HttpProxyTest.java @@ -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