/******************************************************************************* ################################################################################ # Copyright (c) [2020-2021] [HCL Technologies Ltd.] # # # # Licensed under the Apache License, Version 2.0 (the "License"); # # you may not use this file except in compliance with the License. # # You may obtain a copy of the License at # # # # http://www.apache.org/licenses/LICENSE-2.0 # # # # Unless required by applicable law or agreed to in writing, software # # distributed under the License is distributed on an "AS IS" BASIS, # # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # # See the License for the specific language governing permissions and # # limitations under the License. # ################################################################################ *******************************************************************************/ /* This file contains functions to support the curl/http related operation*/ #include #include #include #include "HttpClient.hpp" /* Overloaded constructor */ HttpClient::HttpClient(string ip, string port, string username, \ string password): mServerIp(ip), mServerPort(port), \ mServerUsername(username), mServerPassword(password) { } /******************************************************************* * * @brief set options in the curl object * * @details * * Function : setCurlOptions * * Functionality: * - sets curl options in its object * * * @params[in] pointer to curl * @return true : success * false : failure ******************************************************************/ bool HttpClient::setCurlOptions(CURL *curl) { const char *method = "POST"; curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1L); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1L); curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1L); curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); // disable SSL verifications curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); curl_easy_setopt(curl, CURLOPT_PROXY_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_PROXY_SSL_VERIFYHOST, 0L); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method); return true; } /******************************************************************* * * @brief prepare HTTP Header componentes * * @details * * Function : prepareHttpHeader * * Functionality: * - prepares HTTP Header componentes and set in the * header object * * * @params[in] pointer to curl and header * @return true : success * false : failure ******************************************************************/ bool HttpClient::prepareHttpHeader(struct curl_slist *header, CURL *curl) { if(!setUserPassword(curl)) { O1_LOG("O1 VES : unable to set user:password \n"); curl_slist_free_all(header); } setCurlOptions(curl); header = curl_slist_append(header, "Content-Type: application/json"); if(!header) { O1_LOG("O1 VES : curl_slist_append failed\n"); curl_easy_cleanup(curl); return false; } header = curl_slist_append(header, "Accept: application/json"); if(!header) { O1_LOG("O1 VES : curl_slist_append failed\n"); curl_easy_cleanup(curl); return false; } curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header); if(!createUrl(curl)) { O1_LOG("O1 VES : could not create URL\n"); return false; } return true; } /******************************************************************* * * @brief set username and password into the curl header * * @details * * Function : setUserPassword * * Functionality: * - prepares username and password string and set into the * header object * * * @params[in] pointer to curl * @return true : success * false : failure ******************************************************************/ bool HttpClient::setUserPassword(CURL *curl) { //make string username:password char *credentials = 0; if((mServerUsername.c_str()) && (mServerPassword.c_str())) { asprintf(&credentials, "%s:%s", mServerUsername.c_str(), mServerPassword.c_str()); if(credentials == 0) { O1_LOG("O1 VES : credentials is blank\n"); curl_easy_cleanup(curl); return false; } } else { return false; } curl_easy_setopt(curl, CURLOPT_USERPWD, credentials); free(credentials); return true; } /******************************************************************* * * @brief creates and set URL into the curl header * * @details * * Function : createUrl * * Functionality: * - creates URL string and set into the curl * * * @params[in] pointer to curl * @return true : success * false : failure ******************************************************************/ bool HttpClient::createUrl(CURL *curl) { //make the url format -- https://10.0.2.132:8443/eventListener/v7 std::ostringstream oss; if((mServerIp.c_str()) && (mServerPort.c_str())) { oss <<"https://"<res, mem->size + realsize + 1); if(ptr == NULL) { O1_LOG("O1 VES : realloc failed"); return 0; } mem->res = ptr; memcpy(&(mem->res[mem->size]), data, realsize); mem->size += realsize; mem->res[mem->size] = 0; return realsize; } /********************************************************************** End of file **********************************************************************/