commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1128446 - in /commons/proper/validator/trunk/src: main/java/org/apache/commons/validator/routines/UrlValidator.java test/java/org/apache/commons/validator/routines/UrlValidatorTest.java
Date Fri, 27 May 2011 20:29:27 GMT
Author: nick
Date: Fri May 27 20:29:27 2011
New Revision: 1128446

URL: http://svn.apache.org/viewvc?rev=1128446&view=rev
Log:
VALIDATOR-276 - Allow file:///foo/bar.txt and file://localhost/foo/bar.txt urls to correctly
url validate

Modified:
    commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/UrlValidator.java
    commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java

Modified: commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/UrlValidator.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/UrlValidator.java?rev=1128446&r1=1128445&r2=1128446&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/UrlValidator.java
(original)
+++ commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/UrlValidator.java
Fri May 27 20:29:27 2011
@@ -286,12 +286,19 @@ public class UrlValidator implements Ser
             return false;
         }
 
-        if (!isValidScheme(urlMatcher.group(PARSE_URL_SCHEME))) {
+        String scheme = urlMatcher.group(PARSE_URL_SCHEME);
+        if (!isValidScheme(scheme)) {
             return false;
         }
 
-        if (!isValidAuthority(urlMatcher.group(PARSE_URL_AUTHORITY))) {
-            return false;
+        String authority = urlMatcher.group(PARSE_URL_AUTHORITY); 
+        if ("file".equals(scheme) && "".equals(authority)) {
+           // Special case - file: allows an empty authority
+        } else {
+           // Validate the authority
+           if (!isValidAuthority(authority)) {
+               return false;
+            }
         }
 
         if (!isValidPath(urlMatcher.group(PARSE_URL_PATH))) {

Modified: commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java?rev=1128446&r1=1128445&r2=1128446&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java
(original)
+++ commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java
Fri May 27 20:29:27 2011
@@ -206,6 +206,59 @@ public class UrlValidatorTest extends Te
         assertTrue("www.apache.org should still validate",
                 validator.isValid("http://www.apache.org/test/index.html"));
     }
+    
+    public void testValidator276() {
+        // file:// isn't allowed by default
+        UrlValidator validator = new UrlValidator();
+        
+        assertTrue("http://apache.org/ should be allowed by default",
+                 validator.isValid("http://www.apache.org/test/index.html"));
+       
+        assertFalse("file:///c:/ shouldn't be allowed by default",
+                 validator.isValid("file:///C:/some.file"));
+        
+        assertFalse("file:///c:\\ shouldn't be allowed by default",
+              validator.isValid("file:///C:\\some.file"));
+        
+        assertFalse("file:///etc/ shouldn't be allowed by default",
+              validator.isValid("file:///etc/hosts"));
+        
+        assertFalse("file://localhost/etc/ shouldn't be allowed by default",
+              validator.isValid("file://localhost/etc/hosts"));
+        
+        assertFalse("file://localhost/c:/ shouldn't be allowed by default",
+              validator.isValid("file://localhost/c:/some.file"));
+        
+        // Turn it on, and check
+        // Note - we need to enable local urls when working with file:
+        validator = new UrlValidator(new String[] {"http","file"}, UrlValidator.ALLOW_LOCAL_URLS);
+        
+        assertTrue("http://apache.org/ should be allowed by default",
+                 validator.isValid("http://www.apache.org/test/index.html"));
+       
+        assertTrue("file:///c:/ should now be allowed",
+                 validator.isValid("file:///C:/some.file"));
+        
+        // Currently, we don't support the c:\ form
+        assertFalse("file:///c:\\ shouldn't be allowed",
+              validator.isValid("file:///C:\\some.file"));
+        
+        assertTrue("file:///etc/ should now be allowed",
+              validator.isValid("file:///etc/hosts"));
+        
+        assertTrue("file://localhost/etc/ should now be allowed",
+              validator.isValid("file://localhost/etc/hosts"));
+        
+        assertTrue("file://localhost/c:/ should now be allowed",
+              validator.isValid("file://localhost/c:/some.file"));
+        
+        // These are never valid
+        assertFalse("file://c:/ shouldn't ever be allowed, needs file:///c:/",
+              validator.isValid("file://C:/some.file"));
+     
+        assertFalse("file://c:\\ shouldn't ever be allowed, needs file:///c:/",
+              validator.isValid("file://C:\\some.file"));
+    }
 
     
    static boolean incrementTestPartsIndex(int[] testPartsIndex, Object[] testParts) {



Mime
View raw message