#pragma once #include #include #include 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 : ""); }