]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/curl-util: add curl_append_to_header
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Mon, 13 Apr 2026 19:46:44 +0000 (21:46 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Thu, 16 Apr 2026 19:07:43 +0000 (21:07 +0200)
src/imds/imdsd.c
src/shared/curl-util.c
src/shared/curl-util.h

index 7831d1c68cb226d5f273faa4a9e03cab2c89b8ea..2b7bf113353cfbea875894885193415e90639c69 100644 (file)
@@ -1112,20 +1112,14 @@ static int context_acquire_data(Context *c) {
                 if (!token_header)
                         return context_log_oom(c);
 
-                struct curl_slist *n = curl_slist_append(c->request_header_data, token_header);
-                if (!n)
-                        return context_log_oom(c);
-
-                c->request_header_data = n;
+                r = curl_append_to_header(&c->request_header_data, STRV_MAKE(token_header));
+                if (r < 0)
+                        return context_log_errno(c, LOG_ERR, r, "Failed to create curl header: %m");
         }
 
-        STRV_FOREACH(i, arg_extra_header) {
-                struct curl_slist *n = curl_slist_append(c->request_header_data, *i);
-                if (!n)
-                        return context_log_oom(c);
-
-                c->request_header_data = n;
-        }
+        r = curl_append_to_header(&c->request_header_data, arg_extra_header);
+        if (r < 0)
+                return context_log_errno(c, LOG_ERR, r, "Failed to create curl header: %m");
 
         if (c->request_header_data)
                 if (curl_easy_setopt(c->curl_data, CURLOPT_HTTPHEADER, c->request_header_data) != CURLE_OK)
index 52321d08e1a434071fd5b350b27aebfeb3d1f8a8..405c083afc712dcf1ac288207fecf6ef667fa0e4 100644 (file)
@@ -8,6 +8,7 @@
 #include "hashmap.h"
 #include "log.h"
 #include "string-util.h"
+#include "strv.h"
 #include "time-util.h"
 #include "version.h"
 
@@ -407,3 +408,19 @@ int curl_parse_http_time(const char *t, usec_t *ret) {
 
         return 0;
 }
+
+int curl_append_to_header(struct curl_slist **list, char **headers) {
+        /* This function leaves 'list' modified on partial failure.
+         * Input/output param list may point to NULL. */
+
+        assert(list);
+
+        STRV_FOREACH(h, headers) {
+                struct curl_slist *l = curl_slist_append(*list, *h);
+                if (!l)
+                        return -ENOMEM;
+                *list = l;
+        }
+
+        return 0;
+}
index 6b922d5003697c7735adbe19ec33d94a0486e3df..03b6ba7d214518f1e91a2ceb834393ca3affdc19 100644 (file)
@@ -43,3 +43,4 @@ void curl_glue_remove_and_free(CurlGlue *g, CURL *c);
 struct curl_slist *curl_slist_new(const char *first, ...) _sentinel_;
 int curl_header_strdup(const void *contents, size_t sz, const char *field, char **value);
 int curl_parse_http_time(const char *t, usec_t *ret);
+int curl_append_to_header(struct curl_slist **list, char **headers);