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/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