Skip to content

Commit aa351a6

Browse files
authored
make the runtime client's user agent overrideable (#106)
* make the runtime client's user agent overrideable * remove extra empty string * clang-format -i src/*
1 parent 3b90c85 commit aa351a6

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

Diff for: include/aws/lambda-runtime/runtime.h

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class runtime {
137137
using next_outcome = aws::lambda_runtime::outcome<invocation_request, aws::http::response_code>;
138138
using post_outcome = aws::lambda_runtime::outcome<no_result, aws::http::response_code>;
139139

140+
runtime(std::string const& endpoint, std::string const& user_agent);
140141
runtime(std::string const& endpoint);
141142
~runtime();
142143

@@ -164,6 +165,7 @@ class runtime {
164165
invocation_response const& handler_response);
165166

166167
private:
168+
std::string const m_user_agent_header;
167169
std::array<std::string const, 3> const m_endpoints;
168170
CURL* const m_curl_handle;
169171
};

Diff for: src/runtime.cpp

+8-12
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,6 @@ static size_t write_header(char* ptr, size_t size, size_t nmemb, void* userdata)
124124
return size * nmemb;
125125
}
126126

127-
static std::string const& get_user_agent_header()
128-
{
129-
static std::string user_agent = std::string("User-Agent: AWS_Lambda_Cpp/") + get_version();
130-
return user_agent;
131-
}
132-
133127
static size_t read_data(char* buffer, size_t size, size_t nitems, void* userdata)
134128
{
135129
auto const limit = size * nitems;
@@ -163,10 +157,12 @@ static int rt_curl_debug_callback(CURL* handle, curl_infotype type, char* data,
163157
}
164158
#endif
165159

166-
runtime::runtime(std::string const& endpoint)
167-
: m_endpoints{{endpoint + "/2018-06-01/runtime/init/error",
168-
endpoint + "/2018-06-01/runtime/invocation/next",
169-
endpoint + "/2018-06-01/runtime/invocation/"}},
160+
runtime::runtime(std::string const& endpoint) : runtime(endpoint, "AWS_Lambda_Cpp/" + std::string(get_version())) {}
161+
162+
runtime::runtime(std::string const& endpoint, std::string const& user_agent)
163+
: m_user_agent_header("User-Agent: " + user_agent), m_endpoints{{endpoint + "/2018-06-01/runtime/init/error",
164+
endpoint + "/2018-06-01/runtime/invocation/next",
165+
endpoint + "/2018-06-01/runtime/invocation/"}},
170166
m_curl_handle(curl_easy_init())
171167
{
172168
if (!m_curl_handle) {
@@ -234,7 +230,7 @@ runtime::next_outcome runtime::get_next()
234230
curl_easy_setopt(m_curl_handle, CURLOPT_HEADERDATA, &resp);
235231

236232
curl_slist* headers = nullptr;
237-
headers = curl_slist_append(headers, get_user_agent_header().c_str());
233+
headers = curl_slist_append(headers, m_user_agent_header.c_str());
238234
curl_easy_setopt(m_curl_handle, CURLOPT_HTTPHEADER, headers);
239235

240236
logging::log_debug(LOG_TAG, "Making request to %s", m_endpoints[Endpoints::NEXT].c_str());
@@ -339,7 +335,7 @@ runtime::post_outcome runtime::do_post(
339335

340336
headers = curl_slist_append(headers, "Expect:");
341337
headers = curl_slist_append(headers, "transfer-encoding:");
342-
headers = curl_slist_append(headers, get_user_agent_header().c_str());
338+
headers = curl_slist_append(headers, m_user_agent_header.c_str());
343339
auto const& payload = handler_response.get_payload();
344340
logging::log_debug(
345341
LOG_TAG, "calculating content length... %s", ("content-length: " + std::to_string(payload.length())).c_str());

0 commit comments

Comments
 (0)