From c79f9d65bf778b79f52584195244b0d21d315194 Mon Sep 17 00:00:00 2001 From: Andy Doan Date: Thu, 24 Sep 2020 16:22:58 -0500 Subject: [PATCH] aktualizr-get: Allow passing HTTP headers Signed-off-by: Andy Doan --- src/aktualizr_get/get.cc | 4 ++-- src/aktualizr_get/get.h | 2 +- src/aktualizr_get/get_test.cc | 3 ++- src/aktualizr_get/main.cc | 7 ++++++- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/aktualizr_get/get.cc b/src/aktualizr_get/get.cc index 5b471c9f17..abe89debcf 100644 --- a/src/aktualizr_get/get.cc +++ b/src/aktualizr_get/get.cc @@ -3,11 +3,11 @@ #include "http/httpclient.h" #include "storage/invstorage.h" -std::string aktualizrGet(Config &config, const std::string &url) { +std::string aktualizrGet(Config &config, const std::string &url, const std::vector &headers) { auto storage = INvStorage::newStorage(config.storage); storage->importData(config.import); - auto client = std_::make_unique(); + auto client = std_::make_unique(&headers); KeyManager keys(storage, config.keymanagerConfig()); keys.copyCertsToCurl(*client); auto resp = client->get(url, HttpInterface::kNoLimit); diff --git a/src/aktualizr_get/get.h b/src/aktualizr_get/get.h index 8e6a7ecb4d..a1421eea15 100644 --- a/src/aktualizr_get/get.h +++ b/src/aktualizr_get/get.h @@ -3,6 +3,6 @@ #include "libaktualizr/config.h" -std::string aktualizrGet(Config &config, const std::string &url); +std::string aktualizrGet(Config &config, const std::string &url, const std::vector &headers); #endif // AKTUALIZR_GET_HELPERS diff --git a/src/aktualizr_get/get_test.cc b/src/aktualizr_get/get_test.cc index c6e709972e..7ac14591c7 100644 --- a/src/aktualizr_get/get_test.cc +++ b/src/aktualizr_get/get_test.cc @@ -12,7 +12,8 @@ TEST(aktualizr_get, good) { TemporaryDirectory dir; config.storage.path = dir.Path(); - std::string body = aktualizrGet(config, server + "/path/1/2/3"); + std::vector headers; + std::string body = aktualizrGet(config, server + "/path/1/2/3", headers); EXPECT_EQ("{\"path\": \"/path/1/2/3\"}", body); } diff --git a/src/aktualizr_get/main.cc b/src/aktualizr_get/main.cc index d787859429..994df0b633 100644 --- a/src/aktualizr_get/main.cc +++ b/src/aktualizr_get/main.cc @@ -32,6 +32,7 @@ bpo::variables_map parse_options(int argc, char **argv) { ("help,h", "print usage") ("version,v", "Current aktualizr-get version") ("config,c", bpo::value >()->composing(), "configuration file or directory, by default /var/sota") + ("header,H", bpo::value >()->composing(), "Additional headers to pass") ("loglevel", bpo::value(), "set log level 0-5 (trace, debug, info, warning, error, fatal)") ("url,u", bpo::value(), "url to get, mandatory"); // clang-format on @@ -65,7 +66,11 @@ int main(int argc, char *argv[]) { int r = EXIT_FAILURE; try { Config config(commandline_map); - std::string body = aktualizrGet(config, commandline_map["url"].as()); + std::vector headers; + if (commandline_map.count("header") == 1) { + headers = commandline_map["header"].as>(); + } + std::string body = aktualizrGet(config, commandline_map["url"].as(), headers); std::cout << body; r = EXIT_SUCCESS;