summaryrefslogtreecommitdiff
path: root/http/utility.hpp
diff options
context:
space:
mode:
authorEd Tanous <edtanous@google.com>2022-02-09 23:14:12 +0300
committerEd Tanous <ed@tanous.net>2022-02-15 08:33:22 +0300
commit11baefe467d1b6d4c845325276bc46de01d23cdb (patch)
treedfa5230cfd80fa258697d25fc2a7a9fc50c61412 /http/utility.hpp
parentdbb59d4dd8e4fc13b1cad11fd3a093f374a2a7ed (diff)
downloadbmcweb-11baefe467d1b6d4c845325276bc46de01d23cdb.tar.xz
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
Diffstat (limited to 'http/utility.hpp')
-rw-r--r--http/utility.hpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/http/utility.hpp b/http/utility.hpp
index 873cfe41ed..37c06cd7ed 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 @@ inline boost::urls::url urlFromPieces(const AV... args)
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