aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/string_utils
diff options
context:
space:
mode:
authorSergey Polovko <sergey@polovko.me>2022-02-10 16:47:02 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:47:02 +0300
commit3e0b762a82514bac89c1dd6ea7211e381d8aa248 (patch)
treec2d1b379ecaf05ca8f11ed0b5da9d1a950e6e554 /library/cpp/string_utils
parentab3783171cc30e262243a0227c86118f7080c896 (diff)
downloadydb-3e0b762a82514bac89c1dd6ea7211e381d8aa248.tar.gz
Restoring authorship annotation for Sergey Polovko <sergey@polovko.me>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/string_utils')
-rw-r--r--library/cpp/string_utils/url/url.cpp52
-rw-r--r--library/cpp/string_utils/url/url.h60
-rw-r--r--library/cpp/string_utils/url/url_ut.cpp98
3 files changed, 105 insertions, 105 deletions
diff --git a/library/cpp/string_utils/url/url.cpp b/library/cpp/string_utils/url/url.cpp
index 85f4ac5d69..dc3d5aa00e 100644
--- a/library/cpp/string_utils/url/url.cpp
+++ b/library/cpp/string_utils/url/url.cpp
@@ -206,35 +206,35 @@ void SeparateUrlFromQueryAndFragment(const TStringBuf url, TStringBuf& sanitized
}
}
-bool TryGetSchemeHostAndPort(const TStringBuf url, TStringBuf& scheme, TStringBuf& host, ui16& port) {
- const size_t schemeSize = GetSchemePrefixSize(url);
- if (schemeSize != 0) {
- scheme = url.Head(schemeSize);
- }
-
- TStringBuf portStr;
- TStringBuf hostAndPort = GetHostAndPort(url.Tail(schemeSize));
+bool TryGetSchemeHostAndPort(const TStringBuf url, TStringBuf& scheme, TStringBuf& host, ui16& port) {
+ const size_t schemeSize = GetSchemePrefixSize(url);
+ if (schemeSize != 0) {
+ scheme = url.Head(schemeSize);
+ }
+
+ TStringBuf portStr;
+ TStringBuf hostAndPort = GetHostAndPort(url.Tail(schemeSize));
if (hostAndPort && hostAndPort.back() != ']' && hostAndPort.TryRSplit(':', host, portStr)) {
- // URL has port
- if (!TryFromString(portStr, port)) {
- return false;
- }
- } else {
- host = hostAndPort;
+ // URL has port
+ if (!TryFromString(portStr, port)) {
+ return false;
+ }
+ } else {
+ host = hostAndPort;
if (scheme == TStringBuf("https://")) {
- port = 443;
+ port = 443;
} else if (scheme == TStringBuf("http://")) {
- port = 80;
- }
- }
- return true;
-}
-
-void GetSchemeHostAndPort(const TStringBuf url, TStringBuf& scheme, TStringBuf& host, ui16& port) {
- bool isOk = TryGetSchemeHostAndPort(url, scheme, host, port);
- Y_ENSURE(isOk, "cannot parse port number from URL: " << url);
-}
-
+ port = 80;
+ }
+ }
+ return true;
+}
+
+void GetSchemeHostAndPort(const TStringBuf url, TStringBuf& scheme, TStringBuf& host, ui16& port) {
+ bool isOk = TryGetSchemeHostAndPort(url, scheme, host, port);
+ Y_ENSURE(isOk, "cannot parse port number from URL: " << url);
+}
+
TStringBuf GetOnlyHost(const TStringBuf url) noexcept {
return GetHost(CutSchemePrefix(url));
}
diff --git a/library/cpp/string_utils/url/url.h b/library/cpp/string_utils/url/url.h
index 84137ccc57..5e797e374a 100644
--- a/library/cpp/string_utils/url/url.h
+++ b/library/cpp/string_utils/url/url.h
@@ -65,14 +65,14 @@ TString AddSchemePrefix(const TString& url);
Y_PURE_FUNCTION
TStringBuf GetHost(const TStringBuf url) noexcept;
-
+
Y_PURE_FUNCTION
TStringBuf GetHostAndPort(const TStringBuf url) noexcept;
Y_PURE_FUNCTION
TStringBuf GetSchemeHostAndPort(const TStringBuf url, bool trimHttp = true, bool trimDefaultPort = true) noexcept;
-/**
+/**
* Splits URL to host and path
*
* @param[in] url any URL
@@ -93,35 +93,35 @@ void SplitUrlToHostAndPath(const TStringBuf url, TString& host, TString& path);
void SeparateUrlFromQueryAndFragment(const TStringBuf url, TStringBuf& sanitizedUrl, TStringBuf& query, TStringBuf& fragment);
/**
- * Extracts scheme, host and port from URL.
- *
- * Port will be parsed from URL with checks against ui16 overflow. If URL doesn't
- * contain port it will be determined by one of the known schemes (currently
- * https:// and http:// only).
- * Given parameters will not be modified if URL has no appropriate components.
- *
- * @param[in] url any URL
- * @param[out] scheme URL scheme
- * @param[out] host host name
- * @param[out] port parsed port number
- * @return false if present port number cannot be parsed into ui16
- * true otherwise.
- */
-bool TryGetSchemeHostAndPort(const TStringBuf url, TStringBuf& scheme, TStringBuf& host, ui16& port);
-
-/**
- * Extracts scheme, host and port from URL.
- *
+ * Extracts scheme, host and port from URL.
+ *
+ * Port will be parsed from URL with checks against ui16 overflow. If URL doesn't
+ * contain port it will be determined by one of the known schemes (currently
+ * https:// and http:// only).
+ * Given parameters will not be modified if URL has no appropriate components.
+ *
+ * @param[in] url any URL
+ * @param[out] scheme URL scheme
+ * @param[out] host host name
+ * @param[out] port parsed port number
+ * @return false if present port number cannot be parsed into ui16
+ * true otherwise.
+ */
+bool TryGetSchemeHostAndPort(const TStringBuf url, TStringBuf& scheme, TStringBuf& host, ui16& port);
+
+/**
+ * Extracts scheme, host and port from URL.
+ *
* This function perform the same actions as TryGetSchemeHostAndPort(), but in
- * case of impossibility to parse port number throws yexception.
- *
- * @param[in] url any URL
- * @param[out] scheme URL scheme
- * @param[out] host host name
- * @param[out] port parsed port number
- * @throws yexception if present port number cannot be parsed into ui16.
- */
-void GetSchemeHostAndPort(const TStringBuf url, TStringBuf& scheme, TStringBuf& host, ui16& port);
+ * case of impossibility to parse port number throws yexception.
+ *
+ * @param[in] url any URL
+ * @param[out] scheme URL scheme
+ * @param[out] host host name
+ * @param[out] port parsed port number
+ * @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..3e64d3e2c5 100644
--- a/library/cpp/string_utils/url/url_ut.cpp
+++ b/library/cpp/string_utils/url/url_ut.cpp
@@ -126,7 +126,7 @@ Y_UNIT_TEST_SUITE(TUtilUrlTest) {
UNIT_ASSERT_VALUES_EQUAL("m", CutMPrefix("m"));
UNIT_ASSERT_VALUES_EQUAL("ya.ru", CutMPrefix("m.ya.ru"));
}
-
+
Y_UNIT_TEST(TestSplitUrlToHostAndPath) {
TStringBuf host, path;
@@ -176,46 +176,46 @@ Y_UNIT_TEST_SUITE(TUtilUrlTest) {
}
Y_UNIT_TEST(TestGetSchemeHostAndPort) {
- { // all components are present
- TStringBuf scheme("unknown"), host("unknown");
- ui16 port = 0;
- GetSchemeHostAndPort("https://ya.ru:8080/bebe", scheme, host, port);
- UNIT_ASSERT_VALUES_EQUAL(scheme, "https://");
- UNIT_ASSERT_VALUES_EQUAL(host, "ya.ru");
- UNIT_ASSERT_VALUES_EQUAL(port, 8080);
- }
- { // scheme is abset
- TStringBuf scheme("unknown"), host("unknown");
- ui16 port = 0;
- GetSchemeHostAndPort("ya.ru:8080/bebe", scheme, host, port);
- UNIT_ASSERT_VALUES_EQUAL(scheme, "unknown");
- UNIT_ASSERT_VALUES_EQUAL(host, "ya.ru");
- UNIT_ASSERT_VALUES_EQUAL(port, 8080);
- }
- { // scheme and port are absent
- TStringBuf scheme("unknown"), host("unknown");
- ui16 port = 0;
- GetSchemeHostAndPort("ya.ru/bebe", scheme, host, port);
- UNIT_ASSERT_VALUES_EQUAL(scheme, "unknown");
- UNIT_ASSERT_VALUES_EQUAL(host, "ya.ru");
- UNIT_ASSERT_VALUES_EQUAL(port, 0);
- }
- { // port is absent, but returned its default value for HTTP
- TStringBuf scheme("unknown"), host("unknown");
- ui16 port = 0;
- GetSchemeHostAndPort("http://ya.ru/bebe", scheme, host, port);
- UNIT_ASSERT_VALUES_EQUAL(scheme, "http://");
- UNIT_ASSERT_VALUES_EQUAL(host, "ya.ru");
- UNIT_ASSERT_VALUES_EQUAL(port, 80);
- }
- { // port is absent, but returned its default value for HTTPS
- TStringBuf scheme("unknown"), host("unknown");
- ui16 port = 0;
- GetSchemeHostAndPort("https://ya.ru/bebe", scheme, host, port);
- UNIT_ASSERT_VALUES_EQUAL(scheme, "https://");
- UNIT_ASSERT_VALUES_EQUAL(host, "ya.ru");
- UNIT_ASSERT_VALUES_EQUAL(port, 443);
- }
+ { // all components are present
+ TStringBuf scheme("unknown"), host("unknown");
+ ui16 port = 0;
+ GetSchemeHostAndPort("https://ya.ru:8080/bebe", scheme, host, port);
+ UNIT_ASSERT_VALUES_EQUAL(scheme, "https://");
+ UNIT_ASSERT_VALUES_EQUAL(host, "ya.ru");
+ UNIT_ASSERT_VALUES_EQUAL(port, 8080);
+ }
+ { // scheme is abset
+ TStringBuf scheme("unknown"), host("unknown");
+ ui16 port = 0;
+ GetSchemeHostAndPort("ya.ru:8080/bebe", scheme, host, port);
+ UNIT_ASSERT_VALUES_EQUAL(scheme, "unknown");
+ UNIT_ASSERT_VALUES_EQUAL(host, "ya.ru");
+ UNIT_ASSERT_VALUES_EQUAL(port, 8080);
+ }
+ { // scheme and port are absent
+ TStringBuf scheme("unknown"), host("unknown");
+ ui16 port = 0;
+ GetSchemeHostAndPort("ya.ru/bebe", scheme, host, port);
+ UNIT_ASSERT_VALUES_EQUAL(scheme, "unknown");
+ UNIT_ASSERT_VALUES_EQUAL(host, "ya.ru");
+ UNIT_ASSERT_VALUES_EQUAL(port, 0);
+ }
+ { // port is absent, but returned its default value for HTTP
+ TStringBuf scheme("unknown"), host("unknown");
+ ui16 port = 0;
+ GetSchemeHostAndPort("http://ya.ru/bebe", scheme, host, port);
+ UNIT_ASSERT_VALUES_EQUAL(scheme, "http://");
+ UNIT_ASSERT_VALUES_EQUAL(host, "ya.ru");
+ UNIT_ASSERT_VALUES_EQUAL(port, 80);
+ }
+ { // port is absent, but returned its default value for HTTPS
+ TStringBuf scheme("unknown"), host("unknown");
+ ui16 port = 0;
+ GetSchemeHostAndPort("https://ya.ru/bebe", scheme, host, port);
+ UNIT_ASSERT_VALUES_EQUAL(scheme, "https://");
+ UNIT_ASSERT_VALUES_EQUAL(host, "ya.ru");
+ UNIT_ASSERT_VALUES_EQUAL(port, 443);
+ }
{ // ipv6
TStringBuf scheme("unknown"), host("unknown");
ui16 port = 0;
@@ -240,14 +240,14 @@ Y_UNIT_TEST_SUITE(TUtilUrlTest) {
UNIT_ASSERT_VALUES_EQUAL(host, "");
UNIT_ASSERT_VALUES_EQUAL(port, 0);
}
- // port overflow
- auto testCase = []() {
- TStringBuf scheme("unknown"), host("unknown");
- ui16 port = 0;
- GetSchemeHostAndPort("https://ya.ru:65536/bebe", scheme, host, port);
- };
- UNIT_ASSERT_EXCEPTION(testCase(), yexception);
- }
+ // port overflow
+ auto testCase = []() {
+ TStringBuf scheme("unknown"), host("unknown");
+ ui16 port = 0;
+ GetSchemeHostAndPort("https://ya.ru:65536/bebe", scheme, host, port);
+ };
+ UNIT_ASSERT_EXCEPTION(testCase(), yexception);
+ }
Y_UNIT_TEST(TestCutUrlPrefixes) {
UNIT_ASSERT_VALUES_EQUAL("ya.ru/bebe", CutUrlPrefixes("http://ya.ru/bebe"));