11 #include <sys/errno.h>
14 #include <curl/curl.h>
21 "title" :
"VuoUrlFetch",
39 curl_global_init(CURL_GLOBAL_DEFAULT);
57 size_t realsize = size * nmemb;
60 mem->memory = (
char *)realloc(mem->memory, mem->size + realsize + 1);
61 if(mem->memory == NULL)
63 VUserLog(
"Error: realloc() returned NULL (out of memory?).");
67 memcpy(&(mem->memory[mem->size]), contents, realsize);
68 mem->size += realsize;
69 mem->memory[mem->size] = 0;
85 bool VuoUrl_fetch(
const char *url,
void **data,
unsigned int *dataLength)
90 if (strncmp(url,
"data:", 5) == 0)
95 const char *urlData = url + 5;
98 urlData = strchr(urlData,
',');
106 bool isBase64 = (urlData - url >= 5 + 7 + 1)
107 && (strncmp(urlData - 7 - 1,
";base64", 7) == 0);
114 long long outputLength;
116 *dataLength = outputLength;
120 *data = strdup(decoded);
121 *dataLength = strlen(decoded);
132 curl = curl_easy_init();
135 VUserLog(
"Error: cURL initialization failed.");
138 VuoDefer(^{ curl_easy_cleanup(curl); });
140 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
144 curl_easy_setopt(curl, CURLOPT_URL, resolvedUrl);
146 curl_easy_setopt(curl, CURLOPT_USERAGENT,
"Vuo/" VUO_VERSION_STRING
" (https://vuo.org/)");
148 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
149 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
152 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (
void *)&buffer);
154 res = curl_easy_perform(curl);
157 if (res == CURLE_FILE_COULDNT_READ_FILE)
167 VUserLog(
"Error: Could not read URL \"%s\"", resolvedUrl);
171 FILE *fp = fopen(posixPath,
"rb");
174 VUserLog(
"Error: Could not read file \"%s\"", posixPath);
179 fseek(fp, 0, SEEK_END);
180 *dataLength = ftell(fp);
182 *data = (
char *)malloc(*dataLength);
183 if (fread(*data, 1, *dataLength, fp) != *dataLength)
185 VUserLog(
"Error: Could not read all the data from file \"%s\": %s", posixPath, strerror(errno));
191 VUserLog(
"Error: cURL request failed: %s (%d)", curl_easy_strerror(res), res);
195 *data = buffer.memory;
196 *dataLength = buffer.size;