Move validateAndSplitUri to common utility header
This function in practice looks like it has another use where the code
is essentially the same. Move it to a header so it can be used by other
things. And add unit tests to ensure it stays reliable.
Tested: Unit tests pass.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I3343ba1aa9c0dd542fbb98628b7628cb0704fb3b
diff --git a/http/ut/utility_test.cpp b/http/ut/utility_test.cpp
index eecd1f6..5fdfcb5 100644
--- a/http/ut/utility_test.cpp
+++ b/http/ut/utility_test.cpp
@@ -1,6 +1,9 @@
+#include "bmcweb_config.h"
+
#include "utility.hpp"
#include "gmock/gmock.h"
+#include "gtest/gtest.h"
namespace crow::utility
{
@@ -123,5 +126,43 @@
url = urlFromPieces("/", "bad&tring");
EXPECT_EQ(std::string_view(url.data(), url.size()), "/%2f/bad&tring");
}
+
+TEST(Utility, ValidateAndSplitUrlPositive)
+{
+ using crow::utility::validateAndSplitUrl;
+ std::string host;
+ std::string urlProto;
+ std::string port;
+ std::string path;
+ ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar", urlProto, host,
+ port, path));
+ EXPECT_EQ(host, "foo.com");
+ EXPECT_EQ(urlProto, "https");
+ EXPECT_EQ(port, "18080");
+
+ EXPECT_EQ(path, "/bar");
+
+ // query string
+ ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar?foobar=1",
+ urlProto, host, port, path));
+ EXPECT_EQ(path, "/bar?foobar=1");
+
+ // Missing port
+ ASSERT_TRUE(
+ validateAndSplitUrl("https://foo.com/bar", urlProto, host, port, path));
+ EXPECT_EQ(port, "443");
+
+ // If http push eventing is allowed, allow http, if it's not, parse should
+ // fail.
+#ifdef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING
+ ASSERT_TRUE(
+ validateAndSplitUrl("http://foo.com/bar", urlProto, host, port, path));
+ EXPECT_EQ(port, "80");
+#else
+ ASSERT_FALSE(
+ validateAndSplitUrl("http://foo.com/bar", urlProto, host, port, path));
+#endif
+}
+
} // namespace
} // namespace crow::utility
diff --git a/http/utility.hpp b/http/utility.hpp
index 873cfe4..37c06cd 100644
--- a/http/utility.hpp
+++ b/http/utility.hpp
@@ -11,6 +11,7 @@
#include <ctime>
#include <functional>
#include <limits>
+#include <regex>
#include <stdexcept>
#include <string>
#include <string_view>
@@ -692,5 +693,50 @@
return details::urlFromPiecesDetail({args...});
}
+inline bool validateAndSplitUrl(std::string_view destUrl, std::string& urlProto,
+ std::string& host, std::string& port,
+ std::string& path)
+{
+ // Validate URL using regex expression
+ // Format: <protocol>://<host>:<port>/<path>
+ // protocol: http/https
+ const std::regex urlRegex(
+ "(http|https)://([^/\\x20\\x3f\\x23\\x3a]+):?([0-9]*)(/"
+ "([^\\x20\\x23\\x3f]*\\x3f?([^\\x20\\x23\\x3f])*)?)");
+ std::cmatch match;
+ if (!std::regex_match(destUrl.begin(), destUrl.end(), match, urlRegex))
+ {
+ return false;
+ }
+
+ urlProto = std::string(match[1].first, match[1].second);
+ if (urlProto == "http")
+ {
+#ifndef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING
+ return false;
+#endif
+ }
+
+ host = std::string(match[2].first, match[2].second);
+ port = std::string(match[3].first, match[3].second);
+ path = std::string(match[4].first, match[4].second);
+ if (port.empty())
+ {
+ if (urlProto == "http")
+ {
+ port = "80";
+ }
+ else
+ {
+ port = "443";
+ }
+ }
+ if (path.empty())
+ {
+ path = "/";
+ }
+ return true;
+}
+
} // namespace utility
} // namespace crow