KiCad PCB EDA Suite
kicad_curl_easy.h
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 #ifndef KICAD_CURL_EASY_H_
25 #define KICAD_CURL_EASY_H_
26 
27 /*
28  * KICAD_CURL_EASY.h must included before wxWidgets because on Windows,
29  * wxWidgets ends up including windows.h before winsocks2.h inside curl
30  * this causes build warnings
31  * Because we are before wx, we must explicitly define we are building with unicode
32  * wxWidgets defaults to supporting unicode now, so this should be safe.
33  */
34 #if defined(WIN32)
35  #ifndef UNICODE
36  # define UNICODE
37  #endif
38 
39  #ifndef _UNICODE
40  # define _UNICODE
41  #endif
42 #endif
43 
44 
45 #include <string>
46 #include <curl/curl.h>
47 #include <kicad_curl/kicad_curl.h>
48 
49 
68 {
69 public:
72 
80  void Perform();
81 
89  void SetHeader( const std::string& aName, const std::string& aValue )
90  {
91  std::string header = aName + ':' + aValue;
92  m_headers = curl_slist_append( m_headers, header.c_str() );
93  }
94 
102  bool SetUserAgent( const std::string& aAgent )
103  {
104  if( SetOption<const char*>( CURLOPT_USERAGENT, aAgent.c_str() ) == CURLE_OK )
105  {
106  return true;
107  }
108  return false;
109  }
110 
118  bool SetURL( const std::string& aURL )
119  {
120  if( SetOption<const char *>( CURLOPT_URL, aURL.c_str() ) == CURLE_OK )
121  {
122  return true;
123  }
124  return false;
125  }
126 
135  bool SetFollowRedirects( bool aFollow )
136  {
137  if( SetOption<long>( CURLOPT_FOLLOWLOCATION , (aFollow ? 1 : 0) ) == CURLE_OK )
138  {
139  return true;
140  }
141  return false;
142  }
143 
151  const std::string GetErrorText( CURLcode aCode )
152  {
153  return curl_easy_strerror( aCode );
154  }
155 
164  template <typename T> CURLcode SetOption( CURLoption aOption, T aArg )
165  {
166  return curl_easy_setopt( m_CURL, aOption, aArg );
167  }
168 
173  const std::string& GetBuffer()
174  {
175  return m_buffer;
176  }
177 
178 private:
179  CURL* m_CURL;
180  curl_slist* m_headers;
181  std::string m_buffer;
182 };
183 
184 #endif // KICAD_CURL_EASY_H_
bool SetUserAgent(const std::string &aAgent)
Function SetUserAgent sets the request user agent.
void Perform()
Function perform equivalent to curl_easy_perform.
bool SetFollowRedirects(bool aFollow)
Function SetFollowRedirects enables the following of HTTP(s) and other redirects, by default curl doe...
T
enum T contains all this lexer's tokens.
Class KICAD_CURL_EASY wrapper interface around the curl_easy API.
CURLcode SetOption(CURLoption aOption, T aArg)
Function SetOption sets a curl option, only supports single parameter curl options.
const std::string & GetBuffer()
Function GetBuffer returns a const reference to the recevied data buffer.
curl_slist * m_headers
bool SetURL(const std::string &aURL)
Function SetURL sets the request URL.
std::string m_buffer
const std::string GetErrorText(CURLcode aCode)
Function GetErrorText fetches CURL's "friendly" error string for a given error code.
void SetHeader(const std::string &aName, const std::string &aValue)
Function SetHeader sets an arbitrary header for the HTTP(s) request.