feat uri update

This commit is contained in:
tqcq 2024-03-29 10:17:47 +08:00
parent 37a1317bd4
commit 0c6c0cfbd1
3 changed files with 59 additions and 7 deletions

View File

@ -42,6 +42,11 @@ public:
__SLED_URI_GETTER_AND_SETTER(ParamMap, query) __SLED_URI_GETTER_AND_SETTER(ParamMap, query)
__SLED_URI_GETTER_AND_SETTER(std::string, anchor) __SLED_URI_GETTER_AND_SETTER(std::string, anchor)
std::string href() const;
std::string authority() const;
std::string user_info() const;
std::string query_string() const;
private: private:
std::string scheme_; std::string scheme_;
std::string content_; std::string content_;

View File

@ -2,6 +2,7 @@
#include "sled/strings/utils.h" #include "sled/strings/utils.h"
#include <cctype> #include <cctype>
#include <map> #include <map>
#include <sstream>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <utility> #include <utility>
@ -510,14 +511,14 @@ URI
URI::ParseURI(const std::string &uri_str) URI::ParseURI(const std::string &uri_str)
{ {
URI uri; URI uri;
detail::uri uri_impl(uri_str.c_str()); detail::uri uri_impl(uri_str.c_str(), detail::uri::scheme_category::Hierarchical);
uri.set_scheme(uri_impl.get_scheme()); uri.set_scheme(uri_impl.get_scheme());
uri.set_content(uri_impl.get_content()); // uri.set_content(uri_impl.get_content());
uri.set_username(uri_impl.get_username()); uri.set_username(uri_impl.get_username());
uri.set_password(uri_impl.get_password()); uri.set_password(uri_impl.get_password());
uri.set_host(uri_impl.get_host()); uri.set_host(uri_impl.get_host());
uri.set_port(uri_impl.get_port()); uri.set_port(uri_impl.get_port());
uri.set_path(uri_impl.get_path()); uri.set_path(std::string("/") + uri_impl.get_path());
uri.set_query(uri_impl.get_query_dictionary()); uri.set_query(uri_impl.get_query_dictionary());
uri.set_anchor(uri_impl.get_fragment()); uri.set_anchor(uri_impl.get_fragment());
@ -525,4 +526,48 @@ URI::ParseURI(const std::string &uri_str)
} }
URI::URI(const std::string &uri_str) { *this = ParseURI(uri_str); } URI::URI(const std::string &uri_str) { *this = ParseURI(uri_str); }
std::string
URI::href() const
{
std::stringstream ss;
if (!scheme().empty()) { ss << scheme() << ":"; }
if (!user_info.empty()) { ss << user_info() << "@"; }
if (!authority().empty()) { ss << authority(); }
ss << path();
ss << "?" << query_string();
ss << "#" << anchor();
return ss.str();
}
std::string
URI::authority() const
{
if (port() == 0) {
return host();
} else {
return host() + ":" + std::to_string(port());
}
}
std::string
URI::user_info() const
{
if (password().empty()) { return username(); }
if (username().empty()) { return ":" + password(); }
return username() + ":" + password();
}
std::string
URI::query_string() const
{
std::stringstream ss;
for (auto item : query()) {
std::string key = item.first;
std::string value = item.second;
if (key.empty()) { return value; }
ss << key + "=" + value;
}
return ss.str();
}
}// namespace sled }// namespace sled

View File

@ -3,10 +3,12 @@
TEST(URI, Absolute) TEST(URI, Absolute)
{ {
sled::URI uri("http://example.com"); sled::URI uri("http://example.com:1234/dir1/dir2/file?a=1#anchor");
EXPECT_EQ(uri.scheme(), "http"); EXPECT_EQ(uri.scheme(), "http");
EXPECT_EQ(uri.host(), "example.com"); EXPECT_EQ(uri.host(), "example.com");
EXPECT_EQ(uri.path(), "/"); EXPECT_EQ(uri.port(), 1234);
EXPECT_TRUE(uri.query().empty()); EXPECT_EQ(uri.path(), "dir1/dir2/file");
EXPECT_EQ(uri.anchor(), ""); EXPECT_EQ(uri.query().size(), 1);
EXPECT_EQ(uri.query()["a"], "1");
EXPECT_EQ(uri.anchor(), "anchor");
} }