]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
72648326 | 2 | |
b5efdb8a | 3 | #include "alloc-util.h" |
72648326 | 4 | #include "curl-util.h" |
3ffd4af2 | 5 | #include "fd-util.h" |
e520e0fc | 6 | #include "locale-util.h" |
3ffd4af2 | 7 | #include "string-util.h" |
72648326 LP |
8 | |
9 | static void curl_glue_check_finished(CurlGlue *g) { | |
10 | CURLMsg *msg; | |
11 | int k = 0; | |
12 | ||
13 | assert(g); | |
14 | ||
15 | msg = curl_multi_info_read(g->curl, &k); | |
16 | if (!msg) | |
17 | return; | |
18 | ||
19 | if (msg->msg != CURLMSG_DONE) | |
20 | return; | |
21 | ||
22 | if (g->on_finished) | |
23 | g->on_finished(g, msg->easy_handle, msg->data.result); | |
24 | } | |
25 | ||
26 | static int curl_glue_on_io(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
27 | CurlGlue *g = userdata; | |
28 | int action, k = 0, translated_fd; | |
29 | ||
30 | assert(s); | |
31 | assert(g); | |
32 | ||
23e096cc | 33 | translated_fd = PTR_TO_FD(hashmap_get(g->translate_fds, FD_TO_PTR(fd))); |
72648326 | 34 | |
d94a24ca | 35 | if (FLAGS_SET(revents, EPOLLIN | EPOLLOUT)) |
72648326 LP |
36 | action = CURL_POLL_INOUT; |
37 | else if (revents & EPOLLIN) | |
38 | action = CURL_POLL_IN; | |
39 | else if (revents & EPOLLOUT) | |
40 | action = CURL_POLL_OUT; | |
41 | else | |
42 | action = 0; | |
43 | ||
e6b538d0 | 44 | if (curl_multi_socket_action(g->curl, translated_fd, action, &k) != CURLM_OK) { |
72648326 LP |
45 | log_debug("Failed to propagate IO event."); |
46 | return -EINVAL; | |
47 | } | |
48 | ||
49 | curl_glue_check_finished(g); | |
50 | return 0; | |
51 | } | |
52 | ||
53 | static int curl_glue_socket_callback(CURLM *curl, curl_socket_t s, int action, void *userdata, void *socketp) { | |
54 | sd_event_source *io; | |
55 | CurlGlue *g = userdata; | |
56 | uint32_t events = 0; | |
57 | int r; | |
58 | ||
59 | assert(curl); | |
60 | assert(g); | |
61 | ||
23e096cc | 62 | io = hashmap_get(g->ios, FD_TO_PTR(s)); |
72648326 LP |
63 | |
64 | if (action == CURL_POLL_REMOVE) { | |
65 | if (io) { | |
66 | int fd; | |
67 | ||
68 | fd = sd_event_source_get_io_fd(io); | |
69 | assert(fd >= 0); | |
70 | ||
71 | sd_event_source_set_enabled(io, SD_EVENT_OFF); | |
72 | sd_event_source_unref(io); | |
73 | ||
23e096cc LP |
74 | hashmap_remove(g->ios, FD_TO_PTR(s)); |
75 | hashmap_remove(g->translate_fds, FD_TO_PTR(fd)); | |
72648326 LP |
76 | |
77 | safe_close(fd); | |
78 | } | |
79 | ||
80 | return 0; | |
81 | } | |
82 | ||
83 | r = hashmap_ensure_allocated(&g->ios, &trivial_hash_ops); | |
84 | if (r < 0) { | |
85 | log_oom(); | |
86 | return -1; | |
87 | } | |
88 | ||
89 | r = hashmap_ensure_allocated(&g->translate_fds, &trivial_hash_ops); | |
90 | if (r < 0) { | |
91 | log_oom(); | |
92 | return -1; | |
93 | } | |
94 | ||
95 | if (action == CURL_POLL_IN) | |
96 | events = EPOLLIN; | |
97 | else if (action == CURL_POLL_OUT) | |
98 | events = EPOLLOUT; | |
99 | else if (action == CURL_POLL_INOUT) | |
100 | events = EPOLLIN|EPOLLOUT; | |
101 | ||
102 | if (io) { | |
103 | if (sd_event_source_set_io_events(io, events) < 0) | |
104 | return -1; | |
105 | ||
106 | if (sd_event_source_set_enabled(io, SD_EVENT_ON) < 0) | |
107 | return -1; | |
108 | } else { | |
109 | _cleanup_close_ int fd = -1; | |
110 | ||
111 | /* When curl needs to remove an fd from us it closes | |
112 | * the fd first, and only then calls into us. This is | |
113 | * nasty, since we cannot pass the fd on to epoll() | |
114 | * anymore. Hence, duplicate the fds here, and keep a | |
115 | * copy for epoll which we control after use. */ | |
116 | ||
117 | fd = fcntl(s, F_DUPFD_CLOEXEC, 3); | |
118 | if (fd < 0) | |
119 | return -1; | |
120 | ||
121 | if (sd_event_add_io(g->event, &io, fd, events, curl_glue_on_io, g) < 0) | |
122 | return -1; | |
123 | ||
edfd706d | 124 | (void) sd_event_source_set_description(io, "curl-io"); |
72648326 | 125 | |
23e096cc | 126 | r = hashmap_put(g->ios, FD_TO_PTR(s), io); |
72648326 LP |
127 | if (r < 0) { |
128 | log_oom(); | |
129 | sd_event_source_unref(io); | |
130 | return -1; | |
131 | } | |
132 | ||
23e096cc | 133 | r = hashmap_put(g->translate_fds, FD_TO_PTR(fd), FD_TO_PTR(s)); |
72648326 LP |
134 | if (r < 0) { |
135 | log_oom(); | |
23e096cc | 136 | hashmap_remove(g->ios, FD_TO_PTR(s)); |
72648326 LP |
137 | sd_event_source_unref(io); |
138 | return -1; | |
139 | } | |
140 | ||
141 | fd = -1; | |
142 | } | |
143 | ||
144 | return 0; | |
145 | } | |
146 | ||
147 | static int curl_glue_on_timer(sd_event_source *s, uint64_t usec, void *userdata) { | |
148 | CurlGlue *g = userdata; | |
149 | int k = 0; | |
150 | ||
151 | assert(s); | |
152 | assert(g); | |
153 | ||
154 | if (curl_multi_socket_action(g->curl, CURL_SOCKET_TIMEOUT, 0, &k) != CURLM_OK) { | |
155 | log_debug("Failed to propagate timeout."); | |
156 | return -EINVAL; | |
157 | } | |
158 | ||
159 | curl_glue_check_finished(g); | |
160 | return 0; | |
161 | } | |
162 | ||
163 | static int curl_glue_timer_callback(CURLM *curl, long timeout_ms, void *userdata) { | |
164 | CurlGlue *g = userdata; | |
165 | usec_t usec; | |
166 | ||
167 | assert(curl); | |
168 | assert(g); | |
169 | ||
170 | if (timeout_ms < 0) { | |
171 | if (g->timer) { | |
172 | if (sd_event_source_set_enabled(g->timer, SD_EVENT_OFF) < 0) | |
173 | return -1; | |
174 | } | |
175 | ||
176 | return 0; | |
177 | } | |
178 | ||
179 | usec = now(clock_boottime_or_monotonic()) + (usec_t) timeout_ms * USEC_PER_MSEC + USEC_PER_MSEC - 1; | |
180 | ||
181 | if (g->timer) { | |
182 | if (sd_event_source_set_time(g->timer, usec) < 0) | |
183 | return -1; | |
184 | ||
185 | if (sd_event_source_set_enabled(g->timer, SD_EVENT_ONESHOT) < 0) | |
186 | return -1; | |
187 | } else { | |
188 | if (sd_event_add_time(g->event, &g->timer, clock_boottime_or_monotonic(), usec, 0, curl_glue_on_timer, g) < 0) | |
189 | return -1; | |
190 | ||
edfd706d | 191 | (void) sd_event_source_set_description(g->timer, "curl-timer"); |
72648326 LP |
192 | } |
193 | ||
194 | return 0; | |
195 | } | |
196 | ||
197 | CurlGlue *curl_glue_unref(CurlGlue *g) { | |
198 | sd_event_source *io; | |
199 | ||
200 | if (!g) | |
201 | return NULL; | |
202 | ||
203 | if (g->curl) | |
204 | curl_multi_cleanup(g->curl); | |
205 | ||
206 | while ((io = hashmap_steal_first(g->ios))) { | |
207 | int fd; | |
208 | ||
209 | fd = sd_event_source_get_io_fd(io); | |
210 | assert(fd >= 0); | |
211 | ||
23e096cc | 212 | hashmap_remove(g->translate_fds, FD_TO_PTR(fd)); |
72648326 LP |
213 | |
214 | safe_close(fd); | |
215 | sd_event_source_unref(io); | |
216 | } | |
217 | ||
218 | hashmap_free(g->ios); | |
219 | ||
220 | sd_event_source_unref(g->timer); | |
221 | sd_event_unref(g->event); | |
6b430fdb | 222 | return mfree(g); |
72648326 LP |
223 | } |
224 | ||
225 | int curl_glue_new(CurlGlue **glue, sd_event *event) { | |
226 | _cleanup_(curl_glue_unrefp) CurlGlue *g = NULL; | |
227 | int r; | |
228 | ||
229 | g = new0(CurlGlue, 1); | |
230 | if (!g) | |
231 | return -ENOMEM; | |
232 | ||
233 | if (event) | |
234 | g->event = sd_event_ref(event); | |
235 | else { | |
236 | r = sd_event_default(&g->event); | |
237 | if (r < 0) | |
238 | return r; | |
239 | } | |
240 | ||
241 | g->curl = curl_multi_init(); | |
242 | if (!g->curl) | |
243 | return -ENOMEM; | |
244 | ||
245 | if (curl_multi_setopt(g->curl, CURLMOPT_SOCKETDATA, g) != CURLM_OK) | |
246 | return -EINVAL; | |
247 | ||
248 | if (curl_multi_setopt(g->curl, CURLMOPT_SOCKETFUNCTION, curl_glue_socket_callback) != CURLM_OK) | |
249 | return -EINVAL; | |
250 | ||
251 | if (curl_multi_setopt(g->curl, CURLMOPT_TIMERDATA, g) != CURLM_OK) | |
252 | return -EINVAL; | |
253 | ||
254 | if (curl_multi_setopt(g->curl, CURLMOPT_TIMERFUNCTION, curl_glue_timer_callback) != CURLM_OK) | |
255 | return -EINVAL; | |
256 | ||
1cc6c93a | 257 | *glue = TAKE_PTR(g); |
72648326 LP |
258 | |
259 | return 0; | |
260 | } | |
261 | ||
262 | int curl_glue_make(CURL **ret, const char *url, void *userdata) { | |
263 | const char *useragent; | |
264 | CURL *c; | |
265 | int r; | |
266 | ||
267 | assert(ret); | |
268 | assert(url); | |
269 | ||
270 | c = curl_easy_init(); | |
271 | if (!c) | |
272 | return -ENOMEM; | |
273 | ||
274 | /* curl_easy_setopt(c, CURLOPT_VERBOSE, 1L); */ | |
275 | ||
276 | if (curl_easy_setopt(c, CURLOPT_URL, url) != CURLE_OK) { | |
277 | r = -EIO; | |
278 | goto fail; | |
279 | } | |
280 | ||
281 | if (curl_easy_setopt(c, CURLOPT_PRIVATE, userdata) != CURLE_OK) { | |
282 | r = -EIO; | |
283 | goto fail; | |
284 | } | |
285 | ||
63c372cb | 286 | useragent = strjoina(program_invocation_short_name, "/" PACKAGE_VERSION); |
72648326 LP |
287 | if (curl_easy_setopt(c, CURLOPT_USERAGENT, useragent) != CURLE_OK) { |
288 | r = -EIO; | |
289 | goto fail; | |
290 | } | |
291 | ||
292 | if (curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L) != CURLE_OK) { | |
293 | r = -EIO; | |
294 | goto fail; | |
295 | } | |
296 | ||
297 | *ret = c; | |
298 | return 0; | |
299 | ||
300 | fail: | |
301 | curl_easy_cleanup(c); | |
302 | return r; | |
303 | } | |
304 | ||
305 | int curl_glue_add(CurlGlue *g, CURL *c) { | |
306 | assert(g); | |
307 | assert(c); | |
308 | ||
309 | if (curl_multi_add_handle(g->curl, c) != CURLM_OK) | |
310 | return -EIO; | |
311 | ||
312 | return 0; | |
313 | } | |
314 | ||
315 | void curl_glue_remove_and_free(CurlGlue *g, CURL *c) { | |
316 | assert(g); | |
317 | ||
318 | if (!c) | |
319 | return; | |
320 | ||
321 | if (g->curl) | |
322 | curl_multi_remove_handle(g->curl, c); | |
323 | ||
324 | curl_easy_cleanup(c); | |
325 | } | |
326 | ||
327 | struct curl_slist *curl_slist_new(const char *first, ...) { | |
328 | struct curl_slist *l; | |
329 | va_list ap; | |
330 | ||
331 | if (!first) | |
332 | return NULL; | |
333 | ||
334 | l = curl_slist_append(NULL, first); | |
335 | if (!l) | |
336 | return NULL; | |
337 | ||
338 | va_start(ap, first); | |
339 | ||
340 | for (;;) { | |
341 | struct curl_slist *n; | |
342 | const char *i; | |
343 | ||
344 | i = va_arg(ap, const char*); | |
345 | if (!i) | |
346 | break; | |
347 | ||
348 | n = curl_slist_append(l, i); | |
349 | if (!n) { | |
350 | va_end(ap); | |
351 | curl_slist_free_all(l); | |
352 | return NULL; | |
353 | } | |
354 | ||
355 | l = n; | |
356 | } | |
357 | ||
358 | va_end(ap); | |
359 | return l; | |
360 | } | |
361 | ||
362 | int curl_header_strdup(const void *contents, size_t sz, const char *field, char **value) { | |
d27b725a | 363 | const char *p; |
72648326 LP |
364 | char *s; |
365 | ||
21224070 | 366 | p = memory_startswith_no_case(contents, sz, field); |
d27b725a | 367 | if (!p) |
72648326 LP |
368 | return 0; |
369 | ||
d27b725a | 370 | sz -= p - (const char*) contents; |
72648326 LP |
371 | |
372 | if (memchr(p, 0, sz)) | |
373 | return 0; | |
374 | ||
375 | /* Skip over preceeding whitespace */ | |
376 | while (sz > 0 && strchr(WHITESPACE, p[0])) { | |
377 | p++; | |
378 | sz--; | |
379 | } | |
380 | ||
13e785f7 | 381 | /* Truncate trailing whitespace */ |
72648326 LP |
382 | while (sz > 0 && strchr(WHITESPACE, p[sz-1])) |
383 | sz--; | |
384 | ||
385 | s = strndup(p, sz); | |
386 | if (!s) | |
387 | return -ENOMEM; | |
388 | ||
389 | *value = s; | |
390 | return 1; | |
391 | } | |
90199220 | 392 | |
5fa89b2c | 393 | int curl_parse_http_time(const char *t, usec_t *ret) { |
e520e0fc | 394 | _cleanup_(freelocalep) locale_t loc = (locale_t) 0; |
0193ad26 | 395 | const char *e; |
90199220 LP |
396 | struct tm tm; |
397 | time_t v; | |
398 | ||
399 | assert(t); | |
400 | assert(ret); | |
401 | ||
0193ad26 CR |
402 | loc = newlocale(LC_TIME_MASK, "C", (locale_t) 0); |
403 | if (loc == (locale_t) 0) | |
404 | return -errno; | |
405 | ||
406 | /* RFC822 */ | |
407 | e = strptime_l(t, "%a, %d %b %Y %H:%M:%S %Z", &tm, loc); | |
408 | if (!e || *e != 0) | |
409 | /* RFC 850 */ | |
410 | e = strptime_l(t, "%A, %d-%b-%y %H:%M:%S %Z", &tm, loc); | |
411 | if (!e || *e != 0) | |
412 | /* ANSI C */ | |
413 | e = strptime_l(t, "%a %b %d %H:%M:%S %Y", &tm, loc); | |
0193ad26 CR |
414 | if (!e || *e != 0) |
415 | return -EINVAL; | |
90199220 | 416 | |
0193ad26 | 417 | v = timegm(&tm); |
90199220 LP |
418 | if (v == (time_t) -1) |
419 | return -EINVAL; | |
420 | ||
5fa89b2c | 421 | *ret = (usec_t) v * USEC_PER_SEC; |
90199220 LP |
422 | return 0; |
423 | } |