Separate url parser class 78/59878/1
authorKyungwook Tak <k.tak@samsung.com>
Fri, 19 Feb 2016 05:10:32 +0000 (14:10 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Fri, 19 Feb 2016 06:25:23 +0000 (15:25 +0900)
Change-Id: If5254e6c8ff5851871bff61f6e828f866e11263f
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
src/common/CMakeLists.txt
src/common/include/tpkp_parser.h [new file with mode: 0644]
src/common/src/tpkp_common.cpp
src/common/src/tpkp_parser.cpp [new file with mode: 0644]

index 6933a6d..51b724e 100644 (file)
@@ -35,6 +35,7 @@ SET(TPKP_COMMON_SRCS
        url/url_parse_file.cc
        net/http/transport_security_state.cpp
        src/tpkp_common.cpp
+       src/tpkp_parser.cpp
        )
 
 ADD_LIBRARY(${TARGET_TPKP_COMMON_LIB} SHARED ${TPKP_COMMON_SRCS})
diff --git a/src/common/include/tpkp_parser.h b/src/common/include/tpkp_parser.h
new file mode 100644 (file)
index 0000000..227dce7
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+/*
+ * @file        tpkp_parser.h
+ * @author      Kyungwook Tak (k.tak@samsung.com)
+ * @version     1.0
+ * @brief       Tizen Https Public Key Pinning url parser.
+ */
+#pragma once
+
+#include <string>
+
+namespace TPKP {
+
+class Parser {
+public:
+       Parser() = delete;
+
+       static std::string extractHostname(const std::string &url);
+};
+
+}
index 2a3a485..a3d76c6 100644 (file)
@@ -19,6 +19,7 @@
  * @version     1.0
  * @brief       Https Public Key Pinning common implementation.
  */
+#include "tpkp_common.h"
 
 #include <sys/syscall.h>
 #include <unistd.h>
@@ -31,9 +32,8 @@
 
 #include "net/http/transport_security_state.h"
 #include "net/http/transport_security_state_static.h"
-#include "url/third_party/mozilla/url_parse.h"
 
-#include "tpkp_common.h"
+#include "tpkp_parser.h"
 
 namespace {
 
@@ -43,31 +43,6 @@ inline size_t _arraySize(const T &t)
        return sizeof(t) / sizeof(*t);
 }
 
-inline std::string _toLower(const std::string &host)
-{
-       std::string lowerHost;
-       std::for_each(
-               host.begin(),
-               host.end(),
-               [&lowerHost](const std::string::value_type &ch)
-               {
-                       lowerHost.push_back(std::tolower(ch));
-               });
-
-       return lowerHost;
-}
-
-std::string prependHttps(const std::string &url)
-{
-       const std::string separator = "://";
-       auto pos = url.find(separator);
-
-       if (pos != std::string::npos)
-               return url;
-
-       return std::string("https") + separator + url;
-}
-
 } // anonymous namespace
 
 namespace TPKP {
@@ -141,16 +116,7 @@ private:
 
 Context::Impl::Impl(const std::string &url)
 {
-       url::Parsed parsed;
-       auto newUrl = prependHttps(url);
-
-       url::ParseStandardURL(newUrl.c_str(), newUrl.length(), &parsed);
-       TPKP_CHECK_THROW_EXCEPTION(parsed.host.is_valid(),
-               TPKP_E_INVALID_URL, "Failed to parse url: " << newUrl);
-
-       m_host = _toLower(newUrl.substr(
-                       static_cast<size_t>(parsed.host.begin),
-                       static_cast<size_t>(parsed.host.len)));
+       m_host = Parser::extractHostname(url);
 
        SLOGD("HPKP ready to check on host[%s]", m_host.c_str());
 
diff --git a/src/common/src/tpkp_parser.cpp b/src/common/src/tpkp_parser.cpp
new file mode 100644 (file)
index 0000000..c121659
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+/*
+ * @file        tpkp_parser.cpp
+ * @author      Kyungwook Tak (k.tak@samsung.com)
+ * @version     1.0
+ * @brief       Tizen Https Public Key Pinning url parser.
+ */
+#include "tpkp_parser.h"
+
+#include <algorithm>
+
+#include "url/third_party/mozilla/url_parse.h"
+#include "tpkp_common.h"
+
+namespace {
+
+inline std::string _toLower(const std::string &host)
+{
+       std::string lowerHost;
+       std::for_each(
+               host.begin(),
+               host.end(),
+               [&lowerHost](const std::string::value_type &ch)
+               {
+                       lowerHost.push_back(std::tolower(ch));
+               });
+
+       return lowerHost;
+}
+
+std::string prependHttps(const std::string &url)
+{
+       const std::string separator = "://";
+       auto pos = url.find(separator);
+
+       if (pos != std::string::npos)
+               return url;
+
+       return std::string("https") + separator + url;
+}
+
+}
+
+namespace TPKP {
+
+std::string Parser::extractHostname(const std::string &url)
+{
+       url::Parsed parsed;
+       auto newUrl = prependHttps(url);
+
+       url::ParseStandardURL(newUrl.c_str(), newUrl.length(), &parsed);
+       TPKP_CHECK_THROW_EXCEPTION(parsed.host.is_valid(),
+               TPKP_E_INVALID_URL, "Failed to parse url: " << newUrl);
+
+       std::string hostname = _toLower(newUrl.substr(
+                       static_cast<size_t>(parsed.host.begin),
+                       static_cast<size_t>(parsed.host.len)));
+
+       return hostname;
+}
+
+}