Skip to content

Commit

Permalink
Merge branch 'tim/handle_middlebox_errors' into 'master'
Browse files Browse the repository at this point in the history
feat: Return explicit NetworkError when intercepted by a middlebox

See merge request TankerHQ/sdk-native!966
  • Loading branch information
tux3 committed Jun 4, 2024
2 parents 9f9b379 + cc1f392 commit 4633162
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
30 changes: 18 additions & 12 deletions modules/sdk-core/src/Network/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,24 @@ AppdErrc getErrorFromCode(std::string_view code)
return AppdErrc::UnknownError;
}

HttpError handleErrorResponse(HttpResponse const& res, HttpRequest const& req) {
// We assume non-JSON responses come from a proxy or other middlebox
Exception newMiddleboxError(HttpResponse const& res, HttpRequest const& req)
{
return Errors::formatEx(Errors::Errc::NetworkError,
"Request may have been intercepted by proxy, received non-JSON response for "
"{} {}, status: {}, body: {}",
httpMethodToString(req.method),
req.url,
res.statusCode,
res.body);
}

HttpError handleErrorResponse(HttpResponse const& res, HttpRequest const& req)
{
auto contentType = res.headers.get(HttpHeader::CONTENT_TYPE);

if (!(contentType && boost::algorithm::starts_with(*contentType, "application/json")))
{
throw Errors::formatEx(Errors::AppdErrc::InternalError,
"{} {}, status: {}",
httpMethodToString(req.method),
req.url,
res.statusCode);
}
throw newMiddleboxError(res, req);

try
{
Expand All @@ -90,7 +97,7 @@ HttpError handleErrorResponse(HttpResponse const& res, HttpRequest const& req) {
}
catch (nlohmann::json::exception const& ex)
{
throw Errors::formatEx(Errors::AppdErrc::InternalError, "invalid {} http response format: {}", res.statusCode, res.body);
throw newMiddleboxError(res, req);
}
}

Expand All @@ -110,7 +117,7 @@ HttpResult handleResponse(HttpResponse res, HttpRequest const& req)
}
catch (nlohmann::json::exception const& ex)
{
throw Errors::formatEx(Errors::AppdErrc::InternalError, "invalid http response format: {}", res.body);
throw newMiddleboxError(res, req);
}
}
}
Expand Down Expand Up @@ -383,8 +390,7 @@ tc::cotask<HttpResult> HttpClient::fetch(HttpRequest req)
TC_RETURN(handleResponse(std::move(res), req));
}

tc::cotask<std::string> HttpClient::asyncGetRedirectLocation(std::string_view target,
std::optional<std::string> cookie)
tc::cotask<std::string> HttpClient::asyncGetRedirectLocation(std::string_view target, std::optional<std::string> cookie)
{
HttpRequest req;
req.method = HttpMethod::Get;
Expand Down
2 changes: 2 additions & 0 deletions modules/tcurl/src/curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ request::request() : _easy(curl_easy_init())
if (!_easy)
throw std::runtime_error("curl_easy_init() failed");

curl_easy_setopt(_easy.get(), CURLOPT_USERAGENT, curl_version());

curl_easy_setopt(_easy.get(), CURLOPT_WRITEFUNCTION, &write_cb_c);
curl_easy_setopt(_easy.get(), CURLOPT_WRITEDATA, this);
// curl_easy_setopt(_easy.get(), CURLOPT_VERBOSE, 1L);
Expand Down

0 comments on commit 4633162

Please sign in to comment.