2019-11-19 07:39:21 +00:00
|
|
|
//
|
|
|
|
// Created by xajhu on 2019/11/18 0018.
|
|
|
|
//
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.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];
|
|
|
|
unsigned int dlSize;
|
|
|
|
OnHttpResponse onRspCb;
|
|
|
|
CURL *pCurl;
|
|
|
|
int errCode;
|
|
|
|
} HTTP_REQ_PARAMS, *PHTTP_REQ_PARAMS;
|
|
|
|
|
|
|
|
static size_t __writeDataCb(void *pData, size_t size, size_t nmemb, void *pParams)
|
|
|
|
{
|
2019-11-20 07:12:15 +00:00
|
|
|
PHTTP_REQ_PARAMS pReq = (PHTTP_REQ_PARAMS)pParams;
|
|
|
|
size_t iMemSize = size * nmemb;
|
2019-11-19 07:39:21 +00:00
|
|
|
|
|
|
|
memcpy(pReq->reqResult + pReq->dlSize, pData, iMemSize);
|
|
|
|
pReq->dlSize += iMemSize;
|
|
|
|
|
|
|
|
return (iMemSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
int http_post_request(const char *pURL, const char *pPost, OnHttpResponse onRespCb)
|
|
|
|
{
|
2019-11-21 02:53:27 +00:00
|
|
|
long flag = (long)CURL_GLOBAL_ALL;
|
2019-11-20 07:12:15 +00:00
|
|
|
int value = 1;
|
2019-11-19 07:39:21 +00:00
|
|
|
CURL *pCurl = NULL;
|
2019-11-20 07:12:15 +00:00
|
|
|
int ret = 0;
|
|
|
|
struct curl_slist *pList;
|
2019-11-19 07:39:21 +00:00
|
|
|
PHTTP_REQ_PARAMS pParams = NULL;
|
|
|
|
|
2019-11-20 07:12:15 +00:00
|
|
|
pParams = (PHTTP_REQ_PARAMS)malloc(sizeof(HTTP_REQ_PARAMS));
|
2019-11-19 07:39:21 +00:00
|
|
|
|
2019-11-20 07:12:15 +00:00
|
|
|
if(pParams == NULL) {
|
2019-11-19 07:39:21 +00:00
|
|
|
LOG_EX(LOG_Error, "Malloc %u memory error\n", sizeof(HTTP_REQ_PARAMS));
|
|
|
|
return -ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
2019-11-21 02:53:27 +00:00
|
|
|
ret = curl_global_init(flag);
|
2019-11-19 07:39:21 +00:00
|
|
|
|
2019-11-20 07:12:15 +00:00
|
|
|
if(ret != CURLE_OK) {
|
2019-11-19 07:39:21 +00:00
|
|
|
LOG_EX(LOG_Error, "Init curl global error: %d\n", ret);
|
|
|
|
free(pParams);
|
|
|
|
return -ERR_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
pCurl = curl_easy_init();
|
|
|
|
|
2019-11-20 07:12:15 +00:00
|
|
|
if(!pCurl) {
|
2019-11-19 07:39:21 +00:00
|
|
|
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;
|
2019-11-20 07:12:15 +00:00
|
|
|
pParams->pReqUrl = (char *)malloc(strlen(pURL) + 1);
|
2019-11-19 07:39:21 +00:00
|
|
|
pParams->dlSize = 0;
|
|
|
|
pParams->pCurl = pCurl;
|
|
|
|
|
|
|
|
memset(pParams->pReqUrl, 0, strlen(pURL) + 1);
|
|
|
|
strcpy(pParams->pReqUrl, pURL);
|
|
|
|
|
2019-11-20 07:12:15 +00:00
|
|
|
pList = curl_slist_append(NULL, "Content-Type:application/json;charset=UTF-8");
|
|
|
|
|
2019-11-19 07:39:21 +00:00
|
|
|
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);
|
2019-11-20 07:12:15 +00:00
|
|
|
curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, value);
|
2019-11-19 07:39:21 +00:00
|
|
|
curl_easy_setopt(pCurl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
|
2019-11-20 07:12:15 +00:00
|
|
|
curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, pList);
|
2019-11-19 07:39:21 +00:00
|
|
|
|
2019-11-20 07:12:15 +00:00
|
|
|
if(pPost != NULL && strlen(pPost) > 0) {
|
2019-11-19 07:39:21 +00:00
|
|
|
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, pPost);
|
2019-11-20 07:12:15 +00:00
|
|
|
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDSIZE, (long)strlen(pPost));
|
2019-11-19 07:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#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);
|
2019-11-20 07:12:15 +00:00
|
|
|
curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, value);
|
2019-11-19 07:39:21 +00:00
|
|
|
#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);
|
|
|
|
|
2019-11-20 07:12:15 +00:00
|
|
|
if(ret != CURLE_OK) {
|
2019-11-19 07:39:21 +00:00
|
|
|
LOG_EX(LOG_Error, "Http Post error: %u\n", ret);
|
|
|
|
}
|
|
|
|
|
2019-11-20 07:12:15 +00:00
|
|
|
curl_slist_free_all(pList);
|
2019-11-19 07:39:21 +00:00
|
|
|
curl_easy_cleanup(pCurl);
|
|
|
|
curl_global_cleanup();
|
|
|
|
|
2019-11-20 07:12:15 +00:00
|
|
|
if(onRespCb) {
|
2019-11-19 07:39:21 +00:00
|
|
|
onRespCb(pParams->reqResult, pParams->dlSize, pParams->pReqUrl, pParams->sPath,
|
2019-11-20 07:12:15 +00:00
|
|
|
NULL, -pParams->errCode, NULL);
|
2019-11-19 07:39:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-20 07:12:15 +00:00
|
|
|
if(pParams->pReqUrl) {
|
2019-11-19 07:39:21 +00:00
|
|
|
free(pParams->pReqUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(pParams);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|