]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/curl-util.c
tree-wide: make use of new relative time events in sd-event.h
[thirdparty/systemd.git] / src / import / curl-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <fcntl.h>
4
5 #include "alloc-util.h"
6 #include "build.h"
7 #include "curl-util.h"
8 #include "fd-util.h"
9 #include "locale-util.h"
10 #include "string-util.h"
11
12 static void curl_glue_check_finished(CurlGlue *g) {
13 CURLMsg *msg;
14 int k = 0;
15
16 assert(g);
17
18 msg = curl_multi_info_read(g->curl, &k);
19 if (!msg)
20 return;
21
22 if (msg->msg != CURLMSG_DONE)
23 return;
24
25 if (g->on_finished)
26 g->on_finished(g, msg->easy_handle, msg->data.result);
27 }
28
29 static int curl_glue_on_io(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
30 CurlGlue *g = userdata;
31 int action, k = 0;
32
33 assert(s);
34 assert(g);
35
36 if (FLAGS_SET(revents, EPOLLIN | EPOLLOUT))
37 action = CURL_POLL_INOUT;
38 else if (revents & EPOLLIN)
39 action = CURL_POLL_IN;
40 else if (revents & EPOLLOUT)
41 action = CURL_POLL_OUT;
42 else
43 action = 0;
44
45 if (curl_multi_socket_action(g->curl, fd, action, &k) != CURLM_OK)
46 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
47 "Failed to propagate IO event.");
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 = socketp;
55 CurlGlue *g = userdata;
56 uint32_t events = 0;
57 int r;
58
59 assert(curl);
60 assert(g);
61
62 if (action == CURL_POLL_REMOVE) {
63 if (io) {
64 sd_event_source_disable_unref(io);
65
66 hashmap_remove(g->ios, FD_TO_PTR(s));
67 }
68
69 return 0;
70 }
71
72 r = hashmap_ensure_allocated(&g->ios, &trivial_hash_ops);
73 if (r < 0) {
74 log_oom();
75 return -1;
76 }
77
78 if (action == CURL_POLL_IN)
79 events = EPOLLIN;
80 else if (action == CURL_POLL_OUT)
81 events = EPOLLOUT;
82 else if (action == CURL_POLL_INOUT)
83 events = EPOLLIN|EPOLLOUT;
84
85 if (io) {
86 if (sd_event_source_set_io_events(io, events) < 0)
87 return -1;
88
89 if (sd_event_source_set_enabled(io, SD_EVENT_ON) < 0)
90 return -1;
91 } else {
92 if (sd_event_add_io(g->event, &io, s, events, curl_glue_on_io, g) < 0)
93 return -1;
94
95 if (curl_multi_assign(g->curl, s, io) != CURLM_OK)
96 return -1;
97
98 (void) sd_event_source_set_description(io, "curl-io");
99
100 r = hashmap_put(g->ios, FD_TO_PTR(s), io);
101 if (r < 0) {
102 log_oom();
103 sd_event_source_unref(io);
104 return -1;
105 }
106 }
107
108 return 0;
109 }
110
111 static int curl_glue_on_timer(sd_event_source *s, uint64_t usec, void *userdata) {
112 CurlGlue *g = userdata;
113 int k = 0;
114
115 assert(s);
116 assert(g);
117
118 if (curl_multi_socket_action(g->curl, CURL_SOCKET_TIMEOUT, 0, &k) != CURLM_OK)
119 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
120 "Failed to propagate timeout.");
121
122 curl_glue_check_finished(g);
123 return 0;
124 }
125
126 static int curl_glue_timer_callback(CURLM *curl, long timeout_ms, void *userdata) {
127 CurlGlue *g = userdata;
128 usec_t usec;
129
130 assert(curl);
131 assert(g);
132
133 if (timeout_ms < 0) {
134 if (g->timer) {
135 if (sd_event_source_set_enabled(g->timer, SD_EVENT_OFF) < 0)
136 return -1;
137 }
138
139 return 0;
140 }
141
142 usec = (usec_t) timeout_ms * USEC_PER_MSEC + USEC_PER_MSEC - 1;
143
144 if (g->timer) {
145 if (sd_event_source_set_time_relative(g->timer, usec) < 0)
146 return -1;
147
148 if (sd_event_source_set_enabled(g->timer, SD_EVENT_ONESHOT) < 0)
149 return -1;
150 } else {
151 if (sd_event_add_time_relative(g->event, &g->timer, clock_boottime_or_monotonic(), usec, 0, curl_glue_on_timer, g) < 0)
152 return -1;
153
154 (void) sd_event_source_set_description(g->timer, "curl-timer");
155 }
156
157 return 0;
158 }
159
160 CurlGlue *curl_glue_unref(CurlGlue *g) {
161 sd_event_source *io;
162
163 if (!g)
164 return NULL;
165
166 if (g->curl)
167 curl_multi_cleanup(g->curl);
168
169 while ((io = hashmap_steal_first(g->ios)))
170 sd_event_source_unref(io);
171
172 hashmap_free(g->ios);
173
174 sd_event_source_unref(g->timer);
175 sd_event_unref(g->event);
176 return mfree(g);
177 }
178
179 int curl_glue_new(CurlGlue **glue, sd_event *event) {
180 _cleanup_(curl_glue_unrefp) CurlGlue *g = NULL;
181 _cleanup_(curl_multi_cleanupp) CURL *c = NULL;
182 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
183 int r;
184
185 if (event)
186 e = sd_event_ref(event);
187 else {
188 r = sd_event_default(&e);
189 if (r < 0)
190 return r;
191 }
192
193 c = curl_multi_init();
194 if (!c)
195 return -ENOMEM;
196
197 g = new(CurlGlue, 1);
198 if (!g)
199 return -ENOMEM;
200
201 *g = (CurlGlue) {
202 .event = TAKE_PTR(e),
203 .curl = TAKE_PTR(c),
204 };
205
206 if (curl_multi_setopt(g->curl, CURLMOPT_SOCKETDATA, g) != CURLM_OK)
207 return -EINVAL;
208
209 if (curl_multi_setopt(g->curl, CURLMOPT_SOCKETFUNCTION, curl_glue_socket_callback) != CURLM_OK)
210 return -EINVAL;
211
212 if (curl_multi_setopt(g->curl, CURLMOPT_TIMERDATA, g) != CURLM_OK)
213 return -EINVAL;
214
215 if (curl_multi_setopt(g->curl, CURLMOPT_TIMERFUNCTION, curl_glue_timer_callback) != CURLM_OK)
216 return -EINVAL;
217
218 *glue = TAKE_PTR(g);
219
220 return 0;
221 }
222
223 int curl_glue_make(CURL **ret, const char *url, void *userdata) {
224 _cleanup_(curl_easy_cleanupp) CURL *c = NULL;
225 const char *useragent;
226
227 assert(ret);
228 assert(url);
229
230 c = curl_easy_init();
231 if (!c)
232 return -ENOMEM;
233
234 /* curl_easy_setopt(c, CURLOPT_VERBOSE, 1L); */
235
236 if (curl_easy_setopt(c, CURLOPT_URL, url) != CURLE_OK)
237 return -EIO;
238
239 if (curl_easy_setopt(c, CURLOPT_PRIVATE, userdata) != CURLE_OK)
240 return -EIO;
241
242 useragent = strjoina(program_invocation_short_name, "/" GIT_VERSION);
243 if (curl_easy_setopt(c, CURLOPT_USERAGENT, useragent) != CURLE_OK)
244 return -EIO;
245
246 if (curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L) != CURLE_OK)
247 return -EIO;
248
249 if (curl_easy_setopt(c, CURLOPT_NOSIGNAL, 1L) != CURLE_OK)
250 return -EIO;
251
252 if (curl_easy_setopt(c, CURLOPT_LOW_SPEED_TIME, 60L) != CURLE_OK)
253 return -EIO;
254
255 if (curl_easy_setopt(c, CURLOPT_LOW_SPEED_LIMIT, 30L) != CURLE_OK)
256 return -EIO;
257
258 *ret = TAKE_PTR(c);
259 return 0;
260 }
261
262 int curl_glue_add(CurlGlue *g, CURL *c) {
263 assert(g);
264 assert(c);
265
266 if (curl_multi_add_handle(g->curl, c) != CURLM_OK)
267 return -EIO;
268
269 return 0;
270 }
271
272 void curl_glue_remove_and_free(CurlGlue *g, CURL *c) {
273 assert(g);
274
275 if (!c)
276 return;
277
278 if (g->curl)
279 curl_multi_remove_handle(g->curl, c);
280
281 curl_easy_cleanup(c);
282 }
283
284 struct curl_slist *curl_slist_new(const char *first, ...) {
285 struct curl_slist *l;
286 va_list ap;
287
288 if (!first)
289 return NULL;
290
291 l = curl_slist_append(NULL, first);
292 if (!l)
293 return NULL;
294
295 va_start(ap, first);
296
297 for (;;) {
298 struct curl_slist *n;
299 const char *i;
300
301 i = va_arg(ap, const char*);
302 if (!i)
303 break;
304
305 n = curl_slist_append(l, i);
306 if (!n) {
307 va_end(ap);
308 curl_slist_free_all(l);
309 return NULL;
310 }
311
312 l = n;
313 }
314
315 va_end(ap);
316 return l;
317 }
318
319 int curl_header_strdup(const void *contents, size_t sz, const char *field, char **value) {
320 const char *p;
321 char *s;
322
323 p = memory_startswith_no_case(contents, sz, field);
324 if (!p)
325 return 0;
326
327 sz -= p - (const char*) contents;
328
329 if (memchr(p, 0, sz))
330 return 0;
331
332 /* Skip over preceding whitespace */
333 while (sz > 0 && strchr(WHITESPACE, p[0])) {
334 p++;
335 sz--;
336 }
337
338 /* Truncate trailing whitespace */
339 while (sz > 0 && strchr(WHITESPACE, p[sz-1]))
340 sz--;
341
342 s = strndup(p, sz);
343 if (!s)
344 return -ENOMEM;
345
346 *value = s;
347 return 1;
348 }
349
350 int curl_parse_http_time(const char *t, usec_t *ret) {
351 _cleanup_(freelocalep) locale_t loc = (locale_t) 0;
352 const char *e;
353 struct tm tm;
354 time_t v;
355
356 assert(t);
357 assert(ret);
358
359 loc = newlocale(LC_TIME_MASK, "C", (locale_t) 0);
360 if (loc == (locale_t) 0)
361 return -errno;
362
363 /* RFC822 */
364 e = strptime_l(t, "%a, %d %b %Y %H:%M:%S %Z", &tm, loc);
365 if (!e || *e != 0)
366 /* RFC 850 */
367 e = strptime_l(t, "%A, %d-%b-%y %H:%M:%S %Z", &tm, loc);
368 if (!e || *e != 0)
369 /* ANSI C */
370 e = strptime_l(t, "%a %b %d %H:%M:%S %Y", &tm, loc);
371 if (!e || *e != 0)
372 return -EINVAL;
373
374 v = timegm(&tm);
375 if (v == (time_t) -1)
376 return -EINVAL;
377
378 *ret = (usec_t) v * USEC_PER_SEC;
379 return 0;
380 }