Loading...
Searching...
No Matches
Http.hpp
Go to the documentation of this file.
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
35#include <SFML/System/Time.hpp>
36#include <map>
37#include <string>
38
39
40namespace sf
41{
47{
48public :
49
55 {
56 public :
57
62 enum Method
63 {
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
192 {
193 public :
194
200 {
201 // 2xx: success
202 Ok = 200,
203 Created = 201,
204 Accepted = 202,
205 NoContent = 204,
208
209 // 3xx: redirection
214
215 // 4xx: client error
218 Forbidden = 403,
219 NotFound = 404,
221
222 // 5xx: server error
229
230 // 10xx: SFML custom codes
233 };
234
242
255 const std::string& getField(const std::string& field) const;
256
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
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
393private :
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
#define SFML_NETWORK_API
Define a HTTP request.
Definition Http.hpp:55
void setUri(const std::string &uri)
Set the requested URI.
Method
Enumerate the available HTTP methods for a request.
Definition Http.hpp:63
@ Head
Request a page's header only.
Definition Http.hpp:66
@ Get
Request in get mode, standard method to retrieve a page.
Definition Http.hpp:64
@ Post
Request in post mode, usually to send data to a page.
Definition Http.hpp:65
Request(const std::string &uri="/", Method method=Get, const std::string &body="")
Default constructor.
void setHttpVersion(unsigned int major, unsigned int minor)
Set the HTTP version for the request.
friend class Http
Definition Http.hpp:146
void setMethod(Method method)
Set the request method.
void setBody(const std::string &body)
Set the body of the request.
void setField(const std::string &field, const std::string &value)
Set the value of a field.
Define a HTTP response.
Definition Http.hpp:192
Response()
Default constructor.
Status getStatus() const
Get the response status code.
Status
Enumerate all the valid status codes for a response.
Definition Http.hpp:200
@ Ok
Most common code returned when operation was successful.
Definition Http.hpp:202
@ MovedTemporarily
The requested page has temporarily moved to a new location.
Definition Http.hpp:212
@ NotModified
For conditionnal requests, means the requested page hasn't changed and doesn't need to be refreshed.
Definition Http.hpp:213
@ Created
The resource has successfully been created.
Definition Http.hpp:203
@ InvalidResponse
Response is not a valid HTTP one.
Definition Http.hpp:231
@ PartialContent
The server has sent a part of the resource, as a response to a partial GET request.
Definition Http.hpp:207
@ RangeNotSatisfiable
The server can't satisfy the partial GET request (with a "Range" header field).
Definition Http.hpp:220
@ GatewayTimeout
The gateway server couldn't receive a response from the source server.
Definition Http.hpp:227
@ MovedPermanently
The requested page has permanently moved to a new location.
Definition Http.hpp:211
@ BadRequest
The server couldn't understand the request (syntax error).
Definition Http.hpp:216
@ Forbidden
The requested page cannot be accessed at all, even with authentification.
Definition Http.hpp:218
@ NotImplemented
The server doesn't implement a requested feature.
Definition Http.hpp:224
@ ResetContent
The server informs the client that it should clear the view (form) that caused the request to be sent...
Definition Http.hpp:206
@ ConnectionFailed
Connection with server failed.
Definition Http.hpp:232
@ BadGateway
The gateway server has received an error from the source server.
Definition Http.hpp:225
@ Unauthorized
The requested page needs an authentification to be accessed.
Definition Http.hpp:217
@ ServiceNotAvailable
The server is temporarily unavailable (overloaded, in maintenance, ...).
Definition Http.hpp:226
@ Accepted
The request has been accepted, but will be processed later by the server.
Definition Http.hpp:204
@ InternalServerError
The server encountered an unexpected error.
Definition Http.hpp:223
@ MultipleChoices
The requested page can be accessed from several locations.
Definition Http.hpp:210
@ VersionNotSupported
The server doesn't support the requested HTTP version.
Definition Http.hpp:228
@ NoContent
The server didn't send any data in return.
Definition Http.hpp:205
@ NotFound
The requested page doesn't exist.
Definition Http.hpp:219
unsigned int getMajorHttpVersion() const
Get the major HTTP version number of the response.
friend class Http
Definition Http.hpp:306
const std::string & getBody() const
Get the body of the response.
const std::string & getField(const std::string &field) const
Get the value of a field.
unsigned int getMinorHttpVersion() const
Get the minor HTTP version number of the response.
void setHost(const std::string &host, unsigned short port=0)
Set the target host.
Http(const std::string &host, unsigned short port=0)
Construct the HTTP client with the target host.
Response sendRequest(const Request &request, Time timeout=Time::Zero)
Send a HTTP request and return the server's response.
Http()
Default constructor.
Encapsulate an IPv4 network address.
Definition IpAddress.hpp:45
NonCopyable()
Default constructor.
Specialized socket using the TCP protocol.
Definition TcpSocket.hpp:47
Represents a time value.
Definition Time.hpp:41
static const Time Zero
Predefined "zero" time value.
Definition Time.hpp:85