Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
henryzhx8 committed Dec 19, 2024
1 parent 6c0c5de commit 0f9d6b0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 58 deletions.
53 changes: 47 additions & 6 deletions core/common/http/Curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,46 @@ using namespace std;

namespace logtail {

NetworkCode GetNetworkStatus(CURLcode code) {
// please refer to https://curl.se/libcurl/c/libcurl-errors.html
switch (code) {
case CURLE_OK:
return NetworkCode::Ok;
case CURLE_COULDNT_CONNECT:
return NetworkCode::ConnectionFailed;
case CURLE_LOGIN_DENIED:
case CURLE_REMOTE_ACCESS_DENIED:
return NetworkCode::RemoteAccessDenied;
case CURLE_OPERATION_TIMEDOUT:
return NetworkCode::Timeout;
case CURLE_SSL_CONNECT_ERROR:
return NetworkCode::SSLConnectError;
case CURLE_SSL_CERTPROBLEM:
case CURLE_SSL_CACERT:
return NetworkCode::SSLCertError;
case CURLE_SEND_ERROR:
case CURLE_SEND_FAIL_REWIND:
return NetworkCode::SendDataFailed;
case CURLE_RECV_ERROR:
return NetworkCode::RecvDataFailed;
case CURLE_SSL_PINNEDPUBKEYNOTMATCH:
case CURLE_SSL_INVALIDCERTSTATUS:
case CURLE_SSL_CACERT_BADFILE:
case CURLE_SSL_CIPHER:
case CURLE_SSL_ENGINE_NOTFOUND:
case CURLE_SSL_ENGINE_SETFAILED:
case CURLE_USE_SSL_FAILED:
case CURLE_SSL_ENGINE_INITFAILED:
case CURLE_SSL_CRL_BADFILE:
case CURLE_SSL_ISSUER_ERROR:
case CURLE_SSL_SHUTDOWN_FAILED:
return NetworkCode::SSLOtherProblem;
case CURLE_FAILED_INIT:
default:
return NetworkCode::Other;
}
}

static size_t header_write_callback(char* buffer,
size_t size,
size_t nmemb,
Expand Down Expand Up @@ -211,7 +251,7 @@ bool AddRequestToMultiCurlHandler(CURLM* multiCurl, unique_ptr<AsynHttpRequest>&
request->mTls);
if (curl == NULL) {
LOG_ERROR(sLogger, ("failed to send request", "failed to init curl handler")("request address", request.get()));
request->mResponse.SetNetworkStatus(CURLE_FAILED_INIT);
request->mResponse.SetNetworkStatus(NetworkCode::Other, "failed to init curl handler");
request->OnSendDone(request->mResponse);
return false;
}
Expand All @@ -224,7 +264,7 @@ bool AddRequestToMultiCurlHandler(CURLM* multiCurl, unique_ptr<AsynHttpRequest>&
LOG_ERROR(sLogger,
("failed to send request", "failed to add the easy curl handle to multi_handle")(
"errMsg", curl_multi_strerror(res))("request address", request.get()));
request->mResponse.SetNetworkStatus(CURLE_FAILED_INIT);
request->mResponse.SetNetworkStatus(NetworkCode::Other, "failed to add the easy curl handle to multi_handle");
request->OnSendDone(request->mResponse);
curl_easy_cleanup(curl);
return false;
Expand All @@ -249,7 +289,7 @@ void HandleCompletedAsynRequests(CURLM* multiCurl, int& runningHandlers) {
case CURLE_OK: {
long statusCode = 0;
curl_easy_getinfo(handler, CURLINFO_RESPONSE_CODE, &statusCode);
request->mResponse.SetNetworkStatus(CURLE_OK);
request->mResponse.SetNetworkStatus(NetworkCode::Ok, "");
request->mResponse.SetStatusCode(statusCode);
request->mResponse.SetResponseTime(responseTimeMs);
request->OnSendDone(request->mResponse);
Expand All @@ -275,12 +315,13 @@ void HandleCompletedAsynRequests(CURLM* multiCurl, int& runningHandlers) {
++runningHandlers;
requestReused = true;
} else {
request->mResponse.SetNetworkStatus(msg->data.result);
auto errMsg = curl_easy_strerror(msg->data.result);
request->mResponse.SetNetworkStatus(GetNetworkStatus(msg->data.result), errMsg);
request->OnSendDone(request->mResponse);
LOG_DEBUG(sLogger,
("failed to send http request", "abort")("request address", request)(
"response time",
ToString(responseTimeMs.count()) + "ms")("try cnt", ToString(request->mTryCnt)));
"response time", ToString(responseTimeMs.count()) + "ms")(
"try cnt", ToString(request->mTryCnt))("errMsg", errMsg));
}
break;
}
Expand Down
2 changes: 2 additions & 0 deletions core/common/http/Curl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

namespace logtail {

NetworkCode GetNetworkStatus(CURLcode code);

CURL* CreateCurlHandler(const std::string& method,
bool httpsFlag,
const std::string& host,
Expand Down
56 changes: 4 additions & 52 deletions core/common/http/HttpResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

#pragma once

#include <curl/curl.h>

#include <chrono>
#include <cstdint>
#include <functional>
Expand Down Expand Up @@ -100,56 +98,10 @@ class HttpResponse {
void SetResponseTime(const std::chrono::milliseconds& time) { mResponseTime = time; }
std::chrono::milliseconds GetResponseTime() const { return mResponseTime; }

NetworkStatus GetNetworkStatus() { return mNetworkStatus; }
void SetNetworkStatus(CURLcode code) {
mNetworkStatus.mMessage = curl_easy_strerror(code);
// please refer to https://curl.se/libcurl/c/libcurl-errors.html
switch (code) {
case CURLE_OK:
mNetworkStatus.mCode = NetworkCode::Ok;
break;
case CURLE_COULDNT_CONNECT:
mNetworkStatus.mCode = NetworkCode::ConnectionFailed;
break;
case CURLE_LOGIN_DENIED:
case CURLE_REMOTE_ACCESS_DENIED:
mNetworkStatus.mCode = NetworkCode::RemoteAccessDenied;
break;
case CURLE_OPERATION_TIMEDOUT:
mNetworkStatus.mCode = NetworkCode::Timeout;
break;
case CURLE_SSL_CONNECT_ERROR:
mNetworkStatus.mCode = NetworkCode::SSLConnectError;
break;
case CURLE_SSL_CERTPROBLEM:
case CURLE_SSL_CACERT:
mNetworkStatus.mCode = NetworkCode::SSLCertError;
break;
case CURLE_SEND_ERROR:
case CURLE_SEND_FAIL_REWIND:
mNetworkStatus.mCode = NetworkCode::SendDataFailed;
break;
case CURLE_RECV_ERROR:
mNetworkStatus.mCode = NetworkCode::RecvDataFailed;
break;
case CURLE_SSL_PINNEDPUBKEYNOTMATCH:
case CURLE_SSL_INVALIDCERTSTATUS:
case CURLE_SSL_CACERT_BADFILE:
case CURLE_SSL_CIPHER:
case CURLE_SSL_ENGINE_NOTFOUND:
case CURLE_SSL_ENGINE_SETFAILED:
case CURLE_USE_SSL_FAILED:
case CURLE_SSL_ENGINE_INITFAILED:
case CURLE_SSL_CRL_BADFILE:
case CURLE_SSL_ISSUER_ERROR:
case CURLE_SSL_SHUTDOWN_FAILED:
mNetworkStatus.mCode = NetworkCode::SSLOtherProblem;
break;
case CURLE_FAILED_INIT:
default:
mNetworkStatus.mCode = NetworkCode::Other;
break;
}
const NetworkStatus& GetNetworkStatus() { return mNetworkStatus; }
void SetNetworkStatus(NetworkCode code, const std::string& msg) {
mNetworkStatus.mCode = code;
mNetworkStatus.mMessage = msg;
}

#ifdef APSARA_UNIT_TEST_MAIN
Expand Down

0 comments on commit 0f9d6b0

Please sign in to comment.