RDK Documentation (Open Sourced RDK Components)
netSrvCurl.cpp
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 #include <string>
20 #include <iostream>
21 #include <curl/curl.h>
22 #include <glib.h>
23 #include "netSrvCurl.h"
24 #include "netsrvmgrUtiles.h"
25 
26 CurlObject::CurlObject(std::string url,char *data)
27 {
28  LOG_ENTRY_EXIT;
29 
30  CURLcode res;
31  char errbuf[CURL_ERROR_SIZE];
32  long http_code;
33 
34  curl_handle = curl_easy_init();
35  if(!curl_handle)
36  {
37  LOG_ERR("curl failed in init");
38  }
39  errbuf[0] = 0;
40  LOG_DBG("curl url is %s", url.c_str());
41  res=curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
42  if(CURLE_OK != res)
43  LOG_ERR("curl failed with curl error %d",res);
44  if(data != NULL)
45  {
46  curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, data);
47  curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, strlen(data));
48  }
49 
50  curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, errbuf);
51  errbuf[0] = 0;
52  res=curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, &CurlObject::curlwritefunc);
53  if(CURLE_OK != res)
54  LOG_ERR("curl failed with curl error %d ", res);
55  res=curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &curlDataBuffer);
56  if(CURLE_OK != res)
57  LOG_ERR("curl failed with curl error %d", res);
58  res=curl_easy_perform(curl_handle);
59  if(CURLE_OK != res)
60  {
61  LOG_ERR("curl failed with curl error %d ",res);
62  {
63  size_t len = strlen(errbuf);
64  fprintf(stderr, "\nlibcurl: (%d) ", res);
65  if(len)
66  fprintf(stderr, "%s%s", errbuf,
67  ((errbuf[len - 1] != '\n') ? "\n" : ""));
68  else
69  fprintf(stderr, "%s\n", curl_easy_strerror(res));
70  }
71  }
72  curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
73  if(http_code != 200)
74  LOG_ERR("curl failed with http error %ld",http_code);
75  //Set member variable so that we can access the http code
76  m_httpcode = http_code;
77  curl_easy_cleanup(curl_handle);
78 
79 }
80 
81 int CurlObject::curlwritefunc(char *data, size_t size, size_t nmemb, std::string *buffer) {
82  int result = 0;
83  if (buffer != NULL) {
84  buffer->append(data, size * nmemb);
85  result = size * nmemb;
86  }
87  else
88  {
89  LOG_ERR("curl buffer NULL");
90 
91  }
92  return result;
93 }
94 
95 gchar* CurlObject::getCurlData() {
96  LOG_DBG("Authservice url output %s ",curlDataBuffer.c_str());
97  return g_strdup(curlDataBuffer.c_str());
98 }
99 
100 long CurlObject::gethttpcode()
101 {
102  return m_httpcode;
103 }
104 
105 CurlObject::~CurlObject()
106 {
107 }
netsrvmgrUtiles.h
The header file provides components netSrvMgrUtiles information APIs.