Skip to content
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

Send User-Agent header to service #182

Merged
merged 7 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 9 additions & 3 deletions client/src/details/connection/CurlConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ CurlConnection::CurlConnection(const ConnectionConfig& config, const ReportingHa
m_handler,
"Failed to set up curl");

// TODO #40: Allow passing user agent in the header
// TODO #41: Pass AAD token in the header if it is available
// TODO #42: Cert pinning with service
}
Expand Down Expand Up @@ -328,8 +327,7 @@ std::string CurlConnection::CurlPerform(const std::string& url, CurlHeaderList&
THROW_IF_CURL_SETUP_ERROR(curl_easy_setopt(m_handle, CURLOPT_URL, url.c_str()));

const std::string cv = m_cv.IncrementAndGet();
headers.Add(HttpHeader::MSCV, cv);
THROW_IF_CURL_SETUP_ERROR(curl_easy_setopt(m_handle, CURLOPT_HTTPHEADER, headers.m_slist));
SetHeaders(headers, cv);

// Setting up error buffer where error messages get written - this gets unset in the destructor
CurlErrorBuffer errorBuffer(m_handle, m_handler);
Expand Down Expand Up @@ -424,3 +422,11 @@ void CurlConnection::ProcessRetry(int attempt, const Result& httpResult)
LOG_INFO(m_handler, "Sleeping for %lld ms", static_cast<long long>(retryDelay.count()));
std::this_thread::sleep_for(retryDelay);
}

void CurlConnection::SetHeaders(CurlHeaderList& headers, const std::string cv)
arthuraraujo-msft marked this conversation as resolved.
Show resolved Hide resolved
arthuraraujo-msft marked this conversation as resolved.
Show resolved Hide resolved
arthuraraujo-msft marked this conversation as resolved.
Show resolved Hide resolved
{
headers.Add(HttpHeader::UserAgent, GetUserAgentValue());
headers.Add(HttpHeader::MSCV, cv);

THROW_IF_CURL_SETUP_ERROR(curl_easy_setopt(m_handle, CURLOPT_HTTPHEADER, headers.m_slist));
}
5 changes: 5 additions & 0 deletions client/src/details/connection/CurlConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class CurlConnection : public Connection
*/
void ProcessRetry(int attempt, const Result& httpResult);

/**
* @brief Set the headers for the request
*/
void SetHeaders(CurlHeaderList& headers, const std::string cv);

protected:
/**
* @brief Perform a REST request to the given @param url with the given @param headers
Expand Down
9 changes: 9 additions & 0 deletions client/src/details/connection/HttpHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <correlation_vector/correlation_vector.h>

constexpr const char* c_userAgent = "Microsoft-SFSClient/" SFS_VERSION;
arthuraraujo-msft marked this conversation as resolved.
Show resolved Hide resolved

std::string SFS::details::ToString(HttpHeader header)
{
switch (header)
Expand All @@ -15,7 +17,14 @@ std::string SFS::details::ToString(HttpHeader header)
return microsoft::correlation_vector::HEADER_NAME;
case HttpHeader::RetryAfter:
return "Retry-After";
case HttpHeader::UserAgent:
return "User-Agent";
}

return "";
}

std::string SFS::details::GetUserAgentValue()
{
return c_userAgent;
}
3 changes: 3 additions & 0 deletions client/src/details/connection/HttpHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ enum class HttpHeader
ContentType,
MSCV,
RetryAfter,
UserAgent,
};

std::string ToString(HttpHeader header);

std::string GetUserAgentValue();
} // namespace SFS::details
1 change: 1 addition & 0 deletions client/tests/functional/details/CurlConnectionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ TEST("Testing CurlConnection()")
CurlConnectionManager connectionManager(handler);
auto connection = connectionManager.MakeConnection({});
SFSUrlBuilder urlBuilder(SFSCustomUrl(server.GetBaseUrl()), c_instanceId, c_namespace, handler);
server.RegisterExpectedRequestHeader(HttpHeader::UserAgent, GetUserAgentValue());

SECTION("Testing CurlConnection::Get()")
{
Expand Down
1 change: 1 addition & 0 deletions client/tests/functional/details/SFSClientImplTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ TEST("Testing class SFSClientImpl()")
ConnectionConfig config;
config.baseCV = cv;
auto connection = sfsClient.MakeConnection(config);
server.RegisterExpectedRequestHeader(HttpHeader::UserAgent, GetUserAgentValue());

SECTION("Generic products")
{
Expand Down