From 775f70c77247ab91a58b48c3798fbf91d104e4d2 Mon Sep 17 00:00:00 2001 From: Thomas Lenz Date: Mon, 21 Sep 2020 10:10:16 +0200 Subject: add support for http.nonProxyHosts --- .../apache/commons/httpclient/MOAHttpClient.java | 104 ++++++++++++++------- 1 file changed, 70 insertions(+), 34 deletions(-) diff --git a/id/server/moa-id-commons/src/main/java/org/apache/commons/httpclient/MOAHttpClient.java b/id/server/moa-id-commons/src/main/java/org/apache/commons/httpclient/MOAHttpClient.java index 4d4c7fa88..04e9a8ab9 100644 --- a/id/server/moa-id-commons/src/main/java/org/apache/commons/httpclient/MOAHttpClient.java +++ b/id/server/moa-id-commons/src/main/java/org/apache/commons/httpclient/MOAHttpClient.java @@ -25,6 +25,7 @@ package org.apache.commons.httpclient; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; +import java.util.StringTokenizer; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.protocol.Protocol; @@ -43,42 +44,9 @@ public class MOAHttpClient extends HttpClient { public MOAHttpClient() { super(); - injectProxyCredentials(); } - public void injectProxyCredentials() { - //set proxy functionality - String host = System.getProperty("http.proxyHost"); //$NON-NLS-1$ - String port = System.getProperty("http.proxyPort"); //$NON-NLS-1$ - String user = System.getProperty("http.proxyUser"); //$NON-NLS-1$ - String pass = System.getProperty("http.proxyPassword"); //$NON-NLS-1$ - - if (MiscUtil.isNotEmpty(host)) { - int p = -1; - if (MiscUtil.isNotEmpty(port)) { - try { - p = Integer.parseInt(port); - - } catch (Exception e) { - Logger.error("'http.proxyPort' not valid!", e); - - } - } - Logger.info("Set HTTP-Proxy to Host: " + host + " and port: " + p); - getHostConfiguration().setProxy(host, p); - if (MiscUtil.isNotEmpty(user) && pass != null) { - //set proxy credentials - AuthScope authscope = new AuthScope(host, p); - Credentials credentials = new UsernamePasswordCredentials(user, pass); - getState().setProxyCredentials(authscope, credentials); - Logger.info(" Use Proxy with Username: " + user + " and password: " - + (Logger.isTraceEnabled() ? pass : "*******")); - - } - - } - } public void setCustomSSLTrustStore(String metadataURL, ProtocolSocketFactory protoSocketFactory) throws MalformedURLException { @@ -92,6 +60,7 @@ public class MOAHttpClient extends HttpClient { Protocol authhttps = new Protocol("https", protoSocketFactory, 443); getHostConfiguration().setHost(url.getHost(), port, authhttps); + } @@ -122,12 +91,79 @@ public class MOAHttpClient extends HttpClient { } } + HttpState interalState = (state == null ? getState() : state); + + if (requiresProxy(uri.getURI())) { + injectProxyCredentials(hostconfig, interalState); + + } + HttpMethodDirector methodDirector = new HttpMethodDirector( getHttpConnectionManager(), hostconfig, getParams(), - (state == null ? getState() : state)); + state); methodDirector.executeMethod(method); return method.getStatusCode(); } + + public void injectProxyCredentials(HostConfiguration hostconfig, HttpState interalState) { + //set proxy functionality + String host = System.getProperty("http.proxyHost"); //$NON-NLS-1$ + String port = System.getProperty("http.proxyPort"); //$NON-NLS-1$ + String user = System.getProperty("http.proxyUser"); //$NON-NLS-1$ + String pass = System.getProperty("http.proxyPassword"); //$NON-NLS-1$ + + if (MiscUtil.isNotEmpty(host)) { + int p = -1; + if (MiscUtil.isNotEmpty(port)) { + try { + p = Integer.parseInt(port); + + } catch (Exception e) { + Logger.error("'http.proxyPort' not valid!", e); + + } + } + Logger.debug("Set HTTP-Proxy to Host: " + host + " and port: " + p); + hostconfig.setProxy(host, p); + if (MiscUtil.isNotEmpty(user) && pass != null) { + //set proxy credentials + AuthScope authscope = new AuthScope(host, p); + Credentials credentials = new UsernamePasswordCredentials(user, pass); + interalState.setProxyCredentials(authscope, credentials); + Logger.debug(" Use Proxy with Username: " + user + " and password: " + + (Logger.isTraceEnabled() ? pass : "*******")); + + } + + } + } + + /** + * Return true unless the given target host is specified in the http.nonProxyHosts system property (used for both protocols, http and https). + * See Networking Properties. + * @param targetHost Non-null host name to verify + * @return true if not specified in the list, false if it is specified and therefore should be excluded from proxy + */ + private static boolean requiresProxy(final String targetHost) + { + boolean requiresProxy = true; + final String nonProxyHosts = System.getProperty("http.nonProxyHosts"); + if (nonProxyHosts != null) + { + StringTokenizer tokenizer = new StringTokenizer(nonProxyHosts, "|"); + while (tokenizer.hasMoreTokens()) + { + String pattern = tokenizer.nextToken(); + pattern = pattern.replaceAll("\\.", "\\\\.").replaceAll("\\*", ".*"); + if (targetHost.matches(pattern)) + { + requiresProxy = false; + break; + } + } + } + return requiresProxy; + } } -- cgit v1.2.3