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