diff --git a/tdutils/td/utils/HttpUrl.cpp b/tdutils/td/utils/HttpUrl.cpp index f24e21d5..7e3c3ba4 100644 --- a/tdutils/td/utils/HttpUrl.cpp +++ b/tdutils/td/utils/HttpUrl.cpp @@ -164,4 +164,15 @@ StringBuilder &operator<<(StringBuilder &sb, const HttpUrl &url) { return sb; } +string get_url_query_file_name(const string &query) { + Slice query_slice = query; + query_slice.truncate(query.find_first_of("?#")); + + auto slash_pos = query_slice.rfind('/'); + if (slash_pos < query_slice.size()) { + return query_slice.substr(slash_pos + 1).str(); + } + return query_slice.str(); +} + } // namespace td diff --git a/tdutils/td/utils/HttpUrl.h b/tdutils/td/utils/HttpUrl.h index d91d7057..39851867 100644 --- a/tdutils/td/utils/HttpUrl.h +++ b/tdutils/td/utils/HttpUrl.h @@ -32,4 +32,6 @@ Result parse_url(MutableSlice url, StringBuilder &operator<<(StringBuilder &sb, const HttpUrl &url); +string get_url_query_file_name(const string &query); + } // namespace td diff --git a/tdutils/test/misc.cpp b/tdutils/test/misc.cpp index a34ab883..20f7e96d 100644 --- a/tdutils/test/misc.cpp +++ b/tdutils/test/misc.cpp @@ -5,6 +5,7 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #include "td/utils/base64.h" +#include "td/utils/HttpUrl.h" #include "td/utils/logging.h" #include "td/utils/misc.h" #include "td/utils/port/EventFd.h" @@ -240,3 +241,18 @@ TEST(Misc, to_double) { std::locale::global(std::locale::classic()); test_to_double(); } + +static void test_get_url_query_file_name_one(const char *prefix, const char *suffix, const char *file_name) { + ASSERT_STREQ(file_name, get_url_query_file_name(string(prefix) + string(file_name) + string(suffix))); +} + +TEST(Misc, get_url_query_file_name) { + for (auto suffix : {"?t=1#test", "#test?t=1", "#?t=1", "?t=1#", "#test", "?t=1", "#", "?", ""}) { + test_get_url_query_file_name_one("", suffix, ""); + test_get_url_query_file_name_one("/", suffix, ""); + test_get_url_query_file_name_one("/a/adasd/", suffix, ""); + test_get_url_query_file_name_one("/a/lklrjetn/", suffix, "adasd.asdas"); + test_get_url_query_file_name_one("/", suffix, "a123asadas"); + test_get_url_query_file_name_one("/", suffix, "\\a\\1\\2\\3\\a\\s\\a\\das"); + } +}