report: add option to inject additional HTTP headers

This is useful when debugging things. The option is named and implements
the same logic as imdsd.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-04-16 21:12:20 +02:00
parent fca491e036
commit ce400ef2d9
6 changed files with 31 additions and 5 deletions
-4
View File
@@ -2243,10 +2243,6 @@ static bool http_header_name_valid(const char *a) {
return a && ascii_is_valid(a) && !string_has_cc(a, /* ok= */ NULL) && !strchr(a, ':');
}
static bool http_header_valid(const char *a) {
return a && ascii_is_valid(a) && !string_has_cc(a, /* ok= */ NULL) && strchr(a, ':');
}
static int parse_argv(int argc, char *argv[]) {
int r;
+4
View File
@@ -97,6 +97,10 @@ int upload_collected(Context *context) {
if (r < 0)
return log_error_errno(r, "Failed to create curl header: %m");
r = curl_append_to_header(&header, arg_extra_headers);
if (r < 0)
return log_error_errno(r, "Failed to create curl header: %m");
_cleanup_(curl_easy_cleanupp) CURL *curl = curl_easy_init();
if (!curl)
return log_error_errno(SYNTHETIC_ERRNO(ENOSR),
+18 -1
View File
@@ -26,6 +26,7 @@
#include "time-util.h"
#include "varlink-idl-util.h"
#include "verbs.h"
#include "web-util.h"
#define METRICS_OR_FACTS_MAX 4096U
#define METRICS_OR_FACTS_LINKS_MAX 128U
@@ -40,6 +41,7 @@ char *arg_url = NULL;
char *arg_key = NULL;
char *arg_cert = NULL;
char *arg_trust = NULL;
char **arg_extra_headers = NULL;
usec_t arg_network_timeout_usec = TIMEOUT_USEC;
STATIC_DESTRUCTOR_REGISTER(arg_matches, strv_freep);
@@ -47,6 +49,7 @@ STATIC_DESTRUCTOR_REGISTER(arg_url, freep);
STATIC_DESTRUCTOR_REGISTER(arg_key, freep);
STATIC_DESTRUCTOR_REGISTER(arg_cert, freep);
STATIC_DESTRUCTOR_REGISTER(arg_trust, freep);
STATIC_DESTRUCTOR_REGISTER(arg_extra_headers, strv_freep);
typedef struct LinkInfo {
Context *context;
@@ -1052,9 +1055,23 @@ static int parse_argv(int argc, char *argv[], char ***ret_args) {
if (r < 0)
return log_error_errno(r, "Failed to parse --network-timeout value: %s", arg);
break;
OPTION_LONG("extra-header", "NAME: VALUE",
"Inject additional header into the upload request"):
if (isempty(arg)) {
arg_extra_headers = strv_free(arg_extra_headers);
break;
}
if (!http_header_valid(arg))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid HTTP header: %s", arg);
if (strv_extend(&arg_extra_headers, arg) < 0)
return log_oom();
break;
}
if ((arg_url || arg_key || arg_cert || arg_trust) && !HAVE_LIBCURL)
if ((arg_url || arg_key || arg_cert || arg_trust || arg_extra_headers) && !HAVE_LIBCURL)
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Compiled without libcurl.");
*ret_args = option_parser_get_args(&state);
+1
View File
@@ -10,6 +10,7 @@
#define REPORT_TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem"
extern char *arg_url, *arg_key, *arg_cert, *arg_trust;
extern char **arg_extra_headers;
extern usec_t arg_network_timeout_usec;
typedef enum Action {
+7
View File
@@ -50,6 +50,13 @@ bool documentation_url_is_valid(const char *url) {
return ascii_is_valid(p);
}
bool http_header_valid(const char *header) {
return header &&
ascii_is_valid(header) &&
!string_has_cc(header, /* ok= */ NULL) &&
strchr(header, ':');
}
bool http_etag_is_valid(const char *etag) {
if (isempty(etag))
return false;
+1
View File
@@ -6,4 +6,5 @@
bool http_url_is_valid(const char *url) _pure_;
bool file_url_is_valid(const char *url) _pure_;
bool documentation_url_is_valid(const char *url) _pure_;
bool http_header_valid(const char *header) _pure_;
bool http_etag_is_valid(const char *etag) _pure_;