-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClient.cpp
More file actions
227 lines (183 loc) · 4.34 KB
/
Copy pathHttpClient.cpp
File metadata and controls
227 lines (183 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "HttpClient.h"
#include "SysConfig.h"
#include "Config.h"
#include <libutils/FileUtils.h>
#include <utility>
namespace OPI
{
HttpClient::HttpClient(const string& host, bool verifyca): host(host),port(0), timeout(0), verifyca(verifyca)
{
this->curl = curl_easy_init();
if( ! this->curl )
{
throw runtime_error("Unable to init Curl");
}
try
{
this->defaultca = SysConfig().GetKeyAsString("hostinfo", "cafile");
string rpca = Utils::File::RealPath(this->defaultca);
if( ! Utils::File::FileExists(rpca) )
{
// Config points at none existant file, reset ca
this->defaultca = "";
}
}
catch (std::exception& err)
{
(void) err;
// Is could ok to not have this, http client should not fail if we miss sysconfig
this->defaultca = "";
}
}
HttpClient::~HttpClient()
{
curl_easy_cleanup( this->curl );
}
void HttpClient::CurlPre()
{
curl_easy_reset( this->curl );
this->body.str("");
if( verifyca )
{
if( this->defaultca != "" )
{
curl_easy_setopt(this->curl, CURLOPT_CAINFO, this->defaultca.c_str() );
}
if( this->capath != "" )
{
curl_easy_setopt(this->curl, CURLOPT_CAPATH, this->capath.c_str() );
}
}
else
{
curl_easy_setopt(this->curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(this->curl, CURLOPT_SSL_VERIFYHOST, 0L);
}
curl_easy_setopt(this->curl, CURLOPT_WRITEFUNCTION, HttpClient::WriteCallback );
curl_easy_setopt(this->curl, CURLOPT_WRITEDATA, (void *)this);
// Override protocol default port
if( port != 0 )
{
curl_easy_setopt(this->curl, CURLOPT_PORT, this->port);
}
if( this->timeout != 0)
{
curl_easy_setopt(this->curl, CURLOPT_CONNECTTIMEOUT, this->timeout);
}
}
/*
*Set headers for next request, headers reset after request
*/
void HttpClient::CurlSetHeaders(const map<string, string>& headers)
{
this->headers = headers;
}
string HttpClient::DoGet(const string& path, const map<string, string>& data)
{
this->CurlPre();
string url = this->host+path+"?"+this->MakeFormData(data);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
return this->CurlPerform();
}
string HttpClient::DoPost(const string& path, const map<string, string>& data)
{
this->CurlPre();
string url = this->host+path;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
string poststring = this->MakeFormData(data);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, poststring.c_str() );
return this->CurlPerform();
}
string HttpClient::CurlPerform()
{
this->setheaders();
CURLcode res = curl_easy_perform(curl);
this->clearheaders();
if(res != CURLE_OK)
{
throw runtime_error( curl_easy_strerror(res) );
}
res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &this->result_code);
if(res != CURLE_OK)
{
throw runtime_error( curl_easy_strerror(res) );
}
return this->body.str();
}
string HttpClient::MakeFormData(const map<string, string>& data)
{
stringstream postdata;
bool first = true;
for(const auto& arg: data )
{
if (!first)
{
postdata << "&";
}
else
{
first = false;
}
postdata << this->EscapeString(arg.first) << "=" << this->EscapeString(arg.second);
}
return postdata.str();
}
string HttpClient::EscapeString(const string &arg)
{
char *tmparg = curl_easy_escape(curl, arg.c_str(), arg.length());
if( ! tmparg )
{
throw runtime_error("Failed to escape url");
}
string escarg(tmparg);
curl_free(tmparg);
return escarg;
}
size_t HttpClient::WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
HttpClient* serv = static_cast<HttpClient*>(userp);
serv->body.write((char*)contents, size*nmemb);
return size*nmemb;
}
void HttpClient::setheaders()
{
if( this->headers.size() > 0 )
{
this->slist = nullptr;
for(const auto& h: this->headers )
{
string header = h.first+ ":" + h.second;
this->slist = curl_slist_append( this->slist, header.c_str() );
if( ! this->slist )
{
throw runtime_error("Failed to append custom header");
}
}
curl_easy_setopt( this->curl , CURLOPT_HTTPHEADER, this->slist);
}
}
void HttpClient::clearheaders()
{
if( this->headers.size() > 0 )
{
curl_slist_free_all( this->slist );
this->headers.clear();
}
}
void HttpClient::setPort(long value)
{
port = value;
}
void HttpClient::setTimeout(long value)
{
this->timeout = value;
}
void HttpClient::setDefaultCA(const string &path)
{
this->defaultca = path;
}
void HttpClient::setCAPath(const string &path)
{
this->capath = path;
}
} // End NS