From: Zbigniew Jędrzejewski-Szmek Date: Mon, 13 Apr 2026 19:46:44 +0000 (+0200) Subject: shared/curl-util: add curl_append_to_header X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c861496cd3c90b93673e6f5431496325fef8a572;p=thirdparty%2Fsystemd.git shared/curl-util: add curl_append_to_header --- diff --git a/src/imds/imdsd.c b/src/imds/imdsd.c index 7831d1c68cb..2b7bf113353 100644 --- a/src/imds/imdsd.c +++ b/src/imds/imdsd.c @@ -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) diff --git a/src/shared/curl-util.c b/src/shared/curl-util.c index 52321d08e1a..405c083afc7 100644 --- a/src/shared/curl-util.c +++ b/src/shared/curl-util.c @@ -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; +} diff --git a/src/shared/curl-util.h b/src/shared/curl-util.h index 6b922d50036..03b6ba7d214 100644 --- a/src/shared/curl-util.h +++ b/src/shared/curl-util.h @@ -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);