Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honor system proxy settings #510

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement handling for http.nonProxyHosts property
  • Loading branch information
Drew Hamilton committed Jun 2, 2021
commit 24b92bf0836dd04c67d01be9b429107697bf76f2
Original file line number Diff line number Diff line change
@@ -210,15 +210,30 @@ private void configureProxy(String contextUrl, ArtifactoryClientConfiguration cl

// If no proxyHost is explicitly set, check for the JVM system proxyHost property:
if (StringUtils.isBlank(proxyHost)) {
String systemPropertyName = isHttps ? "https.proxyHost" : "http.proxyHost";
proxyHost = System.getProperty(systemPropertyName);
// Note: "http.nonProxyHosts" is used for both http and https, despite its prefix
String systemNonProxyHostsString = System.getProperty("http.nonProxyHosts");
String[] systemNonProxyHosts = StringUtils.split(systemNonProxyHostsString, '|');

String contextUrlHost = getHost(contextUrl);
boolean isContextUrlOnNonProxyList = false;
for (String nonProxyHost : systemNonProxyHosts) {
if (nonProxyHost.contains("*") && matchesWildcardPattern(contextUrlHost, nonProxyHost)) {
isContextUrlOnNonProxyList = true;
}
}

if (!isContextUrlOnNonProxyList) {
String systemPropertyName = isHttps ? "https.proxyHost" : "http.proxyHost";
proxyHost = System.getProperty(systemPropertyName);
}
}

// If no proxyPort is explicitly set, check for the JVM system proxyPort property:
if (proxyPort == null) {
String systemPropertyName = isHttps ? "https.proxyPort" : "http.proxyPort";
String systemProxyPortValue = System.getProperty(systemPropertyName);
if (StringUtils.isNotBlank(systemProxyPortValue)) {
proxyPort = Integer.valueOf(systemProxyPortValue);
String systemProxyPort = System.getProperty(systemPropertyName);
if (StringUtils.isNotBlank(systemProxyPort)) {
proxyPort = Integer.valueOf(systemProxyPort);
}
}

@@ -235,6 +250,46 @@ private void configureProxy(String contextUrl, ArtifactoryClientConfiguration cl
}
}

private static String getHost(String url) {
String afterProtocol = url.contains("://") ? StringUtils.substringAfter(url, "://") : url;
return StringUtils.substringBefore(afterProtocol, "/");
}

/**
* Checks whether a string matches a pattern with * wildcards. * is the only supported wildcard character.
*
* @param string The string to check.
* @param pattern The wildcard pattern to match with the string.
* @return True if the string matches the wildcard pattern, or if the pattern contains no wildcards and is equal to
* the string. False otherwise.
*/
private static boolean matchesWildcardPattern(@Nonnull String string, @Nonnull String pattern) {
if (!pattern.contains("*")) {
// Shortcut wildcard matching if the pattern has no wildcard:
return string.equals(pattern);
}

String[] patternParts = StringUtils.split(pattern, "*");

if (!pattern.startsWith("*") && !string.startsWith(patternParts[0])) {
// Shortcut loop if string doesn't start with an exact match:
return false;
} else if (!pattern.endsWith("*") && !string.endsWith(patternParts[patternParts.length - 1])) {
// Shortcut loop if string doesn't end with an exact match:
return false;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This "else" clause is redundant, because of the above "return".

String remainingString = string;
for (String patternPart : patternParts) {
if (remainingString.contains(patternPart)) {
remainingString = StringUtils.substringAfter(remainingString, patternPart);
} else {
return false;
}
}
return true;
}
}

private void configConnectionTimeout(ArtifactoryClientConfiguration clientConf, ArtifactoryManager artifactoryManager) {
if (clientConf.getTimeout() != null) {
artifactoryManager.setConnectionTimeout(clientConf.getTimeout());