aboutsummaryrefslogtreecommitdiff
path: root/libcmix-network/uriparser.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'libcmix-network/uriparser.hpp')
-rw-r--r--libcmix-network/uriparser.hpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/libcmix-network/uriparser.hpp b/libcmix-network/uriparser.hpp
new file mode 100644
index 0000000..c46b70a
--- /dev/null
+++ b/libcmix-network/uriparser.hpp
@@ -0,0 +1,59 @@
+#pragma once
+
+#include <string>
+#include <regex>
+#include <iostream>
+
+struct Uri {
+ std::string scheme;
+ std::string username;
+ std::string password;
+ std::string host;
+ std::string port;
+ std::string query;
+ std::string hash;
+};
+
+Uri parse_uri(std::string str) {
+ std::string scheme = "(?:(.+?)://)?";
+ std::string uname_pass = "(?:(.*?)?(?::(.*?))@)?";
+ std::string host = "(.+?)";
+ std::string port = "(?::(\\d+?))?";
+ std::string query = "(?:\\?(.+?))?";
+ std::string hash = "(?:#(.+?))?";
+
+ std::regex expr("^" + scheme + uname_pass + host + port + query + hash + "$");
+ std::smatch matches;
+ std::regex_match(str, matches, expr);
+
+ return {
+ matches[1].str(),
+ matches[2].str(),
+ matches[3].str(),
+ matches[4].str(),
+ matches[5].str(),
+ matches[6].str(),
+ matches[7].str()
+ };
+}
+
+std::string debug_uri(Uri uri) {
+ std::stringstream ss;
+ ss << "scheme: " << uri.scheme << std::endl
+ << "username: " << uri.username << std::endl
+ << "password: " << uri.password << std::endl
+ << "host: " << uri.host << std::endl
+ << "port: " << uri.port << std::endl
+ << "query: " << uri.query << std::endl
+ << "hash: " << uri.hash << std::endl;
+ return ss.str();
+}
+
+std::string uri_to_string(Uri uri) {
+ return
+ (!uri.scheme.empty() ? uri.scheme + "://" : "") +
+ (!uri.username.empty() ? uri.username + (!uri.password.empty() ? ":" + uri.password : "") + "@" : "") +
+ uri.host +
+ (!uri.port.empty() ? ":" + uri.port : "") +
+ (!uri.query.empty() ? "?" + uri.query : "");
+}