1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
8 #include "locale-util.h"
9 #include "string-util.h"
12 static void curl_glue_check_finished(CurlGlue
*g
) {
18 msg
= curl_multi_info_read(g
->curl
, &k
);
22 if (msg
->msg
!= CURLMSG_DONE
)
26 g
->on_finished(g
, msg
->easy_handle
, msg
->data
.result
);
29 static int curl_glue_on_io(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
30 CurlGlue
*g
= ASSERT_PTR(userdata
);
35 if (FLAGS_SET(revents
, EPOLLIN
| EPOLLOUT
))
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
;
44 if (curl_multi_socket_action(g
->curl
, fd
, action
, &k
) != CURLM_OK
)
45 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
46 "Failed to propagate IO event.");
48 curl_glue_check_finished(g
);
52 static int curl_glue_socket_callback(CURL
*curl
, curl_socket_t s
, int action
, void *userdata
, void *socketp
) {
53 sd_event_source
*io
= socketp
;
54 CurlGlue
*g
= ASSERT_PTR(userdata
);
60 if (action
== CURL_POLL_REMOVE
) {
62 sd_event_source_disable_unref(io
);
64 hashmap_remove(g
->ios
, FD_TO_PTR(s
));
70 r
= hashmap_ensure_allocated(&g
->ios
, &trivial_hash_ops
);
76 if (action
== CURL_POLL_IN
)
78 else if (action
== CURL_POLL_OUT
)
80 else if (action
== CURL_POLL_INOUT
)
81 events
= EPOLLIN
|EPOLLOUT
;
84 if (sd_event_source_set_io_events(io
, events
) < 0)
87 if (sd_event_source_set_enabled(io
, SD_EVENT_ON
) < 0)
90 if (sd_event_add_io(g
->event
, &io
, s
, events
, curl_glue_on_io
, g
) < 0)
93 if (curl_multi_assign(g
->curl
, s
, io
) != CURLM_OK
)
96 (void) sd_event_source_set_description(io
, "curl-io");
98 r
= hashmap_put(g
->ios
, FD_TO_PTR(s
), io
);
101 sd_event_source_unref(io
);
109 static int curl_glue_on_timer(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
110 CurlGlue
*g
= ASSERT_PTR(userdata
);
115 if (curl_multi_socket_action(g
->curl
, CURL_SOCKET_TIMEOUT
, 0, &k
) != CURLM_OK
)
116 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
117 "Failed to propagate timeout.");
119 curl_glue_check_finished(g
);
123 static int curl_glue_timer_callback(CURLM
*curl
, long timeout_ms
, void *userdata
) {
124 CurlGlue
*g
= ASSERT_PTR(userdata
);
129 if (timeout_ms
< 0) {
131 if (sd_event_source_set_enabled(g
->timer
, SD_EVENT_OFF
) < 0)
138 usec
= (usec_t
) timeout_ms
* USEC_PER_MSEC
+ USEC_PER_MSEC
- 1;
141 if (sd_event_source_set_time_relative(g
->timer
, usec
) < 0)
144 if (sd_event_source_set_enabled(g
->timer
, SD_EVENT_ONESHOT
) < 0)
147 if (sd_event_add_time_relative(g
->event
, &g
->timer
, CLOCK_BOOTTIME
, usec
, 0, curl_glue_on_timer
, g
) < 0)
150 (void) sd_event_source_set_description(g
->timer
, "curl-timer");
156 CurlGlue
*curl_glue_unref(CurlGlue
*g
) {
163 curl_multi_cleanup(g
->curl
);
165 while ((io
= hashmap_steal_first(g
->ios
)))
166 sd_event_source_unref(io
);
168 hashmap_free(g
->ios
);
170 sd_event_source_unref(g
->timer
);
171 sd_event_unref(g
->event
);
175 int curl_glue_new(CurlGlue
**glue
, sd_event
*event
) {
176 _cleanup_(curl_glue_unrefp
) CurlGlue
*g
= NULL
;
177 _cleanup_(curl_multi_cleanupp
) CURLM
*c
= NULL
;
178 _cleanup_(sd_event_unrefp
) sd_event
*e
= NULL
;
182 e
= sd_event_ref(event
);
184 r
= sd_event_default(&e
);
189 c
= curl_multi_init();
193 g
= new(CurlGlue
, 1);
198 .event
= TAKE_PTR(e
),
202 if (curl_multi_setopt(g
->curl
, CURLMOPT_SOCKETDATA
, g
) != CURLM_OK
)
205 if (curl_multi_setopt(g
->curl
, CURLMOPT_SOCKETFUNCTION
, curl_glue_socket_callback
) != CURLM_OK
)
208 if (curl_multi_setopt(g
->curl
, CURLMOPT_TIMERDATA
, g
) != CURLM_OK
)
211 if (curl_multi_setopt(g
->curl
, CURLMOPT_TIMERFUNCTION
, curl_glue_timer_callback
) != CURLM_OK
)
219 int curl_glue_make(CURL
**ret
, const char *url
, void *userdata
) {
220 _cleanup_(curl_easy_cleanupp
) CURL
*c
= NULL
;
221 const char *useragent
;
226 c
= curl_easy_init();
231 (void) curl_easy_setopt(c
, CURLOPT_VERBOSE
, 1L);
233 if (curl_easy_setopt(c
, CURLOPT_URL
, url
) != CURLE_OK
)
236 if (curl_easy_setopt(c
, CURLOPT_PRIVATE
, userdata
) != CURLE_OK
)
239 useragent
= strjoina(program_invocation_short_name
, "/" GIT_VERSION
);
240 if (curl_easy_setopt(c
, CURLOPT_USERAGENT
, useragent
) != CURLE_OK
)
243 if (curl_easy_setopt(c
, CURLOPT_FOLLOWLOCATION
, 1L) != CURLE_OK
)
246 if (curl_easy_setopt(c
, CURLOPT_NOSIGNAL
, 1L) != CURLE_OK
)
249 if (curl_easy_setopt(c
, CURLOPT_LOW_SPEED_TIME
, 60L) != CURLE_OK
)
252 if (curl_easy_setopt(c
, CURLOPT_LOW_SPEED_LIMIT
, 30L) != CURLE_OK
)
255 if (curl_easy_setopt(c
, CURLOPT_PROTOCOLS
, CURLPROTO_HTTP
|CURLPROTO_HTTPS
|CURLPROTO_FILE
) != CURLE_OK
)
262 int curl_glue_add(CurlGlue
*g
, CURL
*c
) {
266 if (curl_multi_add_handle(g
->curl
, c
) != CURLM_OK
)
272 void curl_glue_remove_and_free(CurlGlue
*g
, CURL
*c
) {
279 curl_multi_remove_handle(g
->curl
, c
);
281 curl_easy_cleanup(c
);
284 struct curl_slist
*curl_slist_new(const char *first
, ...) {
285 struct curl_slist
*l
;
291 l
= curl_slist_append(NULL
, first
);
298 struct curl_slist
*n
;
301 i
= va_arg(ap
, const char*);
305 n
= curl_slist_append(l
, i
);
308 curl_slist_free_all(l
);
319 int curl_header_strdup(const void *contents
, size_t sz
, const char *field
, char **value
) {
323 p
= memory_startswith_no_case(contents
, sz
, field
);
327 sz
-= p
- (const char*) contents
;
329 if (memchr(p
, 0, sz
))
332 /* Skip over preceding whitespace */
333 while (sz
> 0 && strchr(WHITESPACE
, p
[0])) {
338 /* Truncate trailing whitespace */
339 while (sz
> 0 && strchr(WHITESPACE
, p
[sz
-1]))
350 int curl_parse_http_time(const char *t
, usec_t
*ret
) {
351 _cleanup_(freelocalep
) locale_t loc
= (locale_t
) 0;
359 loc
= newlocale(LC_TIME_MASK
, "C", (locale_t
) 0);
360 if (loc
== (locale_t
) 0)
364 e
= strptime_l(t
, "%a, %d %b %Y %H:%M:%S %Z", &tm
, loc
);
367 e
= strptime_l(t
, "%A, %d-%b-%y %H:%M:%S %Z", &tm
, loc
);
370 e
= strptime_l(t
, "%a %b %d %H:%M:%S %Y", &tm
, loc
);
375 if (v
== (time_t) -1)
378 *ret
= (usec_t
) v
* USEC_PER_SEC
;