| 1 |
#include "curl-common.h" |
| 2 |
#include <time.h> |
| 3 |
|
| 4 |
#include <Rversion.h> |
| 5 |
#if R_VERSION < R_Version(4, 5, 0) |
| 6 |
# define R_ClosureFormals(x) FORMALS(x) |
| 7 |
#endif |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
multiref *get_multiref(SEXP ptr){ |
| 15 |
if(TYPEOF(ptr) != EXTPTRSXP || !Rf_inherits(ptr, "curl_multi")) |
| 16 |
Rf_error("pool ptr is not a curl_multi handle"); |
| 17 |
multiref *mref = (multiref*) R_ExternalPtrAddr(ptr); |
| 18 |
if(!mref) |
| 19 |
Rf_error("multiref pointer is dead"); |
| 20 |
return mref; |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
CURLM *get_curlm(SEXP ptr){ |
| 25 |
CURLM *multi; |
| 26 |
if(Rf_inherits(ptr, "curl")){ |
| 27 |
ptr = Rf_getAttrib(ptr, Rf_install("conn_id")); |
| 28 |
if (TYPEOF(ptr) != EXTPTRSXP) |
| 29 |
Rf_error("pool ptr is not a curl connection"); |
| 30 |
multi = (CURLM*) R_ExternalPtrAddr(ptr); |
| 31 |
if(!multi) |
| 32 |
Rf_error("CURLM pointer is dead"); |
| 33 |
} else { |
| 34 |
multiref *mref = get_multiref(ptr); |
| 35 |
multi = mref->m; |
| 36 |
} |
| 37 |
return multi; |
| 38 |
} |
| 39 |
|
| 40 |
void multi_release(reference *ref){ |
| 41 |
|
| 42 |
CURL *handle = ref->handle; |
| 43 |
CURLM *multi = ref->async.mref->m; |
| 44 |
massert(curl_multi_remove_handle(multi, handle)); |
| 45 |
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, NULL); |
| 46 |
curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL); |
| 47 |
|
| 48 |
|
| 49 |
ref->async.mref->handles = reflist_remove(ref->async.mref->handles, ref->handleptr); |
| 50 |
R_SetExternalPtrProtected(ref->async.mref->multiptr, ref->async.mref->handles); |
| 51 |
SET_VECTOR_ELT(R_ExternalPtrProtected(ref->handleptr), 0, R_NilValue); |
| 52 |
|
| 53 |
|
| 54 |
if(ref->async.content.buf){ |
| 55 |
free(ref->async.content.buf); |
| 56 |
ref->async.content.buf = NULL; |
| 57 |
ref->async.content.size = 0; |
| 58 |
} |
| 59 |
ref->async.mref = NULL; |
| 60 |
ref->async.content.buf = NULL; |
| 61 |
ref->async.content.size = 0; |
| 62 |
ref->async.complete = NULL; |
| 63 |
ref->async.error = NULL; |
| 64 |
ref->async.data = NULL; |
| 65 |
ref->async.node = NULL; |
| 66 |
|
| 67 |
|
| 68 |
ref->locked = 0; |
| 69 |
ref->refCount--; |
| 70 |
clean_handle(ref); |
| 71 |
} |
| 72 |
|
| 73 |
SEXP R_multi_cancel(SEXP handle_ptr){ |
| 74 |
reference *ref = get_ref(handle_ptr); |
| 75 |
if(ref->async.mref) |
| 76 |
multi_release(ref); |
| 77 |
return handle_ptr; |
| 78 |
} |
| 79 |
|
| 80 |
SEXP R_multi_add(SEXP handle_ptr, SEXP cb_complete, SEXP cb_error, SEXP cb_data, SEXP pool_ptr){ |
| 81 |
multiref *mref = get_multiref(pool_ptr); |
| 82 |
CURLM *multi = mref->m; |
| 83 |
|
| 84 |
reference *ref = get_ref(handle_ptr); |
| 85 |
if(ref->locked) |
| 86 |
Rf_error("Handle is locked. Probably in use in a connection or async request."); |
| 87 |
|
| 88 |
|
| 89 |
if(Rf_isFunction(cb_data)){ |
| 90 |
curl_easy_setopt(ref->handle, CURLOPT_WRITEFUNCTION, (curl_write_callback) data_callback); |
| 91 |
curl_easy_setopt(ref->handle, CURLOPT_WRITEDATA, cb_data); |
| 92 |
} else { |
| 93 |
curl_easy_setopt(ref->handle, CURLOPT_WRITEFUNCTION, append_buffer); |
| 94 |
curl_easy_setopt(ref->handle, CURLOPT_WRITEDATA, &(ref->async.content)); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
massert(curl_multi_add_handle(multi, ref->handle)); |
| 99 |
|
| 100 |
|
| 101 |
ref->async.mref = mref; |
| 102 |
mref->handles = reflist_add(mref->handles, handle_ptr); |
| 103 |
R_SetExternalPtrProtected(pool_ptr, mref->handles); |
| 104 |
|
| 105 |
|
| 106 |
ref->async.complete = cb_complete; |
| 107 |
ref->async.error = cb_error; |
| 108 |
ref->async.data = cb_data; |
| 109 |
SET_VECTOR_ELT(R_ExternalPtrProtected(handle_ptr), 0, |
| 110 |
Rf_list3(cb_error, cb_complete, cb_data)); |
| 111 |
|
| 112 |
|
| 113 |
ref->refCount++; |
| 114 |
ref->locked = 1; |
| 115 |
return handle_ptr; |
| 116 |
} |
| 117 |
|
| 118 |
SEXP R_multi_run(SEXP pool_ptr, SEXP timeout, SEXP max){ |
| 119 |
multiref *mref = get_multiref(pool_ptr); |
| 120 |
CURLM *multi = mref->m; |
| 121 |
|
| 122 |
int total_pending = -1; |
| 123 |
int total_success = 0; |
| 124 |
int total_fail = 0; |
| 125 |
int result_max = Rf_asInteger(max); |
| 126 |
double time_max = Rf_asReal(timeout); |
| 127 |
time_t time_start = time(NULL); |
| 128 |
|
| 129 |
double seconds_elapsed = 0; |
| 130 |
while(1) { |
| 131 |
|
| 132 |
int dirty = 0; |
| 133 |
int msgq = 1; |
| 134 |
while (msgq > 0) { |
| 135 |
CURLMsg *m = curl_multi_info_read(multi, &msgq); |
| 136 |
if(m && (m->msg == CURLMSG_DONE)){ |
| 137 |
dirty = 1; |
| 138 |
reference *ref = NULL; |
| 139 |
CURL *handle = m->easy_handle; |
| 140 |
CURLcode status = m->data.result; |
| 141 |
assert(curl_easy_getinfo(handle, CURLINFO_PRIVATE, (char**) &ref)); |
| 142 |
|
| 143 |
|
| 144 |
SEXP cb_complete = PROTECT(ref->async.complete); |
| 145 |
SEXP cb_error = PROTECT(ref->async.error); |
| 146 |
SEXP cb_data = PROTECT(ref->async.data); |
| 147 |
SEXP buf = PROTECT(Rf_allocVector(RAWSXP, ref->async.content.size)); |
| 148 |
if(ref->async.content.buf && ref->async.content.size) |
| 149 |
memcpy(RAW(buf), ref->async.content.buf, ref->async.content.size); |
| 150 |
|
| 151 |
|
| 152 |
multi_release(ref); |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
if(Rf_isFunction(cb_data)){ |
| 157 |
SEXP buf = PROTECT(Rf_allocVector(RAWSXP, 0)); |
| 158 |
SEXP call = PROTECT(Rf_lang3(cb_data, buf, Rf_ScalarInteger(1))); |
| 159 |
Rf_eval(call, R_GlobalEnv); |
| 160 |
UNPROTECT(2); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
if(status == CURLE_OK){ |
| 165 |
total_success++; |
| 166 |
if(Rf_isFunction(cb_complete)){ |
| 167 |
int arglen = Rf_length(R_ClosureFormals(cb_complete)); |
| 168 |
SEXP out = PROTECT(make_handle_response(ref)); |
| 169 |
SET_VECTOR_ELT(out, 9, buf); |
| 170 |
SEXP call = PROTECT(Rf_lcons(cb_complete, arglen ? Rf_lcons(out, R_NilValue) : R_NilValue)); |
| 171 |
|
| 172 |
Rf_eval(call, R_GlobalEnv); |
| 173 |
UNPROTECT(2); |
| 174 |
} |
| 175 |
} else { |
| 176 |
total_fail++; |
| 177 |
if(Rf_isFunction(cb_error)){ |
| 178 |
raise_libcurl_error(status, ref, cb_error); |
| 179 |
} |
| 180 |
} |
| 181 |
UNPROTECT(4); |
| 182 |
} |
| 183 |
R_CheckUserInterrupt(); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
R_CheckUserInterrupt(); |
| 189 |
|
| 190 |
|
| 191 |
if(total_pending > 0 && result_max > 0 && total_success + total_fail >= result_max) |
| 192 |
break; |
| 193 |
if(time_max == 0 && total_pending != -1) |
| 194 |
break; |
| 195 |
if(time_max > 0){ |
| 196 |
seconds_elapsed = (double) (time(NULL) - time_start); |
| 197 |
if(seconds_elapsed > time_max) |
| 198 |
break; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
if(total_pending == 0 && !dirty) |
| 203 |
break; |
| 204 |
|
| 205 |
int numfds; |
| 206 |
double waitforit = fmin(time_max - seconds_elapsed, 1); |
| 207 |
if(time_max > 0) |
| 208 |
massert(curl_multi_wait(multi, NULL, 0, (int) waitforit * 1000, &numfds)); |
| 209 |
|
| 210 |
|
| 211 |
if(curl_multi_perform(multi, &(total_pending)) != CURLM_OK) |
| 212 |
break; |
| 213 |
} |
| 214 |
|
| 215 |
SEXP res = PROTECT(Rf_allocVector(VECSXP, 3)); |
| 216 |
SET_VECTOR_ELT(res, 0, Rf_ScalarInteger(total_success)); |
| 217 |
SET_VECTOR_ELT(res, 1, Rf_ScalarInteger(total_fail)); |
| 218 |
SET_VECTOR_ELT(res, 2, Rf_ScalarInteger(total_pending)); |
| 219 |
|
| 220 |
SEXP names = PROTECT(Rf_allocVector(STRSXP, 3)); |
| 221 |
SET_STRING_ELT(names, 0, Rf_mkChar("success")); |
| 222 |
SET_STRING_ELT(names, 1, Rf_mkChar("error")); |
| 223 |
SET_STRING_ELT(names, 2, Rf_mkChar("pending")); |
| 224 |
Rf_setAttrib(res, R_NamesSymbol, names); |
| 225 |
UNPROTECT(2); |
| 226 |
return res; |
| 227 |
} |
| 228 |
|
| 229 |
void fin_multi(SEXP ptr){ |
| 230 |
multiref *mref = get_multiref(ptr); |
| 231 |
SEXP handles = mref->handles; |
| 232 |
while(handles != R_NilValue){ |
| 233 |
multi_release(get_ref(CAR(handles))); |
| 234 |
handles = CDR(handles); |
| 235 |
} |
| 236 |
curl_multi_cleanup(mref->m); |
| 237 |
free(mref); |
| 238 |
R_ClearExternalPtr(ptr); |
| 239 |
} |
| 240 |
|
| 241 |
SEXP R_multi_new(void){ |
| 242 |
multiref *ref = calloc(1, sizeof(multiref)); |
| 243 |
ref->m = curl_multi_init(); |
| 244 |
ref->handles = reflist_init(); |
| 245 |
SEXP ptr = PROTECT(R_MakeExternalPtr(ref, R_NilValue, ref->handles)); |
| 246 |
ref->multiptr = ptr; |
| 247 |
R_RegisterCFinalizerEx(ptr, fin_multi, 1); |
| 248 |
Rf_setAttrib(ptr, R_ClassSymbol, Rf_mkString("curl_multi")); |
| 249 |
UNPROTECT(1); |
| 250 |
return ptr; |
| 251 |
} |
| 252 |
|
| 253 |
SEXP R_multi_setopt(SEXP pool_ptr, SEXP total_con, SEXP host_con, SEXP max_streams, SEXP multiplex){ |
| 254 |
CURLM *multi = get_multiref(pool_ptr)->m; |
| 255 |
massert(curl_multi_setopt(multi, CURLMOPT_MAX_TOTAL_CONNECTIONS, (long) Rf_asInteger(total_con))); |
| 256 |
massert(curl_multi_setopt(multi, CURLMOPT_MAX_HOST_CONNECTIONS, (long) Rf_asInteger(host_con))); |
| 257 |
massert(curl_multi_setopt(multi, CURLMOPT_PIPELINING, |
| 258 |
Rf_asLogical(multiplex) ? CURLPIPE_MULTIPLEX : CURLPIPE_NOTHING)); |
| 259 |
massert(curl_multi_setopt(multi, CURLMOPT_MAX_CONCURRENT_STREAMS, (long) Rf_asInteger(max_streams))); |
| 260 |
return pool_ptr; |
| 261 |
} |
| 262 |
|
| 263 |
SEXP R_multi_list(SEXP pool_ptr){ |
| 264 |
return get_multiref(pool_ptr)->handles; |
| 265 |
} |
| 266 |
|
| 267 |
SEXP R_multi_fdset(SEXP pool_ptr){ |
| 268 |
CURLM *multi = get_curlm(pool_ptr); |
| 269 |
fd_set read_fd_set, write_fd_set, exc_fd_set; |
| 270 |
int max_fd, i, num_read = 0, num_write = 0, num_exc = 0; |
| 271 |
int *pread, *pwrite, *pexc; |
| 272 |
long timeout; |
| 273 |
SEXP result, names; |
| 274 |
|
| 275 |
FD_ZERO(&read_fd_set); |
| 276 |
FD_ZERO(&write_fd_set); |
| 277 |
FD_ZERO(&exc_fd_set); |
| 278 |
|
| 279 |
massert(curl_multi_fdset(multi, &read_fd_set, &write_fd_set, |
| 280 |
&exc_fd_set, &max_fd)); |
| 281 |
|
| 282 |
massert(curl_multi_timeout(multi, &timeout)); |
| 283 |
|
| 284 |
for (i = 0; i <= max_fd; i++){ |
| 285 |
if (FD_ISSET(i, &read_fd_set)) num_read++; |
| 286 |
if (FD_ISSET(i, &write_fd_set)) num_write++; |
| 287 |
if (FD_ISSET(i, &exc_fd_set)) num_exc++; |
| 288 |
} |
| 289 |
|
| 290 |
result = PROTECT(Rf_allocVector(VECSXP, 4)); |
| 291 |
SET_VECTOR_ELT(result, 0, Rf_allocVector(INTSXP, num_read)); |
| 292 |
SET_VECTOR_ELT(result, 1, Rf_allocVector(INTSXP, num_write)); |
| 293 |
SET_VECTOR_ELT(result, 2, Rf_allocVector(INTSXP, num_exc)); |
| 294 |
SET_VECTOR_ELT(result, 3, Rf_ScalarReal((double) timeout)); |
| 295 |
|
| 296 |
names = PROTECT(Rf_allocVector(STRSXP, 4)); |
| 297 |
SET_STRING_ELT(names, 0, Rf_mkChar("reads")); |
| 298 |
SET_STRING_ELT(names, 1, Rf_mkChar("writes")); |
| 299 |
SET_STRING_ELT(names, 2, Rf_mkChar("exceptions")); |
| 300 |
SET_STRING_ELT(names, 3, Rf_mkChar("timeout")); |
| 301 |
Rf_setAttrib(result, R_NamesSymbol, names); |
| 302 |
|
| 303 |
pread = INTEGER(VECTOR_ELT(result, 0)); |
| 304 |
pwrite = INTEGER(VECTOR_ELT(result, 1)); |
| 305 |
pexc = INTEGER(VECTOR_ELT(result, 2)); |
| 306 |
|
| 307 |
for (i = 0; i <= max_fd; i++){ |
| 308 |
if (FD_ISSET(i, &read_fd_set)) *(pread++) = i; |
| 309 |
if (FD_ISSET(i, &write_fd_set)) *(pwrite++) = i; |
| 310 |
if (FD_ISSET(i, &exc_fd_set)) *(pexc++) = i; |
| 311 |
} |
| 312 |
|
| 313 |
UNPROTECT(2); |
| 314 |
return result; |
| 315 |
} |