forked from mrtazz/restclient-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestclient.cpp
More file actions
536 lines (493 loc) · 15.8 KB
/
restclient.cpp
File metadata and controls
536 lines (493 loc) · 15.8 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/**
* @file restclient.cpp
* @brief implementation of the restclient class
* @author Daniel Schauenberg <d@unwiredcouch.com>
*/
/*========================
INCLUDES
========================*/
#include "restclient-cpp/restclient.h"
#include "restclient-cpp/meta.h"
#include <curl/curl.h>
#include <cstring>
#include <string>
#include <iostream>
#include <map>
/** initialize user agent string */
const char* RestClient::user_agent = "restclient-cpp/" VERSION;
/** initialize authentication variable */
std::string RestClient::user_pass = std::string();
/** Authentication Methods implementation */
void RestClient::clearAuth(){
RestClient::user_pass.clear();
}
void RestClient::setAuth(const std::string& user,const std::string& password){
RestClient::user_pass.clear();
RestClient::user_pass += user+":"+password;
}
/**
* @brief HTTP GET method
*
* @param url to query
*
* @return response struct
*/
RestClient::response RestClient::get(const std::string& url, const size_t timeout)
{
headermap emptyMap;
return RestClient::get(url, emptyMap, timeout);
}
/**
* @brief HTTP GET method
*
* @param url to query
* @param headers HTTP headers
*
* @return response struct
*/
RestClient::response RestClient::get(const std::string& url, const headermap& headers, const size_t timeout)
{
/** create return struct */
RestClient::response ret = {};
// use libcurl
CURL *curl = NULL;
CURLcode res = CURLE_OK;
std::string header;
curl = curl_easy_init();
if (curl)
{
/** set basic authentication if present*/
if(RestClient::user_pass.length()>0){
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERPWD, RestClient::user_pass.c_str());
}
/** set user agent */
curl_easy_setopt(curl, CURLOPT_USERAGENT, RestClient::user_agent);
/** set query URL */
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
/** set callback function */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, RestClient::write_callback);
/** set data object to pass to callback function */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ret);
/** set the header callback function */
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, RestClient::header_callback);
/** callback object for headers */
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &ret);
/** set http headers */
curl_slist* hlist = NULL;
for (headermap::const_iterator it = headers.begin(); it != headers.end(); ++it) {
header = it->first;
header += ": ";
header += it->second;
hlist = curl_slist_append(hlist, header.c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hlist);
/** perform the actual query */
//set timeout
if (timeout) {
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); // dont want to get a sig alarm on timeout
}
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
if (res == CURLE_OPERATION_TIMEDOUT) {
ret.code = res;
ret.body = "Operation Timeout.";
return ret;
}
ret.body = "Failed to query.";
ret.code = -1;
return ret;
}
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
ret.code = static_cast<int>(http_code);
curl_slist_free_all(hlist);
curl_easy_cleanup(curl);
curl_global_cleanup();
}
return ret;
}
/**
* @brief HTTP POST method
*
* @param url to query
* @param ctype content type as string
* @param data HTTP POST body
*
* @return response struct
*/
RestClient::response RestClient::post(const std::string& url,
const std::string& ctype,
const std::string& data,
const size_t timeout)
{
headermap emptyMap;
return RestClient::post(url, ctype, data, emptyMap, timeout);
}
/**
* @brief HTTP POST method
*
* @param url to query
* @param ctype content type as string
* @param data HTTP POST body
* @param headers HTTP headers
*
* @return response struct
*/
RestClient::response RestClient::post(const std::string& url,
const std::string& ctype,
const std::string& data,
const headermap& headers,
const size_t timeout)
{
/** create return struct */
RestClient::response ret = {};
/** build content-type header string */
std::string ctype_header = "Content-Type: " + ctype;
std::string header;
// use libcurl
CURL *curl = NULL;
CURLcode res = CURLE_OK;
curl = curl_easy_init();
if (curl)
{
/** set basic authentication if present*/
if(RestClient::user_pass.length()>0){
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERPWD, RestClient::user_pass.c_str());
}
/** set user agent */
curl_easy_setopt(curl, CURLOPT_USERAGENT, RestClient::user_agent);
/** set query URL */
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
/** Now specify we want to POST data */
curl_easy_setopt(curl, CURLOPT_POST, 1L);
/** set post fields */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data.size());
/** set callback function */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, RestClient::write_callback);
/** set data object to pass to callback function */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ret);
/** set the header callback function */
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, RestClient::header_callback);
/** callback object for headers */
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &ret);
/** set content-type header */
curl_slist* hlist = NULL;
hlist = curl_slist_append(hlist, ctype_header.c_str());
for (headermap::const_iterator it = headers.begin(); it != headers.end(); ++it) {
header = it->first;
header += ": ";
header += it->second;
hlist = curl_slist_append(hlist, header.c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hlist);
//set timeout
if (timeout) {
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); // dont want to get a sig alarm on timeout
}
/** perform the actual query */
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
if (res == CURLE_OPERATION_TIMEDOUT) {
ret.code = res;
ret.body = "Operation Timeout.";
return ret;
}
ret.body = "Failed to query.";
ret.code = -1;
return ret;
}
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
ret.code = static_cast<int>(http_code);
curl_slist_free_all(hlist);
curl_easy_cleanup(curl);
curl_global_cleanup();
}
return ret;
}
/**
* @brief HTTP PUT method
*
* @param url to query
* @param ctype content type as string
* @param data HTTP PUT body
*
* @return response struct
*/
RestClient::response RestClient::put(const std::string& url,
const std::string& ctype,
const std::string& data,
const size_t timeout)
{
headermap emptyMap;
return RestClient::put(url, ctype, data, emptyMap, timeout);
}
/**
* @brief HTTP PUT method
*
* @param url to query
* @param ctype content type as string
* @param data HTTP PUT body
* @param headers HTTP headers
*
* @return response struct
*/
RestClient::response RestClient::put(const std::string& url,
const std::string& ctype,
const std::string& data,
const headermap& headers,
const size_t timeout)
{
/** create return struct */
RestClient::response ret = {};
/** build content-type header string */
std::string ctype_header = "Content-Type: " + ctype;
std::string header;
/** initialize upload object */
RestClient::upload_object up_obj;
up_obj.data = data.c_str();
up_obj.length = data.size();
// use libcurl
CURL *curl = NULL;
CURLcode res = CURLE_OK;
curl = curl_easy_init();
if (curl)
{
/** set basic authentication if present*/
if(RestClient::user_pass.length()>0){
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERPWD, RestClient::user_pass.c_str());
}
/** set user agent */
curl_easy_setopt(curl, CURLOPT_USERAGENT, RestClient::user_agent);
/** set query URL */
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
/** Now specify we want to PUT data */
curl_easy_setopt(curl, CURLOPT_PUT, 1L);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/** set read callback function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, RestClient::read_callback);
/** set data object to pass to callback function */
curl_easy_setopt(curl, CURLOPT_READDATA, &up_obj);
/** set callback function */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, RestClient::write_callback);
/** set data object to pass to callback function */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ret);
/** set the header callback function */
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, RestClient::header_callback);
/** callback object for headers */
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &ret);
/** set data size */
curl_easy_setopt(curl, CURLOPT_INFILESIZE,
static_cast<long>(up_obj.length));
/** set content-type header */
curl_slist* hlist = NULL;
hlist = curl_slist_append(hlist, ctype_header.c_str());
for (headermap::const_iterator it = headers.begin(); it != headers.end(); ++it) {
header = it->first;
header += ": ";
header += it->second;
hlist = curl_slist_append(hlist, header.c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hlist);
//set timeout
if (timeout) {
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); // dont want to get a sig alarm on timeout
}
/** perform the actual query */
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
if (res == CURLE_OPERATION_TIMEDOUT) {
ret.code = res;
ret.body = "Operation Timeout.";
return ret;
}
ret.body = "Failed to query.";
ret.code = -1;
return ret;
}
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
ret.code = static_cast<int>(http_code);
curl_slist_free_all(hlist);
curl_easy_cleanup(curl);
curl_global_cleanup();
}
return ret;
}
/**
* @brief HTTP DELETE method
*
* @param url to query
*
* @return response struct
*/
RestClient::response RestClient::del(const std::string& url, const size_t timeout)
{
headermap emptyMap;
return RestClient::del(url, emptyMap, timeout);
}
/**
* @brief HTTP DELETE method
*
* @param url to query
* @param headers HTTP headers
*
* @return response struct
*/
RestClient::response RestClient::del(const std::string& url,
const headermap& headers,
const size_t timeout)
{
/** create return struct */
RestClient::response ret = {};
/** we want HTTP DELETE */
const char* http_delete = "DELETE";
// use libcurl
CURL *curl = NULL;
CURLcode res = CURLE_OK;
std::string header;
curl = curl_easy_init();
if (curl)
{
/** set basic authentication if present*/
if(RestClient::user_pass.length()>0){
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERPWD, RestClient::user_pass.c_str());
}
/** set user agent */
curl_easy_setopt(curl, CURLOPT_USERAGENT, RestClient::user_agent);
/** set query URL */
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
/** set HTTP DELETE METHOD */
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, http_delete);
/** set callback function */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, RestClient::write_callback);
/** set data object to pass to callback function */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ret);
/** set the header callback function */
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, RestClient::header_callback);
/** callback object for headers */
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &ret);
/** set http headers */
curl_slist* hlist = NULL;
for (headermap::const_iterator it = headers.begin(); it != headers.end(); ++it) {
header = it->first;
header += ": ";
header += it->second;
hlist = curl_slist_append(hlist, header.c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hlist);
//set timeout
if (timeout) {
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); // dont want to get a sig alarm on timeout
}
/** perform the actual query */
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
if (res == CURLE_OPERATION_TIMEDOUT) {
ret.code = res;
ret.body = "Operation Timeout.";
return ret;
}
ret.body = "Failed to query.";
ret.code = -1;
return ret;
}
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
ret.code = static_cast<int>(http_code);
curl_slist_free_all(hlist);
curl_easy_cleanup(curl);
curl_global_cleanup();
}
return ret;
}
/**
* @brief write callback function for libcurl
*
* @param data returned data of size (size*nmemb)
* @param size size parameter
* @param nmemb memblock parameter
* @param userdata pointer to user data to save/work with return data
*
* @return (size * nmemb)
*/
size_t RestClient::write_callback(void *data, size_t size, size_t nmemb,
void *userdata)
{
RestClient::response* r;
r = reinterpret_cast<RestClient::response*>(userdata);
r->body.append(reinterpret_cast<char*>(data), size*nmemb);
return (size * nmemb);
}
/**
* @brief header callback for libcurl
*
* @param data returned (header line)
* @param size of data
* @param nmemb memblock
* @param userdata pointer to user data object to save headr data
* @return size * nmemb;
*/
size_t RestClient::header_callback(void *data, size_t size, size_t nmemb,
void *userdata)
{
RestClient::response* r;
r = reinterpret_cast<RestClient::response*>(userdata);
std::string header(reinterpret_cast<char*>(data), size*nmemb);
size_t seperator = header.find_first_of(":");
if ( std::string::npos == seperator ) {
//roll with non seperated headers...
trim(header);
if ( 0 == header.length() ){
return (size * nmemb); //blank line;
}
r->headers[header] = "present";
} else {
std::string key = header.substr(0, seperator);
trim(key);
std::string value = header.substr(seperator + 1);
trim (value);
r->headers[key] = value;
}
return (size * nmemb);
}
/**
* @brief read callback function for libcurl
*
* @param pointer of max size (size*nmemb) to write data to
* @param size size parameter
* @param nmemb memblock parameter
* @param userdata pointer to user data to read data from
*
* @return (size * nmemb)
*/
size_t RestClient::read_callback(void *data, size_t size, size_t nmemb,
void *userdata)
{
/** get upload struct */
RestClient::upload_object* u;
u = reinterpret_cast<RestClient::upload_object*>(userdata);
/** set correct sizes */
size_t curl_size = size * nmemb;
size_t copy_size = (u->length < curl_size) ? u->length : curl_size;
/** copy data to buffer */
memcpy(data, u->data, copy_size);
/** decrement length and increment data pointer */
u->length -= copy_size;
u->data += copy_size;
/** return copied size */
return copy_size;
}