Skip to content

Commit

Permalink
make the runtime client's user agent overrideable (#106)
Browse files Browse the repository at this point in the history
* make the runtime client's user agent overrideable

* remove extra empty string

* clang-format -i src/*
  • Loading branch information
bmoffatt committed Oct 21, 2020
1 parent 3b90c85 commit aa351a6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
2 changes: 2 additions & 0 deletions include/aws/lambda-runtime/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class runtime {
using next_outcome = aws::lambda_runtime::outcome<invocation_request, aws::http::response_code>;
using post_outcome = aws::lambda_runtime::outcome<no_result, aws::http::response_code>;

runtime(std::string const& endpoint, std::string const& user_agent);
runtime(std::string const& endpoint);
~runtime();

Expand Down Expand Up @@ -164,6 +165,7 @@ class runtime {
invocation_response const& handler_response);

private:
std::string const m_user_agent_header;
std::array<std::string const, 3> const m_endpoints;
CURL* const m_curl_handle;
};
Expand Down
20 changes: 8 additions & 12 deletions src/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -163,10 +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",
endpoint + "/2018-06-01/runtime/invocation/next",
endpoint + "/2018-06-01/runtime/invocation/"}},
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())
{
if (!m_curl_handle) {
Expand Down Expand Up @@ -234,7 +230,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());
Expand Down Expand Up @@ -339,7 +335,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());
Expand Down

0 comments on commit aa351a6

Please sign in to comment.