From d19cddca7e5512bc3594d8dd178d7fcbd117e2d3 Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 26 Jul 2026 14:11:18 -0400 Subject: [PATCH] create_query --- libi2pd/HTTP.cpp | 58 +++++++++++++++++++++++++++++++++--------------- libi2pd/HTTP.h | 6 +++-- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/libi2pd/HTTP.cpp b/libi2pd/HTTP.cpp index e9951511..5839dd25 100644 --- a/libi2pd/HTTP.cpp +++ b/libi2pd/HTTP.cpp @@ -209,7 +209,7 @@ namespace http return true; } - bool URL::parse_query(std::map & params) + bool URL::parse_query(std::map & params) const { std::vector tokens; strsplit(query, tokens, '&'); @@ -230,31 +230,53 @@ namespace http return true; } - std::string URL::to_string() const + void URL::create_query (const std::map& params) + { + if (params.empty ()) return; + hasquery = true; + std::stringstream s; + bool isFirst = true; + for (const auto& [param, value]: params) + { + if (isFirst) + isFirst = false; + else + s << '&'; + s << param << '=' << value; + } + query = s.str (); + } + + std::string URL::to_string(bool requestTargetOnly) const { std::string out = ""; - if (schema != "") { - out = schema + "://"; - if (user != "" && pass != "") { - out += user + ":" + pass + "@"; - } else if (user != "") { - out += user + "@"; - } - if (ipv6) { - if (port) { - out += "[" + host + "]:" + std::to_string(port); - } else { - out += "[" + host + "]"; + if (!requestTargetOnly) + { + if (schema != "") + { + out = schema + "://"; + if (user != "" && pass != "") { + out += user + ":" + pass + "@"; + } else if (user != "") { + out += user + "@"; } - } else { - if (port) { - out += host + ":" + std::to_string(port); + if (ipv6) { + if (port) { + out += "[" + host + "]:" + std::to_string(port); + } else { + out += "[" + host + "]"; + } } else { - out += host; + if (port) { + out += host + ":" + std::to_string(port); + } else { + out += host; + } } } } out += path; + if (out.empty ()) out = '/'; if (hasquery) // add query even if it was empty out += "?"; if (query != "") diff --git a/libi2pd/HTTP.h b/libi2pd/HTTP.h index ab6b4166..d70fbdf5 100644 --- a/libi2pd/HTTP.h +++ b/libi2pd/HTTP.h @@ -62,13 +62,15 @@ namespace http * @brief Parse query part of url to key/value map * @note Honestly, this should be implemented with std::multimap */ - bool parse_query(std::map & params); + bool parse_query(std::map & params) const; + + void create_query (const std::map& params); /** * @brief Serialize URL structure to url * @note Returns relative url if schema if empty, absolute url otherwise */ - std::string to_string () const; + std::string to_string (bool requestTargetOnly = false) const; /** * @brief return true if the host is inside i2p