| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include "curl-common.h" |
| 16 |
#include <Rconfig.h> |
| 17 |
|
| 18 |
|
| 19 |
#ifdef WORDS_BIGENDIAN |
| 20 |
#if (defined(__sun) && defined(__SVR4)) |
| 21 |
#include <sys/byteorder.h> |
| 22 |
#elif (defined(__APPLE__) && defined(__ppc__) || defined(__ppc64__)) |
| 23 |
#include <libkern/OSByteOrder.h> |
| 24 |
#define BSWAP_32 OSSwapInt32 |
| 25 |
#elif (defined(__OpenBSD__)) |
| 26 |
#define BSWAP_32(x) swap32(x) |
| 27 |
#elif (defined(__NetBSD__)) |
| 28 |
#include <sys/types.h> |
| 29 |
#include <machine/bswap.h> |
| 30 |
#define BSWAP_32(x) bswap32(x) |
| 31 |
#elif (defined(__GLIBC__)) |
| 32 |
#include <byteswap.h> |
| 33 |
#define BSWAP_32(x) bswap_32(x) |
| 34 |
#elif (defined(_AIX)) |
| 35 |
#define BSWAP_32(x) __builtin_bswap32(x) |
| 36 |
#endif |
| 37 |
#endif |
| 38 |
|
| 39 |
|
| 40 |
#include <R_ext/Connections.h> |
| 41 |
#if ! defined(R_CONNECTIONS_VERSION) || R_CONNECTIONS_VERSION != 1 |
| 42 |
#error "Unsupported connections API version" |
| 43 |
#endif |
| 44 |
|
| 45 |
#define min(a, b) (((a) < (b)) ? (a) : (b)) |
| 46 |
#define R_EOF -1 |
| 47 |
|
| 48 |
typedef struct { |
| 49 |
char *url; |
| 50 |
char *buf; |
| 51 |
char *cur; |
| 52 |
int has_data; |
| 53 |
int has_more; |
| 54 |
int used; |
| 55 |
int partial; |
| 56 |
size_t size; |
| 57 |
size_t limit; |
| 58 |
CURLM *manager; |
| 59 |
CURL *handle; |
| 60 |
reference *ref; |
| 61 |
} request; |
| 62 |
|
| 63 |
|
| 64 |
static size_t push(void *contents, size_t sz, size_t nmemb, void *ctx) { |
| 65 |
|
| 66 |
request* req = (request*) ctx; |
| 67 |
req->has_data = 1; |
| 68 |
|
| 69 |
|
| 70 |
memmove(req->buf, req->cur, req->size); |
| 71 |
|
| 72 |
|
| 73 |
size_t realsize = sz * nmemb; |
| 74 |
size_t newsize = req->size + realsize; |
| 75 |
while(newsize > req->limit) { |
| 76 |
size_t newlimit = 2 * req->limit; |
| 77 |
|
| 78 |
void *newbuf = realloc(req->buf, newlimit); |
| 79 |
if(!newbuf) |
| 80 |
Rf_error("Failure in realloc. Out of memory?"); |
| 81 |
req->buf = newbuf; |
| 82 |
req->limit = newlimit; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
memcpy(req->buf + req->size, contents, realsize); |
| 87 |
req->size = newsize; |
| 88 |
req->cur = req->buf; |
| 89 |
return realsize; |
| 90 |
} |
| 91 |
|
| 92 |
static size_t pop(void *target, size_t max, request *req){ |
| 93 |
size_t copy_size = min(req->size, max); |
| 94 |
memcpy(target, req->cur, copy_size); |
| 95 |
req->cur += copy_size; |
| 96 |
req->size -= copy_size; |
| 97 |
|
| 98 |
return copy_size; |
| 99 |
} |
| 100 |
|
| 101 |
static void check_handles(CURLM *manager, reference *ref) { |
| 102 |
for(int msg = 1; msg > 0;){ |
| 103 |
CURLMsg *out = curl_multi_info_read(manager, &msg); |
| 104 |
if(out) |
| 105 |
assert_status(out->data.result, ref); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
static void fetchdata(request *req) { |
| 110 |
R_CheckUserInterrupt(); |
| 111 |
massert(curl_multi_perform(req->manager, &(req->has_more))); |
| 112 |
check_handles(req->manager, req->ref); |
| 113 |
} |
| 114 |
|
| 115 |
static size_t rcurl_read(void *target, size_t sz, size_t ni, Rconnection con) { |
| 116 |
request *req = (request*) con->private; |
| 117 |
size_t req_size = sz * ni; |
| 118 |
size_t total_size = pop(target, req_size, req); |
| 119 |
if (total_size > 0 && (!con->blocking || req->partial)) { |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
con->incomplete = req->has_more || req->size; |
| 125 |
return total_size; |
| 126 |
} |
| 127 |
|
| 128 |
while((req_size > total_size) && req->has_more) { |
| 129 |
int numfds; |
| 130 |
massert(curl_multi_wait(req->manager, NULL, 0, con->blocking ? 1000 : 10, &numfds)); |
| 131 |
|
| 132 |
fetchdata(req); |
| 133 |
total_size += pop((char*)target + total_size, (req_size-total_size), req); |
| 134 |
|
| 135 |
|
| 136 |
if(!con->blocking || req->partial) |
| 137 |
break; |
| 138 |
} |
| 139 |
con->incomplete = req->has_more || req->size; |
| 140 |
return total_size; |
| 141 |
} |
| 142 |
|
| 143 |
static int rcurl_fgetc(Rconnection con) { |
| 144 |
int x = 0; |
| 145 |
#ifdef WORDS_BIGENDIAN |
| 146 |
return rcurl_read(&x, 1, 1, con) ? BSWAP_32(x) : R_EOF; |
| 147 |
#else |
| 148 |
return rcurl_read(&x, 1, 1, con) ? x : R_EOF; |
| 149 |
#endif |
| 150 |
} |
| 151 |
|
| 152 |
static void cleanup(Rconnection con) { |
| 153 |
request *req = (request*) con->private; |
| 154 |
reference *ref = req->ref; |
| 155 |
|
| 156 |
|
| 157 |
curl_multi_remove_handle(req->manager, req->handle); |
| 158 |
curl_easy_setopt(req->handle, CURLOPT_WRITEFUNCTION, NULL); |
| 159 |
curl_easy_setopt(req->handle, CURLOPT_WRITEDATA, NULL); |
| 160 |
curl_easy_setopt(req->handle, CURLOPT_FAILONERROR, 0L); |
| 161 |
ref->locked = 0; |
| 162 |
|
| 163 |
|
| 164 |
(ref->refCount)--; |
| 165 |
clean_handle(ref); |
| 166 |
|
| 167 |
|
| 168 |
curl_multi_cleanup(req->manager); |
| 169 |
free(req->buf); |
| 170 |
free(req->url); |
| 171 |
free(req); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
static void reset(Rconnection con) { |
| 176 |
request *req = (request*) con->private; |
| 177 |
curl_multi_remove_handle(req->manager, req->handle); |
| 178 |
curl_easy_setopt(req->handle, CURLOPT_WRITEFUNCTION, NULL); |
| 179 |
curl_easy_setopt(req->handle, CURLOPT_WRITEDATA, NULL); |
| 180 |
curl_easy_setopt(req->handle, CURLOPT_FAILONERROR, 0L); |
| 181 |
req->ref->locked = 0; |
| 182 |
con->isopen = FALSE; |
| 183 |
con->text = TRUE; |
| 184 |
con->incomplete = FALSE; |
| 185 |
strcpy(con->mode, "r"); |
| 186 |
} |
| 187 |
|
| 188 |
static Rboolean rcurl_open(Rconnection con) { |
| 189 |
request *req = (request*) con->private; |
| 190 |
|
| 191 |
|
| 192 |
if (con->mode[0] != 'r' || strchr(con->mode, 'w')) |
| 193 |
Rf_error("can only open URLs for reading"); |
| 194 |
|
| 195 |
if(req->ref->locked) |
| 196 |
Rf_error("Handle is already in use elsewhere."); |
| 197 |
|
| 198 |
|
| 199 |
CURL *handle = req->handle; |
| 200 |
assert(curl_easy_setopt(handle, CURLOPT_URL, req->url)); |
| 201 |
assert(curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, push)); |
| 202 |
assert(curl_easy_setopt(handle, CURLOPT_WRITEDATA, req)); |
| 203 |
|
| 204 |
|
| 205 |
massert(curl_multi_add_handle(req->manager, handle)); |
| 206 |
req->ref->locked = 1; |
| 207 |
|
| 208 |
|
| 209 |
req->handle = handle; |
| 210 |
req->cur = req->buf; |
| 211 |
req->size = 0; |
| 212 |
req->used = 1; |
| 213 |
req->has_data = 0; |
| 214 |
req->has_more = 1; |
| 215 |
|
| 216 |
|
| 217 |
int block_open = strchr(con->mode, 's') == NULL; |
| 218 |
int force_open = strchr(con->mode, 'f') != NULL; |
| 219 |
if(block_open && !force_open) |
| 220 |
curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1L); |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
while(block_open && req->has_more && !req->has_data) { |
| 225 |
int numfds; |
| 226 |
massert(curl_multi_wait(req->manager, NULL, 0, 1000, &numfds)); |
| 227 |
if(pending_interrupt()) { |
| 228 |
reset(con); |
| 229 |
assert_message(CURLE_ABORTED_BY_CALLBACK, NULL); |
| 230 |
} |
| 231 |
massert(curl_multi_perform(req->manager, &(req->has_more))); |
| 232 |
for(int msg = 1; msg > 0;){ |
| 233 |
CURLMsg *out = curl_multi_info_read(req->manager, &msg); |
| 234 |
if(out && out->data.result != CURLE_OK){ |
| 235 |
const char *errmsg = strlen(req->ref->errbuf) ? req->ref->errbuf : curl_easy_strerror(out->data.result); |
| 236 |
Rf_warningcall(R_NilValue, "Failed to open '%s': %s", req->url, errmsg); |
| 237 |
reset(con); |
| 238 |
return FALSE; |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
con->text = strchr(con->mode, 'b') ? FALSE : TRUE; |
| 249 |
con->isopen = TRUE; |
| 250 |
con->incomplete = TRUE; |
| 251 |
return TRUE; |
| 252 |
} |
| 253 |
|
| 254 |
SEXP R_curl_connection(SEXP url, SEXP ptr, SEXP partial) { |
| 255 |
if(!Rf_isString(url)) |
| 256 |
Rf_error("Argument 'url' must be string."); |
| 257 |
|
| 258 |
|
| 259 |
Rconnection con; |
| 260 |
|
| 261 |
|
| 262 |
SEXP rc = PROTECT(R_new_custom_connection(Rf_translateChar(STRING_ELT(url, 0)), "r", "curl", &con)); |
| 263 |
|
| 264 |
|
| 265 |
request *req = malloc(sizeof(request)); |
| 266 |
req->handle = get_handle(ptr); |
| 267 |
req->ref = get_ref(ptr); |
| 268 |
req->limit = CURL_MAX_WRITE_SIZE; |
| 269 |
req->buf = malloc(req->limit); |
| 270 |
req->manager = curl_multi_init(); |
| 271 |
req->partial = Rf_asLogical(partial); |
| 272 |
req->used = 0; |
| 273 |
|
| 274 |
|
| 275 |
req->url = malloc(strlen(Rf_translateCharUTF8(Rf_asChar(url))) + 1); |
| 276 |
strcpy(req->url, Rf_translateCharUTF8(Rf_asChar(url))); |
| 277 |
|
| 278 |
|
| 279 |
con->incomplete = FALSE; |
| 280 |
con->private = req; |
| 281 |
con->canseek = FALSE; |
| 282 |
con->canwrite = FALSE; |
| 283 |
con->isopen = FALSE; |
| 284 |
con->blocking = TRUE; |
| 285 |
con->text = TRUE; |
| 286 |
con->UTF8out = TRUE; |
| 287 |
con->open = rcurl_open; |
| 288 |
con->close = reset; |
| 289 |
con->destroy = cleanup; |
| 290 |
con->read = rcurl_read; |
| 291 |
con->fgetc = rcurl_fgetc; |
| 292 |
con->fgetc_internal = rcurl_fgetc; |
| 293 |
|
| 294 |
|
| 295 |
(req->ref->refCount)++; |
| 296 |
|
| 297 |
|
| 298 |
R_SetExternalPtrAddr((SEXP) con->ex_ptr, req->manager); |
| 299 |
|
| 300 |
UNPROTECT(1); |
| 301 |
return rc; |
| 302 |
} |