TDME2  1.9.200
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 
8 #include <tdme/tdme.h>
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 { true };
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  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 
76  static const constexpr int16_t HTTP_STATUSCODE_OK { 200 };
77 
78  /**
79  * Public constructor
80  */
82 
83  /**
84  * Get username
85  * @return username
86  */
87  inline const string& getUsername() {
88  return username;
89  }
90 
91  /**
92  * Set username
93  * @param username user name
94  */
95  inline void setUsername(const string& username) {
96  this->username = username;
97  }
98 
99  /**
100  * Get password
101  * @return password
102  */
103  inline const string& getPassword() {
104  return password;
105  }
106 
107  /**
108  * Set password
109  * @param password password
110  */
111  inline void setPassword(const string& password) {
112  this->password = password;
113  }
114 
115  /**
116  * Get request headers
117  * @return request headers
118  */
119  inline const unordered_map<string, string>& getHeaders() {
120  return headers;
121  }
122 
123  /**
124  * Set request headers
125  * @param headers request headers
126  */
127  inline void setHeaders(const unordered_map<string, string>& headers) {
128  this->headers = headers;
129  }
130 
131  /**
132  * Get GET parameter
133  * @return GET parameter
134  */
135  inline const unordered_map<string, string>& getGETParameters() {
136  return getParameters;
137  }
138 
139  /**
140  * Set GET parameter
141  * @param GET parameter
142  */
143  inline void setGETParameters(const unordered_map<string, string>& parameters) {
144  this->getParameters = parameters;
145  }
146 
147  /**
148  * Get URL
149  * @return URL
150  */
151  inline const string& getURL() {
152  return url;
153  }
154 
155  /**
156  * Set URL
157  * @param url URL
158  */
159  inline void setURL(const string& url) {
160  this->url = url;
161  }
162 
163  /**
164  * Get file to download to
165  * @return file
166  */
167  inline const string& getFile() {
168  return file;
169  }
170 
171  /**
172  * Set file to download to
173  * @param file file
174  */
175  inline void setFile(const string& file) {
176  this->file = file;
177  }
178 
179  /**
180  * Reset this HTTP client
181  */
182  void reset();
183 
184  /**
185  * Starts the HTTP download to file
186  */
187  void start();
188 
189  /**
190  * @return HTTP status code
191  */
192  inline int16_t getStatusCode() {
193  return statusCode;
194  }
195 
196  /**
197  * @return HTTP response headers
198  */
199  inline const unordered_map<string, string>& getResponseHeaders() {
200  return headers;
201  }
202 
203  /**
204  * @return is finished
205  */
206  inline bool isFinished() {
207  return finished;
208  }
209 
210  /**
211  * @return progress
212  */
213  inline float getProgress() {
214  return progress;
215  }
216 
217  /**
218  * Cancel a started download
219  */
220  void cancel();
221 
222  /**
223  * Wait until underlying thread has finished
224  */
225  void join();
226 
227 };
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.
unordered_map< string, string > getParameters
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.
unordered_map< string, string > responseHeaders
static string urlEncode(const string &value)
Returns a URL encoded representation of value.
void setHeaders(const unordered_map< string, string > &headers)
Set request headers.
static constexpr const int16_t HTTP_STATUSCODE_OK
void setFile(const string &file)
Set file to download to.
string createHTTPRequestHeaders(const string &hostName, const string &relativeUrl)
Create HTTP request headers.
Base exception class for network exceptions.
Mutex implementation.
Definition: Mutex.h:19
Base class for threads.
Definition: Thread.h:20