Skip to content

make the runtime client's user agent overrideable #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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