KiCad PCB EDA Suite
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 <mark.roszko@gmail.com>
5  * Copyright (C) 2015 KiCad Developers, see CHANGELOG.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 
26 
27 #include <cstddef>
28 #include <exception>
29 #include <stdarg.h>
30 #include <sstream>
31 #include <richio.h>
32 
33 
34 static size_t write_callback( void* contents, size_t size, size_t nmemb, void* userp )
35 {
36  size_t realsize = size * nmemb;
37 
38  std::string* p = (std::string*) userp;
39 
40  p->append( (const char*) contents, realsize );
41 
42  return realsize;
43 }
44 
45 
47  m_headers( NULL )
48 {
49  // Call KICAD_CURL::Init() from in here everytime, but only the first time
50  // will incur any overhead. This strategy ensures that libcurl is never loaded
51  // unless it is needed.
52 
54 
55  m_CURL = curl_easy_init();
56 
57  if( !m_CURL )
58  {
59  THROW_IO_ERROR( "Unable to initialize CURL session" );
60  }
61 
62  curl_easy_setopt( m_CURL, CURLOPT_WRITEFUNCTION, write_callback );
63  curl_easy_setopt( m_CURL, CURLOPT_WRITEDATA, (void*) &m_buffer );
64 }
65 
66 
68 {
69  if( m_headers )
70  curl_slist_free_all( m_headers );
71 
72  curl_easy_cleanup( m_CURL );
73 }
74 
75 
77 {
78  if( m_headers )
79  {
80  curl_easy_setopt( m_CURL, CURLOPT_HTTPHEADER, m_headers );
81  }
82 
83  // bonus: retain worst case memory allocation, should re-use occur
84  m_buffer.clear();
85 
86  CURLcode res = curl_easy_perform( m_CURL );
87 
88  if( res != CURLE_OK )
89  {
90  std::string msg = StrPrintf( "curl_easy_perform()=%d: %s",
91  res, GetErrorText( res ).c_str() );
92  THROW_IO_ERROR( msg );
93  }
94 }
static size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp)
void Perform()
Function perform equivalent to curl_easy_perform.
static void Init()
Function Init calls curl_global_init for the application.
Definition: kicad_curl.cpp:135
int StrPrintf(std::string *result, const char *format,...)
Function StrPrintf is like sprintf() but the output is appended to a std::string instead of to a char...
Definition: richio.cpp:75
curl_slist * m_headers
std::string m_buffer
const std::string GetErrorText(CURLcode aCode)
Function GetErrorText fetches CURL's "friendly" error string for a given error code.
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38