From 2f0efec7ab0430f4f4858db87b7eecfbccc0f47c Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Mon, 29 Sep 2025 10:36:32 +0200 Subject: 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. --- src/zencore/string.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'src/zencore/string.cpp') 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 @@ -482,6 +482,44 @@ StringBuilderImpl::Fail(const char* reason) template class StringBuilderImpl; template class StringBuilderImpl; +////////////////////////////////////////////////////////////////////////// + +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 @@ -489,6 +527,23 @@ template class StringBuilderImpl; #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]; -- cgit v1.2.3