197 lines
5.0 KiB
C++
197 lines
5.0 KiB
C++
#include "dl_http.h"
|
||
|
||
#ifndef DL_DISABLE_CURL
|
||
#include <curl/curl.h>
|
||
#include <dl.h>
|
||
|
||
#define TO_STRING(a) #a
|
||
|
||
namespace dl
|
||
{
|
||
Http* g_http;
|
||
|
||
inline size_t HttpWriteFunc(void* buffer, size_t size, size_t nmemb, void* userp)
|
||
{
|
||
Http::Request* req = (Http::Request*)userp;
|
||
BufferAppend(req->GetDataRecvRef(), { (std::byte*)buffer, size * nmemb });
|
||
return nmemb;
|
||
}
|
||
|
||
Http::Http() :
|
||
_pathCACert{ "cacert.pem" },
|
||
_bShowResultLog{true}
|
||
{
|
||
curl_global_init(CURL_GLOBAL_ALL);
|
||
}
|
||
|
||
Http::~Http()
|
||
{
|
||
curl_global_cleanup();
|
||
}
|
||
|
||
bool Http::SendRequest(Request& req)
|
||
{
|
||
CURL* curl = curl_easy_init();
|
||
if (!curl)
|
||
{
|
||
dl::log_err("curl 初始化失败!");
|
||
return false;
|
||
}
|
||
curl_slist* header = nullptr;
|
||
finally f = [&]() {
|
||
curl_easy_cleanup(curl);
|
||
if (header)
|
||
curl_slist_free_all(header);
|
||
};
|
||
|
||
CURLcode c;
|
||
auto fn_check = [&](const char* str) {
|
||
if (c != CURLE_OK)
|
||
{
|
||
dl::log_warn("curl: {}失败: {}", str, (int)c);
|
||
return false;
|
||
}
|
||
return true;
|
||
};
|
||
// 在完成交互以后强迫断开连接,不能重用
|
||
c = curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1);
|
||
if (!fn_check(TO_STRING(CURLOPT_FORBID_REUSE)))
|
||
return false;
|
||
// 毫秒超时需要
|
||
c = curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
||
if (!fn_check(TO_STRING(CURLOPT_NOSIGNAL)))
|
||
return false;
|
||
// 连接超时
|
||
c = curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 3000);
|
||
if (!fn_check(TO_STRING(CURLOPT_CONNECTTIMEOUT_MS)))
|
||
return false;
|
||
// 响应超时
|
||
c = curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 10000);
|
||
if (!fn_check(TO_STRING(CURLOPT_TIMEOUT_MS)))
|
||
return false;
|
||
// 验证服务器证书(暂时关闭)
|
||
if (false)
|
||
{
|
||
c = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
|
||
if (!fn_check(TO_STRING(CURLOPT_SSL_VERIFYPEER)))
|
||
return false;
|
||
c = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2);
|
||
if (!fn_check(TO_STRING(CURLOPT_SSL_VERIFYHOST)))
|
||
return false;
|
||
c = curl_easy_setopt(curl, CURLOPT_CAINFO, _pathCACert.c_str());
|
||
if (!fn_check(TO_STRING(CURLOPT_CAINFO)))
|
||
return false;
|
||
}
|
||
else
|
||
{
|
||
c = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
|
||
if (!fn_check(TO_STRING(CURLOPT_SSL_VERIFYPEER)))
|
||
return false;
|
||
c = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
|
||
if (!fn_check(TO_STRING(CURLOPT_SSL_VERIFYHOST)))
|
||
return false;
|
||
}
|
||
|
||
// 回调函数
|
||
c = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HttpWriteFunc);
|
||
if (!fn_check(TO_STRING(CURLOPT_WRITEFUNCTION)))
|
||
return false;
|
||
// 缓冲区
|
||
c = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &req);
|
||
if (!fn_check(TO_STRING(CURLOPT_WRITEDATA)))
|
||
return false;
|
||
// url
|
||
c = curl_easy_setopt(curl, CURLOPT_URL, req.GetUrl().c_str());
|
||
if (!fn_check(TO_STRING(CURLOPT_URL)))
|
||
return false;
|
||
|
||
//
|
||
switch (req.GetType())
|
||
{
|
||
case Request::Type::GET:
|
||
// 跟踪重定向(参考cocos源码,Get有)
|
||
c = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
|
||
if (!fn_check(TO_STRING(CURLOPT_FOLLOWLOCATION)))
|
||
return false;
|
||
break;
|
||
case Request::Type::POST:
|
||
// 标记为POST
|
||
c = curl_easy_setopt(curl, CURLOPT_POST, 1);
|
||
if (!fn_check(TO_STRING(CURLOPT_POST)))
|
||
return false;
|
||
c = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req.GetDataSend().data());
|
||
if (!fn_check(TO_STRING(CURLOPT_POSTFIELDS)))
|
||
return false;
|
||
c = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, req.GetDataSend().size());
|
||
if (!fn_check(TO_STRING(CURLOPT_POSTFIELDSIZE)))
|
||
return false;
|
||
break;
|
||
default:
|
||
log_err("curl: {},暂不支持的请求类型 {}", req.GetUrl(), (int)req.GetType());
|
||
return false;
|
||
break;
|
||
}
|
||
// 设置自定义头
|
||
std::vector<std::string> vec_header = req.GetHeader();
|
||
if (!vec_header.empty())
|
||
{
|
||
for (auto& iter : vec_header)
|
||
header = curl_slist_append(header, iter.c_str());
|
||
c = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
|
||
if (!fn_check(TO_STRING(CURLOPT_HTTPHEADER)))
|
||
return false;
|
||
}
|
||
|
||
// 创建连接
|
||
c = curl_easy_perform(curl);
|
||
if (!fn_check(TO_STRING(curl_easy_perform)))
|
||
return false;
|
||
|
||
// 获取状态码
|
||
long code;
|
||
c = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
|
||
if (!fn_check(TO_STRING(CURLINFO_RESPONSE_CODE)))
|
||
return false;
|
||
if (code < 200 || code > 299)
|
||
{
|
||
dl::log_warn("curl 请求失败{}:{}", code, req.GetUrl());
|
||
return false;
|
||
}
|
||
|
||
|
||
if (_bShowResultLog)
|
||
{
|
||
std::string str_send;
|
||
std::string str_recv;
|
||
if (true)
|
||
{
|
||
Json::DisableLog l;
|
||
str_send = Json::Dump(req.GetDataSend());
|
||
str_recv = Json::Dump(req.GetDataRecv());
|
||
}
|
||
else
|
||
{
|
||
str_send = dl::to_string(req.GetDataSend());
|
||
str_recv = dl::to_string(req.GetDataRecv());
|
||
}
|
||
std::string str_header;
|
||
for (auto& iter : req.GetHeader())
|
||
{
|
||
str_header += iter;
|
||
str_header += "\n";
|
||
}
|
||
if (!str_header.empty())
|
||
str_header.pop_back();
|
||
log_msg("---------------------curl请求完成--------------------\n"
|
||
"url:{}\n"
|
||
"send:{}\n"
|
||
"recv:{}\n"
|
||
"header: {}",
|
||
req.GetUrl(), str_send, str_recv, str_header);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
}
|
||
|
||
#endif |