SuperTuxKart
Loading...
Searching...
No Matches
http_request.hpp
1// SuperTuxKart - a fun racing game with go-kart
2// Copyright (C) 2011-2015 Joerg Henrichs
3// 2013 Glenn De Jonghe
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License
7// as published by the Free Software Foundation; either version 3
8// of the License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19#ifndef HEADER_HTTP_REQUEST_HPP
20#define HEADER_HTTP_REQUEST_HPP
21
22#include "online/request.hpp"
23#include "utils/cpp2011.hpp"
24#include "utils/string_utils.hpp"
25
26#ifdef WIN32
27# include <winsock2.h>
28#endif
29#include <atomic>
30#include <curl/curl.h>
31#include <assert.h>
32#include <string>
33
34#if defined(CURLOPT_XFERINFODATA)
35#define PROGRESSDATA CURLOPT_XFERINFODATA
36#define PROGRESSFUNCTION CURLOPT_XFERINFOFUNCTION
37typedef curl_off_t progress_t;
38#else
39#define PROGRESSDATA CURLOPT_PROGRESSDATA
40#define PROGRESSFUNCTION CURLOPT_PROGRESSFUNCTION
41typedef double progress_t;
42#endif
43
44namespace Online
45{
46 class API
47 {
48 public:
49 static const std::string USER_PATH;
50 static const std::string SERVER_PATH;
51 };
52
55 class HTTPRequest : public Request
56 {
57 private:
62 std::atomic<float> m_progress;
63
64 std::atomic<double> m_total_size;
65
67 std::string m_url;
68
70 std::string m_parameters;
71
72
74 CURL *m_curl_session = NULL;
75
77 CURLcode m_curl_code;
78
80 std::string m_string_buffer;
81
82 struct curl_slist* m_http_header = NULL;
83 protected:
86 std::string m_filename;
87
88 bool m_disable_sending_log;
89 /* If true, it will not call curl_easy_setopt CURLOPT_POSTFIELDS so
90 * it's just a GET request. */
91 bool m_download_assets_request = false;
92
93 virtual void prepareOperation() OVERRIDE;
94 virtual void operation() OVERRIDE;
95 virtual void afterOperation() OVERRIDE;
96
97 static int progressDownload(void *clientp, progress_t dltotal,
98 progress_t dlnow, progress_t ultotal,
99 progress_t ulnow);
100
101 static size_t writeCallback(void *contents, size_t size,
102 size_t nmemb, void *userp);
103 void init();
104
105 public :
106 HTTPRequest(int priority = 1);
107 HTTPRequest(const std::string &filename, int priority = 1);
108 HTTPRequest(const char * const filename, int priority = 1);
109 virtual ~HTTPRequest()
110 {
111 if (m_http_header)
112 curl_slist_free_all(m_http_header);
113 if (m_curl_session)
114 {
115 curl_easy_cleanup(m_curl_session);
116 m_curl_session = NULL;
117 }
118 }
119 virtual bool isAllowedToAdd() const OVERRIDE;
120 void setApiURL(const std::string& url, const std::string &action);
121 void setAddonsURL(const std::string& path);
122
123 // ------------------------------------------------------------------------
125 virtual bool hadDownloadError() const { return m_curl_code != CURLE_OK; }
126 // ------------------------------------------------------------------------
127 void setDownloadAssetsRequest(bool val)
128 { m_download_assets_request = val; }
129 // ------------------------------------------------------------------------
133 const char* getDownloadErrorMessage() const
134 {
135 assert(hadDownloadError());
136 return curl_easy_strerror(m_curl_code);
137 } // getDownloadErrorMessage
138
139 // ------------------------------------------------------------------------
144 const std::string & getData() const
145 {
146 assert(hasBeenExecuted());
147 return m_string_buffer;
148 } // getData
149
150 // --------------------------------------------------------------------
152 void addParameter(const std::string & name, const std::string &value)
153 {
154 // Call the template, so that the strings are escaped properly
155 addParameter(name, value.c_str());
156 } // addParameter
157
158 // --------------------------------------------------------------------
160 void addParameter(const std::string &name,
161 const irr::core::stringw &value)
162 {
163 std::string s = StringUtils::wideToUtf8(value);
164
165 // Call the template to escape strings properly
166 addParameter(name, s.c_str());
167 } // addParameter
168
169 // --------------------------------------------------------------------
171 template <typename T>
172 void addParameter(const std::string &name, const T& value)
173 {
174 assert(isPreparing());
175 std::string s = StringUtils::toString(value);
176
177 char *s1 = curl_easy_escape(m_curl_session, name.c_str(), (int)name.size());
178 char *s2 = curl_easy_escape(m_curl_session, s.c_str(), (int)s.size());
179 m_parameters.append(std::string(s1) + "=" + s2 + "&");
180 curl_free(s1);
181 curl_free(s2);
182 } // addParameter
183
184 // --------------------------------------------------------------------
186 float getProgress() const { return m_progress.load(); }
187
188 // --------------------------------------------------------------------
190 void setProgress(float f) { m_progress.store(f); }
191
192 // --------------------------------------------------------------------
193 const std::string & getURL() const { assert(isBusy()); return m_url;}
194
195 // --------------------------------------------------------------------
197 void setURL(const std::string & url)
198 {
199 assert(isPreparing());
200 m_url = url;
201 } // setURL
202 // --------------------------------------------------------------------
203 const std::string& getFileName() const { return m_filename; }
204 // --------------------------------------------------------------------
205 double getTotalSize() const { return m_total_size.load(); }
206 // --------------------------------------------------------------------
207 void setTotalSize(double d) { m_total_size.store(d); }
208 }; // class HTTPRequest
209} //namespace Online
210#endif // HEADER_HTTP_REQUEST_HPP
Definition: http_request.hpp:47
A http request.
Definition: http_request.hpp:56
std::string m_string_buffer
String to store the received data in.
Definition: http_request.hpp:80
void addParameter(const std::string &name, const T &value)
Sets a parameter to 'value' (arbitrary types).
Definition: http_request.hpp:172
float getProgress() const
Returns the current progress.
Definition: http_request.hpp:186
std::atomic< float > m_progress
The progress indicator.
Definition: http_request.hpp:62
CURL * m_curl_session
Pointer to the curl data structure for this request.
Definition: http_request.hpp:74
void setProgress(float f)
Sets the current progress.
Definition: http_request.hpp:190
virtual bool isAllowedToAdd() const OVERRIDE
Checks the request if it has enough (correct) information to be executed (and thus allowed to add to ...
Definition: http_request.cpp:145
CURLcode m_curl_code
curl return code.
Definition: http_request.hpp:77
void init()
Initialises all member variables.
Definition: http_request.cpp:86
void setAddonsURL(const std::string &path)
A handy shortcut that appends the given path to the URL of the addons server.
Definition: http_request.cpp:136
virtual void operation() OVERRIDE
The actual curl download happens here.
Definition: http_request.cpp:196
virtual void prepareOperation() OVERRIDE
Sets up the curl data structures.
Definition: http_request.cpp:153
static size_t writeCallback(void *contents, size_t size, size_t nmemb, void *userp)
Callback from curl.
Definition: http_request.cpp:337
std::string m_filename
Contains a filename if the data should be saved into a file instead of being kept in in memory.
Definition: http_request.hpp:86
std::string m_parameters
The POST parameters that will be send with the request.
Definition: http_request.hpp:70
const char * getDownloadErrorMessage() const
Returns the curl error message if an error has occurred.
Definition: http_request.hpp:133
const std::string & getData() const
Returns the downloaded string.
Definition: http_request.hpp:144
void addParameter(const std::string &name, const irr::core::stringw &value)
Sets a parameter to 'value' (stringw).
Definition: http_request.hpp:160
void setApiURL(const std::string &url, const std::string &action)
A handy shortcut that appends the given path to the URL of the mutiplayer server.
Definition: http_request.cpp:105
virtual void afterOperation() OVERRIDE
Cleanup once the download is finished.
Definition: http_request.cpp:309
void addParameter(const std::string &name, const std::string &value)
Sets a parameter to 'value' (std::string).
Definition: http_request.hpp:152
virtual bool hadDownloadError() const
Returns true if there was an error downloading the file.
Definition: http_request.hpp:125
static int progressDownload(void *clientp, progress_t dltotal, progress_t dlnow, progress_t ultotal, progress_t ulnow)
Callback function from curl: inform about progress.
Definition: http_request.cpp:354
void setURL(const std::string &url)
Sets the URL for this request.
Definition: http_request.hpp:197
std::string m_url
The url to download.
Definition: http_request.hpp:67
Stores a request for the HTTP Manager.
Definition: request.hpp:62
bool isPreparing() const
Returns if this request is being prepared.
Definition: request.hpp:191
bool hasBeenExecuted() const
Checks if the request has completed or done (i.e.
Definition: request.hpp:201
bool isBusy() const
Returns if this request is busy.
Definition: request.hpp:195