2024-10-22 20:56:21 +08:00
|
|
|
|
#include "data_trans.hpp"
|
2024-10-22 19:04:25 +08:00
|
|
|
|
#include "dirent.h"
|
|
|
|
|
|
#include <stdio.h>
|
2024-10-23 17:15:40 +08:00
|
|
|
|
#include <zlog.h>
|
2024-10-22 19:04:25 +08:00
|
|
|
|
|
2024-10-23 17:15:40 +08:00
|
|
|
|
extern zlog_category_t *zct;
|
|
|
|
|
|
extern zlog_category_t *zbt;
|
|
|
|
|
|
|
|
|
|
|
|
DataTrans::DataTrans() : debug_(false) {}
|
2024-10-22 19:04:25 +08:00
|
|
|
|
|
|
|
|
|
|
DataTrans::~DataTrans() {}
|
|
|
|
|
|
|
|
|
|
|
|
static int OnDebug(CURL *, curl_infotype itype, char *pData, size_t size, void *) {
|
|
|
|
|
|
if (itype == CURLINFO_TEXT) {
|
2024-10-24 19:56:25 +08:00
|
|
|
|
zlog_debug(zbt, "[TEXT]%s", pData);
|
2024-10-22 19:04:25 +08:00
|
|
|
|
} else if (itype == CURLINFO_HEADER_IN) {
|
2024-10-23 17:15:40 +08:00
|
|
|
|
zlog_debug(zbt, "[HEADER_IN]%s", pData);
|
2024-10-22 19:04:25 +08:00
|
|
|
|
} else if (itype == CURLINFO_HEADER_OUT) {
|
2024-10-23 17:15:40 +08:00
|
|
|
|
zlog_debug(zbt, "[HEADER_OUT]%s", pData);
|
2024-10-22 19:04:25 +08:00
|
|
|
|
} else if (itype == CURLINFO_DATA_IN) {
|
2024-10-23 17:15:40 +08:00
|
|
|
|
zlog_debug(zbt, "[DATA_IN]%s", pData);
|
2024-10-22 19:04:25 +08:00
|
|
|
|
} else if (itype == CURLINFO_DATA_OUT) {
|
2024-10-23 17:15:40 +08:00
|
|
|
|
zlog_debug(zbt, "[DATA_OUT]%s", pData);
|
2024-10-24 19:56:25 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
zlog_error(zbt, "invalid itype:%d", itype);
|
2024-10-22 19:04:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static size_t OnWriteData(void *buffer, size_t size, size_t nmemb, void *lpVoid) {
|
|
|
|
|
|
std::string *str = dynamic_cast<std::string *>((std::string *)lpVoid);
|
|
|
|
|
|
if (NULL == str || NULL == buffer) {
|
2024-10-23 17:15:40 +08:00
|
|
|
|
zlog_error(zct, "[OnWriteData] str:%p, buffer:%p", str, buffer);
|
2024-10-24 19:56:25 +08:00
|
|
|
|
return 0;
|
2024-10-22 19:04:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
char *pData = (char *)buffer;
|
|
|
|
|
|
str->append(pData, size * nmemb);
|
|
|
|
|
|
return nmemb;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) {
|
2024-10-23 17:15:40 +08:00
|
|
|
|
zlog_debug(zct, "size = %d", size);
|
2024-10-22 20:56:21 +08:00
|
|
|
|
struct DownloadFile *out = (struct DownloadFile *)stream;
|
2024-10-22 19:04:25 +08:00
|
|
|
|
if (out && !out->stream) {
|
2024-10-23 17:15:40 +08:00
|
|
|
|
out->stream = fopen(out->filename, "wb");
|
2024-10-24 19:56:25 +08:00
|
|
|
|
if (!out->stream) {
|
|
|
|
|
|
zlog_error(zct, "[my_fwrite] fail to open:%s", out->filename);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
2024-10-22 19:04:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
return fwrite(buffer, size, nmemb, out->stream);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-22 20:56:21 +08:00
|
|
|
|
int DataTrans::download(char *pFilename, std::string &strUrl, std::string &strResponse, bool bDownload) {
|
2024-10-22 19:04:25 +08:00
|
|
|
|
CURL *curl = NULL;
|
|
|
|
|
|
CURLcode res;
|
2024-10-24 19:56:25 +08:00
|
|
|
|
struct DownloadFile dlfile = {pFilename, NULL}; //定义下载到本地的文件位置和路径
|
2024-10-22 19:04:25 +08:00
|
|
|
|
curl_global_init(CURL_GLOBAL_DEFAULT);
|
2024-10-24 19:56:25 +08:00
|
|
|
|
curl = curl_easy_init();
|
|
|
|
|
|
if (curl == NULL) {
|
|
|
|
|
|
zlog_error(zct, "[download] curl_easy_init failed.");
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
|
|
|
|
|
if (bDownload) {
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite); //当有数据被写入,回调函数被调用,
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &dlfile); //设置结构体的指针传递给回调函数
|
|
|
|
|
|
} else {
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
|
|
|
|
|
|
}
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8); //连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); //接收数据时超时设置,如果10秒内数据未接收完,直接退出
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_USERPWD, "SUREN:SUREN");
|
|
|
|
|
|
|
|
|
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
if (res != CURLE_OK) {
|
|
|
|
|
|
curl_global_cleanup();
|
|
|
|
|
|
zlog_error(zct, "[download] curl_easy_perform ret:%s", curl_easy_strerror(res));
|
|
|
|
|
|
return -1;
|
2024-10-22 19:04:25 +08:00
|
|
|
|
}
|
2024-10-24 19:56:25 +08:00
|
|
|
|
|
|
|
|
|
|
zlog_debug(zct, "[download] strResponse = %s", strResponse.c_str());
|
2024-10-22 19:04:25 +08:00
|
|
|
|
if (bDownload) {
|
|
|
|
|
|
if (dlfile.stream) {
|
|
|
|
|
|
fclose(dlfile.stream);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-22 20:56:21 +08:00
|
|
|
|
|
2024-10-22 19:04:25 +08:00
|
|
|
|
curl_global_cleanup();
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t writedata2file(void *ptr, size_t size, size_t nmemb, FILE *stream) {
|
|
|
|
|
|
size_t written = fwrite(ptr, size, nmemb, stream);
|
|
|
|
|
|
return written;
|
|
|
|
|
|
}
|
2024-10-22 20:56:21 +08:00
|
|
|
|
|
|
|
|
|
|
int DataTrans::dl_curl_post_req(const std::string &url, const std::string &postParams, std::string &filename) {
|
2024-10-24 19:56:25 +08:00
|
|
|
|
CURL *curl = nullptr;
|
|
|
|
|
|
FILE *fp = nullptr;
|
2024-10-22 19:04:25 +08:00
|
|
|
|
CURLcode res;
|
2024-10-24 19:56:25 +08:00
|
|
|
|
|
2024-10-22 19:04:25 +08:00
|
|
|
|
res = curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
|
|
if (CURLE_OK != res) {
|
2024-10-24 19:56:25 +08:00
|
|
|
|
zlog_error(zct, "[dl_curl_post_req] curl_global_init failed.");
|
2024-10-22 19:04:25 +08:00
|
|
|
|
curl_global_cleanup();
|
2024-10-23 17:15:40 +08:00
|
|
|
|
return 1;
|
2024-10-22 19:04:25 +08:00
|
|
|
|
}
|
2024-10-22 20:56:21 +08:00
|
|
|
|
|
2024-10-22 19:04:25 +08:00
|
|
|
|
curl = curl_easy_init();
|
2024-10-23 17:15:40 +08:00
|
|
|
|
if (curl == NULL) {
|
2024-10-24 19:56:25 +08:00
|
|
|
|
zlog_error(zct, "[dl_curl_post_req] curl_easy_init failed.");
|
2024-10-23 17:15:40 +08:00
|
|
|
|
curl_global_cleanup();
|
|
|
|
|
|
return 2;
|
2024-10-24 19:56:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-23 17:15:40 +08:00
|
|
|
|
res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
2024-10-24 19:56:25 +08:00
|
|
|
|
if (res != CURLE_OK) {
|
|
|
|
|
|
zlog_error(zct, "[dl_curl_post_req] curl_easy_setopt CURLOPT_URL failed.");
|
2024-10-23 17:15:40 +08:00
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
curl_global_cleanup();
|
|
|
|
|
|
return 3;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writedata2file);
|
2024-10-24 19:56:25 +08:00
|
|
|
|
if (res != CURLE_OK) {
|
|
|
|
|
|
zlog_error(zct, "[dl_curl_post_req] curl_easy_setopt CURLOPT_WRITEFUNCTION failed.");
|
2024-10-23 17:15:40 +08:00
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
curl_global_cleanup();
|
|
|
|
|
|
return 4;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-24 19:56:25 +08:00
|
|
|
|
fp = fopen(filename.c_str(), "wb");
|
|
|
|
|
|
if (fp == nullptr) {
|
|
|
|
|
|
zlog_error(zct, "[dl_curl_post_req] fopen:%s failed", filename.c_str());
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
curl_global_cleanup();
|
|
|
|
|
|
return 5;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-23 17:15:40 +08:00
|
|
|
|
res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
|
|
|
|
|
|
if (res != CURLE_OK) {
|
2024-10-24 19:56:25 +08:00
|
|
|
|
zlog_error(zct, "[dl_curl_post_req] curl_easy_setopt CURLOPT_WRITEDATA failed.");
|
2024-10-23 17:15:40 +08:00
|
|
|
|
fclose(fp);
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
curl_global_cleanup();
|
2024-10-24 19:56:25 +08:00
|
|
|
|
return 6;
|
2024-10-23 17:15:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
if (res != CURLE_OK) {
|
2024-10-24 19:56:25 +08:00
|
|
|
|
zlog_error(zct, "[dl_curl_post_req] curl_easy_perform() failed: %s", curl_easy_strerror(res));
|
2024-10-23 17:15:40 +08:00
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
curl_global_cleanup();
|
2024-10-24 19:56:25 +08:00
|
|
|
|
return 7;
|
2024-10-23 17:15:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
2024-10-22 19:04:25 +08:00
|
|
|
|
curl_global_cleanup();
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int DataTrans::Post(const std::string &strUrl, const std::string &strPost, std::string &strResponse) {
|
|
|
|
|
|
CURLcode res;
|
|
|
|
|
|
CURL *curl = curl_easy_init();
|
|
|
|
|
|
if (NULL == curl) {
|
2024-10-24 19:56:25 +08:00
|
|
|
|
zlog_error(zct, "[Post] curl_easy_init failed.");
|
2024-10-22 19:04:25 +08:00
|
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
|
|
}
|
2024-10-23 17:15:40 +08:00
|
|
|
|
if (debug_) {
|
2024-10-22 19:04:25 +08:00
|
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
|
|
|
|
|
|
}
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
|
|
|
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
2024-10-24 19:56:25 +08:00
|
|
|
|
if (res != CURLE_OK) {
|
|
|
|
|
|
zlog_error(zct, "[Post] curl_easy_perform() failed: %s", curl_easy_strerror(res));
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
2024-10-22 19:04:25 +08:00
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int DataTrans::Get(const std::string &strUrl, std::string &strResponse) {
|
|
|
|
|
|
CURLcode res;
|
|
|
|
|
|
CURL *curl = curl_easy_init();
|
|
|
|
|
|
if (NULL == curl) {
|
2024-10-24 19:56:25 +08:00
|
|
|
|
zlog_error(zct, "[Get] curl_easy_init failed.");
|
2024-10-22 19:04:25 +08:00
|
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
|
|
}
|
2024-10-23 17:15:40 +08:00
|
|
|
|
if (debug_) {
|
2024-10-22 19:04:25 +08:00
|
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
|
|
|
|
|
|
}
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。
|
|
|
|
|
|
* 如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
|
|
|
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
2024-10-24 19:56:25 +08:00
|
|
|
|
if (res != CURLE_OK) {
|
|
|
|
|
|
zlog_error(zct, "[Get] curl_easy_perform() failed: %s", curl_easy_strerror(res));
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
2024-10-22 19:04:25 +08:00
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int DataTrans::Posts(const std::string &strUrl, const std::string &strPost, std::string &strResponse, const char *pCaPath) {
|
|
|
|
|
|
CURLcode res;
|
|
|
|
|
|
CURL *curl = curl_easy_init();
|
|
|
|
|
|
if (NULL == curl) {
|
2024-10-24 19:56:25 +08:00
|
|
|
|
zlog_error(zct, "[Posts] curl_easy_init failed.");
|
2024-10-22 19:04:25 +08:00
|
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-23 17:15:40 +08:00
|
|
|
|
if (debug_) {
|
2024-10-22 19:04:25 +08:00
|
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
|
|
|
|
|
|
}
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
|
|
|
|
|
if (NULL == pCaPath) {
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
|
|
|
} else {
|
2024-10-24 19:56:25 +08:00
|
|
|
|
// 缺省情况就是PEM,所以无需设置,另外支持DER
|
2024-10-22 19:04:25 +08:00
|
|
|
|
// curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
|
|
|
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
2024-10-24 19:56:25 +08:00
|
|
|
|
if (res != CURLE_OK) {
|
|
|
|
|
|
zlog_error(zct, "[Posts] curl_easy_perform() failed: %s", curl_easy_strerror(res));
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
2024-10-22 19:04:25 +08:00
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int DataTrans::Gets(const std::string &strUrl, std::string &strResponse, const char *pCaPath) {
|
|
|
|
|
|
CURLcode res;
|
|
|
|
|
|
CURL *curl = curl_easy_init();
|
|
|
|
|
|
if (NULL == curl) {
|
2024-10-24 19:56:25 +08:00
|
|
|
|
zlog_error(zct, "[Gets] curl_easy_init failed.");
|
2024-10-22 19:04:25 +08:00
|
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
|
|
}
|
2024-10-23 17:15:40 +08:00
|
|
|
|
if (debug_) {
|
2024-10-22 19:04:25 +08:00
|
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
|
|
|
|
|
|
}
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
|
|
|
|
|
if (NULL == pCaPath) {
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
|
|
|
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
2024-10-24 19:56:25 +08:00
|
|
|
|
if (res != CURLE_OK) {
|
|
|
|
|
|
zlog_error(zct, "[Gets] curl_easy_perform() failed: %s", curl_easy_strerror(res));
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
2024-10-22 19:04:25 +08:00
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int DataTrans::upload_file(const std::string &strUrl, const std::string &filename, std::string &response) {
|
|
|
|
|
|
CURL *curl;
|
|
|
|
|
|
CURLM *multi_handle;
|
|
|
|
|
|
int still_running;
|
|
|
|
|
|
char res[60];
|
|
|
|
|
|
struct curl_httppost *formpost = NULL;
|
|
|
|
|
|
struct curl_httppost *lastptr = NULL;
|
|
|
|
|
|
struct curl_slist *headerlist = NULL;
|
|
|
|
|
|
static const char buf[] = "Expect:";
|
|
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
|
|
/* Fill in the file upload field. This makes libcurl load data from
|
|
|
|
|
|
the given file name when curl_easy_perform() is called. */
|
|
|
|
|
|
|
|
|
|
|
|
/* Fill in the filename field */
|
|
|
|
|
|
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "filename", CURLFORM_FILE, filename.c_str(), CURLFORM_END);
|
|
|
|
|
|
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "upload", CURLFORM_END);
|
|
|
|
|
|
|
|
|
|
|
|
curl = curl_easy_init();
|
2024-10-23 17:15:40 +08:00
|
|
|
|
if (NULL == curl) {
|
2024-10-24 19:56:25 +08:00
|
|
|
|
zlog_error(zct, "[upload_file] curl_easy_init failed.");
|
2024-10-23 17:15:40 +08:00
|
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
|
|
}
|
2024-10-24 19:56:25 +08:00
|
|
|
|
|
2024-10-22 19:04:25 +08:00
|
|
|
|
multi_handle = curl_multi_init();
|
2024-10-23 17:15:40 +08:00
|
|
|
|
if (NULL == multi_handle) {
|
2024-10-24 19:56:25 +08:00
|
|
|
|
zlog_error(zct, "[upload_file] curl_multi_init failed.");
|
2024-10-23 17:15:40 +08:00
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
|
|
}
|
2024-10-22 19:04:25 +08:00
|
|
|
|
/* initalize custom header list (stating that Expect: 100-continue is not wanted */
|
|
|
|
|
|
headerlist = curl_slist_append(headerlist, buf);
|
2024-10-23 17:15:40 +08:00
|
|
|
|
|
|
|
|
|
|
/* what URL that receives this POST */
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, res);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
|
|
|
|
|
|
curl_multi_add_handle(multi_handle, curl);
|
2024-10-24 21:17:01 +08:00
|
|
|
|
CURLMcode ret1 = curl_multi_perform(multi_handle, &still_running);
|
|
|
|
|
|
if (ret1 != CURLM_OK) {
|
|
|
|
|
|
zlog_error(zct, "[upload_file] curl_multi_perform() failed: %d", ret1);
|
|
|
|
|
|
return ret1;
|
2024-10-24 19:56:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-23 17:15:40 +08:00
|
|
|
|
do {
|
|
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
int rc;
|
|
|
|
|
|
fd_set fdread;
|
|
|
|
|
|
fd_set fdwrite;
|
|
|
|
|
|
fd_set fdexcep;
|
|
|
|
|
|
int maxfd = -1;
|
|
|
|
|
|
long curl_timeo = -1;
|
|
|
|
|
|
FD_ZERO(&fdread);
|
|
|
|
|
|
FD_ZERO(&fdwrite);
|
|
|
|
|
|
FD_ZERO(&fdexcep);
|
|
|
|
|
|
/* set a suitable timeout to play around with */
|
|
|
|
|
|
timeout.tv_sec = 1;
|
|
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
curl_multi_timeout(multi_handle, &curl_timeo);
|
|
|
|
|
|
if (curl_timeo >= 0) {
|
|
|
|
|
|
timeout.tv_sec = curl_timeo / 1000;
|
|
|
|
|
|
if (timeout.tv_sec > 1)
|
|
|
|
|
|
timeout.tv_sec = 1;
|
|
|
|
|
|
else
|
|
|
|
|
|
timeout.tv_usec = (curl_timeo % 1000) * 1000;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* get file descriptors from the transfers */
|
|
|
|
|
|
curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
|
|
|
|
|
|
/* In a real-world program you OF COURSE check the return code of the
|
|
|
|
|
|
function calls. On success, the value of maxfd is guaranteed to be
|
|
|
|
|
|
greater or equal than -1. We call select(maxfd + 1, ...), specially in
|
|
|
|
|
|
case of (maxfd == -1), we call select(0, ...), which is basically equal
|
|
|
|
|
|
to sleep. */
|
|
|
|
|
|
rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
|
|
|
|
|
|
switch (rc) {
|
|
|
|
|
|
case -1: break;
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
default:
|
|
|
|
|
|
/* timeout or readable/writable sockets */
|
|
|
|
|
|
zlog_info(zct, "perform!");
|
|
|
|
|
|
curl_multi_perform(multi_handle, &still_running);
|
|
|
|
|
|
zlog_info(zct, "running: %d!", still_running);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
} while (still_running);
|
|
|
|
|
|
|
|
|
|
|
|
curl_multi_cleanup(multi_handle);
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
curl_formfree(formpost);
|
|
|
|
|
|
curl_slist_free_all(headerlist);
|
2024-10-22 19:04:25 +08:00
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-23 17:15:40 +08:00
|
|
|
|
void DataTrans::SetDebug(bool bDebug) { debug_ = bDebug; }
|