Http.hpp
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2009 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/System/NonCopyable.hpp>
32#include <SFML/Network/IPAddress.hpp>
33#include <SFML/Network/SocketTCP.hpp>
34#include <map>
35#include <string>
36
37
38namespace sf
39{
45class SFML_API Http : NonCopyable
46{
47public :
48
54 class SFML_API Request
55 {
56 public :
57
61 enum Method
62 {
66 };
67
76 Request(Method RequestMethod = Get, const std::string& URI = "/", const std::string& Body = "");
77
85 void SetField(const std::string& Field, const std::string& Value);
86
94 void SetMethod(Method RequestMethod);
95
103 void SetURI(const std::string& URI);
104
113 void SetHttpVersion(unsigned int Major, unsigned int Minor);
114
123 void SetBody(const std::string& Body);
124
125 private :
126
127 friend class Http;
128
135 std::string ToString() const;
136
145 bool HasField(const std::string& Field) const;
146
148 // Types
150 typedef std::map<std::string, std::string> FieldTable;
151
153 // Member data
155 FieldTable myFields;
156 Method myMethod;
157 std::string myURI;
158 unsigned int myMajorVersion;
159 unsigned int myMinorVersion;
160 std::string myBody;
161 };
162
168 class SFML_API Response
169 {
170 public :
171
177 {
178 // 2xx: success
179 Ok = 200,
180 Created = 201,
181 Accepted = 202,
182 NoContent = 204,
183
184 // 3xx: redirection
189
190 // 4xx: client error
193 Forbidden = 403,
194 NotFound = 404,
195
196 // 5xx: server error
201
202 // 10xx: SFML custom codes
205 };
206
211 Response();
212
221 const std::string& GetField(const std::string& Field) const;
222
229 Status GetStatus() const;
230
237 unsigned int GetMajorHttpVersion() const;
238
245 unsigned int GetMinorHttpVersion() const;
246
257 const std::string& GetBody() const;
258
259 private :
260
261 friend class Http;
262
269 void FromString(const std::string& Data);
270
272 // Types
274 typedef std::map<std::string, std::string> FieldTable;
275
277 // Member data
279 FieldTable myFields;
280 Status myStatus;
281 unsigned int myMajorVersion;
282 unsigned int myMinorVersion;
283 std::string myBody;
284 };
285
290 Http();
291
299 Http(const std::string& Host, unsigned short Port = 0);
300
308 void SetHost(const std::string& Host, unsigned short Port = 0);
309
324 Response SendRequest(const Request& Req, float Timeout = 0.f);
325
326private :
327
329 // Member data
331 SocketTCP myConnection;
332 IPAddress myHost;
333 std::string myHostName;
334 unsigned short myPort;
335};
336
337} // namespace sf
338
339
340#endif // SFML_HTTP_HPP
This class wraps an HTTP request, which is basically :
Definition Http.hpp:55
Method
Enumerate the available HTTP methods for a request.
Definition Http.hpp:62
@ Head
Request a page's header only.
Definition Http.hpp:65
@ Get
Request in get mode, standard method to retrieve a page.
Definition Http.hpp:63
@ Post
Request in post mode, usually to send data to a page.
Definition Http.hpp:64
Request(Method RequestMethod=Get, const std::string &URI="/", const std::string &Body="")
Default constructor.
Definition Http.cpp:56
This class wraps an HTTP response, which is basically :
Definition Http.hpp:169
Response()
Default constructor.
Definition Http.cpp:169
Status
Enumerate all the valid status codes returned in a HTTP response.
Definition Http.hpp:177
@ Ok
Most common code returned when operation was successful.
Definition Http.hpp:179
@ MovedTemporarily
The requested page has temporarily moved to a new location.
Definition Http.hpp:187
@ NotModified
For conditionnal requests, means the requested page hasn't changed and doesn't need to be refreshed.
Definition Http.hpp:188
@ Created
The resource has successfully been created.
Definition Http.hpp:180
@ InvalidResponse
Response is not a valid HTTP one.
Definition Http.hpp:203
@ MovedPermanently
The requested page has permanently moved to a new location.
Definition Http.hpp:186
@ BadRequest
The server couldn't understand the request (syntax error).
Definition Http.hpp:191
@ Forbidden
The requested page cannot be accessed at all, even with authentification.
Definition Http.hpp:193
@ NotImplemented
The server doesn't implement a requested feature.
Definition Http.hpp:198
@ ConnectionFailed
Connection with server failed.
Definition Http.hpp:204
@ BadGateway
The gateway server has received an error from the source server.
Definition Http.hpp:199
@ Unauthorized
The requested page needs an authentification to be accessed.
Definition Http.hpp:192
@ ServiceNotAvailable
The server is temporarily unavailable (overloaded, in maintenance, ...).
Definition Http.hpp:200
@ Accepted
The request has been accepted, but will be processed later by the server.
Definition Http.hpp:181
@ InternalServerError
The server encountered an unexpected error.
Definition Http.hpp:197
@ MultipleChoices
The requested page can be accessed from several locations.
Definition Http.hpp:185
@ NoContent
Sent when the server didn't send any data in return.
Definition Http.hpp:182
@ NotFound
The requested page doesn't exist.
Definition Http.hpp:194
This class provides methods for manipulating the HTTP protocol (described in RFC 1945).
Definition Http.hpp:46
Http()
Default constructor.
Definition Http.cpp:307
IPAddress provides easy manipulation of IP v4 addresses.
Definition IPAddress.hpp:43
SocketTCP wraps a socket using TCP protocol to send data safely (but a bit slower).
Definition SocketTCP.hpp:46
NonCopyable()
The default constructor won't be generated, so provide it.