OCT 1. 增加 HTTP POST 接口调用功能

2. 增加中断HTTP下载文件功能
This commit is contained in:
huangxin 2022-12-13 17:14:19 +08:00
parent 2f1349d8d8
commit a6bc3c38e9
2 changed files with 113 additions and 3 deletions

View File

@ -28,6 +28,9 @@ const char *inet_download_file_async(const char *pURL,
on_progress_changed onProgressCb,
void *pData);
const char *inet_http_post_async(const char *pURL, const char *pPost, on_http_response onRespCb, void *pData);
int inet_cancel_dl(const char *pTaskUuid);
#ifdef __cplusplus
}
#endif

View File

@ -14,6 +14,7 @@
#include "misc.h"
#include "uthash/uthash.h"
#include "task_manager.h"
#include "user_errno.h"
#define MAX_TIMEOUT_VALUE (300)
@ -417,19 +418,19 @@ static size_t writeDataCb(void *pData, size_t size, size_t nmemb, void *pParams)
size_t newSize;
if (pReq->uvFsBuf.base == NULL && pReq->uvFsBuf.len == 0) {
newSize = iMemSize + 1;
newSize = iMemSize + 1;
//fprintf(stdout, "size = %d, newsize = %d, dlsize = %d\n", iMemSize, newSize, pReq->dlSize);
pReq->uvFsBuf.base = malloc(newSize);
memcpy(pReq->uvFsBuf.base, pData, iMemSize);
} else {
newSize = pReq->dlSize + iMemSize + 1;
newSize = pReq->dlSize + iMemSize + 1;
//fprintf(stdout, "::size = %d, newsize = %d, dlsize = %d\n", iMemSize, newSize, pReq->dlSize);
pReq->uvFsBuf.base = realloc(pReq->uvFsBuf.base, newSize);
memcpy(pReq->uvFsBuf.base + pReq->dlSize, pData, iMemSize);
}
pReq->uvFsBuf.base[pReq->dlSize] = 0;
pReq->dlSize += iMemSize;
pReq->dlSize += iMemSize;
}
return (size * nmemb);
@ -554,6 +555,29 @@ static void onDlTimeoutCb(uv_timer_t *UNUSED(pufTimer)) {
}
}
int inet_cancel_dl(const char *pTaskUuid) {
if (pTaskUuid && strlen(pTaskUuid) > 0) {
PCURL_HANDLE_TBL pItem = NULL;
HASH_FIND_STR(g_ReqHandleTbl, pTaskUuid, pItem);
if (pItem != NULL && pItem->pCurlItem->isCancel != TRUE) {
cancelDownloadTask(pItem->pCurlItem);
if (pItem->pCurlItem->onRspCb) {
pItem->pCurlItem->onRspCb(NULL,
pItem->pCurlItem->dlSize,
pItem->pCurlItem->pReqUrl,
pItem->pCurlItem->sPath,
pItem->pCurlItem->pTaskUuid,
-CURLE_OPERATION_TIMEDOUT,
pItem->pCurlItem->pData);
}
}
}
return ERR_SUCCESS;
}
const char *inet_download_file_async(const char *pURL,
const char *pPath,
on_http_response onRespCb,
@ -681,6 +705,89 @@ const char *inet_download_file_async(const char *pURL,
}
}
const char *inet_http_post_async(const char *pURL, const char *pPost, on_http_response onRespCb, void *pData) {
int ret;
uuid_t msgId;
char strMsgId[64];
PHTTP_REQ_PARAMS pParams = (PHTTP_REQ_PARAMS)malloc(sizeof(HTTP_REQ_PARAMS));
CURL *pCurl = NULL;
if (pURL == NULL || strlen(pURL) == 0 || onRespCb == NULL) {
free(pParams);
return (NULL);
}
memset(pParams, 0, sizeof(HTTP_REQ_PARAMS));
pCurl = curl_easy_init();
pParams->onRspCb = onRespCb;
pParams->pReqUrl = (char *)malloc(strlen(pURL) + 1);
pParams->type = INET_HTTP_WEBSERVICE_POST;
pParams->dlSize = 0;
pParams->pData = pData;
pParams->pCurl = pCurl;
pParams->lastTm = 0;
pParams->isCancel = FALSE;
memset(pParams->pReqUrl, 0, strlen(pURL) + 1);
strcpy(pParams->pReqUrl, pURL);
uuid_generate_random(msgId);
memset(strMsgId, 0, 64);
uuid_unparse_lower(msgId, strMsgId);
pParams->pTaskUuid = strdup(strMsgId);
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, writeDataCb);
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, pParams);
curl_easy_setopt(pCurl, CURLOPT_PRIVATE, pParams);
curl_easy_setopt(pCurl, CURLOPT_URL, pURL);
curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(pCurl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
if (pPost != NULL && strlen(pPost) > 0) {
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, pPost);
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDSIZE, (long)strlen(pPost));
}
#ifdef SKIP_PEER_VERIFICATION
/*
* If you want to connect to a site who isn't using a certificate that is
* signed by one of the certs in the CA bundle you have, you can skip the
* verification of the server's certificate. This makes the connection
* A LOT LESS SECURE.
*
* If you have a CA cert for the server stored someplace else than in the
* default bundle, then the CURLOPT_CAPATH option might come handy for
* you.
*/
curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0L);
#else
curl_easy_setopt(pCurl, CURLOPT_CAINFO, config_get_ssl_ca_path());
curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 1L);
#endif
#ifdef SKIP_HOSTNAME_VERIFICATION
/*
* If the site you're connecting to uses a different host name that what
* they have mentioned in their server certificate's commonName (or
* subjectAltName) fields, libcurl will refuse to connect. You can skip
* this check, but this will make the connection less secure.
*/
curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
dzlog_debug("Http POST(%u): %s --> %p\n", g_TotalDownloads++, pParams->pTaskUuid, pCurl);
ret = curl_multi_add_handle(g_pCurl, pCurl);
if (ret == CURLE_OK) {
addReqIdToTable(pParams->pTaskUuid, pParams);
return (pParams->pTaskUuid);
} else {
free(pParams->pTaskUuid);
dzlog_error("Add Handle Error: %d\n", ret);
return NULL;
}
}
int inet_api_init(void) {
int ret = 0;