Http.hpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 #ifndef CPP3DS_HTTP_HPP
26 #define CPP3DS_HTTP_HPP
27 
29 // Headers
31 #include <cpp3ds/Network/IpAddress.hpp>
32 #include <cpp3ds/Network/TcpSocket.hpp>
33 #include <cpp3ds/System/NonCopyable.hpp>
34 #include <cpp3ds/System/Time.hpp>
35 #include <map>
36 #include <string>
37 
38 
39 namespace cpp3ds
40 {
46 {
47 public:
48 
53  class Request
54  {
55  public:
56 
61  enum Method
62  {
63  Get,
64  Post,
65  Head,
66  Put,
68  };
69 
81  Request(const std::string& uri = "/", Method method = Get, const std::string& body = "");
82 
96  void setField(const std::string& field, const std::string& value);
97 
108  void setMethod(Method method);
109 
120  void setUri(const std::string& uri);
121 
131  void setHttpVersion(unsigned int major, unsigned int minor);
132 
143  void setBody(const std::string& body);
144 
145  private:
146 
147  friend class Http;
148 
158  std::string prepare() const;
159 
170  bool hasField(const std::string& field) const;
171 
173  // Types
175  typedef std::map<std::string, std::string> FieldTable;
176 
178  // Member data
180  FieldTable m_fields;
181  Method m_method;
182  std::string m_uri;
183  unsigned int m_majorVersion;
184  unsigned int m_minorVersion;
185  std::string m_body;
186  };
187 
192  class Response
193  {
194  public:
195 
200  enum Status
201  {
202  // 2xx: success
203  Ok = 200,
204  Created = 201,
205  Accepted = 202,
206  NoContent = 204,
207  ResetContent = 205,
209 
210  // 3xx: redirection
214  NotModified = 304,
215 
216  // 4xx: client error
217  BadRequest = 400,
218  Unauthorized = 401,
219  Forbidden = 403,
220  NotFound = 404,
222 
223  // 5xx: server error
226  BadGateway = 502,
230 
231  // 10xx: cpp3ds custom codes
234  };
235 
242  Response();
243 
256  const std::string& getField(const std::string& field) const;
257 
269  Status getStatus() const;
270 
279  unsigned int getMajorHttpVersion() const;
280 
289  unsigned int getMinorHttpVersion() const;
290 
303  const std::string& getBody() const;
304 
305  private:
306 
307  friend class Http;
308 
318  void parse(const std::string& data);
319 
320 
330  void parseFields(std::istream &in);
331 
333  // Types
335  typedef std::map<std::string, std::string> FieldTable;
336 
338  // Member data
340  FieldTable m_fields;
341  Status m_status;
342  unsigned int m_majorVersion;
343  unsigned int m_minorVersion;
344  std::string m_body;
345  };
346 
351  Http();
352 
367  Http(const std::string& host, unsigned short port = 0);
368 
384  void setHost(const std::string& host, unsigned short port = 0);
385 
404  Response sendRequest(const Request& request, Time timeout = Time::Zero);
405 
406 private:
407 
409  // Member data
411  TcpSocket m_connection;
412  IpAddress m_host;
413  std::string m_hostName;
414  unsigned short m_port;
415 };
416 
417 } // namespace cpp3ds
418 
419 
420 #endif // CPP3DS_HTTP_HPP
421 
422 
Define a HTTP request.
Definition: Http.hpp:53
Encapsulate an IPv4 network address.
Definition: IpAddress.hpp:43
Method
Enumerate the available HTTP methods for a request.
Definition: Http.hpp:61
The requested page can be accessed from several locations.
Definition: Http.hpp:211
const std::string & getField(const std::string &field) const
Get the value of a field.
A HTTP client.
Definition: Http.hpp:45
Response sendRequest(const Request &request, Time timeout=Time::Zero)
Send a HTTP request and return the server's response.
Request in get mode, standard method to retrieve a page.
Definition: Http.hpp:63
Status getStatus() const
Get the response status code.
Status
Enumerate all the valid status codes for a response.
Definition: Http.hpp:200
The gateway server couldn't receive a response from the source server.
Definition: Http.hpp:228
Request(const std::string &uri="/", Method method=Get, const std::string &body="")
Default constructor.
The requested page needs an authentication to be accessed.
Definition: Http.hpp:218
The server can't satisfy the partial GET request (with a "Range" header field)
Definition: Http.hpp:221
void setBody(const std::string &body)
Set the body of the request.
The gateway server has received an error from the source server.
Definition: Http.hpp:226
void setHost(const std::string &host, unsigned short port=0)
Set the target host.
void setUri(const std::string &uri)
Set the requested URI.
The server informs the client that it should clear the view (form) that caused the request to be sent...
Definition: Http.hpp:207
Represents a time value.
Definition: Time.hpp:37
unsigned int getMajorHttpVersion() const
Get the major HTTP version number of the response.
Request a page's header only.
Definition: Http.hpp:65
The request has been accepted, but will be processed later by the server.
Definition: Http.hpp:205
The resource has successfully been created.
Definition: Http.hpp:204
Connection with server failed.
Definition: Http.hpp:233
The server is temporarily unavailable (overloaded, in maintenance, ...)
Definition: Http.hpp:227
The requested page cannot be accessed at all, even with authentication.
Definition: Http.hpp:219
void setField(const std::string &field, const std::string &value)
Set the value of a field.
Request in put mode, useful for a REST API.
Definition: Http.hpp:66
The requested page has temporarily moved to a new location.
Definition: Http.hpp:213
const std::string & getBody() const
Get the body of the response.
Response()
Default constructor.
unsigned int getMinorHttpVersion() const
Get the minor HTTP version number of the response.
For conditional requests, means the requested page hasn't changed and doesn't need to be refreshed...
Definition: Http.hpp:214
The requested page doesn't exist.
Definition: Http.hpp:220
The server doesn't support the requested HTTP version.
Definition: Http.hpp:229
The server couldn't understand the request (syntax error)
Definition: Http.hpp:217
Utility class that makes any derived class non-copyable.
Definition: NonCopyable.hpp:35
void setHttpVersion(unsigned int major, unsigned int minor)
Set the HTTP version for the request.
Define a HTTP response.
Definition: Http.hpp:192
Specialized socket using the TCP protocol.
Definition: TcpSocket.hpp:45
The server doesn't implement a requested feature.
Definition: Http.hpp:225
The server encountered an unexpected error.
Definition: Http.hpp:224
Request in delete mode, useful for a REST API.
Definition: Http.hpp:67
Most common code returned when operation was successful.
Definition: Http.hpp:203
The requested page has permanently moved to a new location.
Definition: Http.hpp:212
The server has sent a part of the resource, as a response to a partial GET request.
Definition: Http.hpp:208
void setMethod(Method method)
Set the request method.
The server didn't send any data in return.
Definition: Http.hpp:206
Http()
Default constructor.
Request in post mode, usually to send data to a page.
Definition: Http.hpp:64
static const Time Zero
Predefined "zero" time value.
Definition: Time.hpp:82
Response is not a valid HTTP one.
Definition: Http.hpp:232