-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurl_easy.cc
382 lines (339 loc) · 11.6 KB
/
curl_easy.cc
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
#include "curl_easy.hpp"
#include "curl_url.hpp"
#include <new>
#include <curl/curl.h>
#include <utility>
#include <type_traits>
#define CHECK_OOM(code) \
if ((code) == CURLE_OUT_OF_MEMORY) \
return {std::bad_alloc{}}
namespace curl {
void curl_t::Easy_deleter::operator () (void *p) const noexcept
{
if (p)
curl_easy_cleanup(p);
}
auto curl_t::create_easy(std::size_t buffer_size) noexcept -> Easy_t
{
CURL *curl = curl_easy_init();
if (!curl)
return {nullptr};
if (stderr_stream) {
curl_easy_setopt(curl, CURLOPT_STDERR, stderr_stream);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
}
// Enable TCP_keepalive
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
if (buffer_size)
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, buffer_size);
if (disable_signal_handling_v)
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
using Easy_ptr = std::unique_ptr<char, Easy_deleter>;
return {Easy_ptr{static_cast<char*>(curl)}};
}
auto curl_t::dup_easy(const Easy_t &e, std::size_t buffer_size) noexcept -> Easy_t
{
CURL *curl = curl_easy_duphandle(e.get());
if (!curl)
return {nullptr};
if (stderr_stream) {
curl_easy_setopt(curl, CURLOPT_STDERR, stderr_stream);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
}
if (buffer_size)
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, buffer_size);
if (disable_signal_handling_v)
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
using Easy_ptr = std::unique_ptr<char, Easy_deleter>;
return {Easy_ptr{static_cast<char*>(curl)}};
}
void Easy_ref_t::set_verbose(FILE *stderr_stream_arg) noexcept
{
if (stderr_stream_arg) {
curl_easy_setopt(curl_easy, CURLOPT_STDERR, stderr_stream_arg);
curl_easy_setopt(curl_easy, CURLOPT_VERBOSE, 1L);
}
}
std::size_t Easy_ref_t::get_error_buffer_size() noexcept
{
return CURL_ERROR_SIZE;
}
void Easy_ref_t::set_error_buffer(char *error_buffer) noexcept
{
curl_easy_setopt(curl_easy, CURLOPT_ERRORBUFFER, error_buffer);
}
void Easy_ref_t::set_private(void *userp) noexcept
{
curl_easy_setopt(curl_easy, CURLOPT_PRIVATE, userp);
}
void* Easy_ref_t::get_private() const noexcept
{
char *userp;
curl_easy_getinfo(curl_easy, CURLINFO_PRIVATE, &userp);
return userp;
}
void Easy_ref_t::set_writeback(writeback_t writeback, void *userp) noexcept
{
curl_easy_setopt(curl_easy, CURLOPT_WRITEFUNCTION, writeback);
curl_easy_setopt(curl_easy, CURLOPT_WRITEDATA, userp);
}
void Easy_ref_t::set_url(const Url_ref_t &url) noexcept
{
curl_easy_setopt(curl_easy, CURLOPT_CURLU, url.url);
}
auto Easy_ref_t::set_url(const char *url) noexcept -> Ret_except<void, std::bad_alloc>
{
CHECK_OOM(curl_easy_setopt(curl_easy, CURLOPT_URL, url));
return {};
}
auto Easy_ref_t::pin_publickey(const char *pubkey) ->
Ret_except<void, std::bad_alloc, curl::NotBuiltIn_error>
{
auto result = curl_easy_setopt(curl_easy, CURLOPT_PINNEDPUBLICKEY, pubkey);
if (result == CURLE_OUT_OF_MEMORY)
return {std::bad_alloc{}};
else if (result == CURLE_UNKNOWN_OPTION)
return {curl::NotBuiltIn_error{"pining public key is not supported by this version of libcurl "
"on this specific ssl version"}};
else
return {};
}
auto Easy_ref_t::set_cookie(const char *cookies) noexcept ->
Ret_except<void, std::bad_alloc, curl::NotBuiltIn_error>
{
auto code = curl_easy_setopt(curl_easy, CURLOPT_COOKIE, cookies);
if (code == CURLE_UNKNOWN_OPTION)
return curl::NotBuiltIn_error{"cookies not supported"};
else if (code == CURLE_OUT_OF_MEMORY)
return {std::bad_alloc{}};
return {};
}
auto Easy_ref_t::set_cookiefile(const char *cookie_filename) noexcept ->
Ret_except<void, curl::NotBuiltIn_error>
{
if (curl_easy_setopt(curl_easy, CURLOPT_COOKIEFILE, cookie_filename) == CURLE_UNKNOWN_OPTION)
return {curl::NotBuiltIn_error{"cookies not supported"}};
return {};
}
auto Easy_ref_t::set_cookiejar(const char *cookie_filename) noexcept ->
Ret_except<void, std::bad_alloc, curl::NotBuiltIn_error>
{
auto code = curl_easy_setopt(curl_easy, CURLOPT_COOKIEJAR, cookie_filename);
if (code == CURLE_UNKNOWN_OPTION)
return {curl::NotBuiltIn_error{"cookies not supported"}};
else if (code == CURLE_OUT_OF_MEMORY)
return {std::bad_alloc{}};
return {};
}
auto Easy_ref_t::set_cookielist(const char *cookie) noexcept ->
Ret_except<void, std::bad_alloc, curl::NotBuiltIn_error>
{
auto code = curl_easy_setopt(curl_easy, CURLOPT_COOKIELIST, cookie);
if (code == CURLE_UNKNOWN_OPTION)
return {curl::NotBuiltIn_error{"cookies not supported"}};
else if (code == CURLE_OUT_OF_MEMORY)
return {std::bad_alloc{}};
return {};
}
void Easy_ref_t::start_new_cookie_session() noexcept
{
curl_easy_setopt(curl_easy, CURLOPT_COOKIESESSION, 1L);
}
auto Easy_ref_t::erase_all_cookies_in_mem() noexcept ->
Ret_except<void, std::bad_alloc, curl::NotBuiltIn_error>
{
return set_cookielist("ALL");
}
auto Easy_ref_t::erase_all_session_cookies_in_mem() noexcept ->
Ret_except<void, std::bad_alloc, curl::NotBuiltIn_error>
{
return set_cookielist("SESS");
}
auto Easy_ref_t::flush_cookies_to_jar() noexcept ->
Ret_except<void, std::bad_alloc, curl::NotBuiltIn_error>
{
return set_cookielist("FLUSH");
}
auto Easy_ref_t::reload_cookies_from_file() noexcept ->
Ret_except<void, std::bad_alloc, curl::NotBuiltIn_error>
{
return set_cookielist("RELOAD");
}
void Easy_ref_t::set_follow_location(long redir) noexcept
{
if (redir != 0) {
curl_easy_setopt(curl_easy, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl_easy, CURLOPT_MAXREDIRS, redir);
} else
curl_easy_setopt(curl_easy, CURLOPT_FOLLOWLOCATION, 0L);
}
auto Easy_ref_t::set_useragent(const char *useragent) noexcept -> Ret_except<void, std::bad_alloc>
{
CHECK_OOM(curl_easy_setopt(curl_easy, CURLOPT_USERAGENT, useragent));
return {};
}
auto Easy_ref_t::set_encoding(const char *encoding) noexcept -> Ret_except<void, std::bad_alloc>
{
CHECK_OOM(curl_easy_setopt(curl_easy, CURLOPT_ACCEPT_ENCODING, encoding));
return {};
}
auto Easy_ref_t::set_interface(const char *value) noexcept -> Ret_except<void, std::bad_alloc>
{
CHECK_OOM(curl_easy_setopt(curl_easy, CURLOPT_INTERFACE, value));
return {};
}
auto Easy_ref_t::set_ip_addr_only(const char *ip_addr) noexcept -> Ret_except<void, std::bad_alloc>
{
/**
* 46 is the maximum length for ipv6 address represented in string.
*
* Information retrieved from [here][1].
*
* [1]: https://stackoverflow.com/questions/166132/maximum-length-of-the-textual-representation-of-an-ipv6-address
*/
constexpr const auto buffer_size = 5 + 46;
char buffer[buffer_size];
std::snprintf(buffer, buffer_size, "host!%s", ip_addr);
return set_interface(buffer);
}
void Easy_ref_t::set_timeout(unsigned long timeout) noexcept
{
curl_easy_setopt(curl_easy, CURLOPT_TIMEOUT_MS, timeout);
}
void Easy_ref_t::set_http_header(const utils::slist &l, header_option option) noexcept
{
curl_easy_setopt(curl_easy, CURLOPT_HTTPHEADER, static_cast<struct curl_slist*>(l.get_underlying_ptr()));
if (option != header_option::unspecified) {
long value;
if (option == header_option::separate)
value = CURLHEADER_SEPARATE;
else
value = CURLHEADER_UNIFIED;
curl_easy_setopt(curl_easy, CURLOPT_HEADEROPT, value);
}
}
void Easy_ref_t::set_nobody(bool enable) noexcept
{
curl_easy_setopt(curl_easy, CURLOPT_NOBODY, enable);
}
void Easy_ref_t::request_get() noexcept
{
curl_easy_setopt(curl_easy, CURLOPT_HTTPGET, 1L);
}
void Easy_ref_t::request_post(const void *data, std::size_t len) noexcept
{
curl_easy_setopt(curl_easy, CURLOPT_POSTFIELDSIZE_LARGE, len);
curl_easy_setopt(curl_easy, CURLOPT_POSTFIELDS, data);
}
void Easy_ref_t::request_post(readback_t readback, void *userp, std::size_t len) noexcept
{
request_post(nullptr, len);
curl_easy_setopt(curl_easy, CURLOPT_READFUNCTION, readback);
curl_easy_setopt(curl_easy, CURLOPT_READDATA, userp);
}
auto Easy_ref_t::perform() noexcept -> perform_ret_t
{
return check_perform(curl_easy_perform(curl_easy), "curl::Easy_ref_t::perform");
}
auto operator & (Easy_ref_t::PauseOptions x, Easy_ref_t::PauseOptions y) noexcept
{
using type = std::underlying_type_t<Easy_ref_t::PauseOptions>;
return static_cast<type>(x) & static_cast<type>(y);
}
auto Easy_ref_t::set_pause(PauseOptions option) noexcept -> Ret_except<code, std::bad_alloc, Exception>
{
int bitmask = 0;
if (option & PauseOptions::recv)
bitmask |= CURLPAUSE_RECV;
if (option & PauseOptions::send)
bitmask |= CURLPAUSE_SEND;
auto result = curl_easy_pause(curl_easy, bitmask);
if (result == CURLE_WRITE_ERROR)
return {code::writeback_error};
else if (result == CURLE_OUT_OF_MEMORY)
return {std::bad_alloc{}};
else if (result != CURLE_OK)
return {Exception{result}};
else
return {code::ok};
}
long Easy_ref_t::get_response_code() const noexcept
{
long response_code;
curl_easy_getinfo(curl_easy, CURLINFO_RESPONSE_CODE, &response_code);
return response_code;
}
std::size_t Easy_ref_t::getinfo_sizeof_request() const noexcept
{
long size;
curl_easy_getinfo(curl_easy, CURLINFO_REQUEST_SIZE, &size);
return size;
}
std::size_t Easy_ref_t::getinfo_sizeof_uploaded() const noexcept
{
curl_off_t ul;
if (curl_easy_getinfo(curl_easy, CURLINFO_SIZE_UPLOAD_T, &ul)) {
double bytes;
curl_easy_getinfo(curl_easy, CURLINFO_SIZE_UPLOAD, &bytes);
return static_cast<std::size_t>(bytes);
}
return ul;
}
std::size_t Easy_ref_t::getinfo_sizeof_response_header() const noexcept
{
long size;
curl_easy_getinfo(curl_easy, CURLINFO_HEADER_SIZE, &size);
return size;
}
std::size_t Easy_ref_t::getinfo_sizeof_response_body() const noexcept
{
curl_off_t dl;
if (curl_easy_getinfo(curl_easy, CURLINFO_SIZE_DOWNLOAD_T, &dl) == CURLE_UNKNOWN_OPTION) {
double bytes;
curl_easy_getinfo(curl_easy, CURLINFO_SIZE_DOWNLOAD, &bytes);
return static_cast<std::size_t>(bytes);
}
return dl;
}
std::size_t Easy_ref_t::getinfo_transfer_time() const noexcept
{
curl_off_t total;
if (curl_easy_getinfo(curl_easy, CURLINFO_TOTAL_TIME_T, &total) == CURLE_UNKNOWN_OPTION) {
double seconds;
curl_easy_getinfo(curl_easy, CURLINFO_TOTAL_TIME, &seconds);
return static_cast<std::size_t>(seconds * 1000);
}
return total;
}
auto Easy_ref_t::getinfo_redirect_url() const noexcept -> const char*
{
char *url = nullptr;
curl_easy_getinfo(curl_easy, CURLINFO_REDIRECT_URL, &url);
return url;
}
auto Easy_ref_t::getinfo_effective_url() const noexcept -> const char*
{
char *url = nullptr;
curl_easy_getinfo(curl_easy, CURLINFO_EFFECTIVE_URL, &url);
return url;
}
auto Easy_ref_t::getinfo_cookie_list() const noexcept ->
Ret_except<utils::slist, curl::NotBuiltIn_error>
{
curl_slist *cookies = nullptr;
if (curl_easy_getinfo(curl_easy, CURLINFO_COOKIELIST, &cookies) == CURLE_UNKNOWN_OPTION)
return {curl::NotBuiltIn_error{"cookies not supported"}};
return {utils::slist{cookies}};
}
auto Easy_ref_t::get_active_socket() const noexcept -> curl_socket_t
{
curl_socket_t socketfd = CURL_SOCKET_BAD;
curl_easy_getinfo(curl_easy, CURLINFO_ACTIVESOCKET, &socketfd);
return socketfd;
}
void Easy_ref_t::setup_establish_connection_only() noexcept
{
request_get();
curl_easy_setopt(curl_easy, CURLOPT_NOBODY, 1);
}
} /* namespace curl */