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 ip, string port, string username, \
28 string password): mServerIp(ip), mServerPort(port), \
29 mServerUsername(username), 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("O1 VES : unable to set user:password \n");
93 curl_slist_free_all(header);
97 header = curl_slist_append(header, "Content-Type: application/json");
99 O1_LOG("O1 VES : curl_slist_append failed\n");
100 curl_easy_cleanup(curl);
104 header = curl_slist_append(header, "Accept: application/json");
106 O1_LOG("O1 VES : curl_slist_append failed\n");
107 curl_easy_cleanup(curl);
110 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
113 O1_LOG("O1 VES : could not create URL\n");
119 /*******************************************************************
121 * @brief set username and password into the curl header
125 * Function : setUserPassword
128 * - prepares username and password string and set into the
132 * @params[in] pointer to curl
133 * @return true : success
135 ******************************************************************/
138 bool HttpClient::setUserPassword(CURL *curl)
140 //make string username:password
141 char *credentials = 0;
142 if((mServerUsername.c_str()) && (mServerPassword.c_str())) {
143 asprintf(&credentials, "%s:%s", mServerUsername.c_str(), mServerPassword.c_str());
144 if(credentials == 0) {
145 O1_LOG("O1 VES : credentials is blank\n");
146 curl_easy_cleanup(curl);
153 curl_easy_setopt(curl, CURLOPT_USERPWD, credentials);
158 /*******************************************************************
160 * @brief creates and set URL into the curl header
164 * Function : createUrl
167 * - creates URL string and set into the curl
170 * @params[in] pointer to curl
171 * @return true : success
173 ******************************************************************/
176 bool HttpClient::createUrl(CURL *curl)
178 //make the url format -- https://10.0.2.132:8443/eventListener/v7
180 std::ostringstream oss;
181 if((mServerIp.c_str()) && (mServerPort.c_str())) {
182 oss <<"https://"<<mServerIp<<":"<<mServerPort<<"/eventListener/v7";
183 string url = oss.str();
184 O1_LOG("O1 VES : URL=%s\n", url.c_str());
185 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
194 /*******************************************************************
196 * @brief sends HTTP Request with help of curl API
200 * Function : sendHttpRequest
203 * - set curl callback function
204 * - sends HTTP Request with help of curl API
207 * @params[in] pointer to curl, pointer to header
208 * @return true : success
210 ******************************************************************/
213 bool HttpClient::sendHttpRequest(struct curl_slist *header, CURL *curl)
215 struct response response_data = {0};
217 int *response_code = 0;
219 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCb);
220 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response_data);
223 CURLcode res = curl_easy_perform(curl);
224 curl_slist_free_all(header);
226 if(res != CURLE_OK) {
227 O1_LOG("O1 VES : could not send data , received error: %s\n", \
228 curl_easy_strerror(res));
229 curl_easy_cleanup(curl);
235 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_rc);
236 *response_code = http_rc;
240 *recvData = response_data.res;
245 /*******************************************************************
247 * @brief sends VES event to VES collector
254 * - prepare curl header
255 * - sends VES event with help of curl API
258 * @params[in] pointer to data buffer
259 * @return true : success
261 ******************************************************************/
263 bool HttpClient::send(const char *sendData)
267 struct curl_slist *header = 0;
268 const char *sendMsg = sendData;
273 CURL *curl = curl_easy_init();
275 O1_LOG("O1 VES : could not initialize CURL\n");
279 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, sendMsg);
280 if(!prepareHttpHeader(header, curl))
282 O1_LOG("O1 VES : could not prepare HTTP header\n");
283 curl_easy_cleanup(curl);
286 else if(!sendHttpRequest(header, curl))
288 O1_LOG("O1 VES : could not send HTTP request\n");
291 curl_easy_cleanup(curl);
295 /*******************************************************************
297 * @brief callback of VES event send
304 * - callback function of HTTP curl request
308 * @return size of return buffer : success
310 ******************************************************************/
312 size_t HttpClient::writeCb(void *data, size_t size, size_t nmemb, void *userp) {
313 O1_LOG("O1 VES : write Callback called\n");
314 size_t realsize = size * nmemb;
315 struct response *mem = (struct response *)userp;
317 char *ptr = (char *) realloc(mem->res, mem->size + realsize + 1);
319 O1_LOG("O1 VES : realloc failed");
324 memcpy(&(mem->res[mem->size]), data, realsize);
325 mem->size += realsize;
326 mem->res[mem->size] = 0;
331 /**********************************************************************
333 **********************************************************************/