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
43#include <kiplatform/policy.h>
44#include <policy_keys.h>
45
47{
50 curl_off_t m_Last_run_time;
51 curl_off_t m_Interval;
52
53 CURL_PROGRESS( KICAD_CURL_EASY* aCURL, TRANSFER_CALLBACK aCallback, curl_off_t aInterval ) :
54 m_Curl( aCURL ),
55 m_Callback( aCallback ),
56 m_Last_run_time( 0 ),
57 m_Interval( aInterval )
58 {
59 }
60};
61
62
63static size_t write_callback( void* aContents, size_t aSize, size_t aNmemb, void* aUserp )
64{
65 size_t realsize = aSize * aNmemb;
66
67 std::string* p = static_cast<std::string*>( aUserp );
68
69 p->append( static_cast<const char*>( aContents ), realsize );
70
71 return realsize;
72}
73
74
75static size_t stream_write_callback( void* aContents, size_t aSize, size_t aNmemb, void* aUserp )
76{
77 size_t realsize = aSize * aNmemb;
78
79 std::ostream* p = static_cast<std::ostream*>( aUserp );
80
81 p->write( static_cast<const char*>( aContents ), realsize );
82
83 return realsize;
84}
85
86#if LIBCURL_VERSION_NUM >= 0x072000 // 7.32.0
87
88static int xferinfo( void* aProgress, curl_off_t aDLtotal, curl_off_t aDLnow, curl_off_t aULtotal,
89 curl_off_t aULnow )
90{
91 CURL_PROGRESS* progress = static_cast<CURL_PROGRESS*>( aProgress );
92 curl_off_t curtime = 0;
93
94 curl_easy_getinfo( progress->m_Curl->GetCurl(), CURLINFO_TOTAL_TIME, &curtime );
95
96 if( curtime - progress->m_Last_run_time >= progress->m_Interval )
97 {
98 progress->m_Last_run_time = curtime;
99 return progress->m_Callback( aDLtotal, aDLnow, aULtotal, aULnow );
100 }
101
102 return CURLE_OK;
103}
104
105#else
106
107static int progressinfo( void* aProgress, double aDLtotal, double aDLnow, double aULtotal, double aULnow )
108{
109 return xferinfo( aProgress, static_cast<curl_off_t>( aDLtotal ), static_cast<curl_off_t>( aDLnow ),
110 static_cast<curl_off_t>( aULtotal ), static_cast<curl_off_t>( aULnow ) );
111}
112
113#endif
114
115
117 m_headers( nullptr )
118{
119 m_CURL = curl_easy_init();
120
121 if( !m_CURL )
122 THROW_IO_ERROR( "Unable to initialize CURL session" );
123
124 curl_easy_setopt( m_CURL, CURLOPT_WRITEFUNCTION, write_callback );
125 curl_easy_setopt( m_CURL, CURLOPT_WRITEDATA, static_cast<void*>( &m_buffer ) );
126
127 // Only allow HTTP and HTTPS protocols
128#if LIBCURL_VERSION_NUM >= 0x075500 // version 7.85.0
129 curl_easy_setopt(m_CURL, CURLOPT_PROTOCOLS_STR, "http,https");
130#else
131 curl_easy_setopt( m_CURL, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS );
132#endif
133
134#ifdef _WIN32
135 long sslOpts = CURLSSLOPT_NATIVE_CA;
136
137 POLICY_CURL_SSL_REVOKE policyState = KIPLATFORM::POLICY::GetPolicyEnum<POLICY_CURL_SSL_REVOKE>( POLICY_KEY_REQUESTS_CURL_REVOKE );
138 if( policyState == POLICY_CURL_SSL_REVOKE::BEST_EFFORT )
139 {
140 sslOpts |= CURLSSLOPT_REVOKE_BEST_EFFORT;
141 }
142 else if( policyState == POLICY_CURL_SSL_REVOKE::NONE )
143 {
144 sslOpts |= CURLSSLOPT_NO_REVOKE;
145 }
146
147 // We need this to use the Windows Certificate store
148 curl_easy_setopt( m_CURL, CURLOPT_SSL_OPTIONS, sslOpts );
149#endif
150
151 if( wxGetEnv( wxT( "KICAD_CURL_VERBOSE" ), nullptr ) )
152 {
153 // note: curl verbose will end up in stderr
154 curl_easy_setopt( m_CURL, CURLOPT_VERBOSE, 1L );
155 }
156
157 wxPlatformInfo platformInfo;
158 wxString application( wxS( "KiCad" ) );
159 wxString version( GetBuildVersion() );
160 wxString platform = wxS( "(" ) + wxGetOsDescription() + wxS( ";" ) + GetPlatformGetBitnessName();
161
162#if defined( KICAD_BUILD_ARCH_X64 )
163 platform << wxS( ";64-bit" );
164#elif defined( KICAD_BUILD_ARCH_X86 )
165 platform << wxS( ";32-bit" );
166#elif defined( KICAD_BUILD_ARCH_ARM )
167 platform << wxS( ";ARM 32-bit" );
168#elif defined( KICAD_BUILD_ARCH_ARM64 )
169 platform << wxS( ";ARM 64-bit" );
170#endif
171
172 platform << wxS( ")" );
173
174 wxString user_agent = wxS( "KiCad/" ) + version + wxS( " " ) + platform + wxS( " " ) + application;
175
176 user_agent << wxS( "/" ) << GetBuildDate();
177 setOption<const char*>( CURLOPT_USERAGENT, user_agent.ToStdString().c_str() );
178 setOption( CURLOPT_ACCEPT_ENCODING, "gzip,deflate" );
179}
180
181
183{
184 if( m_headers )
185 curl_slist_free_all( m_headers );
186
187 curl_easy_cleanup( m_CURL );
188}
189
190
192{
193 if( m_headers )
194 curl_easy_setopt( m_CURL, CURLOPT_HTTPHEADER, m_headers );
195
196 // bonus: retain worst case memory allocation, should re-use occur
197 m_buffer.clear();
198
199 return curl_easy_perform( m_CURL );
200}
201
202
203void KICAD_CURL_EASY::SetHeader( const std::string& aName, const std::string& aValue )
204{
205 std::string header = aName + ':' + aValue;
206 m_headers = curl_slist_append( m_headers, header.c_str() );
207}
208
209
210template <typename T>
211int KICAD_CURL_EASY::setOption( int aOption, T aArg )
212{
213 return curl_easy_setopt( m_CURL, static_cast<CURLoption>( aOption ), aArg );
214}
215
216
217const std::string KICAD_CURL_EASY::GetErrorText( int aCode )
218{
219 return curl_easy_strerror( static_cast<CURLcode>( aCode ) );
220}
221
222
223bool KICAD_CURL_EASY::SetUserAgent( const std::string& aAgent )
224{
225 if( setOption<const char*>( CURLOPT_USERAGENT, aAgent.c_str() ) == CURLE_OK )
226 return true;
227
228 return false;
229}
230
231
233 const std::vector<std::pair<std::string, std::string>>& aFields )
234{
235 std::string postfields;
236
237 for( size_t i = 0; i < aFields.size(); i++ )
238 {
239 if( i > 0 )
240 postfields += "&";
241
242 postfields += Escape( aFields[i].first );
243 postfields += "=";
244 postfields += Escape( aFields[i].second );
245 }
246
247 if( setOption<const char*>( CURLOPT_COPYPOSTFIELDS, postfields.c_str() ) != CURLE_OK )
248 return false;
249
250 return true;
251}
252
253
254bool KICAD_CURL_EASY::SetPostFields( const std::string& aField )
255{
256 if( setOption<const char*>( CURLOPT_COPYPOSTFIELDS, aField.c_str() ) != CURLE_OK )
257 return false;
258
259 return true;
260}
261
262
263bool KICAD_CURL_EASY::SetURL( const std::string& aURL )
264{
265 if( setOption<const char*>( CURLOPT_URL, aURL.c_str() ) == CURLE_OK )
266 {
268
269 // Unfortunately on Windows land, proxies can be configured depending on destination url
270 // So we also check and set any proxy config here
272 {
273 curl_easy_setopt( m_CURL, CURLOPT_PROXY, static_cast<const char*>( cfg.host.c_str() ) );
274
275 if( !cfg.username.empty() )
276 {
277 curl_easy_setopt( m_CURL, CURLOPT_PROXYUSERNAME,
278 static_cast<const char*>( cfg.username.c_str() ) );
279 }
280
281 if( !cfg.password.empty() )
282 {
283 curl_easy_setopt( m_CURL, CURLOPT_PROXYPASSWORD,
284 static_cast<const char*>( cfg.password.c_str() ) );
285 }
286 }
287
288 return true;
289 }
290
291 return false;
292}
293
294
296{
297 if( setOption<long>( CURLOPT_FOLLOWLOCATION, ( aFollow ? 1 : 0 ) ) == CURLE_OK )
298 return true;
299
300 return false;
301}
302
303
304std::string KICAD_CURL_EASY::Escape( const std::string& aUrl )
305{
306 char* escaped = curl_easy_escape( m_CURL, aUrl.c_str(), aUrl.length() );
307
308 std::string ret( escaped );
309 curl_free( escaped );
310
311 return ret;
312}
313
314
315bool KICAD_CURL_EASY::SetTransferCallback( const TRANSFER_CALLBACK& aCallback, size_t aInterval )
316{
317 progress = std::make_unique<CURL_PROGRESS>( this, aCallback,
318 static_cast<curl_off_t>( aInterval ) );
319#if LIBCURL_VERSION_NUM >= 0x072000 // 7.32.0
320 setOption( CURLOPT_XFERINFOFUNCTION, xferinfo );
321 setOption( CURLOPT_XFERINFODATA, progress.get() );
322#else
323 setOption( CURLOPT_PROGRESSFUNCTION, progressinfo );
324 setOption( CURLOPT_PROGRESSDATA, progress.get() );
325#endif
326 setOption( CURLOPT_NOPROGRESS, 0L );
327 return true;
328}
329
330
331bool KICAD_CURL_EASY::SetOutputStream( const std::ostream* aOutput )
332{
333 curl_easy_setopt( m_CURL, CURLOPT_WRITEFUNCTION, stream_write_callback );
334 curl_easy_setopt( m_CURL, CURLOPT_WRITEDATA, reinterpret_cast<const void*>( aOutput ) );
335 return true;
336}
337
338
339int KICAD_CURL_EASY::GetTransferTotal( uint64_t& aDownloadedBytes ) const
340{
341#if LIBCURL_VERSION_NUM >= 0x073700 // 7.55.0
342 curl_off_t dl;
343 int result = curl_easy_getinfo( m_CURL, CURLINFO_SIZE_DOWNLOAD_T, &dl );
344 aDownloadedBytes = static_cast<uint64_t>( dl );
345#else
346 double dl;
347 int result = curl_easy_getinfo( m_CURL, CURLINFO_SIZE_DOWNLOAD, &dl );
348 aDownloadedBytes = static_cast<uint64_t>( dl );
349#endif
350 return result;
351}
352
353
355{
356 long http_code = 0;
357 curl_easy_getinfo( m_CURL, CURLINFO_RESPONSE_CODE, &http_code );
358
359 return static_cast<int>( http_code );
360}
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:39
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.
#define POLICY_KEY_REQUESTS_CURL_REVOKE
Definition: policy_keys.h:32
POLICY_CURL_SSL_REVOKE
Definition: policy_keys.h:35
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