1 ////////////////////////////////////////////////////////////
\r
3 // SFML - Simple and Fast Multimedia Library
\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
\r
6 // This software is provided 'as-is', without any express or implied warranty.
\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.
\r
9 // Permission is granted to anyone to use this software for any purpose,
\r
10 // including commercial applications, and to alter it and redistribute it freely,
\r
11 // subject to the following restrictions:
\r
13 // 1. The origin of this software must not be misrepresented;
\r
14 // you must not claim that you wrote the original software.
\r
15 // If you use this software in a product, an acknowledgment
\r
16 // in the product documentation would be appreciated but is not required.
\r
18 // 2. Altered source versions must be plainly marked as such,
\r
19 // and must not be misrepresented as being the original software.
\r
21 // 3. This notice may not be removed or altered from any source distribution.
\r
23 ////////////////////////////////////////////////////////////
\r
25 #ifndef SFML_HTTP_HPP
\r
26 #define SFML_HTTP_HPP
\r
28 ////////////////////////////////////////////////////////////
\r
30 ////////////////////////////////////////////////////////////
\r
31 #include <SFML/System/NonCopyable.hpp>
\r
32 #include <SFML/Network/IPAddress.hpp>
\r
33 #include <SFML/Network/SocketTCP.hpp>
\r
40 ////////////////////////////////////////////////////////////
\r
41 /// This class provides methods for manipulating the HTTP
\r
42 /// protocol (described in RFC 1945).
\r
43 /// It can connect to a website, get its files, send requests, etc.
\r
44 ////////////////////////////////////////////////////////////
\r
45 class SFML_API Http : NonCopyable
\r
49 ////////////////////////////////////////////////////////////
\r
50 /// This class wraps an HTTP request, which is basically :
\r
51 /// - a header with a method, a target URI, and a set of field/value pairs
\r
52 /// - an optional body (for POST requests)
\r
53 ////////////////////////////////////////////////////////////
\r
54 class SFML_API Request
\r
58 ////////////////////////////////////////////////////////////
\r
59 /// Enumerate the available HTTP methods for a request
\r
60 ////////////////////////////////////////////////////////////
\r
63 Get, ///< Request in get mode, standard method to retrieve a page
\r
64 Post, ///< Request in post mode, usually to send data to a page
\r
65 Head ///< Request a page's header only
\r
68 ////////////////////////////////////////////////////////////
\r
69 /// Default constructor
\r
71 /// \param RequestMethod : Method to use for the request (Get by default)
\r
72 /// \param URI : Target URI ("/" by default -- index page)
\r
73 /// \param Body : Content of the request's body (empty by default)
\r
75 ////////////////////////////////////////////////////////////
\r
76 Request(Method RequestMethod = Get, const std::string& URI = "/", const std::string& Body = "");
\r
78 ////////////////////////////////////////////////////////////
\r
79 /// Set the value of a field; the field is added if it doesn't exist
\r
81 /// \param Field : Name of the field to set (case-insensitive)
\r
82 /// \param Value : Value of the field
\r
84 ////////////////////////////////////////////////////////////
\r
85 void SetField(const std::string& Field, const std::string& Value);
\r
87 ////////////////////////////////////////////////////////////
\r
88 /// Set the request method.
\r
89 /// This parameter is Http::Request::Get by default
\r
91 /// \param RequestMethod : Method to use for the request
\r
93 ////////////////////////////////////////////////////////////
\r
94 void SetMethod(Method RequestMethod);
\r
96 ////////////////////////////////////////////////////////////
\r
97 /// Set the target URI of the request.
\r
98 /// This parameter is "/" by default
\r
100 /// \param URI : URI to request, local to the host
\r
102 ////////////////////////////////////////////////////////////
\r
103 void SetURI(const std::string& URI);
\r
105 ////////////////////////////////////////////////////////////
\r
106 /// Set the HTTP version of the request.
\r
107 /// This parameter is 1.0 by default
\r
109 /// \param Major : Major version number
\r
110 /// \param Minor : Minor version number
\r
112 ////////////////////////////////////////////////////////////
\r
113 void SetHttpVersion(unsigned int Major, unsigned int Minor);
\r
115 ////////////////////////////////////////////////////////////
\r
116 /// Set the body of the request. This parameter is optional and
\r
117 /// makes sense only for POST requests.
\r
118 /// This parameter is empty by default
\r
120 /// \param Body : Content of the request body
\r
122 ////////////////////////////////////////////////////////////
\r
123 void SetBody(const std::string& Body);
\r
129 ////////////////////////////////////////////////////////////
\r
130 /// Get the string representation of the request header
\r
132 /// \return String containing the request
\r
134 ////////////////////////////////////////////////////////////
\r
135 std::string ToString() const;
\r
137 ////////////////////////////////////////////////////////////
\r
138 /// Check if the given field has been defined
\r
140 /// \param Field : Name of the field to check (case-insensitive)
\r
142 /// \return True if the field exists
\r
144 ////////////////////////////////////////////////////////////
\r
145 bool HasField(const std::string& Field) const;
\r
147 ////////////////////////////////////////////////////////////
\r
149 ////////////////////////////////////////////////////////////
\r
150 typedef std::map<std::string, std::string> FieldTable;
\r
152 ////////////////////////////////////////////////////////////
\r
154 ////////////////////////////////////////////////////////////
\r
155 FieldTable myFields; ///< Fields of the header
\r
156 Method myMethod; ///< Method to use for the request
\r
157 std::string myURI; ///< Target URI of the request
\r
158 unsigned int myMajorVersion; ///< Major HTTP version
\r
159 unsigned int myMinorVersion; ///< Minor HTTP version
\r
160 std::string myBody; ///< Body of the request
\r
163 ////////////////////////////////////////////////////////////
\r
164 /// This class wraps an HTTP response, which is basically :
\r
165 /// - a header with a status code and a set of field/value pairs
\r
166 /// - a body (the content of the requested resource)
\r
167 ////////////////////////////////////////////////////////////
\r
168 class SFML_API Response
\r
172 ////////////////////////////////////////////////////////////
\r
173 /// Enumerate all the valid status codes returned in
\r
174 /// a HTTP response
\r
175 ////////////////////////////////////////////////////////////
\r
179 Ok = 200, ///< Most common code returned when operation was successful
\r
180 Created = 201, ///< The resource has successfully been created
\r
181 Accepted = 202, ///< The request has been accepted, but will be processed later by the server
\r
182 NoContent = 204, ///< Sent when the server didn't send any data in return
\r
184 // 3xx: redirection
\r
185 MultipleChoices = 300, ///< The requested page can be accessed from several locations
\r
186 MovedPermanently = 301, ///< The requested page has permanently moved to a new location
\r
187 MovedTemporarily = 302, ///< The requested page has temporarily moved to a new location
\r
188 NotModified = 304, ///< For conditionnal requests, means the requested page hasn't changed and doesn't need to be refreshed
\r
190 // 4xx: client error
\r
191 BadRequest = 400, ///< The server couldn't understand the request (syntax error)
\r
192 Unauthorized = 401, ///< The requested page needs an authentification to be accessed
\r
193 Forbidden = 403, ///< The requested page cannot be accessed at all, even with authentification
\r
194 NotFound = 404, ///< The requested page doesn't exist
\r
196 // 5xx: server error
\r
197 InternalServerError = 500, ///< The server encountered an unexpected error
\r
198 NotImplemented = 501, ///< The server doesn't implement a requested feature
\r
199 BadGateway = 502, ///< The gateway server has received an error from the source server
\r
200 ServiceNotAvailable = 503, ///< The server is temporarily unavailable (overloaded, in maintenance, ...)
\r
202 // 10xx: SFML custom codes
\r
203 InvalidResponse = 1000, ///< Response is not a valid HTTP one
\r
204 ConnectionFailed = 1001 ///< Connection with server failed
\r
207 ////////////////////////////////////////////////////////////
\r
208 /// Default constructor
\r
210 ////////////////////////////////////////////////////////////
\r
213 ////////////////////////////////////////////////////////////
\r
214 /// Get the value of a field
\r
216 /// \param Field : Name of the field to get (case-insensitive)
\r
218 /// \return Value of the field, or empty string if not found
\r
220 ////////////////////////////////////////////////////////////
\r
221 const std::string& GetField(const std::string& Field) const;
\r
223 ////////////////////////////////////////////////////////////
\r
224 /// Get the header's status code
\r
226 /// \return Header's status code
\r
228 ////////////////////////////////////////////////////////////
\r
229 Status GetStatus() const;
\r
231 ////////////////////////////////////////////////////////////
\r
232 /// Get the major HTTP version number of the response
\r
234 /// \return Major version number
\r
236 ////////////////////////////////////////////////////////////
\r
237 unsigned int GetMajorHttpVersion() const;
\r
239 ////////////////////////////////////////////////////////////
\r
240 /// Get the major HTTP version number of the response
\r
242 /// \return Major version number
\r
244 ////////////////////////////////////////////////////////////
\r
245 unsigned int GetMinorHttpVersion() const;
\r
247 ////////////////////////////////////////////////////////////
\r
248 /// Get the body of the response. The body can contain :
\r
249 /// - the requested page (for GET requests)
\r
250 /// - a response from the server (for POST requests)
\r
251 /// - nothing (for HEAD requests)
\r
252 /// - an error message (in case of an error)
\r
254 /// \return The response body
\r
256 ////////////////////////////////////////////////////////////
\r
257 const std::string& GetBody() const;
\r
263 ////////////////////////////////////////////////////////////
\r
264 /// Construct the header from a response string
\r
266 /// \param Data : Content of the response's header to parse
\r
268 ////////////////////////////////////////////////////////////
\r
269 void FromString(const std::string& Data);
\r
271 ////////////////////////////////////////////////////////////
\r
273 ////////////////////////////////////////////////////////////
\r
274 typedef std::map<std::string, std::string> FieldTable;
\r
276 ////////////////////////////////////////////////////////////
\r
278 ////////////////////////////////////////////////////////////
\r
279 FieldTable myFields; ///< Fields of the header
\r
280 Status myStatus; ///< Status code
\r
281 unsigned int myMajorVersion; ///< Major HTTP version
\r
282 unsigned int myMinorVersion; ///< Minor HTTP version
\r
283 std::string myBody; ///< Body of the response
\r
286 ////////////////////////////////////////////////////////////
\r
287 /// Default constructor
\r
289 ////////////////////////////////////////////////////////////
\r
292 ////////////////////////////////////////////////////////////
\r
293 /// Construct the Http instance with the target host
\r
295 /// \param Host : Web server to connect to
\r
296 /// \param Port : Port to use for connection (0 by default -- use the standard port of the protocol used)
\r
298 ////////////////////////////////////////////////////////////
\r
299 Http(const std::string& Host, unsigned short Port = 0);
\r
301 ////////////////////////////////////////////////////////////
\r
302 /// Set the target host
\r
304 /// \param Host : Web server to connect to
\r
305 /// \param Port : Port to use for connection (0 by default -- use the standard port of the protocol used)
\r
307 ////////////////////////////////////////////////////////////
\r
308 void SetHost(const std::string& Host, unsigned short Port = 0);
\r
310 ////////////////////////////////////////////////////////////
\r
311 /// Send a HTTP request and return the server's response.
\r
312 /// You must be connected to a host before sending requests.
\r
313 /// Any missing mandatory header field will be added with an appropriate value.
\r
314 /// Warning : this function waits for the server's response and may
\r
315 /// not return instantly; use a thread if you don't want to block your
\r
318 /// \param Req : Request to send
\r
319 /// \param Timeout : Maximum time to wait, in seconds (0 by default, means no timeout)
\r
321 /// \return Server's response
\r
323 ////////////////////////////////////////////////////////////
\r
324 Response SendRequest(const Request& Req, float Timeout = 0.f);
\r
328 ////////////////////////////////////////////////////////////
\r
330 ////////////////////////////////////////////////////////////
\r
331 SocketTCP myConnection; ///< Connection to the host
\r
332 IPAddress myHost; ///< Web host address
\r
333 std::string myHostName; ///< Web host name
\r
334 unsigned short myPort; ///< Port used for connection with host
\r
340 #endif // SFML_HTTP_HPP
\r