MinitScript  0.9.31 PRE-BETA
HTTPDownloadClient.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <fstream>
4 #include <memory>
5 #include <unordered_map>
6 #include <vector>
7 
14 
15 using std::ifstream;
16 using std::string;
17 using std::unique_ptr;
18 using std::unordered_map;
19 using std::vector;
20 
25 
26 /**
27  * HTTP download client
28  * @author Andreas Drewke
29  */
31 
32 private:
33  string url;
34  string file;
35  string username;
36  string password;
37 
38  unordered_map<string, string> headers;
39  unordered_map<string, string> getParameters;
40 
41  int16_t statusCode { -1 };
42  unordered_map<string, string> responseHeaders;
43 
44  unique_ptr<_Thread> downloadThread;
46  bool haveHeaders { false };
47  bool haveContentSize { false };
48  uint64_t headerSize { 0LL };
49  uint64_t contentSize { 0LL };
50  volatile bool finished { false };
51  volatile float progress { 0.0f };
52 
53  /**
54  * Returns a URL encoded representation of value
55  * @param value value
56  * @return URL encoded value
57  */
58  static string urlEncode(const string& value);
59 
60  /**
61  * Create HTTP request headers
62  * @param hostName host name
63  * @param relativeUrl url relative to server root
64  */
65  const string createHTTPRequestHeaders(const string& hostName, const string& relativeUrl);
66 
67  /**
68  * Parse HTTP response headers
69  * @param rawResponse raw response
70  * @return http header size or 0 if not yet completely submitted
71  */
72  uint64_t parseHTTPResponseHeaders(ifstream& rawResponse);
73 
74 public:
75  //
77  // https://github.com/j-ulrich/http-status-codes-cpp
142  HTTP_STATUS_MAX = 1023
143  };
144 
145  /**
146  * Public constructor
147  */
149 
150  /**
151  * Get username
152  * @return username
153  */
154  inline const string& getUsername() {
155  return username;
156  }
157 
158  /**
159  * Set username
160  * @param username user name
161  */
162  inline void setUsername(const string& username) {
163  this->username = username;
164  }
165 
166  /**
167  * Get password
168  * @return password
169  */
170  inline const string& getPassword() {
171  return password;
172  }
173 
174  /**
175  * Set password
176  * @param password password
177  */
178  inline void setPassword(const string& password) {
179  this->password = password;
180  }
181 
182  /**
183  * Get request headers
184  * @return request headers
185  */
186  inline const unordered_map<string, string>& getHeaders() {
187  return headers;
188  }
189 
190  /**
191  * Set request headers
192  * @param headers request headers
193  */
194  inline void setHeaders(const unordered_map<string, string>& headers) {
195  this->headers = headers;
196  }
197 
198  /**
199  * Get GET parameter
200  * @return GET parameter
201  */
202  inline const unordered_map<string, string>& getGETParameters() {
203  return getParameters;
204  }
205 
206  /**
207  * Set GET parameter
208  * @param GET parameter
209  */
210  inline void setGETParameters(const unordered_map<string, string>& parameters) {
211  this->getParameters = parameters;
212  }
213 
214  /**
215  * Get URL
216  * @return URL
217  */
218  inline const string& getURL() {
219  return url;
220  }
221 
222  /**
223  * Set URL
224  * @param url URL
225  */
226  inline void setURL(const string& url) {
227  this->url = url;
228  }
229 
230  /**
231  * Get file to download to
232  * @return file
233  */
234  inline const string& getFile() {
235  return file;
236  }
237 
238  /**
239  * Set file to download to
240  * @param file file
241  */
242  inline void setFile(const string& file) {
243  this->file = file;
244  }
245 
246  /**
247  * Reset this HTTP client
248  */
249  void reset();
250 
251  /**
252  * Starts the HTTP download to file
253  */
254  void start();
255 
256  /**
257  * @return HTTP status code
258  */
259  inline int16_t getStatusCode() {
260  return statusCode;
261  }
262 
263  /**
264  * @return HTTP response headers
265  */
266  inline const unordered_map<string, string>& getResponseHeaders() {
267  return responseHeaders;
268  }
269 
270  /**
271  * @return is finished
272  */
273  inline bool isFinished() {
274  return finished;
275  }
276 
277  /**
278  * @return progress
279  */
280  inline float getProgress() {
281  return progress;
282  }
283 
284  /**
285  * Cancel a started download
286  */
287  void cancel();
288 
289  /**
290  * Wait until underlying thread has finished
291  */
292  void join();
293 
294 };
const string & getFile()
Get file to download to.
uint64_t parseHTTPResponseHeaders(ifstream &rawResponse)
Parse HTTP response headers.
void setPassword(const string &password)
Set password.
const unordered_map< string, string > & getResponseHeaders()
void start()
Starts the HTTP download to file.
const unordered_map< string, string > & getHeaders()
Get request headers.
void join()
Wait until underlying thread has finished.
const unordered_map< string, string > & getGETParameters()
Get GET parameter.
void setGETParameters(const unordered_map< string, string > &parameters)
Set GET parameter.
void setUsername(const string &username)
Set username.
const string createHTTPRequestHeaders(const string &hostName, const string &relativeUrl)
Create HTTP request headers.
static string urlEncode(const string &value)
Returns a URL encoded representation of value.
void setHeaders(const unordered_map< string, string > &headers)
Set request headers.
void setFile(const string &file)
Set file to download to.
Base exception class for network exceptions.
Mutex implementation.
Definition: Mutex.h:19
Base class for threads.
Definition: Thread.h:20