struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [1/2] struts git commit: Uses better regex to improve performance
Date Tue, 01 Aug 2017 09:41:59 GMT
Repository: struts
Updated Branches:
  refs/heads/master 91ae3448c -> d29caba5e


Uses better regex to improve performance


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/8df5a897
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/8df5a897
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/8df5a897

Branch: refs/heads/master
Commit: 8df5a897f61f3ef45c36fdd9275e66669ae4516c
Parents: 91ae344
Author: Lukasz Lenart <lukaszlenart@apache.org>
Authored: Tue Aug 1 11:29:23 2017 +0200
Committer: Lukasz Lenart <lukaszlenart@apache.org>
Committed: Tue Aug 1 11:29:23 2017 +0200

----------------------------------------------------------------------
 .../validator/validators/URLValidator.java      | 10 ++++----
 .../xwork2/validator/URLValidatorTest.java      | 26 ++++++++++++++++++++
 2 files changed, 31 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/8df5a897/core/src/main/java/com/opensymphony/xwork2/validator/validators/URLValidator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/validators/URLValidator.java
b/core/src/main/java/com/opensymphony/xwork2/validator/validators/URLValidator.java
index eab0fa8..6cf58ef 100644
--- a/core/src/main/java/com/opensymphony/xwork2/validator/validators/URLValidator.java
+++ b/core/src/main/java/com/opensymphony/xwork2/validator/validators/URLValidator.java
@@ -49,18 +49,18 @@ public class URLValidator extends FieldValidatorSupport {
     private static final Logger LOG = LogManager.getLogger(URLValidator.class);
 
     public static final String DEFAULT_URL_REGEX = "^(https?|ftp):\\/\\/" +
-            "(([a-z0-9$_\\.\\+!\\*\\'\\(\\),;\\?&=-]|%[0-9a-f]{2})+" +
-            "(:([a-z0-9$_\\.\\+!\\*\\'\\(\\),;\\?&=-]|%[0-9a-f]{2})+)?" +
+            "(([a-z0-9$_\\.\\+!\\*\\'\\(\\),;\\?&=\\-]|%[0-9a-f]{2})+" +
+            "(:([a-z0-9$_\\.\\+!\\*\\'\\(\\),;\\?&=\\-]|%[0-9a-f]{2})+)?" +
             "@)?(#?" +
             ")((([a-z0-9]\\.|[a-z0-9][a-z0-9-]*[a-z0-9]\\.)*" +
             "[a-z][a-z0-9-]*[a-z0-9]" +
             "|((\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])\\.){3}" +
             "(\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])" +
             ")(:\\d+)?" +
-            ")(((\\/{0,1}([a-z0-9$_\\.\\+!\\*\\'\\(\\),;:@&=-]|%[0-9a-f]{2})*)*" +
-            "(\\?([a-z0-9$_\\.\\+!\\*\\'\\(\\),;:@&=-]|%[0-9a-f]{2})*)" +
+            ")(((\\/([a-z0-9$_\\.\\+!\\*\\'\\(\\),;:@&=\\-]|%[0-9a-f]{2})*)*" +
+            "(\\?([a-z0-9$_\\.\\+!\\*\\'\\(\\),;:@&=\\-\\/\\:]|%[0-9a-f]{2})*)" +
             "?)?)?" +
-            "(#([a-z0-9$_\\.\\+!\\*\\'\\(\\),;:@&=-]|%[0-9a-f]{2})*)?" +
+            "(#([a-z0-9$_\\.\\+!\\*\\'\\(\\),;:@&=\\-]|%[0-9a-f]{2})*)?" +
             "$";
 
     private String urlRegexExpression;

http://git-wip-us.apache.org/repos/asf/struts/blob/8df5a897/core/src/test/java/com/opensymphony/xwork2/validator/URLValidatorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/URLValidatorTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/URLValidatorTest.java
index 5c83dd1..baa2bb1 100644
--- a/core/src/test/java/com/opensymphony/xwork2/validator/URLValidatorTest.java
+++ b/core/src/test/java/com/opensymphony/xwork2/validator/URLValidatorTest.java
@@ -160,6 +160,32 @@ public class URLValidatorTest extends XWorkTestCase {
 
         assertTrue(pattern.matcher("http://netsol-underconstruction-page-monitor-1.com/__media__/js/netsoltrademark.php?d=www.le-soutien-scolaire.fr%2Favis-et-test-comparatifs-des-robots-multifonctions%2F").matches());
         assertTrue(UrlValidator.getInstance().isValid("http://netsol-underconstruction-page-monitor-1.com/__media__/js/netsoltrademark.php?d=www.le-soutien-scolaire.fr%2Favis-et-test-comparatifs-des-robots-multifonctions%2F"));
+
+        //this will cause test to hang indefinitely using JDK 1.8.0_121, Struts 2.5.10.1
and JUnit 4.5
+        assertTrue(pattern.matcher("http://www.javaroad.jp/news/redirect.jsp?link=http://www.forum-course-de-cote.com/que-penser-dune-trottinette-electrique/").matches());
+        assertTrue(UrlValidator.getInstance().isValid("http://www.javaroad.jp/news/redirect.jsp?link=http://www.forum-course-de-cote.com/que-penser-dune-trottinette-electrique/"));
+
+        //this will cause test to hang indefinitely using JDK 1.8.0_121, Struts 2.5.10.1
and JUnit 4.5
+        assertTrue(pattern.matcher("http://wargame.ch/wc/acw/sub/aotm/guestbook/index.php?page3D183EClearwater20Roofing20Contractors3C/a3E3Ekaldu20non20msg3C/a3E").matches());
+        assertTrue(UrlValidator.getInstance().isValid("http://wargame.ch/wc/acw/sub/aotm/guestbook/index.php?page3D183EClearwater20Roofing20Contractors3C/a3E3Ekaldu20non20msg3C/a3E"));
+    }
+
+    public void testLongRunningValidations() throws Exception {
+        URLValidator validator = new URLValidator();
+
+        Pattern pattern = Pattern.compile(validator.getUrlRegex(), Pattern.CASE_INSENSITIVE);
+
+        long time = System.currentTimeMillis();
+        assertFalse(pattern.matcher("ftp://aaaaaaaaaaaaaaaaaaaaaaaa|").matches());
+        assertTrue("Validation did not complete in half a second", System.currentTimeMillis()
- time < 500);
+
+        time = System.currentTimeMillis();
+        assertFalse(pattern.matcher("ftp://bbbbbbbbbbbbbbbbbbbbbbbb}").matches());
+        assertTrue("Validation did not complete in half a second", System.currentTimeMillis()
- time < 500);
+
+        time = System.currentTimeMillis();
+        assertFalse(pattern.matcher("ftp://cccccccccccccccccccccccc{").matches());
+        assertTrue("Validation did not complete in half a second", System.currentTimeMillis()
- time < 500);
     }
 
     public void testValidUrlCaseInsensitive() throws Exception {


Mime
View raw message