1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <cstdio>
16 #include <string>
17 #include "curl/curl.h"
18 #include "http.h"
19 #include <securec.h>
20 #define CURLEASYGETINFO 200
HttpCurlWriteStrData(void * buffer,size_t size,size_t nmemb,void * userp)21 static size_t HttpCurlWriteStrData(void *buffer, size_t size, size_t nmemb, void *userp)
22 {
23     if (userp == nullptr || buffer == nullptr || size == 0) {
24         return 0;
25     }
26 
27     size_t realSize = size * nmemb;
28     std::string *pstr = (std::string *)userp;
29     if (pstr != nullptr) {
30         pstr->append((const char *)buffer, realSize);
31     }
32 
33     return realSize;
34 }
35 
HttpPost(std::string url,unsigned char * request,uint32_t requestLen,unsigned char * response,int32_t * responseLen,uint32_t timeout)36 int HttpPost(std::string url, unsigned char *request, uint32_t requestLen, unsigned char *response,
37     int32_t *responseLen, uint32_t timeout)
38 {
39     int ret = -1;
40     CURL *curl = curl_easy_init();
41     if (curl == nullptr) {
42         printf("curl_easy_init failed\n");
43         return -1;
44     }
45     // url
46     curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
47     // headers
48     struct curl_slist *headers = nullptr;
49     headers = curl_slist_append(headers, "Content-Type: application/json");
50     headers = curl_slist_append(headers, "charsets: utf-8");
51     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
52     // request raw data
53     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request);
54     curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, requestLen);
55     // response raw data
56     std::string tempStr;
57     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&tempStr);
58     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HttpCurlWriteStrData);
59     // timeout
60     curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
61     // http post
62 #ifndef SUPPORT_FUZZ
63     CURLcode res = curl_easy_perform(curl);
64     if (res != CURLE_OK) {
65         printf("[error] curl_easy_perform failed\n");
66         ret = -1;
67     }
68 #endif
69     long resCode;
70     curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &resCode);
71     if (resCode != CURLEASYGETINFO) {
72         printf("[error] curl_easy_getinfo failed");
73         ret = -1;
74     }
75     ret = memcpy_s(response, tempStr.size(), tempStr.c_str(), tempStr.size());
76     if (ret != 0) {
77         printf("[error] memcpy_s faild!\n");
78         return ret;
79     }
80     *responseLen = tempStr.size();
81     curl_easy_cleanup(curl);
82     curl_slist_free_all(headers);
83     return 0;
84 }
85