Refactoring CompareMime(), CompareUri() 79/41879/12
authorYongseop Kim <yons.kim@samsung.com>
Thu, 18 Jun 2015 14:14:00 +0000 (23:14 +0900)
committerYongseop Kim <yons.kim@samsung.com>
Tue, 7 Jul 2015 07:27:39 +0000 (16:27 +0900)
  - remove regex codes
  - process only '*' wildcard

Change-Id: I469e6588f9bed38db29b1a6e53d21d219cee9d95

src/common/resource_manager.cc
src/common/string_utils.cc
src/common/string_utils.h

index 935e873..5fed09e 100755 (executable)
@@ -20,7 +20,6 @@
 #include <aul.h>
 
 #include <memory>
-#include <regex>
 #include <vector>
 #include <functional>
 #include <algorithm>
@@ -45,6 +44,8 @@ const char* kSchemeTypeApp = "app://";
 const char* kSchemeTypeFile = "file://";
 const char* kSchemeTypeHttp = "http://";
 const char* kSchemeTypeHttps = "https://";
+// lendth of scheme identifier ://
+const int kSchemeIdLen = 3;
 // TODO(wy80.choi): comment out below unused const variables if needed.
 // const char* kSchemeTypeWidget = "widget://";
 
@@ -83,24 +84,25 @@ static std::string GetMimeFromUri(const std::string& uri) {
   }
 }
 
-static bool CompareStringWithWildcard(const std::string& info,
-                                      const std::string& request) {
-  std::string wildcard_str = utils::ReplaceAll(info, "*", ".*");
-  try {
-    std::regex re(wildcard_str, std::regex_constants::icase);
-    return std::regex_match(request.begin(), request.end(), re);
-  } catch (std::regex_error& e) {
-    LOGGER(ERROR) << "regex_error caught: " << e.what();
-    return false;
-  }
-}
-
 static bool CompareMime(const std::string& info_mime,
                         const std::string& request_mime) {
   if (request_mime.empty())
     return info_mime.empty();
 
-  return CompareStringWithWildcard(info_mime, request_mime);
+  // suppose that these mimetypes are valid expressions ('type'/'sub-type')
+  if (info_mime == "*" || info_mime == "*/*")
+    return true;
+
+  std::string info_type;
+  std::string info_sub;
+  std::string request_type;
+  std::string request_sub;
+  if (!(utils::SplitString(info_mime, &info_type, &info_sub, '/') &&
+        utils::SplitString(request_mime, &request_type, &request_sub, '/')))
+    return false;
+
+  return info_type == request_type &&
+         (info_sub == "*") ? true : (info_sub == request_sub);
 }
 
 static bool CompareUri(const std::string& info_uri,
@@ -109,12 +111,19 @@ static bool CompareUri(const std::string& info_uri,
     return info_uri.empty();
 
   std::string info_scheme = utils::SchemeName(info_uri);
-  std::string request_scheme = utils::SchemeName(request_uri);
 
-  if (!info_scheme.empty() && !request_scheme.empty()) {
-    return (info_scheme == request_scheme);
+  // if has only scheme or scheme+star. ex) http, http://, http://*
+  if (!info_scheme.empty() &&
+      (info_uri == info_scheme || utils::EndsWith(info_uri, "://")
+        || utils::EndsWith(info_uri, "://*"))) {
+    return utils::SchemeName(request_uri) == info_scheme;
+  }
+
+  if (utils::EndsWith(info_uri, "*")) {
+    return utils::StartsWith(request_uri, info_uri.substr(0,
+                                                          info_uri.length()-1));
   } else {
-    return CompareStringWithWildcard(info_uri, request_uri);
+    return request_uri == info_uri;
   }
 }
 
index 6eafd6a..80c443b 100755 (executable)
@@ -65,5 +65,19 @@ std::string GetCurrentMilliSeconds() {
   return ss.str();
 }
 
+bool SplitString(const std::string &str,
+                 std::string *part_1, std::string *part_2, const char delim) {
+  if (part_1 == nullptr || part_2 == nullptr)
+    return false;
+
+  size_t pos = str.find(delim);
+  if (pos == std::string::npos)
+    return false;
+
+  *part_1 = str.substr(0, pos);
+  *part_2 = str.substr(pos+1);
+  return true;
+}
+
 }  // namespace utils
 }  // namespace wrt
index fd1d042..5f3187e 100755 (executable)
@@ -28,6 +28,8 @@ bool EndsWith(const std::string& str, const std::string& sub);
 std::string ReplaceAll(const std::string& replace,
                        const std::string& from, const std::string& to);
 std::string GetCurrentMilliSeconds();
+bool SplitString(const std::string &str,
+                 std::string *part_1, std::string *part_2, const char delim);
 
 }  // namespace utils
 }  // namespace wrt