From 56ab3ae17000b0c70bd5321e5ea1fac108dcfb80 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 10 Feb 2025 05:29:12 +0900 Subject: [PATCH] journal-upload: several follow-ups for Accept-Encoding header handling This makes, - When a wildcard value '*' is specified, use the first supported compression algorithm, - disable compression if Accept-Encoding header is unspecified or no supported compression algorithm specified in the header, - ignore all errors in parsing the header. --- src/journal-remote/journal-upload.c | 55 ++++++++++++++++------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/journal-remote/journal-upload.c b/src/journal-remote/journal-upload.c index 38704162f4f..f0af903d8a5 100644 --- a/src/journal-remote/journal-upload.c +++ b/src/journal-remote/journal-upload.c @@ -553,26 +553,39 @@ static int update_content_encoding_header(Uploader *u, const CompressionConfig * log_debug("Disabled compression algorithm."); return 0; } +#endif -static int update_content_encoding(Uploader *u, const char *accept_encoding) { +static int parse_accept_encoding_header(Uploader *u) { +#if LIBCURL_VERSION_NUM >= 0x075300 int r; assert(u); - for (const char *p = accept_encoding;;) { + if (ordered_hashmap_isempty(arg_compression)) + return update_content_encoding_header(u, NULL); + + struct curl_header *header; + CURLHcode hcode = curl_easy_header(u->easy, "Accept-Encoding", 0, CURLH_HEADER, -1, &header); + if (hcode != CURLHE_OK) + goto not_found; + + for (const char *p = header->value;;) { _cleanup_free_ char *word = NULL; r = extract_first_word(&p, &word, ",", 0); if (r < 0) - return log_error_errno(r, "Failed to parse Accept-Encoding header value: %m"); + return log_warning_errno(r, "Failed to parse Accept-Encoding header value, ignoring: %m"); if (r == 0) - return 0; + break; /* Cut the quality value waiting. */ char *q = strchr(word, ';'); if (q) *q = '\0'; + if (streq(word, "*")) + return update_content_encoding_header(u, ordered_hashmap_first(arg_compression)); + Compression c = compression_lowercase_from_string(word); if (c <= 0 || !compression_supported(c)) continue; /* unsupported or invalid algorithm. */ @@ -583,8 +596,16 @@ static int update_content_encoding(Uploader *u, const char *accept_encoding) { return update_content_encoding_header(u, cc); } -} + +not_found: + if (arg_force_compression) + return update_content_encoding_header(u, ordered_hashmap_first(arg_compression)); + + return update_content_encoding_header(u, NULL); +#else + return 0; #endif +} static int perform_upload(Uploader *u) { CURLcode code; @@ -614,29 +635,15 @@ static int perform_upload(Uploader *u) { return log_error_errno(SYNTHETIC_ERRNO(EIO), "Upload to %s failed with code %ld: %s", u->url, status, strna(u->answer)); - else if (status < 200) + if (status < 200) return log_error_errno(SYNTHETIC_ERRNO(EIO), "Upload to %s finished with unexpected code %ld: %s", u->url, status, strna(u->answer)); - else { -#if LIBCURL_VERSION_NUM >= 0x075300 - int r; - if (!u->compression) { - struct curl_header *encoding_header; - CURLHcode hcode; - - hcode = curl_easy_header(u->easy, "Accept-Encoding", 0, CURLH_HEADER, -1, &encoding_header); - if (hcode == CURLHE_OK && encoding_header && encoding_header->value) { - r = update_content_encoding(u, encoding_header->value); - if (r < 0) - return r; - } - } -#endif - log_debug("Upload finished successfully with code %ld: %s", - status, strna(u->answer)); - } + (void) parse_accept_encoding_header(u); + + log_debug("Upload finished successfully with code %ld: %s", + status, strna(u->answer)); free_and_replace(u->last_cursor, u->current_cursor); -- 2.47.3