SuperTuxKart
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 #include <cassert>
27 #include <cstdint>
28 #include <atomic>
29 #include <string>
30 
31 namespace Online
32 {
33  bool globalHTTPRequestInit();
34  void globalHTTPRequestCleanup();
35 
36  class API
37  {
38  public:
39  static const std::string USER_PATH;
40  static const std::string SERVER_PATH;
41  };
42 
45  class HTTPRequest : public Request
46  {
47  private:
52  std::atomic<float> m_progress;
53 
54  std::atomic<double> m_total_size;
55 
57  std::string m_url;
58 
60  std::string m_parameters;
61 
63  int64_t m_result_code;
64 
66  std::string m_string_buffer;
67 
68 #ifdef APPLE_NETWORK_LIBRARIES
69  std::string m_error_string;
70 #endif
71 
72  std::string urlEncode(const std::string& value);
73 
74  void logMessage();
75  protected:
78  std::string m_filename;
79 
80  bool m_disable_sending_log;
81 
82  /* If true, it will not send POST parameters so
83  * it's just a GET request. */
84  bool m_download_assets_request;
85 
86  virtual void operation() OVERRIDE;
87 
88  void init();
89  public:
90  HTTPRequest(int priority = 1);
91  HTTPRequest(const std::string &filename, int priority = 1);
92  HTTPRequest(const char * const filename, int priority = 1);
93  virtual bool isAllowedToAdd() const OVERRIDE;
94  void setApiURL(const std::string& url, const std::string &action);
95  void setAddonsURL(const std::string& path);
96 
97  // ------------------------------------------------------------------------
99  virtual bool hadDownloadError() const { return m_result_code != 0; }
100  // ------------------------------------------------------------------------
101  void setDownloadAssetsRequest(bool val)
102  { m_download_assets_request = val; }
103  // ------------------------------------------------------------------------
106  const char* getDownloadErrorMessage() const;
107  // ------------------------------------------------------------------------
112  const std::string & getData() const
113  {
114  assert(hasBeenExecuted());
115  return m_string_buffer;
116  } // getData
117 
118  // --------------------------------------------------------------------
120  void addParameter(const std::string & name, const std::string &value)
121  {
122  // Call the template, so that the strings are escaped properly
123  addParameter(name, value.c_str());
124  } // addParameter
125 
126  // --------------------------------------------------------------------
128  void addParameter(const std::string &name,
129  const irr::core::stringw &value)
130  {
131  std::string s = StringUtils::wideToUtf8(value);
132 
133  // Call the template to escape strings properly
134  addParameter(name, s.c_str());
135  } // addParameter
136 
137  // --------------------------------------------------------------------
139  template <typename T>
140  void addParameter(const std::string &name, const T& value)
141  {
142  assert(isPreparing());
143  std::string s = StringUtils::toString(value);
144  std::string s1 = urlEncode(name);
145  std::string s2 = urlEncode(s);
146  m_parameters.append(s1 + "=" + s2 + "&");
147  }
148 
149  // --------------------------------------------------------------------
151  float getProgress() const { return m_progress.load(); }
152 
153  // --------------------------------------------------------------------
155  void setProgress(float f) { m_progress.store(f); }
156 
157  // --------------------------------------------------------------------
158  const std::string & getURL() const { assert(isBusy()); return m_url;}
159 
160  // --------------------------------------------------------------------
162  void setURL(const std::string & url)
163  {
164  assert(isPreparing());
165  m_url = url;
166  } // setURL
167  // --------------------------------------------------------------------
168  const std::string& getFileName() const { return m_filename; }
169  // --------------------------------------------------------------------
170  double getTotalSize() const { return m_total_size.load(); }
171  // --------------------------------------------------------------------
172  void setTotalSize(double d) { m_total_size.store(d); }
173  }; // class HTTPRequest
174 } //namespace Online
175 #endif // HEADER_HTTP_REQUEST_HPP
Definition: http_request.hpp:37
A http request.
Definition: http_request.hpp:46
std::string m_string_buffer
String to store the received data in.
Definition: http_request.hpp:66
void addParameter(const std::string &name, const T &value)
Sets a parameter to 'value' (arbitrary types).
Definition: http_request.hpp:140
float getProgress() const
Returns the current progress.
Definition: http_request.hpp:151
std::atomic< float > m_progress
The progress indicator.
Definition: http_request.hpp:52
int64_t m_result_code
Result code from request implementation, 0 if succeeded.
Definition: http_request.hpp:63
void setProgress(float f)
Sets the current progress.
Definition: http_request.hpp:155
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:143
void init()
Initialises all member variables.
Definition: http_request.cpp:83
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:134
virtual void operation() OVERRIDE
The actual operation to be executed.
Definition: http_request_curl.cpp:107
std::string m_filename
Contains a filename if the data should be saved into a file instead of being kept in memory.
Definition: http_request.hpp:78
std::string m_parameters
The POST parameters that will be sent with the request.
Definition: http_request.hpp:60
const char * getDownloadErrorMessage() const
Returns the error message if an error has occurred.
Definition: http_request_curl.cpp:239
void addParameter(const std::string &name, const irr::core::stringw &value)
Sets a parameter to 'value' (stringw).
Definition: http_request.hpp:128
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:103
void addParameter(const std::string &name, const std::string &value)
Sets a parameter to 'value' (std::string).
Definition: http_request.hpp:120
virtual bool hadDownloadError() const
Returns true if there was an error downloading the file.
Definition: http_request.hpp:99
void setURL(const std::string &url)
Sets the URL for this request.
Definition: http_request.hpp:162
const std::string & getData() const
Returns the downloaded string.
Definition: http_request.hpp:112
std::string m_url
The url to download.
Definition: http_request.hpp:57
Stores a request for the HTTP Manager.
Definition: request.hpp:58
bool isPreparing() const
Returns if this request is being prepared.
Definition: request.hpp:187
bool hasBeenExecuted() const
Checks if the request has completed or done (i.e.
Definition: request.hpp:197
bool isBusy() const
Returns if this request is busy.
Definition: request.hpp:191