Split path in HttpUrlQuery.

This commit is contained in:
levlam 2021-05-25 04:39:41 +03:00
parent 198af82254
commit 5f0b24926f
3 changed files with 23 additions and 13 deletions

View File

@ -174,13 +174,20 @@ StringBuilder &operator<<(StringBuilder &sb, const HttpUrl &url) {
} }
HttpUrlQuery parse_url_query(Slice query) { HttpUrlQuery parse_url_query(Slice query) {
if (!query.empty() && query[0] == '/') {
query.remove_prefix(1);
}
size_t path_size = 0; size_t path_size = 0;
while (path_size < query.size() && query[path_size] != '?' && query[path_size] != '#') { while (path_size < query.size() && query[path_size] != '?' && query[path_size] != '#') {
path_size++; path_size++;
} }
HttpUrlQuery result; HttpUrlQuery result;
result.path_ = url_decode(query.substr(0, path_size), false); result.path_ = full_split(url_decode(query.substr(0, path_size), false), '/');
if (!result.path_.empty() && result.path_.back().empty()) {
result.path_.pop_back();
}
if (path_size < query.size() && query[path_size] == '?') { if (path_size < query.size() && query[path_size] == '?') {
query = query.substr(path_size + 1); query = query.substr(path_size + 1);

View File

@ -45,7 +45,7 @@ StringBuilder &operator<<(StringBuilder &sb, const HttpUrl &url);
class HttpUrlQuery { class HttpUrlQuery {
public: public:
string path_; vector<string> path_;
vector<std::pair<string, string>> args_; vector<std::pair<string, string>> args_;
Slice get_arg(Slice key) const; Slice get_arg(Slice key) const;

View File

@ -29,23 +29,26 @@ TEST(HttpUrl, get_url_query_file_name) {
} }
} }
static void test_parse_url_query(const td::string &query, const td::string &path, static void test_parse_url_query(const td::string &query, const td::vector<td::string> &path,
const td::vector<std::pair<td::string, td::string>> &args) { const td::vector<std::pair<td::string, td::string>> &args) {
for (auto hash : {"", "#", "#?t=1", "#t=1&a=b"}) { for (auto hash : {"", "#", "#?t=1", "#t=1&a=b"}) {
auto url_query = td::parse_url_query(query + hash); auto url_query = td::parse_url_query(query + hash);
ASSERT_STREQ(path, url_query.path_); ASSERT_EQ(path, url_query.path_);
ASSERT_EQ(args, url_query.args_); ASSERT_EQ(args, url_query.args_);
} }
} }
TEST(HttpUrl, parce_url_query) { TEST(HttpUrl, parce_url_query) {
test_parse_url_query("", "", {}); test_parse_url_query("", {}, {});
test_parse_url_query("a", "a", {}); test_parse_url_query("a", {"a"}, {});
test_parse_url_query("/a/b/c/", "/a/b/c/", {}); test_parse_url_query("/", {}, {});
test_parse_url_query("/a/b/?c/", "/a/b/", {{"c/", ""}}); test_parse_url_query("//", {""}, {});
test_parse_url_query("?", "", {}); test_parse_url_query("///?a", {td::string(), td::string()}, {{"a", ""}});
test_parse_url_query("???", "", {{"??", ""}}); test_parse_url_query("/a/b/c/", {"a", "b", "c"}, {});
test_parse_url_query("?a=b=c=d?e=f=g=h&x=y=z?d=3&", "", {{"a", "b=c=d?e=f=g=h"}, {"x", "y=z?d=3"}}); test_parse_url_query("/a/b/?c/", {td::string("a"), td::string("b")}, {{"c/", ""}});
test_parse_url_query("c?&&&a=b", "c", {{"a", "b"}}); test_parse_url_query("?", {}, {});
test_parse_url_query("c?&&&=b", "c", {}); test_parse_url_query("???", {}, {{"??", ""}});
test_parse_url_query("?a=b=c=d?e=f=g=h&x=y=z?d=3&", {}, {{"a", "b=c=d?e=f=g=h"}, {"x", "y=z?d=3"}});
test_parse_url_query("c?&&&a=b", {"c"}, {{"a", "b"}});
test_parse_url_query("c?&&&=b", {"c"}, {});
} }