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