diff --git a/repertory/librepertory/include/comm/curl/curl_comm.hpp b/repertory/librepertory/include/comm/curl/curl_comm.hpp index d7fa784b..7875b1f8 100644 --- a/repertory/librepertory/include/comm/curl/curl_comm.hpp +++ b/repertory/librepertory/include/comm/curl/curl_comm.hpp @@ -61,13 +61,14 @@ public: [[nodiscard]] static auto reset_curl(CURL *curl_handle) -> CURL *; public: - [[nodiscard]] static auto - construct_url(CURL *curl, const std::string &relative_path, - const host_config &cfg) -> std::string; + [[nodiscard]] static auto construct_url(CURL *curl, + const std::string &relative_path, + const host_config &cfg) + -> std::string; - [[nodiscard]] static auto - create_host_config(const s3_config &cfg, - bool use_s3_path_style) -> host_config; + [[nodiscard]] static auto create_host_config(const s3_config &cfg, + bool use_s3_path_style) + -> host_config; [[nodiscard]] static auto url_encode(CURL *curl, const std::string &data, bool allow_slash) -> std::string; @@ -75,8 +76,8 @@ public: template [[nodiscard]] static auto make_encrypted_request(const host_config &cfg, const request_type &request, - long &response_code, - stop_type &stop_requested) -> bool { + long &response_code, stop_type &stop_requested) + -> bool { response_code = 0; if (not request.decryption_token.has_value() || @@ -193,6 +194,16 @@ public: request.aws_service.value().c_str()); } + curl_slist *header_list{nullptr}; + if (not request.headers.empty()) { + for (const auto &&header : request.headers) { + header_list = curl_slist_append( + header_list, + fmt::format("{}: {}", header.first, header.second).c_str()); + } + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list); + } + auto url = construct_url(curl, request.get_path(), cfg) + parameters; curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); @@ -200,6 +211,11 @@ public: CURLcode curl_code{}; curl_request.get_result(curl_code, response_code); + + if (header_list != nullptr) { + curl_slist_free_all(header_list); + } + if (curl_code != CURLE_OK) { event_system::instance().raise(url, curl_code); return false; @@ -215,26 +231,30 @@ public: public: void enable_s3_path_style(bool enable) override; - [[nodiscard]] auto - make_request(const curl::requests::http_delete &del, long &response_code, - stop_type &stop_requested) const -> bool override; + [[nodiscard]] auto make_request(const curl::requests::http_delete &del, + long &response_code, + stop_type &stop_requested) const + -> bool override; - [[nodiscard]] auto - make_request(const curl::requests::http_get &get, long &response_code, - stop_type &stop_requested) const -> bool override; + [[nodiscard]] auto make_request(const curl::requests::http_get &get, + long &response_code, + stop_type &stop_requested) const + -> bool override; - [[nodiscard]] auto - make_request(const curl::requests::http_head &head, long &response_code, - stop_type &stop_requested) const -> bool override; + [[nodiscard]] auto make_request(const curl::requests::http_head &head, + long &response_code, + stop_type &stop_requested) const + -> bool override; - [[nodiscard]] auto - make_request(const curl::requests::http_post &post_file, long &response_code, - stop_type &stop_requested) const -> bool override; + [[nodiscard]] auto make_request(const curl::requests::http_post &post_file, + long &response_code, + stop_type &stop_requested) const + -> bool override; - [[nodiscard]] auto - make_request(const curl::requests::http_put_file &put_file, - long &response_code, - stop_type &stop_requested) const -> bool override; + [[nodiscard]] auto make_request(const curl::requests::http_put_file &put_file, + long &response_code, + stop_type &stop_requested) const + -> bool override; }; } // namespace repertory diff --git a/repertory/librepertory/include/comm/curl/requests/http_post.hpp b/repertory/librepertory/include/comm/curl/requests/http_post.hpp index 225d5c3d..c5425e12 100644 --- a/repertory/librepertory/include/comm/curl/requests/http_post.hpp +++ b/repertory/librepertory/include/comm/curl/requests/http_post.hpp @@ -32,15 +32,15 @@ struct http_post final : http_request_base { auto operator=(const http_post &) -> http_post & = default; auto operator=(http_post &&) -> http_post & = default; - ~http_post() override; + ~http_post() override = default; std::optional json; - [[nodiscard]] auto - set_method(CURL *curl, stop_type & /*stop_requested*/) const -> bool override; + [[nodiscard]] auto set_method(CURL *curl, + stop_type & /*stop_requested*/) const + -> bool override; private: - mutable curl_slist *headers{nullptr}; mutable std::optional json_str; }; } // namespace repertory::curl::requests diff --git a/repertory/librepertory/include/comm/curl/requests/http_request_base.hpp b/repertory/librepertory/include/comm/curl/requests/http_request_base.hpp index 4a14cb53..a99d88da 100644 --- a/repertory/librepertory/include/comm/curl/requests/http_request_base.hpp +++ b/repertory/librepertory/include/comm/curl/requests/http_request_base.hpp @@ -51,7 +51,7 @@ struct http_request_base { bool allow_timeout{}; std::optional aws_service; std::optional decryption_token{}; - http_headers headers{}; + mutable http_headers headers{}; std::string path{}; http_query_parameters query{}; std::optional range{}; diff --git a/repertory/librepertory/src/comm/curl/requests/http_post.cpp b/repertory/librepertory/src/comm/curl/requests/http_post.cpp index e7a41b98..ca052a0a 100644 --- a/repertory/librepertory/src/comm/curl/requests/http_post.cpp +++ b/repertory/librepertory/src/comm/curl/requests/http_post.cpp @@ -22,18 +22,11 @@ #include "comm/curl/requests/http_post.hpp" namespace repertory::curl::requests { -http_post::~http_post() { - if (headers != nullptr) { - curl_slist_free_all(headers); - } -} - auto http_post::set_method(CURL *curl, stop_type & /*stop_requested*/) const -> bool { curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); if (json.has_value()) { - headers = curl_slist_append(headers, "content-type: application/json"); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + headers["content-type"] = "application/json"; json_str = json->dump(); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_str->c_str()); diff --git a/support/include/utils/config.hpp b/support/include/utils/config.hpp index 719a6ca0..e995f7f0 100644 --- a/support/include/utils/config.hpp +++ b/support/include/utils/config.hpp @@ -468,7 +468,7 @@ struct http_range final { std::uint64_t end{}; }; -using http_headers = std::unordered_map; +using http_headers = std::map; using http_query_parameters = std::map; using http_ranges = std::vector; #endif // defined(PROJECT_ENABLE_CURL)