From dfcdb68e09d7a1c2e449b70521a3bebea69ffd35 Mon Sep 17 00:00:00 2001 From: dsrowell Date: Mon, 26 Aug 2024 13:37:29 -0400 Subject: [PATCH] feat: Add ability to send arbitrary HTTP headers to HTTP storage (#1496) --- doc/MANUAL.adoc | 3 +++ src/ccache/storage/remote/httpstorage.cpp | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/doc/MANUAL.adoc b/doc/MANUAL.adoc index 68d91328..a597408d 100644 --- a/doc/MANUAL.adoc +++ b/doc/MANUAL.adoc @@ -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. diff --git a/src/ccache/storage/remote/httpstorage.cpp b/src/ccache/storage/remote/httpstorage.cpp index d71cf5cf..9438c45e 100644 --- a/src/ccache/storage/remote/httpstorage.cpp +++ b/src/ccache/storage/remote/httpstorage.cpp @@ -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, RemoteStorage::Backend::Failure> -- 2.47.2