SFML 2.5.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

Http.hpp
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2018 Laurent Gomila (laurent@sfml-dev.org)
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 Put,
68 Delete
69 };
70
82 Request(const std::string& uri = "/", Method method = Get, const std::string& body = "");
83
97 void setField(const std::string& field, const std::string& value);
98
109 void setMethod(Method method);
110
121 void setUri(const std::string& uri);
122
132 void setHttpVersion(unsigned int major, unsigned int minor);
133
144 void setBody(const std::string& body);
145
146 private:
147
148 friend class Http;
149
159 std::string prepare() const;
160
171 bool hasField(const std::string& field) const;
172
174 // Types
176 typedef std::map<std::string, std::string> FieldTable;
177
179 // Member data
181 FieldTable m_fields;
182 Method m_method;
183 std::string m_uri;
184 unsigned int m_majorVersion;
185 unsigned int m_minorVersion;
186 std::string m_body;
187 };
188
193 class SFML_NETWORK_API Response
194 {
195 public:
196
201 enum Status
202 {
203 // 2xx: success
204 Ok = 200,
205 Created = 201,
206 Accepted = 202,
207 NoContent = 204,
208 ResetContent = 205,
209 PartialContent = 206,
210
211 // 3xx: redirection
212 MultipleChoices = 300,
213 MovedPermanently = 301,
214 MovedTemporarily = 302,
215 NotModified = 304,
216
217 // 4xx: client error
218 BadRequest = 400,
219 Unauthorized = 401,
220 Forbidden = 403,
221 NotFound = 404,
222 RangeNotSatisfiable = 407,
223
224 // 5xx: server error
225 InternalServerError = 500,
226 NotImplemented = 501,
227 BadGateway = 502,
228 ServiceNotAvailable = 503,
229 GatewayTimeout = 504,
230 VersionNotSupported = 505,
231
232 // 10xx: SFML custom codes
233 InvalidResponse = 1000,
234 ConnectionFailed = 1001
235 };
236
243 Response();
244
257 const std::string& getField(const std::string& field) const;
258
270 Status getStatus() const;
271
280 unsigned int getMajorHttpVersion() const;
281
290 unsigned int getMinorHttpVersion() const;
291
304 const std::string& getBody() const;
305
306 private:
307
308 friend class Http;
309
319 void parse(const std::string& data);
320
321
331 void parseFields(std::istream &in);
332
334 // Types
336 typedef std::map<std::string, std::string> FieldTable;
337
339 // Member data
341 FieldTable m_fields;
342 Status m_status;
343 unsigned int m_majorVersion;
344 unsigned int m_minorVersion;
345 std::string m_body;
346 };
347
352 Http();
353
368 Http(const std::string& host, unsigned short port = 0);
369
385 void setHost(const std::string& host, unsigned short port = 0);
386
405 Response sendRequest(const Request& request, Time timeout = Time::Zero);
406
407 private:
408
410 // Member data
412 TcpSocket m_connection;
413 IpAddress m_host;
414 std::string m_hostName;
415 unsigned short m_port;
416};
417
418} // namespace sf
419
420
421#endif // SFML_HTTP_HPP
422
423
sf::Http::Request
Define a HTTP request.
Definition: Http.hpp:55
sf::Http::Request::setUri
void setUri(const std::string &uri)
Set the requested URI.
sf::Http::Request::Method
Method
Enumerate the available HTTP methods for a request.
Definition: Http.hpp:63
sf::Http::Request::Head
@ Head
Request a page's header only.
Definition: Http.hpp:66
sf::Http::Request::Put
@ Put
Request in put mode, useful for a REST API.
Definition: Http.hpp:67
sf::Http::Request::Get
@ Get
Request in get mode, standard method to retrieve a page.
Definition: Http.hpp:64
sf::Http::Request::Post
@ Post
Request in post mode, usually to send data to a page.
Definition: Http.hpp:65
sf::Http::Request::Request
Request(const std::string &uri="/", Method method=Get, const std::string &body="")
Default constructor.
sf::Http::Request::setHttpVersion
void setHttpVersion(unsigned int major, unsigned int minor)
Set the HTTP version for the request.
sf::Http::Request::setMethod
void setMethod(Method method)
Set the request method.
sf::Http::Request::setBody
void setBody(const std::string &body)
Set the body of the request.
sf::Http::Request::setField
void setField(const std::string &field, const std::string &value)
Set the value of a field.
sf::Http::Response
Define a HTTP response.
Definition: Http.hpp:194
sf::Http::Response::Response
Response()
Default constructor.
sf::Http::Response::getStatus
Status getStatus() const
Get the response status code.
sf::Http::Response::Status
Status
Enumerate all the valid status codes for a response.
Definition: Http.hpp:202
sf::Http::Response::getMajorHttpVersion
unsigned int getMajorHttpVersion() const
Get the major HTTP version number of the response.
sf::Http::Response::getBody
const std::string & getBody() const
Get the body of the response.
sf::Http::Response::getField
const std::string & getField(const std::string &field) const
Get the value of a field.
sf::Http::Response::getMinorHttpVersion
unsigned int getMinorHttpVersion() const
Get the minor HTTP version number of the response.
sf::Http
A HTTP client.
Definition: Http.hpp:47
sf::Http::setHost
void setHost(const std::string &host, unsigned short port=0)
Set the target host.
sf::Http::Http
Http(const std::string &host, unsigned short port=0)
Construct the HTTP client with the target host.
sf::Http::sendRequest
Response sendRequest(const Request &request, Time timeout=Time::Zero)
Send a HTTP request and return the server's response.
sf::Http::Http
Http()
Default constructor.
sf::IpAddress
Encapsulate an IPv4 network address.
Definition: IpAddress.hpp:45
sf::NonCopyable
Utility class that makes any derived class non-copyable.
Definition: NonCopyable.hpp:42
sf::TcpSocket
Specialized socket using the TCP protocol.
Definition: TcpSocket.hpp:47
sf::Time
Represents a time value.
Definition: Time.hpp:41
sf::Time::Zero
static const Time Zero
Predefined "zero" time value.
Definition: Time.hpp:85