Debian Codegraph @

src/shared/curl-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "sd-dlopen.h"
5
6 #include "shared-forward.h"
7
8 #if HAVE_LIBCURL
9 #include <curl/curl.h> /* IWYU pragma: export */
10
11 #include "dlfcn-util.h"
12
13 extern DLSYM_PROTOTYPE(curl_easy_cleanup);
14 extern DLSYM_PROTOTYPE(curl_easy_getinfo);
15 extern DLSYM_PROTOTYPE(curl_easy_init);
16 extern DLSYM_PROTOTYPE(curl_easy_perform);
17 extern DLSYM_PROTOTYPE(curl_easy_setopt);
18 extern DLSYM_PROTOTYPE(curl_easy_strerror);
19 #if LIBCURL_VERSION_NUM >= 0x075300
20 extern DLSYM_PROTOTYPE(curl_easy_header);
21 #endif
22 extern DLSYM_PROTOTYPE(curl_getdate);
23 extern DLSYM_PROTOTYPE(curl_slist_append);
24 extern DLSYM_PROTOTYPE(curl_slist_free_all);
25
26 #define easy_setopt(curl, log_level, opt, value) ({ \
27 CURLcode code = sym_curl_easy_setopt(ASSERT_PTR(curl), opt, value); \
28 if (code) \
29 log_full(log_level, \
30 "curl_easy_setopt %s failed: %s", \
31 #opt, sym_curl_easy_strerror(code)); \
32 code == CURLE_OK; \
33 })
34
35 typedef int (*curl_finished_t)(CurlSlot *slot, CURL *curl, CURLcode code, void *userdata);
36
37 int curl_glue_new(CurlGlue **glue, sd_event *event);
38 DECLARE_TRIVIAL_REF_UNREF_FUNC(CurlGlue, curl_glue);
39 DEFINE_TRIVIAL_CLEANUP_FUNC(CurlGlue*, curl_glue_unref);
40
41 /* Build a CURL easy handle with sane defaults. The caller configures any
42 * additional options (headers, write callbacks, …) before handing it off to
43 * curl_glue_perform_async(). */
44 int curl_glue_make(CURL **ret, const char *url);
45
46 /* Hand a configured CURL easy handle off to the multi for execution. The slot
47 * takes ownership of the easy handle: once the slot is released (the callback
48 * has fired, the caller has dropped its last ref, or the glue is being freed),
49 * the handle is removed from the multi and freed.
50 *
51 * If ret_slot is NULL the slot is allocated as floating: the glue keeps it
52 * alive until the callback fires or the glue is torn down. Otherwise a
53 * reference is returned to the caller; releasing that reference cancels the
54 * call. */
55 int curl_glue_perform_async(
56 CurlGlue *g,
57 CURL *easy,
58 curl_finished_t cb,
59 void *userdata,
60 CurlSlot **ret_slot);
61
62 CURL* curl_slot_get_easy(CurlSlot *slot);
63 CurlGlue* curl_slot_get_glue(CurlSlot *slot);
64
65 DECLARE_TRIVIAL_REF_UNREF_FUNC(CurlSlot, curl_slot);
66 DEFINE_TRIVIAL_CLEANUP_FUNC(CurlSlot*, curl_slot_unref);
67
68 struct curl_slist *curl_slist_new(const char *first, ...) _sentinel_;
69 int curl_header_strdup(const void *contents, size_t sz, const char *field, char **value);
70 int curl_parse_http_time(const char *t, usec_t *ret);
71 int curl_append_to_header(struct curl_slist **list, char **headers);
72
73 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_RENAME(CURL*, sym_curl_easy_cleanup, curl_easy_cleanupp, NULL);
74 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_RENAME(struct curl_slist*, sym_curl_slist_free_all, curl_slist_free_allp, NULL);
75
76 #define CURL_NOTE(priority) \
77 SD_ELF_NOTE_DLOPEN("curl", \
78 "Support for downloading and uploading files over HTTP", \
79 priority, \
80 "libcurl.so.4")
81
82 #define DLOPEN_CURL(log_level, priority) \
83 ({ \
84 CURL_NOTE(priority); \
85 dlopen_curl(log_level); \
86 })
87 #else
88 #define DLOPEN_CURL(log_level, priority) dlopen_curl(log_level)
89 #endif
90
91 int dlopen_curl(int log_level);