| 1 |
#include "curl-common.h" |
| 2 |
#include "callbacks.h" |
| 3 |
|
| 4 |
|
| 5 |
extern int r_curl_is_slist_option(CURLoption x); |
| 6 |
extern int r_curl_is_long_option(CURLoption x); |
| 7 |
extern int r_curl_is_off_t_option(CURLoption x); |
| 8 |
extern int r_curl_is_string_option(CURLoption x); |
| 9 |
extern int r_curl_is_postfields_option(CURLoption x); |
| 10 |
|
| 11 |
#ifndef MAX_PATH |
| 12 |
#define MAX_PATH 1024 |
| 13 |
#endif |
| 14 |
|
| 15 |
static int total_handles = 0; |
| 16 |
|
| 17 |
void clean_handle(reference *ref){ |
| 18 |
if(ref->refCount == 0){ |
| 19 |
if(ref->headers) |
| 20 |
curl_slist_free_all(ref->headers); |
| 21 |
if(ref->custom) |
| 22 |
curl_slist_free_all(ref->custom); |
| 23 |
if(ref->form) |
| 24 |
curl_formfree(ref->form); |
| 25 |
if(ref->handle) |
| 26 |
curl_easy_cleanup(ref->handle); |
| 27 |
if(ref->resheaders.buf) |
| 28 |
free(ref->resheaders.buf); |
| 29 |
free(ref); |
| 30 |
total_handles--; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
static void fin_handle(SEXP ptr){ |
| 35 |
reference *ref = (reference*) R_ExternalPtrAddr(ptr); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
ref->refCount--; |
| 40 |
if(ref->refCount == 0) |
| 41 |
R_ClearExternalPtr(ptr); |
| 42 |
|
| 43 |
|
| 44 |
clean_handle(ref); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
static size_t dummy_read(char *buffer, size_t size, size_t nitems, void *instream){ |
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
#define xftype curl_off_t |
| 53 |
|
| 54 |
static int xferinfo_callback(void *clientp, xftype dltotal, xftype dlnow, xftype ultotal, xftype ulnow){ |
| 55 |
static xftype dlprev = 0; |
| 56 |
static xftype ulprev = 0; |
| 57 |
if(dlnow && dlnow != dlprev){ |
| 58 |
dlprev = dlnow; |
| 59 |
if(dltotal){ |
| 60 |
int pct_dn = (100 * dlnow)/dltotal; |
| 61 |
REprintf("\r [%d%%] Downloaded %.0lf bytes...", pct_dn, (double) dlnow); |
| 62 |
if(dlnow == dltotal) |
| 63 |
REprintf("\n"); |
| 64 |
} else { |
| 65 |
REprintf("\r Downloaded %.0lf bytes...", (double) dlnow); |
| 66 |
} |
| 67 |
} else if(ulnow && ulnow != ulprev){ |
| 68 |
ulprev = ulnow; |
| 69 |
int pct_up = (100 * ulnow)/ultotal; |
| 70 |
REprintf("\r [%d%%] Uploaded %.0lf bytes...", pct_up, (double) ulnow); |
| 71 |
if(ulnow == ultotal) |
| 72 |
REprintf("\n"); |
| 73 |
} |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
static struct curl_slist * default_headers(void){ |
| 79 |
static struct curl_slist * headers = NULL; |
| 80 |
if(headers == NULL){ |
| 81 |
headers = curl_slist_append(headers, "Expect:"); |
| 82 |
} |
| 83 |
return headers; |
| 84 |
} |
| 85 |
|
| 86 |
static void assert_setopt(CURLcode res, CURLoption opt, const char *optname){ |
| 87 |
if(res != CURLE_OK){ |
| 88 |
char errmsg [256] = {0}; |
| 89 |
if(opt == CURLOPT_MAIL_RCPT || opt == CURLOPT_MAIL_FROM || opt == CURLOPT_MAIL_AUTH){ |
| 90 |
snprintf(errmsg, 256, "Error setting '%s': your libcurl may have disabled SMTP support", optname); |
| 91 |
} else { |
| 92 |
snprintf(errmsg, 256, "Invalid or unsupported value when setting curl option '%s'", optname); |
| 93 |
} |
| 94 |
assert_message(CURLE_BAD_FUNCTION_ARGUMENT, errmsg); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
#define set_user_option(option, value) assert_setopt(curl_easy_setopt(handle, option, value), option, optname) |
| 99 |
|
| 100 |
static void set_headers(reference *ref, struct curl_slist *newheaders){ |
| 101 |
if(ref->headers) |
| 102 |
curl_slist_free_all(ref->headers); |
| 103 |
ref->headers = newheaders; |
| 104 |
assert(curl_easy_setopt(ref->handle, CURLOPT_HTTPHEADER, |
| 105 |
newheaders ? newheaders : default_headers())); |
| 106 |
} |
| 107 |
|
| 108 |
static int default_verbose_cb(CURL *handle, curl_infotype type, char *data, size_t size, void *userptr){ |
| 109 |
if(type < 3){ |
| 110 |
char prefix = type == CURLINFO_TEXT ? '*' : (type == CURLINFO_HEADER_IN ? '<' : '>'); |
| 111 |
REprintf("%c %.*s", prefix, (int) size, data); |
| 112 |
} |
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
static void set_handle_defaults(reference *ref){ |
| 119 |
|
| 120 |
|
| 121 |
CURL *handle = ref->handle; |
| 122 |
assert(curl_easy_setopt(handle, CURLOPT_PRIVATE, ref)); |
| 123 |
|
| 124 |
|
| 125 |
reset_resheaders(ref); |
| 126 |
curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, append_buffer); |
| 127 |
curl_easy_setopt(handle, CURLOPT_HEADERDATA, &(ref->resheaders)); |
| 128 |
|
| 129 |
|
| 130 |
const char *ca_bundle = getenv("CURL_CA_BUNDLE"); |
| 131 |
#ifdef _WIN32 |
| 132 |
curl_easy_setopt(handle, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE); |
| 133 |
struct curl_tlssessioninfo *tlsinfo = NULL; |
| 134 |
if(curl_easy_getinfo(handle, CURLINFO_TLS_SSL_PTR, &tlsinfo) == CURLE_OK){ |
| 135 |
if(tlsinfo->backend == CURLSSLBACKEND_OPENSSL && ca_bundle == NULL) { |
| 136 |
curl_easy_setopt(handle, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE | CURLSSLOPT_NATIVE_CA); |
| 137 |
} else if(tlsinfo->backend == CURLSSLBACKEND_SCHANNEL){ |
| 138 |
ca_bundle = NULL; |
| 139 |
} |
| 140 |
} |
| 141 |
#endif |
| 142 |
|
| 143 |
if(ca_bundle != NULL) { |
| 144 |
curl_easy_setopt(handle, CURLOPT_CAINFO, ca_bundle); |
| 145 |
} |
| 146 |
|
| 147 |
static const curl_version_info_data *version = NULL; |
| 148 |
if(version == NULL) |
| 149 |
version = curl_version_info(CURLVERSION_NOW); |
| 150 |
|
| 151 |
assert(curl_easy_setopt(handle, CURLOPT_ENCODING, version->version_num == 0x080701 ? "gzip" : "")); |
| 152 |
|
| 153 |
|
| 154 |
assert(curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L)); |
| 155 |
assert(curl_easy_setopt(handle, CURLOPT_MAXREDIRS, 10L)); |
| 156 |
|
| 157 |
|
| 158 |
assert(curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, 10L)); |
| 159 |
|
| 160 |
|
| 161 |
assert(curl_easy_setopt(handle, CURLOPT_LOW_SPEED_LIMIT, 1L)); |
| 162 |
assert(curl_easy_setopt(handle, CURLOPT_LOW_SPEED_TIME, 600L)); |
| 163 |
|
| 164 |
|
| 165 |
assert(curl_easy_setopt(handle, CURLOPT_COOKIEFILE, "")); |
| 166 |
assert(curl_easy_setopt(handle, CURLOPT_FILETIME, 1L)); |
| 167 |
|
| 168 |
|
| 169 |
SEXP agent = Rf_GetOption1(Rf_install("HTTPUserAgent")); |
| 170 |
if(Rf_isString(agent) && Rf_length(agent)){ |
| 171 |
assert(curl_easy_setopt(handle, CURLOPT_USERAGENT, CHAR(STRING_ELT(agent, 0)))); |
| 172 |
} else { |
| 173 |
assert(curl_easy_setopt(handle, CURLOPT_USERAGENT, "r/curl/jeroen")); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
SEXP netrc = Rf_GetOption1(Rf_install("netrc")); |
| 178 |
if (Rf_isString(netrc) && Rf_length(netrc)) { |
| 179 |
const char *path = R_ExpandFileName(CHAR(STRING_ELT(netrc, 0))); |
| 180 |
assert(curl_easy_setopt(handle, CURLOPT_NETRC, CURL_NETRC_OPTIONAL)); |
| 181 |
assert(curl_easy_setopt(handle, CURLOPT_NETRC_FILE, path)); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
assert(curl_easy_setopt(handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY)); |
| 186 |
assert(curl_easy_setopt(handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY)); |
| 187 |
|
| 188 |
|
| 189 |
assert(curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, ref->errbuf)); |
| 190 |
|
| 191 |
|
| 192 |
assert(curl_easy_setopt(handle, CURLOPT_READFUNCTION, dummy_read)); |
| 193 |
|
| 194 |
|
| 195 |
assert(curl_easy_setopt(handle, CURLOPT_XFERINFOFUNCTION, xferinfo_callback)); |
| 196 |
|
| 197 |
|
| 198 |
set_headers(ref, NULL); |
| 199 |
assert(curl_easy_setopt(handle, CURLOPT_EXPECT_100_TIMEOUT_MS, 0L)); |
| 200 |
|
| 201 |
|
| 202 |
assert(curl_easy_setopt(handle, CURLOPT_DEBUGFUNCTION, default_verbose_cb)); |
| 203 |
|
| 204 |
|
| 205 |
assert(curl_easy_setopt(handle, CURLOPT_PIPEWAIT, 1L)); |
| 206 |
} |
| 207 |
|
| 208 |
SEXP R_new_handle(void){ |
| 209 |
reference *ref = calloc(1, sizeof(reference)); |
| 210 |
ref->refCount = 1; |
| 211 |
ref->handle = curl_easy_init(); |
| 212 |
total_handles++; |
| 213 |
set_handle_defaults(ref); |
| 214 |
SEXP prot = PROTECT(Rf_allocVector(VECSXP, 8)); |
| 215 |
SEXP ptr = PROTECT(R_MakeExternalPtr(ref, R_NilValue, prot)); |
| 216 |
R_RegisterCFinalizerEx(ptr, fin_handle, TRUE); |
| 217 |
Rf_setAttrib(ptr, R_ClassSymbol, Rf_mkString("curl_handle")); |
| 218 |
UNPROTECT(2); |
| 219 |
ref->handleptr = ptr; |
| 220 |
return ptr; |
| 221 |
} |
| 222 |
|
| 223 |
SEXP R_handle_reset(SEXP ptr){ |
| 224 |
|
| 225 |
reference *ref = get_ref(ptr); |
| 226 |
set_form(ref, NULL); |
| 227 |
reset_errbuf(ref); |
| 228 |
curl_easy_reset(ref->handle); |
| 229 |
|
| 230 |
|
| 231 |
if(ref->custom){ |
| 232 |
curl_slist_free_all(ref->custom); |
| 233 |
ref->custom = NULL; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
set_handle_defaults(ref); |
| 238 |
return Rf_ScalarLogical(1); |
| 239 |
} |
| 240 |
|
| 241 |
SEXP R_handle_getheaders(SEXP ptr){ |
| 242 |
reference *ref = get_ref(ptr); |
| 243 |
return slist_to_vec(ref->headers); |
| 244 |
} |
| 245 |
|
| 246 |
SEXP R_handle_getcustom(SEXP ptr){ |
| 247 |
reference *ref = get_ref(ptr); |
| 248 |
return slist_to_vec(ref->custom); |
| 249 |
} |
| 250 |
|
| 251 |
SEXP R_handle_setopt(SEXP ptr, SEXP keys, SEXP values){ |
| 252 |
reference *ref = get_ref(ptr); |
| 253 |
CURL *handle = get_handle(ptr); |
| 254 |
SEXP prot = R_ExternalPtrProtected(ptr); |
| 255 |
SEXP optnames = PROTECT(Rf_getAttrib(values, R_NamesSymbol)); |
| 256 |
|
| 257 |
if(!Rf_isInteger(keys)) |
| 258 |
Rf_error("keys` must be an integer"); |
| 259 |
|
| 260 |
if(!Rf_isVector(values)) |
| 261 |
Rf_error("`values` must be a list"); |
| 262 |
|
| 263 |
for(int i = 0; i < Rf_length(keys); i++){ |
| 264 |
int key = INTEGER(keys)[i]; |
| 265 |
const char* optname = CHAR(STRING_ELT(optnames, i)); |
| 266 |
SEXP val = VECTOR_ELT(values, i); |
| 267 |
if(val == R_NilValue){ |
| 268 |
set_user_option(key, NULL); |
| 269 |
} else if (key == CURLOPT_XFERINFOFUNCTION) { |
| 270 |
if (TYPEOF(val) != CLOSXP) |
| 271 |
Rf_error("Value for option %s (%d) must be a function.", optname, key); |
| 272 |
|
| 273 |
set_user_option(CURLOPT_XFERINFOFUNCTION, (curl_xferinfo_callback) R_curl_callback_xferinfo); |
| 274 |
set_user_option(CURLOPT_XFERINFODATA, val); |
| 275 |
set_user_option(CURLOPT_NOPROGRESS, 0L); |
| 276 |
SET_VECTOR_ELT(prot, 1, val); |
| 277 |
} else if (key == CURLOPT_PROGRESSFUNCTION) { |
| 278 |
if (TYPEOF(val) != CLOSXP) |
| 279 |
Rf_error("Value for option %s (%d) must be a function.", optname, key); |
| 280 |
|
| 281 |
set_user_option(CURLOPT_PROGRESSFUNCTION,(curl_progress_callback) R_curl_callback_progress); |
| 282 |
set_user_option(CURLOPT_PROGRESSDATA, val); |
| 283 |
set_user_option(CURLOPT_NOPROGRESS, 0L); |
| 284 |
SET_VECTOR_ELT(prot, 2, val); |
| 285 |
} else if (key == CURLOPT_READFUNCTION) { |
| 286 |
if (TYPEOF(val) != CLOSXP) |
| 287 |
Rf_error("Value for option %s (%d) must be a function.", optname, key); |
| 288 |
|
| 289 |
set_user_option(CURLOPT_READFUNCTION, (curl_read_callback) R_curl_callback_read); |
| 290 |
set_user_option(CURLOPT_READDATA, val); |
| 291 |
SET_VECTOR_ELT(prot, 3, val); |
| 292 |
} else if (key == CURLOPT_DEBUGFUNCTION) { |
| 293 |
if (TYPEOF(val) != CLOSXP) |
| 294 |
Rf_error("Value for option %s (%d) must be a function.", optname, key); |
| 295 |
set_user_option(CURLOPT_DEBUGFUNCTION, (curl_debug_callback) R_curl_callback_debug); |
| 296 |
set_user_option(CURLOPT_DEBUGDATA, val); |
| 297 |
SET_VECTOR_ELT(prot, 4, val); |
| 298 |
} else if (key == CURLOPT_SSL_CTX_FUNCTION){ |
| 299 |
if (TYPEOF(val) != CLOSXP) |
| 300 |
Rf_error("Value for option %s (%d) must be a function.", optname, key); |
| 301 |
|
| 302 |
set_user_option(CURLOPT_SSL_CTX_FUNCTION, (curl_ssl_ctx_callback) R_curl_callback_ssl_ctx); |
| 303 |
set_user_option(CURLOPT_SSL_CTX_DATA, val); |
| 304 |
SET_VECTOR_ELT(prot, 5, val); |
| 305 |
} else if (key == CURLOPT_SEEKFUNCTION) { |
| 306 |
if (TYPEOF(val) != CLOSXP) |
| 307 |
Rf_error("Value for option %s (%d) must be a function.", optname, key); |
| 308 |
set_user_option(CURLOPT_SEEKFUNCTION, (curl_seek_callback) R_curl_callback_seek); |
| 309 |
set_user_option(CURLOPT_SEEKDATA, val); |
| 310 |
SET_VECTOR_ELT(prot, 6, val); |
| 311 |
} else if (key == CURLOPT_URL) { |
| 312 |
|
| 313 |
const char * url_utf8 = Rf_translateCharUTF8(STRING_ELT(val, 0)); |
| 314 |
set_user_option(CURLOPT_URL, url_utf8); |
| 315 |
} else if(key == CURLOPT_HTTPHEADER){ |
| 316 |
if(!Rf_isString(val)) |
| 317 |
Rf_error("Value for option %s (%d) must be a string vector", optname, key); |
| 318 |
set_headers(get_ref(ptr), vec_to_slist(val)); |
| 319 |
} else if (r_curl_is_slist_option(key)) { |
| 320 |
if(!Rf_isString(val)) |
| 321 |
Rf_error("Value for option %s (%d) must be a string vector", optname, key); |
| 322 |
ref->custom = vec_to_slist(val); |
| 323 |
set_user_option(key, ref->custom); |
| 324 |
} else if(r_curl_is_long_option(key)){ |
| 325 |
if(!Rf_isNumeric(val) || Rf_length(val) != 1) |
| 326 |
Rf_error("Value for option %s (%d) must be a number.", optname, key); |
| 327 |
set_user_option(key, (long) Rf_asInteger(val)); |
| 328 |
} else if(r_curl_is_off_t_option(key)){ |
| 329 |
if(!Rf_isNumeric(val) || Rf_length(val) != 1) |
| 330 |
Rf_error("Value for option %s (%d) must be a number.", optname, key); |
| 331 |
set_user_option(key, (curl_off_t) Rf_asReal(val)); |
| 332 |
} else if(r_curl_is_postfields_option(key) || r_curl_is_string_option(key)){ |
| 333 |
if(r_curl_is_postfields_option(key)){ |
| 334 |
key = CURLOPT_POSTFIELDS; |
| 335 |
SET_VECTOR_ELT(prot, 7, val); |
| 336 |
} |
| 337 |
switch (TYPEOF(val)) { |
| 338 |
case RAWSXP: |
| 339 |
if(key == CURLOPT_POSTFIELDS) |
| 340 |
set_user_option(CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t) Rf_xlength(val)); |
| 341 |
set_user_option(key, RAW(val)); |
| 342 |
break; |
| 343 |
case STRSXP: |
| 344 |
if (Rf_length(val) != 1) |
| 345 |
Rf_error("Value for option %s (%d) must be length-1 string", optname, key); |
| 346 |
set_user_option(key, CHAR(STRING_ELT(val, 0))); |
| 347 |
break; |
| 348 |
default: |
| 349 |
Rf_error("Value for option %s (%d) must be a string or raw vector.", optname, key); |
| 350 |
} |
| 351 |
} else { |
| 352 |
Rf_error("Option %s (%d) has unknown or unsupported type.", optname, key); |
| 353 |
} |
| 354 |
} |
| 355 |
UNPROTECT(1); |
| 356 |
return Rf_ScalarLogical(1); |
| 357 |
} |
| 358 |
|
| 359 |
SEXP R_handle_setform(SEXP ptr, SEXP form){ |
| 360 |
if(!Rf_isVector(form)) |
| 361 |
Rf_error("Form must be a list."); |
| 362 |
set_form(get_ref(ptr), make_form(form)); |
| 363 |
return Rf_ScalarLogical(1); |
| 364 |
} |
| 365 |
|
| 366 |
static SEXP make_timevec(CURL *handle){ |
| 367 |
double time_redirect, time_lookup, time_connect, time_pre, time_start, time_total; |
| 368 |
assert(curl_easy_getinfo(handle, CURLINFO_REDIRECT_TIME, &time_redirect)); |
| 369 |
assert(curl_easy_getinfo(handle, CURLINFO_NAMELOOKUP_TIME, &time_lookup)); |
| 370 |
assert(curl_easy_getinfo(handle, CURLINFO_CONNECT_TIME, &time_connect)); |
| 371 |
assert(curl_easy_getinfo(handle, CURLINFO_PRETRANSFER_TIME, &time_pre)); |
| 372 |
assert(curl_easy_getinfo(handle, CURLINFO_STARTTRANSFER_TIME, &time_start)); |
| 373 |
assert(curl_easy_getinfo(handle, CURLINFO_TOTAL_TIME, &time_total)); |
| 374 |
|
| 375 |
SEXP result = PROTECT(Rf_allocVector(REALSXP, 6)); |
| 376 |
REAL(result)[0] = time_redirect; |
| 377 |
REAL(result)[1] = time_lookup; |
| 378 |
REAL(result)[2] = time_connect; |
| 379 |
REAL(result)[3] = time_pre; |
| 380 |
REAL(result)[4] = time_start; |
| 381 |
REAL(result)[5] = time_total; |
| 382 |
|
| 383 |
SEXP names = PROTECT(Rf_allocVector(STRSXP, 6)); |
| 384 |
SET_STRING_ELT(names, 0, Rf_mkChar("redirect")); |
| 385 |
SET_STRING_ELT(names, 1, Rf_mkChar("namelookup")); |
| 386 |
SET_STRING_ELT(names, 2, Rf_mkChar("connect")); |
| 387 |
SET_STRING_ELT(names, 3, Rf_mkChar("pretransfer")); |
| 388 |
SET_STRING_ELT(names, 4, Rf_mkChar("starttransfer")); |
| 389 |
SET_STRING_ELT(names, 5, Rf_mkChar("total")); |
| 390 |
Rf_setAttrib(result, R_NamesSymbol, names); |
| 391 |
UNPROTECT(2); |
| 392 |
return result; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
static SEXP make_cookievec(CURL *handle){ |
| 397 |
|
| 398 |
struct curl_slist *cookies; |
| 399 |
assert(curl_easy_getinfo(handle, CURLINFO_COOKIELIST, &cookies)); |
| 400 |
SEXP out = slist_to_vec(cookies); |
| 401 |
curl_slist_free_all(cookies); |
| 402 |
return out; |
| 403 |
} |
| 404 |
|
| 405 |
static SEXP make_info_integer(CURL *handle, CURLINFO info){ |
| 406 |
long res_status; |
| 407 |
assert(curl_easy_getinfo(handle, info, &res_status)); |
| 408 |
return Rf_ScalarInteger(res_status); |
| 409 |
} |
| 410 |
|
| 411 |
static SEXP make_info_string(CURL *handle, CURLINFO info){ |
| 412 |
char *res_url = NULL; |
| 413 |
assert(curl_easy_getinfo(handle, info, &res_url)); |
| 414 |
return make_string(res_url); |
| 415 |
} |
| 416 |
|
| 417 |
static SEXP make_info_http_version(CURL * handle){ |
| 418 |
long res = 0; |
| 419 |
assert(curl_easy_getinfo(handle, CURLINFO_HTTP_VERSION, &res)); |
| 420 |
switch (res) { |
| 421 |
case CURL_HTTP_VERSION_1_0: |
| 422 |
case CURL_HTTP_VERSION_1_1: |
| 423 |
return Rf_ScalarInteger(1); |
| 424 |
case CURL_HTTP_VERSION_2_0: |
| 425 |
return Rf_ScalarInteger(2); |
| 426 |
case CURL_HTTP_VERSION_3: |
| 427 |
return Rf_ScalarInteger(3); |
| 428 |
default: |
| 429 |
return Rf_ScalarInteger(NA_INTEGER); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
static SEXP make_filetime(CURL *handle){ |
| 434 |
long filetime; |
| 435 |
assert(curl_easy_getinfo(handle, CURLINFO_FILETIME, &filetime)); |
| 436 |
if(filetime < 0){ |
| 437 |
filetime = NA_INTEGER; |
| 438 |
} |
| 439 |
|
| 440 |
SEXP classes = PROTECT(Rf_allocVector(STRSXP, 2)); |
| 441 |
SET_STRING_ELT(classes, 0, Rf_mkChar("POSIXct")); |
| 442 |
SET_STRING_ELT(classes, 1, Rf_mkChar("POSIXt")); |
| 443 |
|
| 444 |
SEXP out = PROTECT(Rf_ScalarInteger(filetime)); |
| 445 |
Rf_setAttrib(out, R_ClassSymbol, classes); |
| 446 |
UNPROTECT(2); |
| 447 |
return out; |
| 448 |
} |
| 449 |
|
| 450 |
static SEXP make_rawvec(unsigned char *ptr, size_t size){ |
| 451 |
SEXP out = PROTECT(Rf_allocVector(RAWSXP, size)); |
| 452 |
if(size > 0) |
| 453 |
memcpy(RAW(out), ptr, size); |
| 454 |
UNPROTECT(1); |
| 455 |
return out; |
| 456 |
} |
| 457 |
|
| 458 |
static SEXP make_namesvec(void){ |
| 459 |
SEXP names = PROTECT(Rf_allocVector(STRSXP, 10)); |
| 460 |
SET_STRING_ELT(names, 0, Rf_mkChar("url")); |
| 461 |
SET_STRING_ELT(names, 1, Rf_mkChar("status_code")); |
| 462 |
SET_STRING_ELT(names, 2, Rf_mkChar("type")); |
| 463 |
SET_STRING_ELT(names, 3, Rf_mkChar("headers")); |
| 464 |
SET_STRING_ELT(names, 4, Rf_mkChar("modified")); |
| 465 |
SET_STRING_ELT(names, 5, Rf_mkChar("times")); |
| 466 |
SET_STRING_ELT(names, 6, Rf_mkChar("scheme")); |
| 467 |
SET_STRING_ELT(names, 7, Rf_mkChar("http_version")); |
| 468 |
SET_STRING_ELT(names, 8, Rf_mkChar("method")); |
| 469 |
SET_STRING_ELT(names, 9, Rf_mkChar("content")); |
| 470 |
UNPROTECT(1); |
| 471 |
return names; |
| 472 |
} |
| 473 |
|
| 474 |
SEXP R_get_handle_cookies(SEXP ptr){ |
| 475 |
return make_cookievec(get_handle(ptr)); |
| 476 |
} |
| 477 |
|
| 478 |
SEXP make_handle_response(reference *ref){ |
| 479 |
CURL *handle = ref->handle; |
| 480 |
SEXP res = PROTECT(Rf_allocVector(VECSXP, 10)); |
| 481 |
SET_VECTOR_ELT(res, 0, make_info_string(handle, CURLINFO_EFFECTIVE_URL)); |
| 482 |
SET_VECTOR_ELT(res, 1, make_info_integer(handle, CURLINFO_RESPONSE_CODE)); |
| 483 |
SET_VECTOR_ELT(res, 2, make_info_string(handle, CURLINFO_CONTENT_TYPE)); |
| 484 |
SET_VECTOR_ELT(res, 3, make_rawvec(ref->resheaders.buf, ref->resheaders.size)); |
| 485 |
SET_VECTOR_ELT(res, 4, make_filetime(handle)); |
| 486 |
SET_VECTOR_ELT(res, 5, make_timevec(handle)); |
| 487 |
SET_VECTOR_ELT(res, 6, make_info_string(handle, CURLINFO_SCHEME)); |
| 488 |
SET_VECTOR_ELT(res, 7, make_info_http_version(handle)); |
| 489 |
SET_VECTOR_ELT(res, 8, make_info_string(handle, CURLINFO_EFFECTIVE_METHOD)); |
| 490 |
SET_VECTOR_ELT(res, 9, R_NilValue); |
| 491 |
Rf_setAttrib(res, R_NamesSymbol, make_namesvec()); |
| 492 |
UNPROTECT(1); |
| 493 |
return res; |
| 494 |
} |
| 495 |
|
| 496 |
SEXP R_get_handle_response(SEXP ptr){ |
| 497 |
|
| 498 |
reference *ref = get_ref(ptr); |
| 499 |
return make_handle_response(ref); |
| 500 |
} |
| 501 |
|
| 502 |
SEXP R_get_handle_speed(SEXP ptr){ |
| 503 |
CURL *handle = get_handle(ptr); |
| 504 |
curl_off_t dl = 0; |
| 505 |
curl_off_t ul = 0; |
| 506 |
curl_easy_getinfo(handle, CURLINFO_SPEED_DOWNLOAD_T, &dl); |
| 507 |
curl_easy_getinfo(handle, CURLINFO_SPEED_UPLOAD_T, &ul); |
| 508 |
SEXP out = Rf_allocVector(REALSXP, 2); |
| 509 |
REAL(out)[0] = (double) dl; |
| 510 |
REAL(out)[1] = (double) ul; |
| 511 |
return out; |
| 512 |
} |
| 513 |
|
| 514 |
SEXP R_get_handle_clength(SEXP ptr){ |
| 515 |
CURL *handle = get_handle(ptr); |
| 516 |
curl_off_t cl = 0; |
| 517 |
curl_easy_getinfo(handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &cl); |
| 518 |
return Rf_ScalarReal((double) cl < 0 ? NA_REAL : cl); |
| 519 |
} |
| 520 |
|
| 521 |
SEXP R_get_handle_received(SEXP ptr){ |
| 522 |
CURL *handle = get_handle(ptr); |
| 523 |
curl_off_t dl = 0; |
| 524 |
curl_easy_getinfo(handle, CURLINFO_SIZE_DOWNLOAD_T, &dl); |
| 525 |
return Rf_ScalarReal((double) dl); |
| 526 |
} |
| 527 |
|
| 528 |
SEXP R_get_handle_mtime(SEXP ptr){ |
| 529 |
return make_filetime(get_handle(ptr)); |
| 530 |
} |
| 531 |
|
| 532 |
SEXP R_total_handles(void){ |
| 533 |
return(Rf_ScalarInteger(total_handles)); |
| 534 |
} |