diff options
| author | Stefan Boberg <[email protected]> | 2025-09-29 10:36:32 +0200 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2025-09-29 10:36:32 +0200 |
| commit | 2f0efec7ab0430f4f4858db87b7eecfbccc0f47c (patch) | |
| tree | 80ce35992a220260cf070fac739626f555de738a /src/zencore | |
| parent | fixed race condition in zen::logging::Get (#519) (diff) | |
| download | zen-2f0efec7ab0430f4f4858db87b7eecfbccc0f47c.tar.xz zen-2f0efec7ab0430f4f4858db87b7eecfbccc0f47c.zip | |
make cpr a HttpClient implementation detail (#517)
these changes remove cpr from anything which is not `HttpClient` internals.
The goal is to eventually replace cpr with a more direct curl interface to eliminate cpr since it's proven problematic due to their development practices which frequently breaks APIs and prevents us from updating vcpkg. But this PR is limited to refactoring existing cpr code to use `HttpClient` instead.
Diffstat (limited to 'src/zencore')
| -rw-r--r-- | src/zencore/include/zencore/string.h | 3 | ||||
| -rw-r--r-- | src/zencore/string.cpp | 55 |
2 files changed, 58 insertions, 0 deletions
diff --git a/src/zencore/include/zencore/string.h b/src/zencore/include/zencore/string.h index 68129b691..93f8add0a 100644 --- a/src/zencore/include/zencore/string.h +++ b/src/zencore/include/zencore/string.h @@ -679,6 +679,9 @@ ParseHexNumber(const std::string_view HexString, UnsignedIntegral auto& OutValue return ParseHexNumber(HexString.data(), ExpectedCharacterCount, (uint8_t*)&OutValue); } +void UrlDecode(std::string_view InUrl, StringBuilderBase& OutUrl); +std::string UrlDecode(std::string_view InUrl); + ////////////////////////////////////////////////////////////////////////// // Format numbers for humans // diff --git a/src/zencore/string.cpp b/src/zencore/string.cpp index a0d8c927f..c8c7c2cde 100644 --- a/src/zencore/string.cpp +++ b/src/zencore/string.cpp @@ -483,12 +483,67 @@ template class StringBuilderImpl<char>; template class StringBuilderImpl<wchar_t>; ////////////////////////////////////////////////////////////////////////// + +void +UrlDecode(std::string_view InUrl, StringBuilderBase& OutUrl) +{ + std::string_view::size_type i = 0; + + for (; i != InUrl.size();) + { + char c = InUrl[i]; + + if ((c == '%') && ((i + 2) < InUrl.size())) + { + char hex[2] = {InUrl[i + 1], InUrl[i + 2]}; + uint8_t HexedChar; + if (ParseHexBytes(hex, 2, &HexedChar)) + { + OutUrl.Append(HexedChar); + i += 3; + + continue; + } + } + + OutUrl.Append(c); + ++i; + } +} + +std::string +UrlDecode(std::string_view InUrl) +{ + ExtendableStringBuilder<128> Url; + UrlDecode(InUrl, Url); + + return std::string(Url.ToView()); +} + +////////////////////////////////////////////////////////////////////////// // // Unit tests // #if ZEN_WITH_TESTS +TEST_CASE("url") +{ + using namespace std::literals; + + ExtendableStringBuilder<32> OutUrl; + UrlDecode("http://blah.com/foo?bar=hi%20ho", OutUrl); + CHECK_EQ(OutUrl.ToView(), "http://blah.com/foo?bar=hi ho"sv); + + OutUrl.Reset(); + + UrlDecode("http://blah.com/foo?bar=hi%ho", OutUrl); + CHECK_EQ(OutUrl.ToView(), "http://blah.com/foo?bar=hi%ho"sv); + + CHECK_EQ(UrlDecode("http://blah.com/foo?bar=hi%20ho"), "http://blah.com/foo?bar=hi ho"sv); + CHECK_EQ(UrlDecode("http://blah.com/foo?bar=hi%ho"), "http://blah.com/foo?bar=hi%ho"sv); +} + TEST_CASE("niceNum") { char Buffer[16]; |