Add get_url_query_file_name function.

GitOrigin-RevId: 24d19f870f6a8e96bc727baa0dfeaa6a99d66931
This commit is contained in:
levlam 2018-02-20 01:54:50 +03:00
parent d475864012
commit 824735dc8e
3 changed files with 29 additions and 0 deletions

View File

@ -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

View File

@ -32,4 +32,6 @@ Result<HttpUrl> parse_url(MutableSlice url,
StringBuilder &operator<<(StringBuilder &sb, const HttpUrl &url);
string get_url_query_file_name(const string &query);
} // namespace td

View File

@ -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");
}
}