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 <[email protected]>
5  * Copyright (C) 2015, 2021 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 #ifndef KICAD_CURL_EASY_H_
25 #define KICAD_CURL_EASY_H_
26 
27 /*
28  * curl.h, and therefore kicad_curl.h must be included before wxWidgets headers because on Windows,
29  * wxWidgets ends up including windows.h before winsocks2.h
30  * curl and curl.h causes build warnings if included before any wxxxx.h
31  *
32  * So kicad_curl_easy.h does not include curl.h to avoid constraints,
33  * and including kicad_curl.h could be needed in a few sources
34  */
35 
36 #include <functional>
37 #include <memory>
38 #include <ostream>
39 #include <string>
40 
41 typedef void CURL;
42 struct curl_slist;
43 
62 typedef std::function<int( size_t, size_t, size_t, size_t )> TRANSFER_CALLBACK;
63 struct CURL_PROGRESS;
64 
65 
67 {
68 public:
71 
75  int Perform();
76 
83  void SetHeader( const std::string& aName, const std::string& aValue );
84 
91  bool SetUserAgent( const std::string& aAgent );
92 
99  bool SetURL( const std::string& aURL );
100 
108  bool SetFollowRedirects( bool aFollow );
109 
116  const std::string GetErrorText( int aCode );
117 
118  int GetTransferTotal( uint64_t& aDownloadedBytes ) const;
119 
123  const std::string& GetBuffer() { return m_buffer; }
124 
126  std::string Escape( const std::string& aUrl );
127 
128  bool SetTransferCallback( const TRANSFER_CALLBACK& aCallback, size_t aInterval );
129 
130  bool SetOutputStream( const std::ostream* aOutput );
131 
132  CURL* GetCurl() { return m_CURL; }
133 
134 
135 private:
143  template <typename T>
144  int setOption( int aOption, T aArg );
145 
147  curl_slist* m_headers;
148  std::string m_buffer;
149  std::unique_ptr<CURL_PROGRESS> progress;
150 };
151 
152 
153 #endif // KICAD_CURL_EASY_H_
bool SetUserAgent(const std::string &aAgent)
Set the request user agent.
bool SetFollowRedirects(bool aFollow)
Enable the following of HTTP(s) and other redirects, by default curl does not follow redirects.
int setOption(int aOption, T aArg)
Set a curl option, only supports single parameter curl options.
std::unique_ptr< CURL_PROGRESS > progress
int GetTransferTotal(uint64_t &aDownloadedBytes) const
void CURL
const std::string & GetBuffer()
Return a reference to the received data buffer.
curl_slist * m_headers
std::string Escape(const std::string &aUrl)
Escapes a string for use as a URL.
std::function< int(size_t, size_t, size_t, size_t)> TRANSFER_CALLBACK
Wrapper interface around the curl_easy API/.
int Perform()
Equivalent to curl_easy_perform.
const std::string GetErrorText(int aCode)
Fetch CURL's "friendly" error string for a given error code.
bool SetTransferCallback(const TRANSFER_CALLBACK &aCallback, size_t aInterval)
bool SetURL(const std::string &aURL)
Set the request URL.
std::string m_buffer
bool SetOutputStream(const std::ostream *aOutput)
void SetHeader(const std::string &aName, const std::string &aValue)
Set an arbitrary header for the HTTP(s) request.