KiCad PCB EDA Suite
KICAD_CURL_EASY Class Reference

#include <kicad_curl_easy.h>

Public Member Functions

 KICAD_CURL_EASY ()
 
 ~KICAD_CURL_EASY ()
 
int Perform ()
 Equivalent to curl_easy_perform. More...
 
void SetHeader (const std::string &aName, const std::string &aValue)
 Set an arbitrary header for the HTTP(s) request. More...
 
bool SetUserAgent (const std::string &aAgent)
 Set the request user agent. More...
 
bool SetURL (const std::string &aURL)
 Set the request URL. More...
 
bool SetFollowRedirects (bool aFollow)
 Enable the following of HTTP(s) and other redirects, by default curl does not follow redirects. More...
 
const std::string GetErrorText (int aCode)
 Fetch CURL's "friendly" error string for a given error code. More...
 
int GetTransferTotal (uint64_t &aDownloadedBytes) const
 
const std::string & GetBuffer ()
 Return a reference to the received data buffer. More...
 
std::string Escape (const std::string &aUrl)
 Escapes a string for use as a URL. More...
 
bool SetTransferCallback (const TRANSFER_CALLBACK &aCallback, size_t aInterval)
 
bool SetOutputStream (const std::ostream *aOutput)
 
CURLGetCurl ()
 

Private Member Functions

template<typename T >
int setOption (int aOption, T aArg)
 Set a curl option, only supports single parameter curl options. More...
 

Private Attributes

CURLm_CURL
 
curl_slist * m_headers
 
std::string m_buffer
 
std::unique_ptr< CURL_PROGRESSprogress
 

Detailed Description

Definition at line 66 of file kicad_curl_easy.h.

Constructor & Destructor Documentation

◆ KICAD_CURL_EASY()

KICAD_CURL_EASY::KICAD_CURL_EASY ( )

Definition at line 99 of file kicad_curl_easy.cpp.

99  : m_headers( nullptr )
100 {
101  // Call KICAD_CURL::Init() from in here every time, but only the first time
102  // will incur any overhead. This strategy ensures that libcurl is never loaded
103  // unless it is needed.
104 
106 
107  m_CURL = curl_easy_init();
108 
109  if( !m_CURL )
110  {
111  THROW_IO_ERROR( "Unable to initialize CURL session" );
112  }
113 
114  curl_easy_setopt( m_CURL, CURLOPT_WRITEFUNCTION, write_callback );
115  curl_easy_setopt( m_CURL, CURLOPT_WRITEDATA, (void*) &m_buffer );
116 
117  // Only allow HTTP and HTTPS protocols
118  curl_easy_setopt( m_CURL, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS );
119 
120  wxPlatformInfo platformInfo;
121  wxString application( Pgm().App().GetAppName() );
122  wxString version( GetBuildVersion() );
123  wxString platform = "(" + wxGetOsDescription() + ";" + platformInfo.GetArchName();
124 
125 #if defined( KICAD_BUILD_ARCH_X64 )
126  platform << ";64-bit";
127 #elif defined( KICAD_BUILD_ARCH_X86 )
128  platform << ";32-bit";
129 #elif defined( KICAD_BUILD_ARCH_ARM )
130  platform << ";ARM 32-bit";
131 #elif defined( KICAD_BUILD_ARCH_ARM64 )
132  platform << ";ARM 64-bit";
133 #endif
134 
135  platform << ")";
136 
137  wxString user_agent = "KiCad/" + version + " " + platform + " " + application;
138 
139  user_agent << "/" << GetBuildDate();
140  setOption<const char*>( CURLOPT_USERAGENT, user_agent.ToStdString().c_str() );
141  setOption( CURLOPT_ACCEPT_ENCODING, "gzip,deflate" );
142 }
static size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp)
static void Init()
Call curl_global_init for the application.
Definition: kicad_curl.cpp:50
int setOption(int aOption, T aArg)
Set a curl option, only supports single parameter curl options.
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
curl_slist * m_headers
wxString GetBuildVersion()
Get the full KiCad version string.
std::string m_buffer
wxString GetBuildDate()
Get the build date as a string.
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38

References GetBuildDate(), GetBuildVersion(), KICAD_CURL::Init(), m_buffer, m_CURL, Pgm(), setOption(), THROW_IO_ERROR, and write_callback().

◆ ~KICAD_CURL_EASY()

KICAD_CURL_EASY::~KICAD_CURL_EASY ( )

Definition at line 145 of file kicad_curl_easy.cpp.

146 {
147  if( m_headers )
148  curl_slist_free_all( m_headers );
149 
150  curl_easy_cleanup( m_CURL );
151 }
curl_slist * m_headers

References m_CURL, and m_headers.

Member Function Documentation

◆ Escape()

std::string KICAD_CURL_EASY::Escape ( const std::string &  aUrl)

Escapes a string for use as a URL.

Definition at line 223 of file kicad_curl_easy.cpp.

224 {
225  char* escaped = curl_easy_escape( m_CURL, aUrl.c_str(), aUrl.length() );
226 
227  std::string ret( escaped );
228  curl_free( escaped );
229 
230  return ret;
231 }

References m_CURL.

Referenced by COMMON_CONTROL::ReportBug().

◆ GetBuffer()

const std::string& KICAD_CURL_EASY::GetBuffer ( )
inline

Return a reference to the received data buffer.

Definition at line 123 of file kicad_curl_easy.h.

123 { return m_buffer; }
std::string m_buffer

References m_buffer.

◆ GetCurl()

CURL* KICAD_CURL_EASY::GetCurl ( )
inline

Definition at line 132 of file kicad_curl_easy.h.

132 { return m_CURL; }

References m_CURL.

Referenced by xferinfo().

◆ GetErrorText()

const std::string KICAD_CURL_EASY::GetErrorText ( int  aCode)

Fetch CURL's "friendly" error string for a given error code.

Parameters
aCodeis CURL error code.
Returns
The corresponding error string for the given code.

Definition at line 184 of file kicad_curl_easy.cpp.

185 {
186  return curl_easy_strerror( (CURLcode) aCode );
187 }

Referenced by PLUGIN_CONTENT_MANAGER::DownloadToStream().

◆ GetTransferTotal()

int KICAD_CURL_EASY::GetTransferTotal ( uint64_t &  aDownloadedBytes) const

Definition at line 252 of file kicad_curl_easy.cpp.

253 {
254  curl_off_t dl;
255  int result = curl_easy_getinfo( m_CURL, CURLINFO_SIZE_DOWNLOAD_T, &dl );
256  aDownloadedBytes = (uint64_t) dl;
257  return result;
258 }

References m_CURL.

◆ Perform()

int KICAD_CURL_EASY::Perform ( )

Equivalent to curl_easy_perform.

Executes the request that was previously setup.

Definition at line 154 of file kicad_curl_easy.cpp.

155 {
156  if( m_headers )
157  {
158  curl_easy_setopt( m_CURL, CURLOPT_HTTPHEADER, m_headers );
159  }
160 
161  // bonus: retain worst case memory allocation, should re-use occur
162  m_buffer.clear();
163 
164  CURLcode res = curl_easy_perform( m_CURL );
165 
166  return res;
167 }
curl_slist * m_headers
std::string m_buffer

References m_buffer, m_CURL, and m_headers.

Referenced by PLUGIN_CONTENT_MANAGER::DownloadToStream().

◆ SetFollowRedirects()

bool KICAD_CURL_EASY::SetFollowRedirects ( bool  aFollow)

Enable the following of HTTP(s) and other redirects, by default curl does not follow redirects.

Parameters
aFollowis a boolean where true will enable following redirects.
Returns
True if successful, false if not.

Definition at line 212 of file kicad_curl_easy.cpp.

213 {
214  if( setOption<long>( CURLOPT_FOLLOWLOCATION, ( aFollow ? 1 : 0 ) ) == CURLE_OK )
215  {
216  return true;
217  }
218 
219  return false;
220 }

Referenced by PLUGIN_CONTENT_MANAGER::DownloadToStream().

◆ SetHeader()

void KICAD_CURL_EASY::SetHeader ( const std::string &  aName,
const std::string &  aValue 
)

Set an arbitrary header for the HTTP(s) request.

Parameters
aNameis the left hand side of the header, i.e. Accept without the colon.
aValueis the right hand side of the header, i.e. application/json.

Definition at line 170 of file kicad_curl_easy.cpp.

171 {
172  std::string header = aName + ':' + aValue;
173  m_headers = curl_slist_append( m_headers, header.c_str() );
174 }
curl_slist * m_headers

References m_headers.

◆ setOption()

template<typename T >
int KICAD_CURL_EASY::setOption ( int  aOption,
aArg 
)
private

Set a curl option, only supports single parameter curl options.

Parameters
aOptionis CURL option, see CURL manual for options.
aArgis the argument being passed to CURL, ensure it is the right type per manual.
Returns
A CURL error code, will return CURLE_OK unless a problem was encountered.

Definition at line 178 of file kicad_curl_easy.cpp.

179 {
180  return curl_easy_setopt( m_CURL, (CURLoption) aOption, aArg );
181 }

References m_CURL.

Referenced by KICAD_CURL_EASY(), and SetTransferCallback().

◆ SetOutputStream()

bool KICAD_CURL_EASY::SetOutputStream ( const std::ostream *  aOutput)

Definition at line 244 of file kicad_curl_easy.cpp.

245 {
246  curl_easy_setopt( m_CURL, CURLOPT_WRITEFUNCTION, stream_write_callback );
247  curl_easy_setopt( m_CURL, CURLOPT_WRITEDATA, (void*) aOutput );
248  return true;
249 }
static size_t stream_write_callback(void *contents, size_t size, size_t nmemb, void *userp)

References m_CURL, and stream_write_callback().

Referenced by PCM_TASK_MANAGER::downloadFile(), and PLUGIN_CONTENT_MANAGER::DownloadToStream().

◆ SetTransferCallback()

bool KICAD_CURL_EASY::SetTransferCallback ( const TRANSFER_CALLBACK aCallback,
size_t  aInterval 
)

Definition at line 234 of file kicad_curl_easy.cpp.

235 {
236  progress = std::make_unique<CURL_PROGRESS>( this, aCallback, (curl_off_t) aInterval );
237  setOption( CURLOPT_XFERINFOFUNCTION, xferinfo );
238  setOption( CURLOPT_XFERINFODATA, progress.get() );
239  setOption( CURLOPT_NOPROGRESS, 0L );
240  return true;
241 }
int setOption(int aOption, T aArg)
Set a curl option, only supports single parameter curl options.
std::unique_ptr< CURL_PROGRESS > progress
static int xferinfo(void *p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)

References progress, setOption(), and xferinfo().

Referenced by PLUGIN_CONTENT_MANAGER::DownloadToStream().

◆ SetURL()

bool KICAD_CURL_EASY::SetURL ( const std::string &  aURL)

Set the request URL.

Parameters
aURLis the URL.
Returns
True if successful, false if not.

Definition at line 201 of file kicad_curl_easy.cpp.

202 {
203  if( setOption<const char*>( CURLOPT_URL, aURL.c_str() ) == CURLE_OK )
204  {
205  return true;
206  }
207 
208  return false;
209 }

Referenced by PLUGIN_CONTENT_MANAGER::DownloadToStream().

◆ SetUserAgent()

bool KICAD_CURL_EASY::SetUserAgent ( const std::string &  aAgent)

Set the request user agent.

Parameters
aAgentis the string to set for the user agent.
Returns
True if successful, false if not.

Definition at line 190 of file kicad_curl_easy.cpp.

191 {
192  if( setOption<const char*>( CURLOPT_USERAGENT, aAgent.c_str() ) == CURLE_OK )
193  {
194  return true;
195  }
196 
197  return false;
198 }

Member Data Documentation

◆ m_buffer

std::string KICAD_CURL_EASY::m_buffer
private

Definition at line 148 of file kicad_curl_easy.h.

Referenced by GetBuffer(), KICAD_CURL_EASY(), and Perform().

◆ m_CURL

CURL* KICAD_CURL_EASY::m_CURL
private

◆ m_headers

curl_slist* KICAD_CURL_EASY::m_headers
private

Definition at line 147 of file kicad_curl_easy.h.

Referenced by Perform(), SetHeader(), and ~KICAD_CURL_EASY().

◆ progress

std::unique_ptr<CURL_PROGRESS> KICAD_CURL_EASY::progress
private

Definition at line 149 of file kicad_curl_easy.h.

Referenced by SetTransferCallback().


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