32#include <shared_mutex>
60static size_t write_callback(
void* aContents,
size_t aSize,
size_t aNmemb,
void* aUserp )
62 size_t realsize = aSize * aNmemb;
64 std::string* p =
static_cast<std::string*
>( aUserp );
66 p->append(
static_cast<const char*
>( aContents ), realsize );
74 size_t realsize = aSize * aNmemb;
76 std::ostream* p =
static_cast<std::ostream*
>( aUserp );
78 p->write(
static_cast<const char*
>( aContents ), realsize );
84#if LIBCURL_VERSION_NUM >= 0x072000
86static int xferinfo(
void* aProgress, curl_off_t aDLtotal, curl_off_t aDLnow, curl_off_t aULtotal,
93 curl_off_t curtime = 0;
95 curl_easy_getinfo( progress->
m_Curl->
GetCurl(), CURLINFO_TOTAL_TIME, &curtime );
100 return progress->
m_Callback( aDLtotal, aDLnow, aULtotal, aULnow );
108static int progressinfo(
void* aProgress,
double aDLtotal,
double aDLnow,
double aULtotal,
111 return xferinfo( aProgress,
static_cast<curl_off_t
>( aDLtotal ),
112 static_cast<curl_off_t
>( aDLnow ),
static_cast<curl_off_t
>( aULtotal ),
113 static_cast<curl_off_t
>( aULnow ) );
123 m_CURL = curl_easy_init();
129 curl_easy_setopt(
m_CURL, CURLOPT_WRITEDATA,
static_cast<void*
>( &
m_buffer ) );
132#if LIBCURL_VERSION_NUM >= 0x075500
133 curl_easy_setopt(
m_CURL, CURLOPT_PROTOCOLS_STR,
"http,https");
135 curl_easy_setopt(
m_CURL, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS );
142 curl_easy_setopt(
m_CURL, CURLOPT_CONNECTTIMEOUT, 30L );
145 long sslOpts = CURLSSLOPT_NATIVE_CA;
147 std::optional<POLICY_CURL_SSL_REVOKE> policyState =
153 sslOpts |= CURLSSLOPT_REVOKE_BEST_EFFORT;
155 sslOpts |= CURLSSLOPT_NO_REVOKE;
161 sslOpts |= CURLSSLOPT_REVOKE_BEST_EFFORT;
165 curl_easy_setopt(
m_CURL, CURLOPT_SSL_OPTIONS, sslOpts );
168 if( wxGetEnv( wxT(
"KICAD_CURL_VERBOSE" ),
nullptr ) )
171 curl_easy_setopt(
m_CURL, CURLOPT_VERBOSE, 1L );
174 wxString application( wxS(
"KiCad" ) );
178#if defined( KICAD_BUILD_ARCH_X64 )
179 platform << wxS(
";64-bit" );
180#elif defined( KICAD_BUILD_ARCH_X86 )
181 platform << wxS(
";32-bit" );
182#elif defined( KICAD_BUILD_ARCH_ARM )
183 platform << wxS(
";ARM 32-bit" );
184#elif defined( KICAD_BUILD_ARCH_ARM64 )
185 platform << wxS(
";ARM 64-bit" );
188 platform << wxS(
")" );
190 wxString user_agent = wxS(
"KiCad/" ) + version + wxS(
" " ) + platform + wxS(
" " ) + application;
194 setOption( CURLOPT_ACCEPT_ENCODING,
"gzip,deflate" );
203 curl_easy_cleanup(
m_CURL );
215 return curl_easy_perform(
m_CURL );
221 std::string
header = aName +
':' + aValue;
229 return curl_easy_setopt(
m_CURL,
static_cast<CURLoption
>( aOption ), aArg );
235 return curl_easy_strerror(
static_cast<CURLcode
>( aCode ) );
250 std::string postfields;
252 for(
size_t i = 0; i < aFields.size(); i++ )
257 postfields +=
Escape( aFields[i].first );
259 postfields +=
Escape( aFields[i].second );
288 curl_easy_setopt(
m_CURL, CURLOPT_PROXY,
static_cast<const char*
>( cfg.
host.c_str() ) );
292 curl_easy_setopt(
m_CURL, CURLOPT_PROXYUSERNAME,
293 static_cast<const char*
>( cfg.
username.c_str() ) );
298 curl_easy_setopt(
m_CURL, CURLOPT_PROXYPASSWORD,
299 static_cast<const char*
>( cfg.
password.c_str() ) );
312 if(
setOption<long>( CURLOPT_FOLLOWLOCATION, ( aFollow ? 1 : 0 ) ) == CURLE_OK )
321 char* escaped = curl_easy_escape(
m_CURL, aUrl.c_str(), aUrl.length() );
323 std::string ret( escaped );
324 curl_free( escaped );
332 progress = std::make_unique<CURL_PROGRESS>(
this, aCallback,
static_cast<curl_off_t
>( aInterval ) );
334#if LIBCURL_VERSION_NUM >= 0x072000
335 setOption( CURLOPT_XFERINFOFUNCTION, xferinfo );
350 curl_easy_setopt(
m_CURL, CURLOPT_WRITEDATA,
reinterpret_cast<const void*
>( aOutput ) );
357 return setOption( CURLOPT_CONNECTTIMEOUT, aTimeoutSecs ) == CURLE_OK;
363 return setOption( CURLOPT_TIMEOUT, aTimeoutSecs ) == CURLE_OK;
369 bool ok =
setOption( CURLOPT_LOW_SPEED_LIMIT, aMinBytesPerSec ) == CURLE_OK;
370 ok &=
setOption( CURLOPT_LOW_SPEED_TIME, aDurationSecs ) == CURLE_OK;
377#if LIBCURL_VERSION_NUM >= 0x073700
379 int result = curl_easy_getinfo(
m_CURL, CURLINFO_SIZE_DOWNLOAD_T, &dl );
380 aDownloadedBytes =
static_cast<uint64_t
>( dl );
383 int result = curl_easy_getinfo(
m_CURL, CURLINFO_SIZE_DOWNLOAD, &dl );
384 aDownloadedBytes =
static_cast<uint64_t
>( dl );
394 curl_easy_getinfo(
m_CURL, CURLINFO_RESPONSE_CODE, &http_code );
396 return static_cast<int>( http_code );
wxString GetBuildVersion()
Get the full KiCad version string.
wxString GetPlatformGetBitnessName()
wxString GetBuildDate()
Get the build date as a string.
int Perform()
Equivalent to curl_easy_perform.
bool SetPostFields(const std::vector< std::pair< std::string, std::string > > &aFields)
Set fields for application/x-www-form-urlencoded POST request.
std::unique_ptr< CURL_PROGRESS > progress
std::string Escape(const std::string &aUrl)
Escapes a string for use as a URL.
bool SetUserAgent(const std::string &aAgent)
Set the request user agent.
std::shared_lock< std::shared_mutex > m_curlSharedLock
int GetResponseStatusCode()
void SetHeader(const std::string &aName, const std::string &aValue)
Set an arbitrary header for the HTTP(s) request.
bool SetTransferCallback(const TRANSFER_CALLBACK &aCallback, size_t aInterval)
bool SetURL(const std::string &aURL)
Set the request URL.
bool SetFollowRedirects(bool aFollow)
Enable the following of HTTP(s) and other redirects, by default curl does not follow redirects.
bool SetConnectTimeout(long aTimeoutSecs)
Set the connection timeout in seconds.
bool SetStallTimeout(long aMinBytesPerSec, long aDurationSecs)
Detect stalled transfers without limiting overall transfer time.
int GetTransferTotal(uint64_t &aDownloadedBytes) const
int setOption(int aOption, T aArg)
Set a curl option, only supports single parameter curl options.
bool SetTimeout(long aTimeoutSecs)
Set the total operation timeout in seconds.
bool SetOutputStream(const std::ostream *aOutput)
const std::string GetErrorText(int aCode)
Fetch CURL's "friendly" error string for a given error code.
Simple wrapper class to call curl_global_init and curl_global_cleanup for KiCad.
static bool IsShuttingDown()
Returns true if all curl operations should terminate.
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
static int progressinfo(void *aProgress, double aDLtotal, double aDLnow, double aULtotal, double aULnow)
static size_t write_callback(void *aContents, size_t aSize, size_t aNmemb, void *aUserp)
static size_t stream_write_callback(void *aContents, size_t aSize, size_t aNmemb, void *aUserp)
std::function< int(size_t, size_t, size_t, size_t)> TRANSFER_CALLBACK
Wrapper interface around the curl_easy API/.
#define POLICY_KEY_REQUESTS_CURL_REVOKE
CURL_PROGRESS(KICAD_CURL_EASY *aCURL, TRANSFER_CALLBACK aCallback, curl_off_t aInterval)
TRANSFER_CALLBACK m_Callback
curl_off_t m_Last_run_time
std::vector< std::string > header
wxString result
Test unit parsing edge cases and error handling.