diff options
author | smikler <smikler@yandex-team.ru> | 2022-02-10 16:49:32 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:32 +0300 |
commit | e4f0fd4ab53ca40eb91e750cf3e7f76c21e930db (patch) | |
tree | afee3c8173a0960bf439959f26e7624d1212e11a /library/cpp/string_utils | |
parent | 1503061b80644305b2e6dd1327b57118e35ebd31 (diff) | |
download | ydb-e4f0fd4ab53ca40eb91e750cf3e7f76c21e930db.tar.gz |
Restoring authorship annotation for <smikler@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/string_utils')
-rw-r--r-- | library/cpp/string_utils/url/url.cpp | 68 | ||||
-rw-r--r-- | library/cpp/string_utils/url/url.h | 4 | ||||
-rw-r--r-- | library/cpp/string_utils/url/url_ut.cpp | 34 |
3 files changed, 53 insertions, 53 deletions
diff --git a/library/cpp/string_utils/url/url.cpp b/library/cpp/string_utils/url/url.cpp index 85f4ac5d69..579d1e25be 100644 --- a/library/cpp/string_utils/url/url.cpp +++ b/library/cpp/string_utils/url/url.cpp @@ -147,41 +147,41 @@ static inline TStringBuf GetHostAndPortImpl(const TStringBuf url) { } TStringBuf GetHost(const TStringBuf url) noexcept { - return GetHostAndPortImpl<false>(url); -} - + return GetHostAndPortImpl<false>(url); +} + TStringBuf GetHostAndPort(const TStringBuf url) noexcept { - return GetHostAndPortImpl<true>(url); -} - + return GetHostAndPortImpl<true>(url); +} + TStringBuf GetSchemeHostAndPort(const TStringBuf url, bool trimHttp, bool trimDefaultPort) noexcept { - const size_t schemeSize = GetSchemePrefixSize(url); - const TStringBuf scheme = url.Head(schemeSize); - + const size_t schemeSize = GetSchemePrefixSize(url); + const TStringBuf scheme = url.Head(schemeSize); + const bool isHttp = (schemeSize == 0 || scheme == TStringBuf("http://")); - - TStringBuf hostAndPort = GetHostAndPort(url.Tail(schemeSize)); - - if (trimDefaultPort) { - const size_t pos = hostAndPort.find(':'); - if (pos != TStringBuf::npos) { + + TStringBuf hostAndPort = GetHostAndPort(url.Tail(schemeSize)); + + if (trimDefaultPort) { + const size_t pos = hostAndPort.find(':'); + if (pos != TStringBuf::npos) { const bool isHttps = (scheme == TStringBuf("https://")); - - const TStringBuf port = hostAndPort.Tail(pos + 1); + + const TStringBuf port = hostAndPort.Tail(pos + 1); if ((isHttp && port == TStringBuf("80")) || (isHttps && port == TStringBuf("443"))) { - // trimming default port - hostAndPort = hostAndPort.Head(pos); - } - } - } - - if (isHttp && trimHttp) { - return hostAndPort; - } else { - return TStringBuf(scheme.begin(), hostAndPort.end()); - } -} - + // trimming default port + hostAndPort = hostAndPort.Head(pos); + } + } + } + + if (isHttp && trimHttp) { + return hostAndPort; + } else { + return TStringBuf(scheme.begin(), hostAndPort.end()); + } +} + void SplitUrlToHostAndPath(const TStringBuf url, TStringBuf& host, TStringBuf& path) { auto [hostBuf, pathBuf] = NUrl::SplitUrlToHostAndPath(url); host = hostBuf; @@ -240,14 +240,14 @@ TStringBuf GetOnlyHost(const TStringBuf url) noexcept { } TStringBuf GetPathAndQuery(const TStringBuf url, bool trimFragment) noexcept { - const size_t off = url.find('/', GetHttpPrefixSize(url)); + const size_t off = url.find('/', GetHttpPrefixSize(url)); TStringBuf hostUnused, path; if (!url.TrySplitAt(off, hostUnused, path)) - return "/"; + return "/"; return trimFragment ? path.Before('#') : path; -} - +} + // this strange creature returns 2nd level domain, possibly with port TStringBuf GetDomain(const TStringBuf host) noexcept { const char* c = !host ? host.data() : host.end() - 1; diff --git a/library/cpp/string_utils/url/url.h b/library/cpp/string_utils/url/url.h index 84137ccc57..cf90abb8ff 100644 --- a/library/cpp/string_utils/url/url.h +++ b/library/cpp/string_utils/url/url.h @@ -41,7 +41,7 @@ size_t GetSchemePrefixSize(const TStringBuf url) noexcept; Y_PURE_FUNCTION TStringBuf GetSchemePrefix(const TStringBuf url) noexcept; - + //! removes protocol prefixes 'http://' and 'https://' from given URL //! @note if URL has no prefix or some other prefix the function does nothing //! @param url URL from which the prefix should be removed @@ -122,7 +122,7 @@ bool TryGetSchemeHostAndPort(const TStringBuf url, TStringBuf& scheme, TStringBu * @throws yexception if present port number cannot be parsed into ui16. */ void GetSchemeHostAndPort(const TStringBuf url, TStringBuf& scheme, TStringBuf& host, ui16& port); - + Y_PURE_FUNCTION TStringBuf GetPathAndQuery(const TStringBuf url, bool trimFragment = true) noexcept; /** diff --git a/library/cpp/string_utils/url/url_ut.cpp b/library/cpp/string_utils/url/url_ut.cpp index 1588013893..64988471ea 100644 --- a/library/cpp/string_utils/url/url_ut.cpp +++ b/library/cpp/string_utils/url/url_ut.cpp @@ -7,38 +7,38 @@ Y_UNIT_TEST_SUITE(TUtilUrlTest) { Y_UNIT_TEST(TestGetHostAndGetHostAndPort) { UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHost("ya.ru/bebe")); - UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHostAndPort("ya.ru/bebe")); + UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHostAndPort("ya.ru/bebe")); UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHost("ya.ru")); - UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHostAndPort("ya.ru")); + UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHostAndPort("ya.ru")); UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHost("ya.ru:8080")); - UNIT_ASSERT_VALUES_EQUAL("ya.ru:8080", GetHostAndPort("ya.ru:8080")); + UNIT_ASSERT_VALUES_EQUAL("ya.ru:8080", GetHostAndPort("ya.ru:8080")); UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHost("ya.ru/bebe:8080")); - UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHostAndPort("ya.ru/bebe:8080")); + UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHostAndPort("ya.ru/bebe:8080")); UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHost("ya.ru:8080/bebe")); UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetHost("https://ya.ru:8080/bebe")); UNIT_ASSERT_VALUES_EQUAL("www.ya.ru", GetHost("www.ya.ru:8080/bebe")); UNIT_ASSERT_VALUES_EQUAL("www.ya.ru", GetHost("https://www.ya.ru:8080/bebe")); - UNIT_ASSERT_VALUES_EQUAL("ya.ru:8080", GetHostAndPort("ya.ru:8080/bebe")); + UNIT_ASSERT_VALUES_EQUAL("ya.ru:8080", GetHostAndPort("ya.ru:8080/bebe")); // irl RFC3986 sometimes gets ignored UNIT_ASSERT_VALUES_EQUAL("pravda-kmv.ru", GetHost("pravda-kmv.ru?page=news&id=6973")); - UNIT_ASSERT_VALUES_EQUAL("pravda-kmv.ru", GetHostAndPort("pravda-kmv.ru?page=news&id=6973")); + UNIT_ASSERT_VALUES_EQUAL("pravda-kmv.ru", GetHostAndPort("pravda-kmv.ru?page=news&id=6973")); // check simple string UNIT_ASSERT_VALUES_EQUAL("some_blender_url", GetHost("some_blender_url")); UNIT_ASSERT_VALUES_EQUAL("", GetHost("")); } Y_UNIT_TEST(TestGetPathAndQuery) { - UNIT_ASSERT_VALUES_EQUAL("/", GetPathAndQuery("ru.wikipedia.org")); - UNIT_ASSERT_VALUES_EQUAL("/", GetPathAndQuery("ru.wikipedia.org/")); - UNIT_ASSERT_VALUES_EQUAL("/", GetPathAndQuery("ru.wikipedia.org:8080")); - UNIT_ASSERT_VALUES_EQUAL("/index.php?123/", GetPathAndQuery("ru.wikipedia.org/index.php?123/")); - UNIT_ASSERT_VALUES_EQUAL("/", GetPathAndQuery("http://ru.wikipedia.org:8080")); - UNIT_ASSERT_VALUES_EQUAL("/index.php?123/", GetPathAndQuery("https://ru.wikipedia.org/index.php?123/")); - UNIT_ASSERT_VALUES_EQUAL("/", GetPathAndQuery("ru.wikipedia.org/#comment")); - UNIT_ASSERT_VALUES_EQUAL("/?1", GetPathAndQuery("ru.wikipedia.org/?1#comment")); - UNIT_ASSERT_VALUES_EQUAL("/?1#comment", GetPathAndQuery("ru.wikipedia.org/?1#comment", false)); - } - + UNIT_ASSERT_VALUES_EQUAL("/", GetPathAndQuery("ru.wikipedia.org")); + UNIT_ASSERT_VALUES_EQUAL("/", GetPathAndQuery("ru.wikipedia.org/")); + UNIT_ASSERT_VALUES_EQUAL("/", GetPathAndQuery("ru.wikipedia.org:8080")); + UNIT_ASSERT_VALUES_EQUAL("/index.php?123/", GetPathAndQuery("ru.wikipedia.org/index.php?123/")); + UNIT_ASSERT_VALUES_EQUAL("/", GetPathAndQuery("http://ru.wikipedia.org:8080")); + UNIT_ASSERT_VALUES_EQUAL("/index.php?123/", GetPathAndQuery("https://ru.wikipedia.org/index.php?123/")); + UNIT_ASSERT_VALUES_EQUAL("/", GetPathAndQuery("ru.wikipedia.org/#comment")); + UNIT_ASSERT_VALUES_EQUAL("/?1", GetPathAndQuery("ru.wikipedia.org/?1#comment")); + UNIT_ASSERT_VALUES_EQUAL("/?1#comment", GetPathAndQuery("ru.wikipedia.org/?1#comment", false)); + } + Y_UNIT_TEST(TestGetDomain) { UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetDomain("www.ya.ru")); UNIT_ASSERT_VALUES_EQUAL("ya.ru", GetDomain("ya.ru")); |