> Picked up the methods from Axis 1.x. > Axis 1.x code is not all that bad you know :)
Amen brother!!! :-) -- Tom Jordahl Axis 1 guy -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Saturday, March 08, 2008 1:36 PM To: [EMAIL PROTECTED] Subject: svn commit: r635034 - in /webservices/axis2/trunk/java/modules/kernel: src/org/apache/axis2/transport/http/ProxyConfiguration.java test/org/apache/axis2/transport/http/NonProxyHostTest.java Author: dims Date: Sat Mar 8 10:35:49 2008 New Revision: 635034 URL: http://svn.apache.org/viewvc?rev=635034&view=rev Log: AXIS2-3453 - java.util.regex.PatternSyntaxException in org.apache.axis2.transport.http.ProxyConfiguration Picked up the methods from Axis 1.x. Axis 1.x code is not all that bad you know :) Added: webservices/axis2/trunk/java/modules/kernel/test/org/apache/axis2/transp ort/http/NonProxyHostTest.java Modified: webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transpo rt/http/ProxyConfiguration.java Modified: webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transpo rt/http/ProxyConfiguration.java URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/kernel /src/org/apache/axis2/transport/http/ProxyConfiguration.java?rev=635034& r1=635033&r2=635034&view=diff ======================================================================== ====== --- webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transpo rt/http/ProxyConfiguration.java (original) +++ webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transpo rt/http/ProxyConfiguration.java Sat Mar 8 10:35:49 2008 @@ -27,6 +27,7 @@ import javax.xml.namespace.QName; import java.net.URL; +import java.util.StringTokenizer; /** @@ -241,32 +242,203 @@ * * @return boolean */ - private static boolean validateNonProxyHosts(String targetHost) { - + public static boolean validateNonProxyHosts(String host) { //From system property http.nonProxyHosts String nonProxyHosts = System.getProperty(HTTP_NON_PROXY_HOSTS); - - if (nonProxyHosts == null) { + return isHostInNonProxyList(host, nonProxyHosts); + } + + /** + * Check if the specified host is in the list of non proxy hosts. + * + * @param host host name + * @param nonProxyHosts string containing the list of non proxy hosts + * + * @return true/false + */ + public static boolean isHostInNonProxyList(String host, String nonProxyHosts) { + if ((nonProxyHosts == null) || (host == null)) { return false; } - String[] nonProxyHostsArray = nonProxyHosts.split("\\|"); + /* + * The http.nonProxyHosts system property is a list enclosed in + * double quotes with items separated by a vertical bar. + */ + StringTokenizer tokenizer = new StringTokenizer(nonProxyHosts, "|\""); + + while (tokenizer.hasMoreTokens()) { + String pattern = tokenizer.nextToken(); + if (match(pattern, host, false)) { + return true; + } + } + return false; + } + + /** + * Matches a string against a pattern. The pattern contains two special + * characters: + * '*' which means zero or more characters, + * + * @param pattern the (non-null) pattern to match against + * @param str the (non-null) string that must be matched against the + * pattern + * @param isCaseSensitive + * + * @return <code>true</code> when the string matches against the pattern, + * <code>false</code> otherwise. + */ + protected static boolean match(String pattern, String str, + boolean isCaseSensitive) { + + char[] patArr = pattern.toCharArray(); + char[] strArr = str.toCharArray(); + int patIdxStart = 0; + int patIdxEnd = patArr.length - 1; + int strIdxStart = 0; + int strIdxEnd = strArr.length - 1; + char ch; + boolean containsStar = false; + + for (int i = 0; i < patArr.length; i++) { + if (patArr[i] == '*') { + containsStar = true; + break; + } + } + if (!containsStar) { + + // No '*'s, so we make a shortcut + if (patIdxEnd != strIdxEnd) { + return false; // Pattern and string do not have the same size + } + for (int i = 0; i <= patIdxEnd; i++) { + ch = patArr[i]; + if (isCaseSensitive && (ch != strArr[i])) { + return false; // Character mismatch + } + if (!isCaseSensitive + && (Character.toUpperCase(ch) + != Character.toUpperCase(strArr[i]))) { + return false; // Character mismatch + } + } + return true; // String matches against pattern + } + if (patIdxEnd == 0) { + return true; // Pattern contains only '*', which matches anything + } + + // Process characters before first star + while ((ch = patArr[patIdxStart]) != '*' + && (strIdxStart <= strIdxEnd)) { + if (isCaseSensitive && (ch != strArr[strIdxStart])) { + return false; // Character mismatch + } + if (!isCaseSensitive + && (Character.toUpperCase(ch) + != Character.toUpperCase(strArr[strIdxStart]))) { + return false; // Character mismatch + } + patIdxStart++; + strIdxStart++; + } + if (strIdxStart > strIdxEnd) { + + // All characters in the string are used. Check if only '*'s are + // left in the pattern. If so, we succeeded. Otherwise failure. + for (int i = patIdxStart; i <= patIdxEnd; i++) { + if (patArr[i] != '*') { + return false; + } + } + return true; + } + + // Process characters after last star + while ((ch = patArr[patIdxEnd]) != '*' && (strIdxStart <= strIdxEnd)) { + if (isCaseSensitive && (ch != strArr[strIdxEnd])) { + return false; // Character mismatch + } + if (!isCaseSensitive + && (Character.toUpperCase(ch) + != Character.toUpperCase(strArr[strIdxEnd]))) { + return false; // Character mismatch + } + patIdxEnd--; + strIdxEnd--; + } + if (strIdxStart > strIdxEnd) { + + // All characters in the string are used. Check if only '*'s are + // left in the pattern. If so, we succeeded. Otherwise failure. + for (int i = patIdxStart; i <= patIdxEnd; i++) { + if (patArr[i] != '*') { + return false; + } + } + return true; + } - if (nonProxyHostsArray.length == 1) { - return targetHost.matches(nonProxyHosts); - } else { - boolean pass = false; - for (int i = 0; i < nonProxyHostsArray.length; i++) { - String a = nonProxyHostsArray[i]; - if (targetHost.matches(a)) { - pass = true; + // process pattern between stars. padIdxStart and patIdxEnd point + // always to a '*'. + while ((patIdxStart != patIdxEnd) && (strIdxStart <= strIdxEnd)) { + int patIdxTmp = -1; + + for (int i = patIdxStart + 1; i <= patIdxEnd; i++) { + if (patArr[i] == '*') { + patIdxTmp = i; break; } } - return pass; + if (patIdxTmp == patIdxStart + 1) { + + // Two stars next to each other, skip the first one. + patIdxStart++; + continue; + } + + // Find the pattern between padIdxStart & padIdxTmp in str between + // strIdxStart & strIdxEnd + int patLength = (patIdxTmp - patIdxStart - 1); + int strLength = (strIdxEnd - strIdxStart + 1); + int foundIdx = -1; + + strLoop: + for (int i = 0; i <= strLength - patLength; i++) { + for (int j = 0; j < patLength; j++) { + ch = patArr[patIdxStart + j + 1]; + if (isCaseSensitive + && (ch != strArr[strIdxStart + i + j])) { + continue strLoop; + } + if (!isCaseSensitive && (Character + .toUpperCase(ch) != Character + .toUpperCase(strArr[strIdxStart + i + j]))) { + continue strLoop; + } + } + foundIdx = strIdxStart + i; + break; + } + if (foundIdx == -1) { + return false; + } + patIdxStart = patIdxTmp; + strIdxStart = foundIdx + patLength; } - } + // All characters in the string are used. Check if only '*'s are left + // in the pattern. If so, we succeeded. Otherwise failure. + for (int i = patIdxStart; i <= patIdxEnd; i++) { + if (patArr[i] != '*') { + return false; + } + } + return true; + } + /** * Retrun proxy host * Added: webservices/axis2/trunk/java/modules/kernel/test/org/apache/axis2/transp ort/http/NonProxyHostTest.java URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/kernel /test/org/apache/axis2/transport/http/NonProxyHostTest.java?rev=635034&v iew=auto ======================================================================== ====== --- webservices/axis2/trunk/java/modules/kernel/test/org/apache/axis2/transp ort/http/NonProxyHostTest.java (added) +++ webservices/axis2/trunk/java/modules/kernel/test/org/apache/axis2/transp ort/http/NonProxyHostTest.java Sat Mar 8 10:35:49 2008 @@ -0,0 +1,13 @@ +package org.apache.axis2.transport.http; + +import junit.framework.TestCase; + +public class NonProxyHostTest extends TestCase { + public void testForAxis2_3453() { + String nonProxyHosts = "sealbook.ncl.ac.uk|*.sealbook.ncl.ac.uk|eskdale.ncl.ac.uk|*.eskdale.ncl .ac.uk|giga25.ncl.ac.uk|*.giga25.ncl.ac.uk"; + assertTrue(ProxyConfiguration.isHostInNonProxyList("sealbook.ncl.ac.uk", nonProxyHosts)); + assertFalse(ProxyConfiguration.isHostInNonProxyList("xsealbook.ncl.ac.uk ", nonProxyHosts)); + assertTrue(ProxyConfiguration.isHostInNonProxyList("local","local|*.loca l|169.254/16|*.169.254/16")); + assertFalse(ProxyConfiguration.isHostInNonProxyList("localhost","local|* .local|169.254/16|*.169.254/16")); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
