Debian Codegraph @

src/dryrun.c

raw symbols SCIP
1 /* *
2 * Blocking easy interfaces to libcurl for R.
3 * Example: https://curl.se/libcurl/c/getinmemory.html
4 */
5
6 #include "curl-common.h"
7
8 static size_t write_nothing(void *contents, size_t sz, size_t nmemb, void *ctx) {
9 return sz * nmemb;
10 }
11
12 static void run_httpuv(void *dummy) {
13 SEXP expr = PROTECT(Rf_lang1(Rf_install("later_wrapper")));
14 SEXP env = PROTECT(R_FindNamespace(Rf_mkString("curl")));
15 Rf_eval(expr, env);
16 UNPROTECT(2);
17 }
18
19 static int process_server(void) {
20 return !(R_ToplevelExec(run_httpuv, NULL));
21 }
22
23 SEXP R_curl_dryrun(SEXP ptr){
24 CURL *handle = get_handle(ptr);
25 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_nothing);
26 CURLM * multi_handle = curl_multi_init();
27 if(CURLM_OK != curl_multi_add_handle(multi_handle, handle))
28 Rf_error("Failed to add handle");
29 int still_running = 1;
30 while(still_running) {
31 if(process_server())
32 break;
33 if(curl_multi_perform(multi_handle, &(still_running)) != CURLM_OK)
34 break;
35 }
36 int msgq = 0;
37 CURLMsg *m = curl_multi_info_read(multi_handle, &msgq);
38 CURLcode status = (m && (m->msg == CURLMSG_DONE)) ? m->data.result : CURLE_ABORTED_BY_CALLBACK;
39 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, NULL);
40 curl_multi_remove_handle(multi_handle, handle);
41 curl_multi_cleanup(multi_handle);
42 assert_status(status, get_ref(ptr));
43 return R_NilValue;
44 }