diff --git a/inc/rum/http/uri.h b/inc/rum/http/uri.h index d39dd74..3ef7881 100644 --- a/inc/rum/http/uri.h +++ b/inc/rum/http/uri.h @@ -32,5 +32,8 @@ class URI { void set_port(const std::string& port) { this->port = port; } operator std::string() const; + + static std::string decode(std::string text); + static std::string encode(std::string text); }; } // namespace Rum::HTTP \ No newline at end of file diff --git a/src/rum/http/uri.cpp b/src/rum/http/uri.cpp index 1e30a6b..148b99e 100644 --- a/src/rum/http/uri.cpp +++ b/src/rum/http/uri.cpp @@ -47,12 +47,12 @@ URI::operator std::string() const { } URI::URI(const std::string& uri) { - std::regex uri_regex(R"((([\w\d]+):\/\/)?(([\w\d]+)(:([\w\d]+)?)@)?([\w\d\.]+)(:(\d+))?(\/?[\w\d%\.\/]+)?(\?([^#]+))?(\#([\w\d]*))?)"); + std::regex uri_regex(R"((([\w\d]+):\/\/)?(([\w\d%]+)(:([\w\d%]+)?)@)?([\w\d\.]+)(:(\d+))?(\/?[\w\d%\.\/]+)?(\?([^#]+))?(\#([\w\d%]*))?)"); std::smatch match; #ifdef DEBUG - std::cout << uri << std::endl; + std::cout << uri_decoded << std::endl; #endif if (std::regex_match(uri.cbegin(), uri.cend(), match, uri_regex)) { @@ -62,13 +62,18 @@ URI::URI(const std::string& uri) { MATCH(scheme, 2) MATCH(user, 4) + user = decode(user); MATCH(password, 6) + password = decode(password); MATCH(host, 7) MATCH(port, 9) MATCH(path, 10) + path = decode(path); std::string query; MATCH(query, 12) + query = decode(query); MATCH(fragment, 14) + fragment = decode(fragment); std::stringstream parameters_string(query); std::string parameter; @@ -83,4 +88,19 @@ URI::URI(const std::string& uri) { throw std::exception(); } } + +std::string URI::decode(std::string text) { + std::regex encoded_regex(R"(%([\w\d]{2}))"); + std::smatch match; + while (std::regex_search(text.cbegin(), text.cend(), match, encoded_regex)) { + char decoded = (char)std::stoi(match.str(1), 0, 16); + text = text.substr(0, match.position()) + decoded + text.substr(match.position() + 3); + } + + return text; +} +std::string URI::encode(std::string text) { + throw std::exception(); + return text; +} } // namespace Rum::HTTP \ No newline at end of file