Documentation of SFML 2.0

Warning: this page refers to an old version of SFML. Click here to switch to the latest version.
Http.hpp
1 
2 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
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 SFML_HTTP_HPP
26 #define SFML_HTTP_HPP
27 
29 // Headers
31 #include <SFML/Network/Export.hpp>
32 #include <SFML/Network/IpAddress.hpp>
33 #include <SFML/Network/TcpSocket.hpp>
34 #include <SFML/System/NonCopyable.hpp>
35 #include <SFML/System/Time.hpp>
36 #include <map>
37 #include <string>
38 
39 
40 namespace sf
41 {
46 class SFML_NETWORK_API Http : NonCopyable
47 {
48 public :
49 
54  class SFML_NETWORK_API Request
55  {
56  public :
57 
62  enum Method
63  {
64  Get,
65  Post,
66  Head
67  };
68 
80  Request(const std::string& uri = "/", Method method = Get, const std::string& body = "");
81 
95  void setField(const std::string& field, const std::string& value);
96 
107  void setMethod(Method method);
108 
119  void setUri(const std::string& uri);
120 
130  void setHttpVersion(unsigned int major, unsigned int minor);
131 
142  void setBody(const std::string& body);
143 
144  private :
145 
146  friend class Http;
147 
157  std::string prepare() const;
158 
169  bool hasField(const std::string& field) const;
170 
172  // Types
174  typedef std::map<std::string, std::string> FieldTable;
175 
177  // Member data
179  FieldTable m_fields;
180  Method m_method;
181  std::string m_uri;
182  unsigned int m_majorVersion;
183  unsigned int m_minorVersion;
184  std::string m_body;
185  };
186 
191  class SFML_NETWORK_API Response
192  {
193  public :
194 
199  enum Status
200  {
201  // 2xx: success
202  Ok = 200,
203  Created = 201,
204  Accepted = 202,
205  NoContent = 204,
206  ResetContent = 205,
207  PartialContent = 206,
208 
209  // 3xx: redirection
210  MultipleChoices = 300,
211  MovedPermanently = 301,
212  MovedTemporarily = 302,
213  NotModified = 304,
214 
215  // 4xx: client error
216  BadRequest = 400,
217  Unauthorized = 401,
218  Forbidden = 403,
219  NotFound = 404,
220  RangeNotSatisfiable = 407,
221 
222  // 5xx: server error
223  InternalServerError = 500,
224  NotImplemented = 501,
225  BadGateway = 502,
226  ServiceNotAvailable = 503,
227  GatewayTimeout = 504,
228  VersionNotSupported = 505,
229 
230  // 10xx: SFML custom codes
231  InvalidResponse = 1000,
232  ConnectionFailed = 1001
233  };
234 
241  Response();
242 
255  const std::string& getField(const std::string& field) const;
256 
268  Status getStatus() const;
269 
278  unsigned int getMajorHttpVersion() const;
279 
288  unsigned int getMinorHttpVersion() const;
289 
302  const std::string& getBody() const;
303 
304  private :
305 
306  friend class Http;
307 
317  void parse(const std::string& data);
318 
320  // Types
322  typedef std::map<std::string, std::string> FieldTable;
323 
325  // Member data
327  FieldTable m_fields;
328  Status m_status;
329  unsigned int m_majorVersion;
330  unsigned int m_minorVersion;
331  std::string m_body;
332  };
333 
338  Http();
339 
354  Http(const std::string& host, unsigned short port = 0);
355 
371  void setHost(const std::string& host, unsigned short port = 0);
372 
391  Response sendRequest(const Request& request, Time timeout = Time::Zero);
392 
393 private :
394 
396  // Member data
398  TcpSocket m_connection;
399  IpAddress m_host;
400  std::string m_hostName;
401  unsigned short m_port;
402 };
403 
404 } // namespace sf
405 
406 
407 #endif // SFML_HTTP_HPP
408 
409