3737#include < algorithm>
3838#include < filesystem>
3939#include < boost/algorithm/string.hpp>
40+ #include < boost/asio/ip/host_name.hpp>
4041#include < iostream>
4142#include < mutex>
4243#include < boost/interprocess/sync/named_semaphore.hpp>
@@ -51,6 +52,12 @@ using namespace std;
5152std::mutex gIOMutex ; // to protect TMemFile IO operations
5253unique_ptr<TJAlienCredentials> CcdbApi::mJAlienCredentials = nullptr ;
5354
55+ CcdbApi::CcdbApi ()
56+ {
57+ std::string host = boost::asio::ip::host_name ();
58+ mUniqueAgentID = fmt::format (" {}-{}-{}" , host, getCurrentTimestamp () / 1000 , o2::utils::Str::getRandomString (6 ));
59+ }
60+
5461CcdbApi::~CcdbApi ()
5562{
5663 curl_global_cleanup ();
@@ -74,7 +81,6 @@ void CcdbApi::init(std::string const& host)
7481
7582 if (host.substr (0 , 7 ).compare (SNAPSHOTPREFIX) == 0 ) {
7683 auto path = host.substr (7 );
77- LOG (info) << " Initializing CcdbApi in snapshot readonly mode ... reading snapshot from path " << path;
7884 initInSnapshotMode (path);
7985 } else {
8086 initHostsPool (host);
@@ -83,7 +89,7 @@ void CcdbApi::init(std::string const& host)
8389
8490 // find out if we can can in principle connect to Alien
8591 mHaveAlienToken = checkAlienToken ();
86- LOG (info) << " Is alien token present?: " << mHaveAlienToken ;
92+ LOGP (info, " Init CcdApi with UserAgentID: {}, Host: {}{}, alien- token: {} " , mUniqueAgentID , host, mInSnapshotMode ? " (snapshot readonly mode) " : " " , mHaveAlienToken ) ;
8793}
8894
8995/* *
@@ -189,6 +195,7 @@ int CcdbApi::storeAsBinaryFile(const char* buffer, size_t size, const std::strin
189195 curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headerlist);
190196 curl_easy_setopt (curl, CURLOPT_HTTPPOST, formpost);
191197 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L );
198+ curl_easy_setopt (curl, CURLOPT_USERAGENT, mUniqueAgentID .c_str ());
192199
193200 CURLcode res = CURL_LAST;
194201
@@ -395,7 +402,6 @@ void CcdbApi::initCurlOptionsForRetrieve(CURL* curlHandle, void* chunk, CurlWrit
395402{
396403 curl_easy_setopt (curlHandle, CURLOPT_WRITEFUNCTION, writeCallback);
397404 curl_easy_setopt (curlHandle, CURLOPT_WRITEDATA, chunk);
398- curl_easy_setopt (curlHandle, CURLOPT_USERAGENT, " libcurl-agent/1.0" );
399405 curl_easy_setopt (curlHandle, CURLOPT_FOLLOWLOCATION, followRedirect ? 1L : 0L );
400406}
401407
@@ -441,6 +447,8 @@ void CcdbApi::initHeadersForRetrieve(CURL* curlHandle, long timestamp, std::map<
441447 if (list) {
442448 curl_easy_setopt (curlHandle, CURLOPT_HTTPHEADER, list);
443449 }
450+
451+ curl_easy_setopt (curlHandle, CURLOPT_USERAGENT, mUniqueAgentID .c_str ());
444452}
445453
446454bool CcdbApi::receiveToFile (FILE* fileHandle, std::string const & path, std::map<std::string, std::string> const & metadata,
@@ -634,7 +642,7 @@ bool CcdbApi::retrieveBlob(std::string const& path, std::string const& targetdir
634642
635643 /* some servers don't like requests that are made without a user-agent
636644 field, so we provide one */
637- curl_easy_setopt (curl_handle, CURLOPT_USERAGENT, " libcurl-agent/1.0 " );
645+ curl_easy_setopt (curl_handle, CURLOPT_USERAGENT, mUniqueAgentID . c_str () );
638646
639647 /* if redirected , we tell libcurl to follow redirection */
640648 curl_easy_setopt (curl_handle, CURLOPT_FOLLOWLOCATION, 1L );
@@ -1209,6 +1217,7 @@ std::map<std::string, std::string> CcdbApi::retrieveHeaders(std::string const& p
12091217 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L );
12101218 curl_easy_setopt (curl, CURLOPT_HEADERFUNCTION, header_map_callback<>);
12111219 curl_easy_setopt (curl, CURLOPT_HEADERDATA, &headers);
1220+ curl_easy_setopt (curl, CURLOPT_USERAGENT, mUniqueAgentID .c_str ());
12121221
12131222 curlSetSSLOptions (curl);
12141223
@@ -1239,7 +1248,7 @@ std::map<std::string, std::string> CcdbApi::retrieveHeaders(std::string const& p
12391248 return headers;
12401249}
12411250
1242- bool CcdbApi::getCCDBEntryHeaders (std::string const & url, std::string const & etag, std::vector<std::string>& headers)
1251+ bool CcdbApi::getCCDBEntryHeaders (std::string const & url, std::string const & etag, std::vector<std::string>& headers, const std::string& agentID )
12431252{
12441253 auto curl = curl_easy_init ();
12451254 headers.clear ();
@@ -1258,6 +1267,9 @@ bool CcdbApi::getCCDBEntryHeaders(std::string const& url, std::string const& eta
12581267 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L );
12591268 curl_easy_setopt (curl, CURLOPT_HEADERFUNCTION, header_callback);
12601269 curl_easy_setopt (curl, CURLOPT_HEADERDATA, &headers);
1270+ if (!agentID.empty ()) {
1271+ curl_easy_setopt (curl, CURLOPT_USERAGENT, agentID.c_str ());
1272+ }
12611273
12621274 curlSetSSLOptions (curl);
12631275
@@ -1366,7 +1378,7 @@ void CcdbApi::updateMetadata(std::string const& path, std::map<std::string, std:
13661378 if (curl != nullptr ) {
13671379 curl_easy_setopt (curl, CURLOPT_URL, fullUrl.str ().c_str ());
13681380 curl_easy_setopt (curl, CURLOPT_CUSTOMREQUEST, " PUT" ); // make sure we use PUT
1369-
1381+ curl_easy_setopt (curl, CURLOPT_USERAGENT, mUniqueAgentID . c_str ());
13701382 curlSetSSLOptions (curl);
13711383
13721384 // Perform the request, res will get the return code
0 commit comments