Introduced HTTPHelper class to easily perform HTTP operations within services.
[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 HTTPHelper::HTTPHelper()
13     : Proxy("")
14     , ProxyPort(0)
15     , Log(new Logger)
16 {
17 }
18
19 HTTPHelper::HTTPHelper(Logger::Ptr _log, string _proxy, int _proxy_port)
20 {
21     Log = _log;
22     Proxy = _proxy;
23     ProxyPort = _proxy_port;
24     CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
25 }
26
27
28 HTTPHelper::~HTTPHelper()
29 {
30 }
31
32
33
34 /**
35  * Perform a HTTP GET operation
36  * @param url URL for HTTP GET operation
37  * @return The data GET from the URL
38  */
39 string HTTPHelper::http_get(const string& url)
40 {
41     int curl_err_code;
42
43     set_curl_url(url);
44
45     if ( (curl_err_code = curl_easy_perform(CurlEasyHandle) ) != 0 )
46     {
47         // TODO: Logging errors
48         return "";
49     }
50     // Operation performed without any problems so we can return the CurlWritedataBuff
51     return CurlWritedataBuff;
52 }
53
54
55 /**
56  * Initialized curl easy handle with a few options.
57  * @param curl_writedata_buff Reference to a string wich will be filled with the curl result
58  * @param curl_err_buff A pointer to an char array which will be filled with error message.
59  * @return A pointer to the easy curl handle.
60  */
61 CURL* HTTPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff) const
62 {
63     CURL *curl_easy_handle = curl_easy_init();
64
65     curl_easy_setopt(curl_easy_handle,CURLOPT_NOPROGRESS,1);
66     curl_easy_setopt(curl_easy_handle,CURLOPT_CONNECTTIMEOUT,5);
67     curl_easy_setopt(curl_easy_handle,CURLOPT_TIMEOUT,10);
68     curl_easy_setopt(curl_easy_handle,CURLOPT_BUFFERSIZE,1024);
69     curl_easy_setopt(curl_easy_handle,CURLOPT_ERRORBUFFER,curl_err_buff);
70     curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEFUNCTION,http_receive);
71     curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEDATA,&curl_writedata_buff);
72
73     if ( !Proxy.empty() )
74     {
75         curl_easy_setopt(curl_easy_handle,CURLOPT_PROXY,Proxy.c_str());
76         curl_easy_setopt(curl_easy_handle,CURLOPT_PROXYPORT,ProxyPort);
77     }
78
79     return curl_easy_handle;
80 }
81
82
83 /**
84  * Sets a url to the easy curl handle
85  * @param url The url to set.
86  */
87 void HTTPHelper::set_curl_url(const string& url)
88 {
89     curl_easy_setopt(CurlEasyHandle,CURLOPT_URL,url.c_str());
90 }
91
92
93 /**
94  * Callback Function is called every time CURL is receiving data from HTTPS-Server and will copy all received Data to the given stream pointer
95  * @param inBuffer Pointer to input.
96  * @param size How many mem blocks are received
97  * @param nmemb size of each memblock
98  * @param outBuffer Pointer to output stream.
99  * @return The size received.
100  */
101 int HTTPHelper::http_receive( char *inBuffer, size_t size, size_t nmemb, string *outBuffer )
102 {
103     outBuffer->append(inBuffer);
104     return (size*nmemb);
105 }
106