Audacity 3.2.0
Classes | Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
audacity::network_manager::CurlHandleManager Class Referencefinal

#include <CurlHandleManager.h>

Collaboration diagram for audacity::network_manager::CurlHandleManager:
[legend]

Classes

struct  CachedHandle
 
class  Handle
 

Public Member Functions

 CurlHandleManager ()
 
 ~CurlHandleManager ()
 
void setProxy (std::string proxy)
 
Handle getHandle (RequestVerb verb, const std::string &url)
 

Private Types

using RequestClock = std::chrono::steady_clock
 
using RequestTimePoint = RequestClock::time_point
 

Private Member Functions

std::string getUserAgent () const
 
CURL * getCurlHandleFromCache (RequestVerb verb, const std::string &url)
 
void cacheHandle (Handle &handle)
 
void cleanupHandlesCache ()
 

Static Private Member Functions

static std::string GetSchemeAndDomain (const std::string &url)
 

Private Attributes

std::string mProxy
 
std::string mUserAgent
 
std::mutex mHandleCacheLock
 
std::vector< CachedHandlemHandleCache
 

Static Private Attributes

static constexpr std::chrono::milliseconds KEEP_ALIVE_IDLE { std::chrono::seconds (120) }
 
static constexpr std::chrono::milliseconds KEEP_ALIVE_PROBE { std::chrono::seconds (60) }
 

Detailed Description

Definition at line 31 of file CurlHandleManager.h.

Member Typedef Documentation

◆ RequestClock

using audacity::network_manager::CurlHandleManager::RequestClock = std::chrono::steady_clock
private

Definition at line 99 of file CurlHandleManager.h.

◆ RequestTimePoint

using audacity::network_manager::CurlHandleManager::RequestTimePoint = RequestClock::time_point
private

Definition at line 100 of file CurlHandleManager.h.

Constructor & Destructor Documentation

◆ CurlHandleManager()

audacity::network_manager::CurlHandleManager::CurlHandleManager ( )

Definition at line 266 of file CurlHandleManager.cpp.

267{
268 std::ostringstream ss;
269
270 ss << "Audacity/" <<
271 AUDACITY_VERSION << "." <<
272 AUDACITY_RELEASE << "." <<
273 AUDACITY_REVISION <<
274 " (";
275
277
278 ss << ")";
279
280 mUserAgent = ss.str ();
281
283}
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:202
void GetOSString(std::ostringstream &output, const wxPlatformInfo &platformInfo)

References audacity::network_manager::anonymous_namespace{CurlHandleManager.cpp}::gCurlConfig, BasicUI::Get(), audacity::network_manager::anonymous_namespace{CurlHandleManager.cpp}::GetOSString(), mProxy, mUserAgent, and audacity::network_manager::anonymous_namespace{CurlHandleManager.cpp}::CurlConfig::Proxy.

Here is the call graph for this function:

◆ ~CurlHandleManager()

audacity::network_manager::CurlHandleManager::~CurlHandleManager ( )

Definition at line 285 of file CurlHandleManager.cpp.

286{
287 std::lock_guard<std::mutex> lock (mHandleCacheLock);
288
289 for (auto& cachedHandle : mHandleCache)
290 curl_easy_cleanup (cachedHandle.Handle);
291}

References mHandleCache, and mHandleCacheLock.

Member Function Documentation

◆ cacheHandle()

void audacity::network_manager::CurlHandleManager::cacheHandle ( Handle handle)
private

Definition at line 349 of file CurlHandleManager.cpp.

350{
351 // Reset the state to the safe defaults
352 handle.reset ();
353
354 std::lock_guard<std::mutex> lock (mHandleCacheLock);
355
357
358 mHandleCache.push_back ({
359 handle.mVerb,
360 GetSchemeAndDomain (handle.mUrl),
361 handle.mHandle,
362 RequestClock::now ()
363 });
364}
static std::string GetSchemeAndDomain(const std::string &url)

References cleanupHandlesCache(), GetSchemeAndDomain(), audacity::network_manager::CurlHandleManager::Handle::mHandle, mHandleCache, mHandleCacheLock, audacity::network_manager::CurlHandleManager::Handle::mUrl, audacity::network_manager::CurlHandleManager::Handle::mVerb, and audacity::network_manager::CurlHandleManager::Handle::reset().

Referenced by audacity::network_manager::CurlHandleManager::Handle::~Handle().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cleanupHandlesCache()

void audacity::network_manager::CurlHandleManager::cleanupHandlesCache ( )
private

Definition at line 366 of file CurlHandleManager.cpp.

367{
368 const RequestTimePoint timePoint = RequestClock::now ();
369
370 mHandleCache.erase (std::remove_if (mHandleCache.begin (), mHandleCache.end (), [timePoint](const CachedHandle& cachedHandle) {
371 return (timePoint - cachedHandle.RequestTime) >= KEEP_ALIVE_IDLE;
372 }), mHandleCache.end ());
373}

References mHandleCache.

Referenced by cacheHandle(), and getCurlHandleFromCache().

Here is the caller graph for this function:

◆ getCurlHandleFromCache()

CURL * audacity::network_manager::CurlHandleManager::getCurlHandleFromCache ( RequestVerb  verb,
const std::string &  url 
)
private

Definition at line 327 of file CurlHandleManager.cpp.

328{
329 std::lock_guard<std::mutex> lock (mHandleCacheLock);
330
332
333 const std::string schemeAndDomain = GetSchemeAndDomain (url);
334
335 auto it = std::find_if (mHandleCache.begin (), mHandleCache.end (), [verb, schemeAndDomain](const CachedHandle& handle) {
336 return handle.Verb == verb && handle.SchemeAndDomain == schemeAndDomain;
337 });
338
339 if (it == mHandleCache.end ())
340 return nullptr;
341
342 CURL* handle = it->Handle;
343
344 mHandleCache.erase (it);
345
346 return handle;
347}

References cleanupHandlesCache(), GetSchemeAndDomain(), audacity::network_manager::CurlHandleManager::CachedHandle::Handle, mHandleCache, and mHandleCacheLock.

Referenced by getHandle().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getHandle()

CurlHandleManager::Handle audacity::network_manager::CurlHandleManager::getHandle ( RequestVerb  verb,
const std::string &  url 
)

Definition at line 298 of file CurlHandleManager.cpp.

299{
300 Handle handle (this, getCurlHandleFromCache (verb, url), verb, url);
301
302 if (!mProxy.empty ())
303 {
304 handle.setOption (CURLOPT_PROXY, mProxy);
305 // If we use proxy, checking the CRL will likely break the SSL proxying
306 handle.setOption (CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE);
307 }
308
309 handle.setOption (CURLOPT_TCP_KEEPALIVE, 1L);
310
311 handle.setOption (CURLOPT_TCP_KEEPIDLE,
312 std::chrono::duration_cast<std::chrono::seconds> (KEEP_ALIVE_IDLE).count ()
313 );
314
315 handle.setOption (CURLOPT_TCP_KEEPINTVL,
316 std::chrono::duration_cast<std::chrono::seconds> (KEEP_ALIVE_PROBE).count ()
317 );
318
319 return handle;
320}
static constexpr std::chrono::milliseconds KEEP_ALIVE_PROBE
CURL * getCurlHandleFromCache(RequestVerb verb, const std::string &url)
static constexpr std::chrono::milliseconds KEEP_ALIVE_IDLE

References getCurlHandleFromCache(), KEEP_ALIVE_IDLE, KEEP_ALIVE_PROBE, mProxy, and audacity::network_manager::CurlHandleManager::Handle::setOption().

Referenced by audacity::network_manager::CurlResponse::perform().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetSchemeAndDomain()

std::string audacity::network_manager::CurlHandleManager::GetSchemeAndDomain ( const std::string &  url)
staticprivate

Definition at line 375 of file CurlHandleManager.cpp.

376{
377 const size_t schemeEndPosition = url.find ("://");
378
379 if (schemeEndPosition == std::string::npos) // Is url even valid?
380 return url;
381
382 const size_t domainStartPosition = schemeEndPosition + 3;
383
384 const size_t slashLocation = url.find ('/', domainStartPosition);
385
386 if (slashLocation == std::string::npos)
387 return url;
388
389 return url.substr (domainStartPosition, slashLocation - domainStartPosition);
390}
constexpr size_t npos(-1)

References Tuple::detail::npos().

Referenced by cacheHandle(), and getCurlHandleFromCache().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getUserAgent()

std::string audacity::network_manager::CurlHandleManager::getUserAgent ( ) const
private

Definition at line 322 of file CurlHandleManager.cpp.

323{
324 return mUserAgent;
325}

References mUserAgent.

◆ setProxy()

void audacity::network_manager::CurlHandleManager::setProxy ( std::string  proxy)

Definition at line 293 of file CurlHandleManager.cpp.

294{
295 mProxy = std::move (proxy);
296}

References mProxy.

Member Data Documentation

◆ KEEP_ALIVE_IDLE

constexpr std::chrono::milliseconds audacity::network_manager::CurlHandleManager::KEEP_ALIVE_IDLE { std::chrono::seconds (120) }
staticconstexprprivate

Definition at line 102 of file CurlHandleManager.h.

Referenced by getHandle().

◆ KEEP_ALIVE_PROBE

constexpr std::chrono::milliseconds audacity::network_manager::CurlHandleManager::KEEP_ALIVE_PROBE { std::chrono::seconds (60) }
staticconstexprprivate

Definition at line 103 of file CurlHandleManager.h.

Referenced by getHandle().

◆ mHandleCache

std::vector<CachedHandle> audacity::network_manager::CurlHandleManager::mHandleCache
private

◆ mHandleCacheLock

std::mutex audacity::network_manager::CurlHandleManager::mHandleCacheLock
private

Definition at line 127 of file CurlHandleManager.h.

Referenced by cacheHandle(), getCurlHandleFromCache(), and ~CurlHandleManager().

◆ mProxy

std::string audacity::network_manager::CurlHandleManager::mProxy
private

Definition at line 124 of file CurlHandleManager.h.

Referenced by CurlHandleManager(), getHandle(), and setProxy().

◆ mUserAgent

std::string audacity::network_manager::CurlHandleManager::mUserAgent
private

Definition at line 125 of file CurlHandleManager.h.

Referenced by CurlHandleManager(), and getUserAgent().


The documentation for this class was generated from the following files: