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
|
/**
* @file helpers.cpp
* @brief implementation of the restclient helpers
* @author Daniel Schauenberg <d@unwiredcouch.com>
*/
#include "restclient/helpers.h"
#ifdef USE_LOCAL_HEADERS
#include "../libcurl-7.35.0/curl/curl.h"
#else
#include <curl/curl.h>
#endif
#include <cstring>
#include "restclient/restclient.h"
/**
* @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::Helpers::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::Helpers::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 data 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::Helpers::read_callback(void *data, size_t size,
size_t nmemb, void *userdata) {
/** get upload struct */
RestClient::Helpers::UploadObject* u;
u = reinterpret_cast<RestClient::Helpers::UploadObject*>(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 */
std::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;
}
|