]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
feat: Add ability to send arbitrary HTTP headers to HTTP storage (#1496)
authordsrowell <dsrowell@gmail.com>
Mon, 26 Aug 2024 17:37:29 +0000 (13:37 -0400)
committerGitHub <noreply@github.com>
Mon, 26 Aug 2024 17:37:29 +0000 (19:37 +0200)
doc/MANUAL.adoc
src/ccache/storage/remote/httpstorage.cpp

index 68d91328f8864b7954baa36008ff8f7365cdf07d..a597408d658cdc59d1e30d36188f0f8017969d6a 100644 (file)
@@ -1309,6 +1309,9 @@ values.
 --
 +
 The default is *subdirs*.
+* *header*: Add the key=value pair to the HTTP headers of the request. For example:
+   `+header=Content-Type=application/octet-stream+` adds
+   "Content-Type: application/octet-stream" to the http headers of the request.
 * *operation-timeout*: Timeout (in ms) for HTTP requests. The default is 10000.
 
 
index d71cf5cf15eebb89d8cda3376287e9e4b0f60b34..9438c45ec878efd754fa302bbdbbf2040decfe59 100644 (file)
@@ -119,14 +119,12 @@ HttpStorageBackend::HttpStorageBackend(
     m_http_client.set_basic_auth(std::string(user), std::string(*password));
   }
 
-  m_http_client.set_default_headers({
-    {"User-Agent", FMT("ccache/{}", CCACHE_VERSION)},
-  });
-  m_http_client.set_keep_alive(true);
-
   auto connect_timeout = k_default_connect_timeout;
   auto operation_timeout = k_default_operation_timeout;
 
+  httplib::Headers default_headers;
+  default_headers.emplace("User-Agent", FMT("ccache/{}", CCACHE_VERSION));
+
   for (const auto& attr : attributes) {
     if (attr.key == "bearer-token") {
       m_http_client.set_bearer_token_auth(attr.value);
@@ -146,6 +144,13 @@ HttpStorageBackend::HttpStorageBackend(
       }
     } else if (attr.key == "operation-timeout") {
       operation_timeout = parse_timeout_attribute(attr.value);
+    } else if (attr.key == "header") {
+      const auto [key, value] = util::split_once(attr.value, '=');
+      if (value) {
+        default_headers.emplace(std::string(key), std::string(*value));
+      } else {
+        LOG("Incomplete header specification: {}", attr.value);
+      }
     } else if (!is_framework_attribute(attr.key)) {
       LOG("Unknown attribute: {}", attr.key);
     }
@@ -154,6 +159,8 @@ HttpStorageBackend::HttpStorageBackend(
   m_http_client.set_connection_timeout(connect_timeout);
   m_http_client.set_read_timeout(operation_timeout);
   m_http_client.set_write_timeout(operation_timeout);
+  m_http_client.set_keep_alive(true);
+  m_http_client.set_default_headers(default_headers);
 }
 
 tl::expected<std::optional<util::Bytes>, RemoteStorage::Backend::Failure>