aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorswarmer <swarmer@yandex-team.com>2024-09-16 20:56:18 +0300
committerswarmer <swarmer@yandex-team.com>2024-09-16 21:13:10 +0300
commit701f0ceecf624ac39ed167f69657ffc90a7f821b (patch)
treeb9ead9e39fa629c5dff08b931d08410968458522
parent571ed30daf84e8c684f6ad0b07d7dfc74c951a4f (diff)
downloadydb-701f0ceecf624ac39ed167f69657ffc90a7f821b.tar.gz
check lifetime bounds in the url library
commit_hash:a13b2b608cf10b63726c06e1ece6405e92c41377
-rw-r--r--library/cpp/string_utils/url/url.cpp46
-rw-r--r--library/cpp/string_utils/url/url.h48
2 files changed, 47 insertions, 47 deletions
diff --git a/library/cpp/string_utils/url/url.cpp b/library/cpp/string_utils/url/url.cpp
index 4db902cf65..7d7d7dbd90 100644
--- a/library/cpp/string_utils/url/url.cpp
+++ b/library/cpp/string_utils/url/url.cpp
@@ -71,7 +71,7 @@ namespace NUrl {
return {host, path};
}
- bool HasLowerHost(const TStringBuf &url) {
+ bool HasLowerHost(const TStringBuf url) {
for (size_t n = 0; n < url.length(); ++n) {
if (url[n] == '/')
break;
@@ -81,14 +81,14 @@ namespace NUrl {
return true;
}
- TStringBuf CutHttpWwwPrefixes(const TStringBuf &url) {
+ TStringBuf CutHttpWwwPrefixes(const TStringBuf url Y_LIFETIME_BOUND) {
TStringBuf urlCut = CutWWWPrefix(CutHttpPrefix(url));
if (!urlCut.empty() && urlCut.back() == '/')
urlCut = urlCut.substr(0, urlCut.length() - 1);
return urlCut;
}
- TString MakeLowerHost(const TStringBuf &url, size_t shift) {
+ TString MakeLowerHost(const TStringBuf url, size_t shift) {
TString urlFixed(url);
for (char *c = urlFixed.begin() + shift; *c && (*c != '/'); ++c) {
*c = tolower(*c);
@@ -97,7 +97,7 @@ namespace NUrl {
return urlFixed;
}
- TString MakeNormalized(const TStringBuf &url) {
+ TString MakeNormalized(const TStringBuf url) {
TStringBuf urlCut = CutHttpWwwPrefixes(url);
if (HasLowerHost(urlCut)) {
return ToString(urlCut);
@@ -123,11 +123,11 @@ size_t GetHttpPrefixSize(const TWtringBuf url, bool ignorehttps) noexcept {
return GetHttpPrefixSizeImpl<wchar16>(url.data(), TKnownSize(url.size()), ignorehttps);
}
-TStringBuf CutHttpPrefix(const TStringBuf url, bool ignorehttps) noexcept {
+TStringBuf CutHttpPrefix(const TStringBuf url Y_LIFETIME_BOUND, bool ignorehttps) noexcept {
return CutHttpPrefixImpl(url, ignorehttps);
}
-TWtringBuf CutHttpPrefix(const TWtringBuf url, bool ignorehttps) noexcept {
+TWtringBuf CutHttpPrefix(const TWtringBuf url Y_LIFETIME_BOUND, bool ignorehttps) noexcept {
return CutHttpPrefixImpl(url, ignorehttps);
}
@@ -153,11 +153,11 @@ size_t GetSchemePrefixSize(const TStringBuf url) noexcept {
return n + 3 - url.begin();
}
-TStringBuf GetSchemePrefix(const TStringBuf url) noexcept {
+TStringBuf GetSchemePrefix(const TStringBuf url Y_LIFETIME_BOUND) noexcept {
return url.Head(GetSchemePrefixSize(url));
}
-TStringBuf CutSchemePrefix(const TStringBuf url) noexcept {
+TStringBuf CutSchemePrefix(const TStringBuf url Y_LIFETIME_BOUND) noexcept {
return url.Tail(GetSchemePrefixSize(url));
}
@@ -184,15 +184,15 @@ static inline TStringBuf GetHostAndPortImpl(const TStringBuf url) {
return urlNoScheme;
}
-TStringBuf GetHost(const TStringBuf url) noexcept {
+TStringBuf GetHost(const TStringBuf url Y_LIFETIME_BOUND) noexcept {
return GetHostAndPortImpl<false>(url);
}
-TStringBuf GetHostAndPort(const TStringBuf url) noexcept {
+TStringBuf GetHostAndPort(const TStringBuf url Y_LIFETIME_BOUND) noexcept {
return GetHostAndPortImpl<true>(url);
}
-TStringBuf GetSchemeHost(const TStringBuf url, bool trimHttp) noexcept {
+TStringBuf GetSchemeHost(const TStringBuf url Y_LIFETIME_BOUND, bool trimHttp) noexcept {
const size_t schemeSize = GetSchemePrefixSize(url);
const TStringBuf scheme = url.Head(schemeSize);
@@ -207,7 +207,7 @@ TStringBuf GetSchemeHost(const TStringBuf url, bool trimHttp) noexcept {
}
}
-TStringBuf GetSchemeHostAndPort(const TStringBuf url, bool trimHttp, bool trimDefaultPort) noexcept {
+TStringBuf GetSchemeHostAndPort(const TStringBuf url Y_LIFETIME_BOUND, bool trimHttp, bool trimDefaultPort) noexcept {
const size_t schemeSize = GetSchemePrefixSize(url);
const TStringBuf scheme = url.Head(schemeSize);
@@ -288,11 +288,11 @@ void GetSchemeHostAndPort(const TStringBuf url, TStringBuf& scheme, TStringBuf&
Y_ENSURE(isOk, "cannot parse port number from URL: " << url);
}
-TStringBuf GetOnlyHost(const TStringBuf url) noexcept {
+TStringBuf GetOnlyHost(const TStringBuf url Y_LIFETIME_BOUND) noexcept {
return GetHost(CutSchemePrefix(url));
}
-TStringBuf GetPathAndQuery(const TStringBuf url, bool trimFragment) noexcept {
+TStringBuf GetPathAndQuery(const TStringBuf url Y_LIFETIME_BOUND, bool trimFragment) noexcept {
const size_t off = url.find('/', GetHttpPrefixSize(url));
TStringBuf hostUnused, path;
if (!url.TrySplitAt(off, hostUnused, path))
@@ -302,7 +302,7 @@ TStringBuf GetPathAndQuery(const TStringBuf url, bool trimFragment) noexcept {
}
// this strange creature returns 2nd level domain, possibly with port
-TStringBuf GetDomain(const TStringBuf host) noexcept {
+TStringBuf GetDomain(const TStringBuf host Y_LIFETIME_BOUND) noexcept {
const char* c = !host ? host.data() : host.end() - 1;
for (bool wasPoint = false; c != host.data(); --c) {
if (*c == '.') {
@@ -316,7 +316,7 @@ TStringBuf GetDomain(const TStringBuf host) noexcept {
return TStringBuf(c, host.end());
}
-TStringBuf GetParentDomain(const TStringBuf host, size_t level) noexcept {
+TStringBuf GetParentDomain(const TStringBuf host Y_LIFETIME_BOUND, size_t level) noexcept {
size_t pos = host.size();
for (size_t i = 0; i < level; ++i) {
pos = host.rfind('.', pos);
@@ -326,17 +326,17 @@ TStringBuf GetParentDomain(const TStringBuf host, size_t level) noexcept {
return host.SubStr(pos + 1);
}
-TStringBuf GetZone(const TStringBuf host) noexcept {
+TStringBuf GetZone(const TStringBuf host Y_LIFETIME_BOUND) noexcept {
return GetParentDomain(host, 1);
}
-TStringBuf CutWWWPrefix(const TStringBuf url) noexcept {
+TStringBuf CutWWWPrefix(const TStringBuf url Y_LIFETIME_BOUND) noexcept {
if (url.size() >= 4 && url[3] == '.' && !strnicmp(url.data(), "www", 3))
return url.substr(4);
return url;
}
-TStringBuf CutWWWNumberedPrefix(const TStringBuf url) noexcept {
+TStringBuf CutWWWNumberedPrefix(const TStringBuf url Y_LIFETIME_BOUND) noexcept {
auto it = url.begin();
StripRangeBegin(it, url.end(), [](auto& it){ return *it == 'w' || *it == 'W'; });
@@ -356,7 +356,7 @@ TStringBuf CutWWWNumberedPrefix(const TStringBuf url) noexcept {
return url;
}
-TStringBuf CutMPrefix(const TStringBuf url) noexcept {
+TStringBuf CutMPrefix(const TStringBuf url Y_LIFETIME_BOUND) noexcept {
if (url.size() >= 2 && url[1] == '.' && (url[0] == 'm' || url[0] == 'M')) {
return url.substr(2);
}
@@ -441,20 +441,20 @@ size_t NormalizeHostName(char* dest, const TStringBuf source, size_t dest_size,
return len;
}
-TStringBuf RemoveFinalSlash(TStringBuf str) noexcept {
+TStringBuf RemoveFinalSlash(TStringBuf str Y_LIFETIME_BOUND) noexcept {
if (str.EndsWith('/')) {
str.Chop(1);
}
return str;
}
-TStringBuf CutUrlPrefixes(TStringBuf url) noexcept {
+TStringBuf CutUrlPrefixes(TStringBuf url Y_LIFETIME_BOUND) noexcept {
url = CutSchemePrefix(url);
url = CutWWWPrefix(url);
return url;
}
-bool DoesUrlPathStartWithToken(TStringBuf url, const TStringBuf& token) noexcept {
+bool DoesUrlPathStartWithToken(TStringBuf url, const TStringBuf token) noexcept {
url = CutSchemePrefix(url);
const TStringBuf noHostSuffix = url.After('/');
if (noHostSuffix == url) {
diff --git a/library/cpp/string_utils/url/url.h b/library/cpp/string_utils/url/url.h
index 9b73233078..5e707756c3 100644
--- a/library/cpp/string_utils/url/url.h
+++ b/library/cpp/string_utils/url/url.h
@@ -19,12 +19,12 @@ namespace NUrl {
};
Y_PURE_FUNCTION
- TSplitUrlToHostAndPathResult SplitUrlToHostAndPath(const TStringBuf url);
+ TSplitUrlToHostAndPathResult SplitUrlToHostAndPath(const TStringBuf url Y_LIFETIME_BOUND);
- bool HasLowerHost(const TStringBuf &url);
- TStringBuf CutHttpWwwPrefixes(const TStringBuf &url);
- TString MakeLowerHost(const TStringBuf &url, size_t shift = 0);
- TString MakeNormalized(const TStringBuf &url);
+ bool HasLowerHost(const TStringBuf url);
+ TStringBuf CutHttpWwwPrefixes(const TStringBuf url Y_LIFETIME_BOUND);
+ TString MakeLowerHost(const TStringBuf url, size_t shift = 0);
+ TString MakeNormalized(const TStringBuf url);
} // namespace NUrl
@@ -45,7 +45,7 @@ Y_PURE_FUNCTION
size_t GetSchemePrefixSize(const TStringBuf url) noexcept;
Y_PURE_FUNCTION
-TStringBuf GetSchemePrefix(const TStringBuf url) noexcept;
+TStringBuf GetSchemePrefix(const TStringBuf url Y_LIFETIME_BOUND) noexcept;
//! removes protocol prefixes 'http://' and 'https://' from given URL
//! @note if URL has no prefix or some other prefix the function does nothing
@@ -53,13 +53,13 @@ TStringBuf GetSchemePrefix(const TStringBuf url) noexcept;
//! @param ignorehttps if true, leaves https://
//! @return a new URL without protocol prefix
Y_PURE_FUNCTION
-TStringBuf CutHttpPrefix(const TStringBuf url, bool ignorehttps = false) noexcept;
+TStringBuf CutHttpPrefix(const TStringBuf url Y_LIFETIME_BOUND, bool ignorehttps = false) noexcept;
Y_PURE_FUNCTION
-TWtringBuf CutHttpPrefix(const TWtringBuf url, bool ignorehttps = false) noexcept;
+TWtringBuf CutHttpPrefix(const TWtringBuf url Y_LIFETIME_BOUND, bool ignorehttps = false) noexcept;
Y_PURE_FUNCTION
-TStringBuf CutSchemePrefix(const TStringBuf url) noexcept;
+TStringBuf CutSchemePrefix(const TStringBuf url Y_LIFETIME_BOUND) noexcept;
//! adds specified scheme prefix if URL has no scheme
//! @note if URL has scheme prefix already the function returns unchanged URL
@@ -69,16 +69,16 @@ TString AddSchemePrefix(const TString& url, const TStringBuf scheme);
TString AddSchemePrefix(const TString& url);
Y_PURE_FUNCTION
-TStringBuf GetHost(const TStringBuf url) noexcept;
+TStringBuf GetHost(const TStringBuf url Y_LIFETIME_BOUND) noexcept;
Y_PURE_FUNCTION
-TStringBuf GetHostAndPort(const TStringBuf url) noexcept;
+TStringBuf GetHostAndPort(const TStringBuf url Y_LIFETIME_BOUND) noexcept;
Y_PURE_FUNCTION
-TStringBuf GetSchemeHost(const TStringBuf url, bool trimHttp = true) noexcept;
+TStringBuf GetSchemeHost(const TStringBuf url Y_LIFETIME_BOUND, bool trimHttp = true) noexcept;
Y_PURE_FUNCTION
-TStringBuf GetSchemeHostAndPort(const TStringBuf url, bool trimHttp = true, bool trimDefaultPort = true) noexcept;
+TStringBuf GetSchemeHostAndPort(const TStringBuf url Y_LIFETIME_BOUND, bool trimHttp = true, bool trimDefaultPort = true) noexcept;
/**
* Splits URL to host and path
@@ -132,26 +132,26 @@ bool TryGetSchemeHostAndPort(const TStringBuf url, TStringBuf& scheme, TStringBu
void GetSchemeHostAndPort(const TStringBuf url, TStringBuf& scheme, TStringBuf& host, ui16& port);
Y_PURE_FUNCTION
-TStringBuf GetPathAndQuery(const TStringBuf url, bool trimFragment = true) noexcept;
+TStringBuf GetPathAndQuery(const TStringBuf url Y_LIFETIME_BOUND, bool trimFragment = true) noexcept;
/**
* Extracts host from url and cuts http(https) protocol prefix and port if any.
* @param[in] url any URL
* @return host without port and http(https) prefix.
*/
Y_PURE_FUNCTION
-TStringBuf GetOnlyHost(const TStringBuf url) noexcept;
+TStringBuf GetOnlyHost(const TStringBuf url Y_LIFETIME_BOUND) noexcept;
Y_PURE_FUNCTION
-TStringBuf GetParentDomain(const TStringBuf host, size_t level) noexcept; // ("www.ya.ru", 2) -> "ya.ru"
+TStringBuf GetParentDomain(const TStringBuf host Y_LIFETIME_BOUND, size_t level) noexcept; // ("www.ya.ru", 2) -> "ya.ru"
Y_PURE_FUNCTION
-TStringBuf GetZone(const TStringBuf host) noexcept;
+TStringBuf GetZone(const TStringBuf host Y_LIFETIME_BOUND) noexcept;
Y_PURE_FUNCTION
-TStringBuf CutWWWPrefix(const TStringBuf url) noexcept;
+TStringBuf CutWWWPrefix(const TStringBuf url Y_LIFETIME_BOUND) noexcept;
Y_PURE_FUNCTION
-TStringBuf CutWWWNumberedPrefix(const TStringBuf url) noexcept;
+TStringBuf CutWWWNumberedPrefix(const TStringBuf url Y_LIFETIME_BOUND) noexcept;
/**
* Cuts 'm.' prefix from url if and only if the url starts with it
@@ -162,16 +162,16 @@ TStringBuf CutWWWNumberedPrefix(const TStringBuf url) noexcept;
* @return url without 'm.' or 'M.' prefix.
*/
Y_PURE_FUNCTION
-TStringBuf CutMPrefix(const TStringBuf url) noexcept;
+TStringBuf CutMPrefix(const TStringBuf url Y_LIFETIME_BOUND) noexcept;
Y_PURE_FUNCTION
-TStringBuf GetDomain(const TStringBuf host) noexcept; // should not be used
+TStringBuf GetDomain(const TStringBuf host Y_LIFETIME_BOUND) noexcept; // should not be used
size_t NormalizeUrlName(char* dest, const TStringBuf source, size_t dest_size);
size_t NormalizeHostName(char* dest, const TStringBuf source, size_t dest_size, ui16 defport = 80);
Y_PURE_FUNCTION
-TStringBuf RemoveFinalSlash(TStringBuf str) noexcept;
+TStringBuf RemoveFinalSlash(TStringBuf str Y_LIFETIME_BOUND) noexcept;
-TStringBuf CutUrlPrefixes(TStringBuf url) noexcept;
-bool DoesUrlPathStartWithToken(TStringBuf url, const TStringBuf& token) noexcept;
+TStringBuf CutUrlPrefixes(TStringBuf url Y_LIFETIME_BOUND) noexcept;
+bool DoesUrlPathStartWithToken(TStringBuf url, const TStringBuf token) noexcept;