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