KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kicad_curl_easy.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2015 Mark Roszko <[email protected]>
5 * Copyright (C) 2015-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25// kicad_curl_easy.h **must be** included before any wxWidgets header to avoid conflicts
26// at least on Windows/msys2
27#include <curl/curl.h>
28#include <curl/easy.h>
31
32#include <cstdarg>
33#include <cstddef>
34#include <exception>
35#include <sstream>
36#include <wx/app.h>
37
38#include <build_version.h>
39#include <ki_exception.h> // THROW_IO_ERROR
40#include <kiplatform/app.h>
42#include <pgm_base.h>
43
44#include <kiplatform/policy.h>
45#include <policy_keys.h>
46
48{
51 curl_off_t m_Last_run_time;
52 curl_off_t m_Interval;
53
54 CURL_PROGRESS( KICAD_CURL_EASY* aCURL, TRANSFER_CALLBACK aCallback, curl_off_t aInterval ) :
55 m_Curl( aCURL ),
56 m_Callback( aCallback ),
57 m_Last_run_time( 0 ),
58 m_Interval( aInterval )
59 {
60 }
61};
62
63
64static size_t write_callback( void* aContents, size_t aSize, size_t aNmemb, void* aUserp )
65{
66 size_t realsize = aSize * aNmemb;
67
68 std::string* p = static_cast<std::string*>( aUserp );
69
70 p->append( static_cast<const char*>( aContents ), realsize );
71
72 return realsize;
73}
74
75
76static size_t stream_write_callback( void* aContents, size_t aSize, size_t aNmemb, void* aUserp )
77{
78 size_t realsize = aSize * aNmemb;
79
80 std::ostream* p = static_cast<std::ostream*>( aUserp );
81
82 p->write( static_cast<const char*>( aContents ), realsize );
83
84 return realsize;
85}
86
87#if LIBCURL_VERSION_NUM >= 0x072000 // 7.32.0
88
89static int xferinfo( void* aProgress, curl_off_t aDLtotal, curl_off_t aDLnow, curl_off_t aULtotal,
90 curl_off_t aULnow )
91{
92 CURL_PROGRESS* progress = static_cast<CURL_PROGRESS*>( aProgress );
93 curl_off_t curtime = 0;
94
95 curl_easy_getinfo( progress->m_Curl->GetCurl(), CURLINFO_TOTAL_TIME, &curtime );
96
97 if( curtime - progress->m_Last_run_time >= progress->m_Interval )
98 {
99 progress->m_Last_run_time = curtime;
100 return progress->m_Callback( aDLtotal, aDLnow, aULtotal, aULnow );
101 }
102
103 return CURLE_OK;
104}
105
106#else
107
108static int progressinfo( void* aProgress, double aDLtotal, double aDLnow, double aULtotal, double aULnow )
109{
110 return xferinfo( aProgress, static_cast<curl_off_t>( aDLtotal ), static_cast<curl_off_t>( aDLnow ),
111 static_cast<curl_off_t>( aULtotal ), static_cast<curl_off_t>( aULnow ) );
112}
113
114#endif
115
116
118 m_headers( nullptr )
119{
120 m_CURL = curl_easy_init();
121
122 if( !m_CURL )
123 THROW_IO_ERROR( "Unable to initialize CURL session" );
124
125 curl_easy_setopt( m_CURL, CURLOPT_WRITEFUNCTION, write_callback );
126 curl_easy_setopt( m_CURL, CURLOPT_WRITEDATA, static_cast<void*>( &m_buffer ) );
127
128 // Only allow HTTP and HTTPS protocols
129#if LIBCURL_VERSION_NUM >= 0x075500 // version 7.85.0
130 curl_easy_setopt(m_CURL, CURLOPT_PROTOCOLS_STR, "http,https");
131#else
132 curl_easy_setopt( m_CURL, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS );
133#endif
134
135#ifdef _WIN32
136 long sslOpts = CURLSSLOPT_NATIVE_CA;
137
138 POLICY_CURL_SSL_REVOKE policyState = KIPLATFORM::POLICY::GetPolicyEnum<POLICY_CURL_SSL_REVOKE>( POLICY_KEY_REQUESTS_CURL_REVOKE );
139 if( policyState == POLICY_CURL_SSL_REVOKE::BEST_EFFORT )
140 {
141 sslOpts |= CURLSSLOPT_REVOKE_BEST_EFFORT;
142 }
143 else if( policyState == POLICY_CURL_SSL_REVOKE::NONE )
144 {
145 sslOpts |= CURLSSLOPT_NO_REVOKE;
146 }
147
148 // We need this to use the Windows Certificate store
149 curl_easy_setopt( m_CURL, CURLOPT_SSL_OPTIONS, sslOpts );
150#endif
151
152 if( wxGetEnv( wxT( "KICAD_CURL_VERBOSE" ), nullptr ) )
153 {
154 // note: curl verbose will end up in stderr
155 curl_easy_setopt( m_CURL, CURLOPT_VERBOSE, 1L );
156 }
157
158 wxPlatformInfo platformInfo;
159 wxString application( Pgm().App().GetAppName() );
160 wxString version( GetBuildVersion() );
161 wxString platform = wxS( "(" ) + wxGetOsDescription() + wxS( ";" ) + GetPlatformGetBitnessName();
162
163#if defined( KICAD_BUILD_ARCH_X64 )
164 platform << wxS( ";64-bit" );
165#elif defined( KICAD_BUILD_ARCH_X86 )
166 platform << wxS( ";32-bit" );
167#elif defined( KICAD_BUILD_ARCH_ARM )
168 platform << wxS( ";ARM 32-bit" );
169#elif defined( KICAD_BUILD_ARCH_ARM64 )
170 platform << wxS( ";ARM 64-bit" );
171#endif
172
173 platform << wxS( ")" );
174
175 wxString user_agent = wxS( "KiCad/" ) + version + wxS( " " ) + platform + wxS( " " ) + application;
176
177 user_agent << wxS( "/" ) << GetBuildDate();
178 setOption<const char*>( CURLOPT_USERAGENT, user_agent.ToStdString().c_str() );
179 setOption( CURLOPT_ACCEPT_ENCODING, "gzip,deflate" );
180}
181
182
184{
185 if( m_headers )
186 curl_slist_free_all( m_headers );
187
188 curl_easy_cleanup( m_CURL );
189}
190
191
193{
194 if( m_headers )
195 curl_easy_setopt( m_CURL, CURLOPT_HTTPHEADER, m_headers );
196
197 // bonus: retain worst case memory allocation, should re-use occur
198 m_buffer.clear();
199
200 return curl_easy_perform( m_CURL );
201}
202
203
204void KICAD_CURL_EASY::SetHeader( const std::string& aName, const std::string& aValue )
205{
206 std::string header = aName + ':' + aValue;
207 m_headers = curl_slist_append( m_headers, header.c_str() );
208}
209
210
211template <typename T>
212int KICAD_CURL_EASY::setOption( int aOption, T aArg )
213{
214 return curl_easy_setopt( m_CURL, static_cast<CURLoption>( aOption ), aArg );
215}
216
217
218const std::string KICAD_CURL_EASY::GetErrorText( int aCode )
219{
220 return curl_easy_strerror( static_cast<CURLcode>( aCode ) );
221}
222
223
224bool KICAD_CURL_EASY::SetUserAgent( const std::string& aAgent )
225{
226 if( setOption<const char*>( CURLOPT_USERAGENT, aAgent.c_str() ) == CURLE_OK )
227 return true;
228
229 return false;
230}
231
232
234 const std::vector<std::pair<std::string, std::string>>& aFields )
235{
236 std::string postfields;
237
238 for( size_t i = 0; i < aFields.size(); i++ )
239 {
240 if( i > 0 )
241 postfields += "&";
242
243 postfields += Escape( aFields[i].first );
244 postfields += "=";
245 postfields += Escape( aFields[i].second );
246 }
247
248 if( setOption<const char*>( CURLOPT_COPYPOSTFIELDS, postfields.c_str() ) != CURLE_OK )
249 return false;
250
251 return true;
252}
253
254
255bool KICAD_CURL_EASY::SetURL( const std::string& aURL )
256{
257 if( setOption<const char*>( CURLOPT_URL, aURL.c_str() ) == CURLE_OK )
258 {
260
261 // Unfortunately on Windows land, proxies can be configured depending on destination url
262 // So we also check and set any proxy config here
264 {
265 curl_easy_setopt( m_CURL, CURLOPT_PROXY, static_cast<const char*>( cfg.host.c_str() ) );
266
267 if( !cfg.username.empty() )
268 {
269 curl_easy_setopt( m_CURL, CURLOPT_PROXYUSERNAME,
270 static_cast<const char*>( cfg.username.c_str() ) );
271 }
272
273 if( !cfg.password.empty() )
274 {
275 curl_easy_setopt( m_CURL, CURLOPT_PROXYPASSWORD,
276 static_cast<const char*>( cfg.password.c_str() ) );
277 }
278 }
279
280 return true;
281 }
282
283 return false;
284}
285
286
288{
289 if( setOption<long>( CURLOPT_FOLLOWLOCATION, ( aFollow ? 1 : 0 ) ) == CURLE_OK )
290 return true;
291
292 return false;
293}
294
295
296std::string KICAD_CURL_EASY::Escape( const std::string& aUrl )
297{
298 char* escaped = curl_easy_escape( m_CURL, aUrl.c_str(), aUrl.length() );
299
300 std::string ret( escaped );
301 curl_free( escaped );
302
303 return ret;
304}
305
306
307bool KICAD_CURL_EASY::SetTransferCallback( const TRANSFER_CALLBACK& aCallback, size_t aInterval )
308{
309 progress = std::make_unique<CURL_PROGRESS>( this, aCallback,
310 static_cast<curl_off_t>( aInterval ) );
311#if LIBCURL_VERSION_NUM >= 0x072000 // 7.32.0
312 setOption( CURLOPT_XFERINFOFUNCTION, xferinfo );
313 setOption( CURLOPT_XFERINFODATA, progress.get() );
314#else
315 setOption( CURLOPT_PROGRESSFUNCTION, progressinfo );
316 setOption( CURLOPT_PROGRESSDATA, progress.get() );
317#endif
318 setOption( CURLOPT_NOPROGRESS, 0L );
319 return true;
320}
321
322
323bool KICAD_CURL_EASY::SetOutputStream( const std::ostream* aOutput )
324{
325 curl_easy_setopt( m_CURL, CURLOPT_WRITEFUNCTION, stream_write_callback );
326 curl_easy_setopt( m_CURL, CURLOPT_WRITEDATA, reinterpret_cast<const void*>( aOutput ) );
327 return true;
328}
329
330
331int KICAD_CURL_EASY::GetTransferTotal( uint64_t& aDownloadedBytes ) const
332{
333#if LIBCURL_VERSION_NUM >= 0x073700 // 7.55.0
334 curl_off_t dl;
335 int result = curl_easy_getinfo( m_CURL, CURLINFO_SIZE_DOWNLOAD_T, &dl );
336 aDownloadedBytes = static_cast<uint64_t>( dl );
337#else
338 double dl;
339 int result = curl_easy_getinfo( m_CURL, CURLINFO_SIZE_DOWNLOAD, &dl );
340 aDownloadedBytes = static_cast<uint64_t>( dl );
341#endif
342 return result;
343}
wxString GetBuildVersion()
Get the full KiCad version string.
wxString GetPlatformGetBitnessName()
wxString GetBuildDate()
Get the build date as a string.
std::string m_buffer
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.
curl_slist * m_headers
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.
int GetTransferTotal(uint64_t &aDownloadedBytes) const
int setOption(int aOption, T aArg)
Set a curl option, only supports single parameter curl options.
bool SetOutputStream(const std::ostream *aOutput)
const std::string GetErrorText(int aCode)
Fetch CURL's "friendly" error string for a given error code.
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
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/.
bool GetSystemProxyConfig(const wxString &aURL, PROXY_CONFIG &aCfg)
Retrieves platform level proxying requirements to reach the given url.
see class PGM_BASE
#define POLICY_KEY_REQUESTS_CURL_REVOKE
Definition: policy_keys.h:32
POLICY_CURL_SSL_REVOKE
Definition: policy_keys.h:35
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:115
curl_off_t m_Interval
CURL_PROGRESS(KICAD_CURL_EASY *aCURL, TRANSFER_CALLBACK aCallback, curl_off_t aInterval)
TRANSFER_CALLBACK m_Callback
KICAD_CURL_EASY * m_Curl
curl_off_t m_Last_run_time