1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2020-2021] [HCL Technologies Ltd.] #
5 # Licensed under the Apache License, Version 2.0 (the "License"); #
6 # you may not use this file except in compliance with the License. #
7 # You may obtain a copy of the License at #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
11 # Unless required by applicable law or agreed to in writing, software #
12 # distributed under the License is distributed on an "AS IS" BASIS, #
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
14 # See the License for the specific language governing permissions and #
15 # limitations under the License. #
16 ################################################################################
17 *******************************************************************************/
19 /* This file contains functions to support the curl/http related operation*/
24 #include "HttpClient.hpp"
26 /* Overloaded constructor */
27 HttpClient::HttpClient(string url, string username, string password):
28 mUrl(url), mServerUsername(username),
29 mServerPassword(password)
34 /*******************************************************************
36 * @brief set options in the curl object
40 * Function : setCurlOptions
43 * - sets curl options in its object
46 * @params[in] pointer to curl
47 * @return true : success
49 ******************************************************************/
51 bool HttpClient::setCurlOptions(CURL *curl)
53 const char *method = "POST";
54 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1L);
55 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1L);
56 curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1L);
57 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
59 // disable SSL verifications
60 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
61 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
62 curl_easy_setopt(curl, CURLOPT_PROXY_SSL_VERIFYPEER, 0L);
63 curl_easy_setopt(curl, CURLOPT_PROXY_SSL_VERIFYHOST, 0L);
65 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method);
69 /*******************************************************************
71 * @brief prepare HTTP Header componentes
75 * Function : prepareHttpHeader
78 * - prepares HTTP Header componentes and set in the
82 * @params[in] pointer to curl and header
83 * @return true : success
85 ******************************************************************/
87 bool HttpClient::prepareHttpHeader(struct curl_slist *header, CURL *curl)
90 if(!setUserPassword(curl))
92 O1_LOG("\nO1 HttpClient : unable to set user:password");
93 curl_slist_free_all(header);
97 header = curl_slist_append(header, "Content-Type: application/json");
99 O1_LOG("\nO1 HttpClient : curl_slist_append failed");
100 curl_easy_cleanup(curl);
104 header = curl_slist_append(header, "Accept: application/json");
106 O1_LOG("\nO1 HttpClient : curl_slist_append failed");
107 curl_easy_cleanup(curl);
110 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
112 curl_easy_setopt(curl, CURLOPT_URL, mUrl.c_str());
117 /*******************************************************************
119 * @brief set username and password into the curl header
123 * Function : setUserPassword
126 * - prepares username and password string and set into the
130 * @params[in] pointer to curl
131 * @return true : success
133 ******************************************************************/
136 bool HttpClient::setUserPassword(CURL *curl)
138 //make string username:password
139 char *credentials = 0;
140 if((mServerUsername.c_str()) && (mServerPassword.c_str())) {
141 asprintf(&credentials, "%s:%s", mServerUsername.c_str(), mServerPassword.c_str());
142 if(credentials == 0) {
143 O1_LOG("\nO1 HttpClient : Credential is blank");
144 curl_easy_cleanup(curl);
151 curl_easy_setopt(curl, CURLOPT_USERPWD, credentials);
157 /*******************************************************************
159 * @brief sends HTTP Request with help of curl API
163 * Function : sendHttpRequest
166 * - set curl callback function
167 * - sends HTTP Request with help of curl API
170 * @params[in] pointer to curl, pointer to header
171 * @return true : success
173 ******************************************************************/
176 bool HttpClient::sendHttpRequest(struct curl_slist *header, CURL *curl)
178 struct response response_data = {0};
180 int *response_code = 0;
182 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCb);
183 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response_data);
186 CURLcode res = curl_easy_perform(curl);
187 curl_slist_free_all(header);
189 if(res != CURLE_OK) {
190 O1_LOG("\nO1 HttpClient : Could not send data, error: %s", \
191 curl_easy_strerror(res));
192 curl_easy_cleanup(curl);
198 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_rc);
199 *response_code = http_rc;
203 *recvData = response_data.res;
208 /*******************************************************************
210 * @brief sends VES event to VES collector
217 * - prepare curl header
218 * - sends VES event with help of curl API
221 * @params[in] pointer to data buffer
222 * @return true : success
224 ******************************************************************/
226 bool HttpClient::send(const char *sendData)
230 struct curl_slist *header = 0;
231 const char *sendMsg = sendData;
236 CURL *curl = curl_easy_init();
238 O1_LOG("\nO1 HttpClient : could not initialize CURL");
242 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, sendMsg);
243 if(!prepareHttpHeader(header, curl))
245 O1_LOG("\nO1 HttpClient : could not prepare HTTP header");
246 curl_easy_cleanup(curl);
249 else if(!sendHttpRequest(header, curl))
251 O1_LOG("\nO1 HttpClient : could not send HTTP request");
254 curl_easy_cleanup(curl);
258 /*******************************************************************
260 * @brief callback of VES event send
267 * - callback function of HTTP curl request
271 * @return size of return buffer : success
273 ******************************************************************/
275 size_t HttpClient::writeCb(void *data, size_t size, size_t nmemb, void *userp) {
276 O1_LOG("\nO1 HttpClient : write Callback called");
277 size_t realsize = size * nmemb;
278 struct response *mem = (struct response *)userp;
280 char *ptr = (char *) realloc(mem->res, mem->size + realsize + 1);
282 O1_LOG("\nO1 HttpClient : realloc failed");
287 memcpy(&(mem->res[mem->size]), data, realsize);
288 mem->size += realsize;
289 mem->res[mem->size] = 0;
294 /**********************************************************************
296 **********************************************************************/