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 {
65 Head
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
185 MultipleChoices = 300,
186 MovedPermanently = 301,
187 MovedTemporarily = 302,
188 NotModified = 304,
189
190 // 4xx: client error
191 BadRequest = 400,
192 Unauthorized = 401,
193 Forbidden = 403,
194 NotFound = 404,
195
196 // 5xx: server error
197 InternalServerError = 500,
198 NotImplemented = 501,
199 BadGateway = 502,
200 ServiceNotAvailable = 503,
201
202 // 10xx: SFML custom codes
203 InvalidResponse = 1000,
204 ConnectionFailed = 1001
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
@ 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
This class wraps an HTTP response, which is basically :
Definition Http.hpp:169
Status
Enumerate all the valid status codes returned in a HTTP response.
Definition Http.hpp:177
This class provides methods for manipulating the HTTP protocol (described in RFC 1945).
Definition Http.hpp:46
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
Utility base class to easily declare non-copyable classes.