16#include <wx/platinfo.h>
23namespace network_manager
28void GetOSString (std::ostringstream& output,
const wxPlatformInfo& platformInfo)
30 const wxOperatingSystemId osID = platformInfo.GetOperatingSystemId ();
32 if (osID & wxOS_WINDOWS)
34 else if (osID & wxOS_MAC)
36 else if (osID & wxOS_UNIX_LINUX)
38 else if (osID & wxOS_UNIX_FREEBSD)
40 else if (osID & wxOS_UNIX_OPENBSD)
46 platformInfo.GetOSMajorVersion () <<
48 platformInfo.GetOSMinorVersion () <<
50 platformInfo.GetOSMicroVersion() <<
53#if defined(__amd64__) || defined(__x86_64__) || defined(_M_X64)
55#elif defined(__i386__) || defined(i386) || defined(_M_IX86) || defined(_X86_) || defined(__THW_INTEL)
57#elif defined(__arm64__) || defined(__aarch64__) || defined(_M_ARM64)
59#elif defined(arm) || defined(__arm__) || defined(ARM) || defined(_ARM_)
72 bool SSLValidation {
true };
91 mUrl (std::move (url)),
92 mHandleFromCache (handle !=
nullptr)
94 if (mHandle ==
nullptr)
95 mHandle = curl_easy_init ();
97 setOption (CURLOPT_URL, mUrl);
102 setOption (CURLOPT_NOBODY, 1);
110 setOption (CURLOPT_POST, 1);
113 setOption (CURLOPT_UPLOAD, 1);
116 setOption (CURLOPT_CUSTOMREQUEST,
"DELETE");
119 setOption(CURLOPT_UPLOAD, 1);
120 setOption(CURLOPT_CUSTOMREQUEST,
"PATCH");
124 setOption (CURLOPT_NOSIGNAL, 1L);
127 enableSSLValidation();
129 disableSSLValidation();
131 setOption (CURLOPT_ACCEPT_ENCODING,
"");
136 *
this = std::move (rhs);
154 mUrl = std::move (rhs.mUrl);
155 mHeaders = std::move (rhs.mHeaders);
165 return setOption (option, value.c_str ());
170 return setOption(CURLOPT_COOKIE,
"Set-Cookie: " + cookie.Name +
"=" + cookie.Value);
175 for (
const Cookie& cookie : cookies)
177 const CURLcode result = appendCookie (cookie);
179 if (result != CURLE_OK)
189 mUserAgentSet =
true;
191 mHeaders.append(header.
Name +
": " + header.
Value);
196 for (
const Header& header : headers)
197 appendHeader (header);
203 mHeaders.append (
"User-Agent: " + mOwner->getUserAgent ());
205 CURLcode result = setOption (CURLOPT_HTTPHEADER, mHeaders.getCurlList ());
207 if (result != CURLE_OK)
208 return { result, std::string () };
210 char currentError[CURL_ERROR_SIZE] = {};
211 setOption(CURLOPT_ERRORBUFFER, currentError);
213 result = curl_easy_perform (mHandle);
215 mReuse = mReuse && result == CURLE_OK;
217 return { result, std::string (currentError) };
227 return mHandleFromCache;
234 if (CURLE_OK != curl_easy_getinfo(mHandle, CURLINFO_RESPONSE_CODE, &code))
242 setOption (CURLOPT_COOKIELIST,
nullptr);
243 setOption (CURLOPT_PROXY,
nullptr);
244 setOption (CURLOPT_SSL_OPTIONS, 0);
246 mUserAgentSet =
false;
256 setOption(CURLOPT_SSL_VERIFYPEER, 0L);
257 setOption(CURLOPT_SSL_VERIFYHOST, 0L);
262 setOption(CURLOPT_SSL_VERIFYPEER, 1L);
263 setOption(CURLOPT_SSL_VERIFYHOST, 2L);
268 std::ostringstream ss;
271 AUDACITY_VERSION <<
"." <<
272 AUDACITY_RELEASE <<
"." <<
290 curl_easy_cleanup (cachedHandle.Handle);
295 mProxy = std::move (proxy);
306 handle.
setOption (CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE);
309 handle.
setOption (CURLOPT_TCP_KEEPALIVE, 1L);
312 std::chrono::duration_cast<std::chrono::seconds> (
KEEP_ALIVE_IDLE).count ()
316 std::chrono::duration_cast<std::chrono::seconds> (
KEEP_ALIVE_PROBE).count ()
336 return handle.Verb == verb && handle.SchemeAndDomain == schemeAndDomain;
342 CURL* handle = it->
Handle;
371 return (timePoint - cachedHandle.RequestTime) >= KEEP_ALIVE_IDLE;
377 const size_t schemeEndPosition = url.find (
"://");
382 const size_t domainStartPosition = schemeEndPosition + 3;
384 const size_t slashLocation = url.find (
'/', domainStartPosition);
389 return url.substr (domainStartPosition, slashLocation - domainStartPosition);
Declare functions to perform UTF-8 to std::wstring conversions.
Declare a class responsible for reuse of CURL handles.
This specialization of Setting for bool adds a Toggle method to negate the saved value.
A listener notified of changes in preferences.
bool Read(T *pVar) const
overload of Read returning a boolean that is true if the value was previously defined */
Specialization of Setting for strings.
CURL * getCurlHandle() const noexcept
void enableSSLValidation()
Handle(CurlHandleManager *owner, CURL *handle, RequestVerb verb, std::string url) noexcept
Handle & operator=(Handle &&rhs) noexcept
void disableSSLValidation()
CURLcode appendCookie(const Cookie &cookie) noexcept
CURLcode appendCookies(const CookiesList &cookie) noexcept
bool isHandleFromCache() const noexcept
unsigned getHTTPCode() const noexcept
CURLcode setOption(CURLoption option, Args... value) noexcept
void appendHeader(const Header &header)
CurlHandleManager * mOwner
void appendHeaders(const HeadersList &headers)
void cacheHandle(Handle &handle)
static constexpr std::chrono::milliseconds KEEP_ALIVE_PROBE
RequestClock::time_point RequestTimePoint
Handle getHandle(RequestVerb verb, const std::string &url)
std::vector< CachedHandle > mHandleCache
void cleanupHandlesCache()
std::mutex mHandleCacheLock
CURL * getCurlHandleFromCache(RequestVerb verb, const std::string &url)
std::string getUserAgent() const
void setProxy(std::string proxy)
static std::string GetSchemeAndDomain(const std::string &url)
static constexpr std::chrono::milliseconds KEEP_ALIVE_IDLE
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
constexpr size_t npos(-1)
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
StringSetting ProxyStringPref
static CurlConfig gCurlConfig
BoolSetting EnableSSLValidationPref
void GetOSString(std::ostringstream &output, const wxPlatformInfo &platformInfo)
std::string ToUTF8(const std::wstring &wstr)
void UpdatePrefs() override