From 5f0b24926fa864260ef575ddc162090b91029c44 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 25 May 2021 04:39:41 +0300 Subject: [PATCH] Split path in HttpUrlQuery. --- tdutils/td/utils/HttpUrl.cpp | 9 ++++++++- tdutils/td/utils/HttpUrl.h | 2 +- tdutils/test/HttpUrl.cpp | 25 ++++++++++++++----------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/tdutils/td/utils/HttpUrl.cpp b/tdutils/td/utils/HttpUrl.cpp index c00118d37..bccedf0a2 100644 --- a/tdutils/td/utils/HttpUrl.cpp +++ b/tdutils/td/utils/HttpUrl.cpp @@ -174,13 +174,20 @@ StringBuilder &operator<<(StringBuilder &sb, const HttpUrl &url) { } HttpUrlQuery parse_url_query(Slice query) { + if (!query.empty() && query[0] == '/') { + query.remove_prefix(1); + } + size_t path_size = 0; while (path_size < query.size() && query[path_size] != '?' && query[path_size] != '#') { path_size++; } 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] == '?') { query = query.substr(path_size + 1); diff --git a/tdutils/td/utils/HttpUrl.h b/tdutils/td/utils/HttpUrl.h index 0d343fee2..306512ee5 100644 --- a/tdutils/td/utils/HttpUrl.h +++ b/tdutils/td/utils/HttpUrl.h @@ -45,7 +45,7 @@ StringBuilder &operator<<(StringBuilder &sb, const HttpUrl &url); class HttpUrlQuery { public: - string path_; + vector path_; vector> args_; Slice get_arg(Slice key) const; diff --git a/tdutils/test/HttpUrl.cpp b/tdutils/test/HttpUrl.cpp index bf5c6bcb9..5e324c562 100644 --- a/tdutils/test/HttpUrl.cpp +++ b/tdutils/test/HttpUrl.cpp @@ -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 &path, const td::vector> &args) { for (auto hash : {"", "#", "#?t=1", "#t=1&a=b"}) { 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_); } } TEST(HttpUrl, parce_url_query) { - test_parse_url_query("", "", {}); - test_parse_url_query("a", "a", {}); - test_parse_url_query("/a/b/c/", "/a/b/c/", {}); - test_parse_url_query("/a/b/?c/", "/a/b/", {{"c/", ""}}); - test_parse_url_query("?", "", {}); - 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", {}); + test_parse_url_query("", {}, {}); + test_parse_url_query("a", {"a"}, {}); + test_parse_url_query("/", {}, {}); + test_parse_url_query("//", {""}, {}); + test_parse_url_query("///?a", {td::string(), td::string()}, {{"a", ""}}); + test_parse_url_query("/a/b/c/", {"a", "b", "c"}, {}); + test_parse_url_query("/a/b/?c/", {td::string("a"), td::string("b")}, {{"c/", ""}}); + test_parse_url_query("?", {}, {}); + 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"}, {}); }