Examples to improve code.
[bpdyndnsd] / src / httphelper.cpp
CommitLineData
efbde536
BS
1/** @file
2 * @brief HTTPHelper class implementation. This class represents a Helper to perform HTTP operations easily.
3 *
4 *
5 *
6 * @copyright Intra2net AG
7 * @license GPLv2
8*/
9
10#include "httphelper.h"
11
ca5d6889 12using namespace std;
d5a516ba
BS
13
14/**
7de01277 15 * Constructor. Use this constructor if HTTP AUTH should be used. Username and password will then be set as HTTP auth options.
d5a516ba
BS
16 * @param _log Logger Object
17 * @param _proxy Proxy to use
18 * @param _proxy_port Proxy Port
19 * @param _username Username
20 * @param _password Password
21 */
2dd2db3e 22HTTPHelper::HTTPHelper(Logger::Ptr _log, const string& _proxy, const int _proxy_port, const string& _username, const string& _password)
6ec86aff
TJ
23 : Log(_log)
24 , Proxy(_proxy)
25 , ProxyPort(_proxy_port)
efbde536 26{
efbde536 27 CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
2dd2db3e 28 set_curl_auth(_username,_password);
efbde536
BS
29}
30
31
d5a516ba 32/**
7de01277
BS
33 * Constructor. Use this constructor if you have to encode the username and password into the url
34 * @param _log Logger Object
35 * @param _proxy Proxy to use
36 * @param _proxy_port Proxy Port
37 */
38HTTPHelper::HTTPHelper(Logger::Ptr _log, const string& _proxy, const int _proxy_port)
6ec86aff
TJ
39 : Log(_log)
40 , Proxy(_proxy)
41 , ProxyPort(_proxy_port)
7de01277 42{
7de01277
BS
43 CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
44}
45
46
47/**
d5a516ba
BS
48 * Destructor
49 */
efbde536
BS
50HTTPHelper::~HTTPHelper()
51{
891ae3b9
BS
52 // Free memory
53 curl_easy_cleanup(CurlEasyHandle);
efbde536
BS
54}
55
56
efbde536
BS
57/**
58 * Perform a HTTP GET operation
59 * @param url URL for HTTP GET operation
d5a516ba 60 * @return The status code from the http operation or -1 if an curl error occurs
efbde536 61 */
d5a516ba 62long HTTPHelper::http_get(const string& url)
efbde536
BS
63{
64 int curl_err_code;
d5a516ba 65 long curl_info;
efbde536
BS
66
67 set_curl_url(url);
68
69 if ( (curl_err_code = curl_easy_perform(CurlEasyHandle) ) != 0 )
70 {
d5a516ba 71 Log->print_curl_error(url,curl_err_code,CurlErrBuff);
1fda5599 72 CurlWritedataBuff.clear();
d5a516ba
BS
73 return -1;
74 }
75 if ( (curl_err_code = curl_easy_getinfo(CurlEasyHandle,CURLINFO_RESPONSE_CODE,&curl_info)) != 0 )
76 {
3ef770e2 77 Log->print_curl_error(url,curl_err_code);
1fda5599 78 CurlWritedataBuff.clear();
d5a516ba 79 return -1;
efbde536 80 }
d5a516ba
BS
81
82 Log->print_curl_data(CurlWritedataBuff);
83
1fda5599
BS
84 // Copy the received data received via curl from the curl data buffer member to the received data member. This is needed because curl appends data to the buffer rather than overrites it.
85 ReceivedCurlData = CurlWritedataBuff;
86 CurlWritedataBuff.clear();
87
d5a516ba
BS
88 // Operation performed without any problems so we can return the curl_info
89 return curl_info;
efbde536
BS
90}
91
92
93/**
b6228761
BS
94 * Getter for member CurlWritedataBuff
95 * @return CurlWritedataBuff
96 */
97string HTTPHelper::get_curl_data() const
98{
1fda5599 99 return ReceivedCurlData;
b6228761
BS
100}
101
102
103/**
efbde536
BS
104 * Initialized curl easy handle with a few options.
105 * @param curl_writedata_buff Reference to a string wich will be filled with the curl result
106 * @param curl_err_buff A pointer to an char array which will be filled with error message.
107 * @return A pointer to the easy curl handle.
108 */
109CURL* HTTPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff) const
110{
2d91d15e 111 string user_agent = "Bullet Proof DYNDNS Daemon 0.1.1 - Intra2net AG 2009";
b6228761 112
efbde536
BS
113 CURL *curl_easy_handle = curl_easy_init();
114
115 curl_easy_setopt(curl_easy_handle,CURLOPT_NOPROGRESS,1);
116 curl_easy_setopt(curl_easy_handle,CURLOPT_CONNECTTIMEOUT,5);
117 curl_easy_setopt(curl_easy_handle,CURLOPT_TIMEOUT,10);
118 curl_easy_setopt(curl_easy_handle,CURLOPT_BUFFERSIZE,1024);
119 curl_easy_setopt(curl_easy_handle,CURLOPT_ERRORBUFFER,curl_err_buff);
120 curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEFUNCTION,http_receive);
121 curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEDATA,&curl_writedata_buff);
b6228761 122 curl_easy_setopt(curl_easy_handle,CURLOPT_USERAGENT,&user_agent);
efbde536
BS
123
124 if ( !Proxy.empty() )
125 {
126 curl_easy_setopt(curl_easy_handle,CURLOPT_PROXY,Proxy.c_str());
127 curl_easy_setopt(curl_easy_handle,CURLOPT_PROXYPORT,ProxyPort);
128 }
129
130 return curl_easy_handle;
131}
132
133
134/**
135 * Sets a url to the easy curl handle
136 * @param url The url to set.
137 */
138void HTTPHelper::set_curl_url(const string& url)
139{
140 curl_easy_setopt(CurlEasyHandle,CURLOPT_URL,url.c_str());
141}
142
143
144/**
2dd2db3e
BS
145 * Sets HTTP AUTH parameters
146 * @param username The username for HTTP AUTH
147 * @param password The password for HTTP AUTH
148 */
149void HTTPHelper::set_curl_auth(const string& username, const string& password)
150{
151 curl_easy_setopt(CurlEasyHandle,CURLOPT_USERNAME,username.c_str());
152 curl_easy_setopt(CurlEasyHandle,CURLOPT_PASSWORD,password.c_str());
153}
154
155
156/**
efbde536
BS
157 * Callback Function is called every time CURL is receiving data from HTTPS-Server and will copy all received Data to the given stream pointer
158 * @param inBuffer Pointer to input.
159 * @param size How many mem blocks are received
160 * @param nmemb size of each memblock
161 * @param outBuffer Pointer to output stream.
162 * @return The size received.
163 */
164int HTTPHelper::http_receive( char *inBuffer, size_t size, size_t nmemb, string *outBuffer )
165{
166 outBuffer->append(inBuffer);
167 return (size*nmemb);
168}
169