From 9f33c31a2d50e4b25e052db4387101e824bfa003 Mon Sep 17 00:00:00 2001 From: Bryan Moffatt Date: Wed, 21 Oct 2020 11:21:45 -0700 Subject: [PATCH 1/3] make the runtime client's user agent overrideable --- include/aws/lambda-runtime/runtime.h | 2 ++ src/runtime.cpp | 18 ++++++++---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/aws/lambda-runtime/runtime.h b/include/aws/lambda-runtime/runtime.h index 0dc292c..94e1e22 100644 --- a/include/aws/lambda-runtime/runtime.h +++ b/include/aws/lambda-runtime/runtime.h @@ -137,6 +137,7 @@ class runtime { using next_outcome = aws::lambda_runtime::outcome; using post_outcome = aws::lambda_runtime::outcome; + runtime(std::string const& endpoint, std::string const& user_agent); runtime(std::string const& endpoint); ~runtime(); @@ -164,6 +165,7 @@ class runtime { invocation_response const& handler_response); private: + std::string const m_user_agent_header; std::array const m_endpoints; CURL* const m_curl_handle; }; diff --git a/src/runtime.cpp b/src/runtime.cpp index 36fe22b..bf223d0 100644 --- a/src/runtime.cpp +++ b/src/runtime.cpp @@ -124,12 +124,6 @@ static size_t write_header(char* ptr, size_t size, size_t nmemb, void* userdata) return size * nmemb; } -static std::string const& get_user_agent_header() -{ - static std::string user_agent = std::string("User-Agent: AWS_Lambda_Cpp/") + get_version(); - return user_agent; -} - static size_t read_data(char* buffer, size_t size, size_t nitems, void* userdata) { auto const limit = size * nitems; @@ -163,8 +157,12 @@ static int rt_curl_debug_callback(CURL* handle, curl_infotype type, char* data, } #endif -runtime::runtime(std::string const& endpoint) - : m_endpoints{{endpoint + "/2018-06-01/runtime/init/error", +runtime::runtime(std::string const& endpoint) : runtime(endpoint, "AWS_Lambda_Cpp/" + std::string(get_version())) +{} + +runtime::runtime(std::string const& endpoint, std::string const& user_agent) + : m_user_agent_header("User-Agent: " + user_agent + ""), + m_endpoints{{endpoint + "/2018-06-01/runtime/init/error", endpoint + "/2018-06-01/runtime/invocation/next", endpoint + "/2018-06-01/runtime/invocation/"}}, m_curl_handle(curl_easy_init()) @@ -234,7 +232,7 @@ runtime::next_outcome runtime::get_next() curl_easy_setopt(m_curl_handle, CURLOPT_HEADERDATA, &resp); curl_slist* headers = nullptr; - headers = curl_slist_append(headers, get_user_agent_header().c_str()); + headers = curl_slist_append(headers, m_user_agent_header.c_str()); curl_easy_setopt(m_curl_handle, CURLOPT_HTTPHEADER, headers); logging::log_debug(LOG_TAG, "Making request to %s", m_endpoints[Endpoints::NEXT].c_str()); @@ -339,7 +337,7 @@ runtime::post_outcome runtime::do_post( headers = curl_slist_append(headers, "Expect:"); headers = curl_slist_append(headers, "transfer-encoding:"); - headers = curl_slist_append(headers, get_user_agent_header().c_str()); + headers = curl_slist_append(headers, m_user_agent_header.c_str()); auto const& payload = handler_response.get_payload(); logging::log_debug( LOG_TAG, "calculating content length... %s", ("content-length: " + std::to_string(payload.length())).c_str()); From 1588cb97e4e6afdb2844772de4b34d993b9a2541 Mon Sep 17 00:00:00 2001 From: Bryan Moffatt Date: Wed, 21 Oct 2020 12:10:12 -0700 Subject: [PATCH 2/3] remove extra empty string --- src/runtime.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime.cpp b/src/runtime.cpp index bf223d0..82dd8c3 100644 --- a/src/runtime.cpp +++ b/src/runtime.cpp @@ -161,7 +161,7 @@ runtime::runtime(std::string const& endpoint) : runtime(endpoint, "AWS_Lambda_Cp {} runtime::runtime(std::string const& endpoint, std::string const& user_agent) - : m_user_agent_header("User-Agent: " + user_agent + ""), + : m_user_agent_header("User-Agent: " + user_agent), m_endpoints{{endpoint + "/2018-06-01/runtime/init/error", endpoint + "/2018-06-01/runtime/invocation/next", endpoint + "/2018-06-01/runtime/invocation/"}}, From ebe9fb3d8632334b70dc184717d52fbde4fb7183 Mon Sep 17 00:00:00 2001 From: Bryan Moffatt Date: Wed, 21 Oct 2020 12:26:51 -0700 Subject: [PATCH 3/3] clang-format -i src/* --- src/runtime.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/runtime.cpp b/src/runtime.cpp index 82dd8c3..9175084 100644 --- a/src/runtime.cpp +++ b/src/runtime.cpp @@ -157,14 +157,12 @@ static int rt_curl_debug_callback(CURL* handle, curl_infotype type, char* data, } #endif -runtime::runtime(std::string const& endpoint) : runtime(endpoint, "AWS_Lambda_Cpp/" + std::string(get_version())) -{} +runtime::runtime(std::string const& endpoint) : runtime(endpoint, "AWS_Lambda_Cpp/" + std::string(get_version())) {} runtime::runtime(std::string const& endpoint, std::string const& user_agent) - : m_user_agent_header("User-Agent: " + user_agent), - m_endpoints{{endpoint + "/2018-06-01/runtime/init/error", - endpoint + "/2018-06-01/runtime/invocation/next", - endpoint + "/2018-06-01/runtime/invocation/"}}, + : m_user_agent_header("User-Agent: " + user_agent), m_endpoints{{endpoint + "/2018-06-01/runtime/init/error", + endpoint + "/2018-06-01/runtime/invocation/next", + endpoint + "/2018-06-01/runtime/invocation/"}}, m_curl_handle(curl_easy_init()) { if (!m_curl_handle) {