MinitScript  0.9.31 PRE-BETA
HTTPClient.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <sstream>
4 #include <unordered_map>
5 #include <vector>
6 
11 
12 using std::string;
13 using std::stringstream;
14 using std::unordered_map;
15 using std::vector;
16 
19 
20 /**
21  * Basic HTTP client
22  * @author Andreas Drewke
23  */
25 
26 private:
27  string url;
28  string method;
29  unordered_map<string, string> headers;
30  unordered_map<string, string> getParameters;
31  unordered_map<string, string> postParameters;
32  string body;
33  string contentType;
34  string username;
35  string password;
36 
37  stringstream rawResponse;
38  int16_t statusCode { -1 };
39  unordered_map<string, string> responseHeaders;
40 
41  /**
42  * Returns a URL encoded representation of value
43  * @param value value
44  * @return URL encoded value
45  */
46  static string urlEncode(const string& value);
47 
48  /**
49  * Create HTTP request headers
50  * @param hostname hostname
51  * @param relativeUrl url relative to server root
52  * @param body body
53  */
54  string createHTTPRequestHeaders(const string& hostname, const string& relativeUrl, const string& body);
55 
56  /**
57  * Parse HTTP response headers
58  * @param rawResponse raw response
59  */
60  void parseHTTPResponseHeaders(stringstream& rawResponse);
61 
62 public:
68 
69  //
71  // https://github.com/j-ulrich/http-status-codes-cpp
136  HTTP_STATUS_MAX = 1023
137  };
138 
139  /**
140  * Get URL
141  * @return URL
142  */
143  inline const string& getURL() {
144  return url;
145  }
146 
147  /**
148  * Set URL
149  * @param url URL
150  */
151  inline void setURL(const string& url) {
152  this->url = url;
153  }
154 
155  /**
156  * Get method
157  * @return method
158  */
159  inline const string& getMethod() {
160  return method;
161  }
162 
163  /**
164  * Set method
165  * @param method method
166  */
167  inline void setMethod(const string& method) {
168  this->method = method;
169  }
170 
171  /**
172  * Get username
173  * @return username
174  */
175  inline const string& getUsername() {
176  return username;
177  }
178 
179  /**
180  * Set username
181  * @param username user name
182  */
183  inline void setUsername(const string& username) {
184  this->username = username;
185  }
186 
187  /**
188  * Get password
189  * @return password
190  */
191  inline const string& getPassword() {
192  return password;
193  }
194 
195  /**
196  * Set password
197  * @param password password
198  */
199  inline void setPassword(const string& password) {
200  this->password = password;
201  }
202 
203  /**
204  * Get request headers
205  * @return request headers
206  */
207  inline const unordered_map<string, string>& getHeaders() {
208  return headers;
209  }
210 
211  /**
212  * Set request headers
213  * @param headers request headers
214  */
215  inline void setHeaders(const unordered_map<string, string>& headers) {
216  this->headers = headers;
217  }
218 
219  /**
220  * Get GET parameter
221  * @return GET parameter
222  */
223  inline const unordered_map<string, string>& getGETParameters() {
224  return getParameters;
225  }
226 
227  /**
228  * Set GET parameter
229  * @param GET parameter
230  */
231  inline void setGETParameters(const unordered_map<string, string>& parameters) {
232  this->getParameters = parameters;
233  }
234 
235  /**
236  * Get POST parameter
237  * @return POST parameter
238  */
239  inline const unordered_map<string, string>& getPOSTParameters() {
240  return postParameters;
241  }
242 
243  /**
244  * Set POST parameter
245  * @param POST parameter
246  */
247  inline void setPOSTParameters(const unordered_map<string, string>& parameters) {
248  this->postParameters = parameters;
249  }
250 
251  /**
252  * Get body
253  * @return body
254  */
255  inline const string& getBody() {
256  return body;
257  }
258 
259  /**
260  * Set body
261  * @param contentType content type
262  * @param body body
263  */
264  inline void setBody(const string& contentType, const string& body) {
265  this->contentType = contentType;
266  this->body = body;
267  }
268 
269  /**
270  * Get content type
271  * @return content type
272  */
273  inline const string& getContentType() {
274  return contentType;
275  }
276 
277  /**
278  * Set content type
279  * @param contentType content type
280  */
281  inline void setContentType(const string& contentType) {
282  this->contentType = contentType;
283  }
284 
285 
286  /**
287  * Reset this HTTP client
288  */
289  void reset();
290 
291  /**
292  * Execute HTTP request
293  * @throws minitscript::network::httpclient::_HTTPClientException
294  * @throws minitscript::os::network::_NetworkException
295  */
296  void execute();
297 
298  /**
299  * @return complete response stream
300  */
301  inline stringstream& getResponse() {
302  return rawResponse;
303  }
304 
305  /**
306  * @return HTTP status code
307  */
308  inline int16_t getStatusCode() {
309  return statusCode;
310  }
311 
312  /**
313  * @return response headers
314  */
315  inline const unordered_map<string, string>& getResponseHeaders() {
316  return responseHeaders;
317  }
318 
319 };
void setBody(const string &contentType, const string &body)
Set body.
Definition: HTTPClient.h:264
unordered_map< string, string > headers
Definition: HTTPClient.h:29
static MINITSCRIPT_STATIC_DLL_IMPEXT const string HTTP_METHOD_GET
Definition: HTTPClient.h:63
void setContentType(const string &contentType)
Set content type.
Definition: HTTPClient.h:281
static MINITSCRIPT_STATIC_DLL_IMPEXT const string HTTP_METHOD_DELETE
Definition: HTTPClient.h:67
const string & getUsername()
Get username.
Definition: HTTPClient.h:175
void setPassword(const string &password)
Set password.
Definition: HTTPClient.h:199
void setPOSTParameters(const unordered_map< string, string > &parameters)
Set POST parameter.
Definition: HTTPClient.h:247
const unordered_map< string, string > & getResponseHeaders()
Definition: HTTPClient.h:315
static MINITSCRIPT_STATIC_DLL_IMPEXT const string HTTP_METHOD_POST
Definition: HTTPClient.h:65
const unordered_map< string, string > & getHeaders()
Get request headers.
Definition: HTTPClient.h:207
static MINITSCRIPT_STATIC_DLL_IMPEXT const string HTTP_METHOD_PUT
Definition: HTTPClient.h:66
unordered_map< string, string > getParameters
Definition: HTTPClient.h:30
const unordered_map< string, string > & getGETParameters()
Get GET parameter.
Definition: HTTPClient.h:223
void setGETParameters(const unordered_map< string, string > &parameters)
Set GET parameter.
Definition: HTTPClient.h:231
static MINITSCRIPT_STATIC_DLL_IMPEXT const string HTTP_METHOD_HEAD
Definition: HTTPClient.h:64
void setUsername(const string &username)
Set username.
Definition: HTTPClient.h:183
void setMethod(const string &method)
Set method.
Definition: HTTPClient.h:167
unordered_map< string, string > responseHeaders
Definition: HTTPClient.h:39
unordered_map< string, string > postParameters
Definition: HTTPClient.h:31
static string urlEncode(const string &value)
Returns a URL encoded representation of value.
Definition: HTTPClient.cpp:58
const string & getMethod()
Get method.
Definition: HTTPClient.h:159
void setHeaders(const unordered_map< string, string > &headers)
Set request headers.
Definition: HTTPClient.h:215
void reset()
Reset this HTTP client.
Definition: HTTPClient.cpp:165
string createHTTPRequestHeaders(const string &hostname, const string &relativeUrl, const string &body)
Create HTTP request headers.
Definition: HTTPClient.cpp:83
const string & getPassword()
Get password.
Definition: HTTPClient.h:191
const unordered_map< string, string > & getPOSTParameters()
Get POST parameter.
Definition: HTTPClient.h:239
const string & getContentType()
Get content type.
Definition: HTTPClient.h:273
void setURL(const string &url)
Set URL.
Definition: HTTPClient.h:151
void parseHTTPResponseHeaders(stringstream &rawResponse)
Parse HTTP response headers.
Definition: HTTPClient.cpp:125
Base exception class for network exceptions.
#define MINITSCRIPT_STATIC_DLL_IMPEXT
Definition: minitscript.h:18