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/zen/zen.cpp | 90 +++++++++++++++++++-------------------------------------- 1 file changed, 30 insertions(+), 60 deletions(-) (limited to 'src/zen/zen.cpp') diff --git a/src/zen/zen.cpp b/src/zen/zen.cpp index 0381dd15c..1d2faba7e 100644 --- a/src/zen/zen.cpp +++ b/src/zen/zen.cpp @@ -55,7 +55,6 @@ #endif ZEN_THIRD_PARTY_INCLUDES_START -#include #include #include #include @@ -275,66 +274,37 @@ ZenCmdBase::GetSubCommand(cxxopts::Options&, } static ReturnCode -GetReturnCodeFromHttpResult(int Error, HttpResponseCode ResponseCode) +GetReturnCodeFromHttpResult(const HttpClientError& Ex) { - if ((cpr::ErrorCode)Error != cpr::ErrorCode::OK) - { - switch ((cpr::ErrorCode)Error) - { - case cpr::ErrorCode::CONNECTION_FAILURE: - return ReturnCode::kHttpCantConnectError; - case cpr::ErrorCode::HOST_RESOLUTION_FAILURE: - case cpr::ErrorCode::PROXY_RESOLUTION_FAILURE: - return ReturnCode::kHttpNoHost; - case cpr::ErrorCode::INTERNAL_ERROR: - case cpr::ErrorCode::NETWORK_RECEIVE_ERROR: - case cpr::ErrorCode::NETWORK_SEND_FAILURE: - case cpr::ErrorCode::OPERATION_TIMEDOUT: - return ReturnCode::kHttpTimeout; - case cpr::ErrorCode::SSL_CONNECT_ERROR: - case cpr::ErrorCode::SSL_LOCAL_CERTIFICATE_ERROR: - case cpr::ErrorCode::SSL_REMOTE_CERTIFICATE_ERROR: - case cpr::ErrorCode::SSL_CACERT_ERROR: - case cpr::ErrorCode::GENERIC_SSL_ERROR: - return ReturnCode::kHttpSLLError; - default: - return ReturnCode::kHttpOtherClientError; - } - } - else if (IsHttpSuccessCode(ResponseCode)) - { + HttpClientError::ResponseClass ResponseClass = Ex.GetResponseClass(); + + if (ResponseClass == HttpClientError::ResponseClass::kSuccess) return ReturnCode::kSuccess; - } - else + + switch (ResponseClass) { - switch (ResponseCode) - { - case HttpResponseCode::Unauthorized: - return ReturnCode::kHttpUnauthorized; - case HttpResponseCode::NotFound: - return ReturnCode::kHttpNotFound; - case HttpResponseCode::Forbidden: - return ReturnCode::kHttpForbidden; - case HttpResponseCode::Conflict: - return ReturnCode::kHttpConflict; - case HttpResponseCode::InternalServerError: - return ReturnCode::kHttpInternalServerError; - case HttpResponseCode::ServiceUnavailable: - return ReturnCode::kHttpServiceUnavailable; - case HttpResponseCode::BadGateway: - return ReturnCode::kHttpBadGateway; - case HttpResponseCode::GatewayTimeout: - return ReturnCode::kHttpGatewayTimeout; - default: - if (ResponseCode >= HttpResponseCode::InternalServerError) - { - return ReturnCode::kHttpOtherServerError; - } - else - { - return ReturnCode::kHttpOtherClientError; - } - } +#define HANDLE_CASE(ErrorClass) \ + case HttpClientError::ResponseClass::ErrorClass: \ + return ReturnCode::ErrorClass + + HANDLE_CASE(kHttpOtherClientError); + HANDLE_CASE(kHttpCantConnectError); + HANDLE_CASE(kHttpNotFound); + HANDLE_CASE(kHttpUnauthorized); + HANDLE_CASE(kHttpSLLError); + HANDLE_CASE(kHttpForbidden); + HANDLE_CASE(kHttpTimeout); + HANDLE_CASE(kHttpConflict); + HANDLE_CASE(kHttpNoHost); + HANDLE_CASE(kHttpOtherServerError); + HANDLE_CASE(kHttpInternalServerError); + HANDLE_CASE(kHttpServiceUnavailable); + HANDLE_CASE(kHttpBadGateway); + HANDLE_CASE(kHttpGatewayTimeout); +#undef HANDLE_CASE + + default: + return ReturnCode::kOtherError; } } @@ -1093,7 +1063,7 @@ main(int argc, char** argv) catch (const HttpClientError& Ex) { ZEN_CONSOLE_ERROR("Operation failed due to a http error: {}", Ex.what()); - ReturnCode Result = GetReturnCodeFromHttpResult(Ex.m_Error, Ex.m_ResponseCode); + ReturnCode Result = GetReturnCodeFromHttpResult(Ex); return (int)Result; } catch (const AssertException& Ex) @@ -1144,7 +1114,7 @@ main(int argc, char** argv) catch (const HttpClientError& Ex) { printf("Error: Operation failed due to a http error: %s", Ex.what()); - ReturnCode Result = GetReturnCodeFromHttpResult(Ex.m_Error, Ex.m_ResponseCode); + ReturnCode Result = GetReturnCodeFromHttpResult(Ex); return (int)Result; } catch (const AssertException& Ex) -- cgit v1.2.3