Bug TT-81 Changed URL validation regex 27/45027/1
authort.dakowicz <t.dakowicz@samsung.com>
Thu, 30 Jul 2015 09:24:24 +0000 (11:24 +0200)
committert.dakowicz <t.dakowicz@samsung.com>
Thu, 30 Jul 2015 11:37:23 +0000 (13:37 +0200)
[Issue#] https://bugs.tizen.org/jira/browse/TT-81
[Problem] Browser tries to navigate to malformed URLs instead
          of putting them into search engine query
[Solution] Corrected the regex
[Verify] Check if URLs with spaces are put into Google's search query

Change-Id: Ic49cfa498194cf878bab70007a7dae8ada53708e

services/SimpleURI/SimpleURI.cpp

index 104cfc296ceb025ac892f69d4f5673df3bd1fe1f..d92c64eebe0ef59a7452fba6bf95ec447c0f02aa 100644 (file)
@@ -254,25 +254,21 @@ void SimpleURI::editingCompleted()
 
 std::string SimpleURI::rewriteURI(const std::string& url)
 {
-
     BROWSER_LOGD("%s: %s", __PRETTY_FUNCTION__, url.c_str());
-
-    //regx form site http://code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149
-    //it matches urls with https? sufix or without it
-    boost::regex urlRegex(R"(^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$)");
-    boost::regex protocolRegex(R"(^https?:\/\/)");
-
+    boost::regex urlRegex(R"(^(https?|ftp)://[^\s/$.?#].[^\s]*$)");
 
     if(!url.empty()) {
-        if(url != "about:blank" && url != "about:home" && !boost::regex_search(url, protocolRegex)) {
-            if(!boost::regex_match(url, urlRegex)) {
-                std::string searchString("https://www.google.pl/search?q="); /* this seems to work better than https://www.google.pl/#q= on current WebKit engine */
+        if(url != "about:blank" && url != "about:home") {
+            if(boost::regex_match(url, urlRegex)) {
+                return url;
+            } else if(boost::regex_match( std::string("http://") + url, urlRegex) &&  url.find(".") != std::string::npos) {
+                return std::string("http://") + url;
+            } else {
+                std::string searchString("http://www.google.com/#q=");
                 searchString += url;
                 std::replace( searchString.begin(), searchString.end(), ' ', '+');
-        BROWSER_LOGD("[%s:%d] Rewrited search string: %s", __PRETTY_FUNCTION__, __LINE__, searchString.c_str());
-                return  searchString;
-            } else {
-                return std::string("http://") + url;
+                BROWSER_LOGD("[%s:%d] Search string: %s", __PRETTY_FUNCTION__, __LINE__, searchString.c_str());
+                return searchString;
             }
         }
     }