From a6bc3c38e9d91f81ad98f42632279e9d362dd972 Mon Sep 17 00:00:00 2001 From: huangxin Date: Tue, 13 Dec 2022 17:14:19 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20=E5=A2=9E=E5=8A=A0=20HTTP=20POST=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=B0=83=E7=94=A8=E5=8A=9F=E8=83=BD=202.=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=AD=E6=96=ADHTTP=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/libs/include/inet_misc.h | 3 + srcs/libs/network/inet_misc.c | 113 +++++++++++++++++++++++++++++++++- 2 files changed, 113 insertions(+), 3 deletions(-) diff --git a/srcs/libs/include/inet_misc.h b/srcs/libs/include/inet_misc.h index 7a50532..26b5a76 100644 --- a/srcs/libs/include/inet_misc.h +++ b/srcs/libs/include/inet_misc.h @@ -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 diff --git a/srcs/libs/network/inet_misc.c b/srcs/libs/network/inet_misc.c index ff7cd4f..cd82c3b 100644 --- a/srcs/libs/network/inet_misc.c +++ b/srcs/libs/network/inet_misc.c @@ -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;