Debian Codegraph @

src/interrupt.c

raw symbols SCIP
1 /* Non-blocking drop-in replacement for curl_easy_perform with support for
2 * R interruptions. Based on: https://curl.se/libcurl/c/multi-single.html
3 */
4
5 #include <Rinternals.h>
6 #include "curl-common.h"
7
8 /* Check for interrupt without long jumping */
9 void check_interrupt_fn(void *dummy) {
10 R_CheckUserInterrupt();
11 }
12
13 int pending_interrupt(void) {
14 return !(R_ToplevelExec(check_interrupt_fn, NULL));
15 }
16
17 static int str_starts_with(const char *a, const char *b) {
18 if(strncmp(a, b, strlen(b)) == 0) return 1;
19 return 0;
20 }
21
22 /* created in init.c */
23 extern CURLM * shared_multi_handle;
24
25 /* Don't call Rf_error() until we remove the handle from the multi handle! */
26 CURLcode curl_perform_with_interrupt(CURL *handle){
27 /* start settings */
28 CURLcode status = CURLE_FAILED_INIT;
29 CURLM * temp_multi_handle = NULL;
30 CURLM * multi_handle = NULL;
31 int still_running = 1;
32
33 /* Do not reuse FTP connections, because it is buggy */
34 /* For example https://github.com/jeroen/curl/issues/348 */
35 const char *effective_url = NULL;
36 curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &effective_url);
37 if(effective_url && str_starts_with(effective_url, "ftp")){
38 temp_multi_handle = curl_multi_init();
39 multi_handle = temp_multi_handle;
40 } else {
41 multi_handle = shared_multi_handle;
42 }
43
44 if(CURLM_OK != curl_multi_add_handle(multi_handle, handle)){
45 return CURLE_FAILED_INIT;
46 }
47
48 /* non blocking downloading */
49 while(still_running) {
50 if(pending_interrupt()){
51 status = CURLE_ABORTED_BY_CALLBACK;
52 break;
53 }
54
55 int numfds;
56 if(curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds) != CURLM_OK)
57 break;
58
59 /* check for multi errors */
60 if(curl_multi_perform(multi_handle, &(still_running)) != CURLM_OK)
61 break;
62 }
63
64 /* set status if handle has completed. This might be overkill */
65 if(!still_running){
66 int msgq = 0;
67 do {
68 CURLMsg *m = curl_multi_info_read(multi_handle, &msgq);
69 if(m && (m->msg == CURLMSG_DONE)){
70 status = m->data.result;
71 break;
72 }
73 } while (msgq > 0);
74 }
75
76 /* cleanup first */
77 curl_multi_remove_handle(multi_handle, handle);
78 if(temp_multi_handle)
79 curl_multi_cleanup(temp_multi_handle);
80 return status;
81 }