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