ztp/http/restful.c

153 lines
4.2 KiB
C
Raw Normal View History

//
// Created by xajhu on 2019/11/18 0018.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <curl/curl.h>
#include "common.h"
#include "err_code.h"
#include "log.h"
#include "../include/restful.h"
#define SSL_CA_FILE ("/etc/ssl/certs/ca-certificates.crt")
typedef struct {
char *pReqUrl;
char sPath[MAX_PATH];
char sDlPath[MAX_PATH];
unsigned int reqResult[4096];
char *pTaskUuid;
unsigned int dlSize;
unsigned int lastTm;
unsigned int createTm;
OnHttpResponse onRspCb;
int isCancel;
CURL *pCurl;
void *pData;
int errCode;
} HTTP_REQ_PARAMS, *PHTTP_REQ_PARAMS;
static size_t __writeDataCb(void *pData, size_t size, size_t nmemb, void *pParams)
{
PHTTP_REQ_PARAMS pReq = (PHTTP_REQ_PARAMS) pParams;
int iMemSize = size * nmemb;
if (pReq->isCancel) {
return 0;
}
memcpy(pReq->reqResult + pReq->dlSize, pData, iMemSize);
pReq->dlSize += iMemSize;
return (iMemSize);
}
int http_post_request(const char *pURL, const char *pPost, OnHttpResponse onRespCb)
{
CURL *pCurl = NULL;
CURLcode ret = 0;
PHTTP_REQ_PARAMS pParams = NULL;
pParams = (PHTTP_REQ_PARAMS) malloc(sizeof(HTTP_REQ_PARAMS));
if (pParams == NULL) {
LOG_EX(LOG_Error, "Malloc %u memory error\n", sizeof(HTTP_REQ_PARAMS));
return -ERR_NOMEM;
}
ret = curl_global_init(CURL_GLOBAL_ALL);
if (ret != CURLE_OK) {
LOG_EX(LOG_Error, "Init curl global error: %d\n", ret);
free(pParams);
return -ERR_ERR;
}
pCurl = curl_easy_init();
if (!pCurl) {
LOG_EX(LOG_Error, "Init easy curl error: %d\n", ret);
curl_global_cleanup();
free(pParams);
return -ERR_ERR;
}
memset(pParams, 0, sizeof(HTTP_REQ_PARAMS));
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);
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, SSL_CA_FILE);
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
ret = curl_easy_perform(pCurl);
if (ret != CURLE_OK) {
LOG_EX(LOG_Error, "Http Post error: %u\n", ret);
}
curl_easy_cleanup(pCurl);
curl_global_cleanup();
if (onRespCb) {
onRespCb(pParams->reqResult, pParams->dlSize, pParams->pReqUrl, pParams->sPath,
pParams->pTaskUuid, -pParams->errCode, pParams->pData);
}
if (pParams->pReqUrl) {
free(pParams->pReqUrl);
}
free(pParams);
return ret;
}