SFML 3.0.2
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-2025 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#pragma once
26
28// Headers
30#include <SFML/Network/Export.hpp>
31
32#include <SFML/Network/IpAddress.hpp>
33#include <SFML/Network/TcpSocket.hpp>
34
35#include <SFML/System/Time.hpp>
36
37#include <iosfwd>
38#include <map>
39#include <optional>
40#include <string>
41
42
43 namespace sf
44{
49 class SFML_NETWORK_API Http
50{
51 public:
56 class SFML_NETWORK_API Request
57 {
58 public:
63 enum class Method
64 {
65 Get,
66 Post,
67 Head,
68 Put,
69 Delete
70 };
71
83 Request(const std::string& uri = "/", Method method = Method::Get, const std::string& body = "");
84
98 void setField(const std::string& field, const std::string& value);
99
110 void setMethod(Method method);
111
122 void setUri(const std::string& uri);
123
133 void setHttpVersion(unsigned int major, unsigned int minor);
134
145 void setBody(const std::string& body);
146
147 private:
148 friend class Http;
149
159 [[nodiscard]] std::string prepare() const;
160
171 [[nodiscard]] bool hasField(const std::string& field) const;
172
174 // Types
176 using FieldTable = std::map<std::string, std::string>; // Use an ordered map for predictable payloads
177
179 // Member data
181 FieldTable m_fields;
182 Method m_method;
183 std::string m_uri;
184 unsigned int m_majorVersion{1};
185 unsigned int m_minorVersion{};
186 std::string m_body;
187 };
188
193 class SFML_NETWORK_API Response
194 {
195 public:
200 enum class Status
201 {
202 // 2xx: success
203 Ok = 200,
204 Created = 201,
205 Accepted = 202,
206 NoContent = 204,
207 ResetContent = 205,
208 PartialContent = 206,
209
210 // 3xx: redirection
211 MultipleChoices = 300,
212 MovedPermanently = 301,
213 MovedTemporarily = 302,
214 NotModified = 304,
215
216 // 4xx: client error
217 BadRequest = 400,
218 Unauthorized = 401,
219 Forbidden = 403,
220 NotFound = 404,
221 RangeNotSatisfiable = 407,
222
223 // 5xx: server error
224 InternalServerError = 500,
225 NotImplemented = 501,
226 BadGateway = 502,
227 ServiceNotAvailable = 503,
228 GatewayTimeout = 504,
229 VersionNotSupported = 505,
230
231 // 10xx: SFML custom codes
232 InvalidResponse = 1000,
233 ConnectionFailed = 1001
234 };
235
248 [[nodiscard]] const std::string& getField(const std::string& field) const;
249
261 [[nodiscard]] Status getStatus() const;
262
271 [[nodiscard]] unsigned int getMajorHttpVersion() const;
272
281 [[nodiscard]] unsigned int getMinorHttpVersion() const;
282
295 [[nodiscard]] const std::string& getBody() const;
296
297 private:
298 friend class Http;
299
309 void parse(const std::string& data);
310
311
321 void parseFields(std::istream& in);
322
324 // Types
326 using FieldTable = std::map<std::string, std::string>; // Use an ordered map for predictable payloads
327
329 // Member data
331 FieldTable m_fields;
332 Status m_status{Status::ConnectionFailed};
333 unsigned int m_majorVersion{};
334 unsigned int m_minorVersion{};
335 std::string m_body;
336 };
337
342 Http() = default;
343
358 Http(const std::string& host, unsigned short port = 0);
359
364 Http(const Http&) = delete;
365
370 Http& operator=(const Http&) = delete;
371
387 void setHost(const std::string& host, unsigned short port = 0);
388
407 [[nodiscard]] Response sendRequest(const Request& request, Time timeout = Time::Zero);
408
409 private:
411 // Member data
413 TcpSocket m_connection;
414 std::optional<IpAddress> m_host;
415 std::string m_hostName;
416 unsigned short m_port{};
417};
418
419} // namespace sf
420
421
IpAddress.hpp
Export.hpp
SFML_NETWORK_API
#define SFML_NETWORK_API
Definition Network/Export.hpp:42
TcpSocket.hpp
Time.hpp
sf::Http::Request
HTTP request.
Definition Http.hpp:57
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:64
sf::Http::Request::Method::Post
@ Post
Request in post mode, usually to send data to a page.
Definition Http.hpp:66
sf::Http::Request::Method::Head
@ Head
Request a page's header only.
Definition Http.hpp:67
sf::Http::Request::Method::Get
@ Get
Request in get mode, standard method to retrieve a page.
Definition Http.hpp:65
sf::Http::Request::Method::Put
@ Put
Request in put mode, useful for a REST API.
Definition Http.hpp:68
sf::Http::Request::Method::Delete
@ Delete
Request in delete mode, useful for a REST API.
Definition Http.hpp:69
sf::Http::Request::setHttpVersion
void setHttpVersion(unsigned int major, unsigned int minor)
Set the HTTP version for the request.
sf::Http::Request::Http
friend class Http
Definition Http.hpp:148
sf::Http::Request::setMethod
void setMethod(Method method)
Set the request method.
sf::Http::Request::Request
Request(const std::string &uri="/", Method method=Method::Get, const std::string &body="")
Default constructor.
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
HTTP response.
Definition Http.hpp:194
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:201
sf::Http::Response::Status::BadGateway
@ BadGateway
The gateway server has received an error from the source server.
Definition Http.hpp:226
sf::Http::Response::Status::Created
@ Created
The resource has successfully been created.
Definition Http.hpp:204
sf::Http::Response::Status::ServiceNotAvailable
@ ServiceNotAvailable
The server is temporarily unavailable (overloaded, in maintenance, ...)
Definition Http.hpp:227
sf::Http::Response::Status::MovedPermanently
@ MovedPermanently
The requested page has permanently moved to a new location.
Definition Http.hpp:212
sf::Http::Response::Status::InvalidResponse
@ InvalidResponse
Response is not a valid HTTP one.
Definition Http.hpp:232
sf::Http::Response::Status::Accepted
@ Accepted
The request has been accepted, but will be processed later by the server.
Definition Http.hpp:205
sf::Http::Response::Status::NotFound
@ NotFound
The requested page doesn't exist.
Definition Http.hpp:220
sf::Http::Response::Status::ResetContent
@ ResetContent
The server informs the client that it should clear the view (form) that caused the request to be sent...
Definition Http.hpp:207
sf::Http::Response::Status::Forbidden
@ Forbidden
The requested page cannot be accessed at all, even with authentication.
Definition Http.hpp:219
sf::Http::Response::Status::PartialContent
@ PartialContent
The server has sent a part of the resource, as a response to a partial GET request.
Definition Http.hpp:208
sf::Http::Response::Status::RangeNotSatisfiable
@ RangeNotSatisfiable
The server can't satisfy the partial GET request (with a "Range" header field)
Definition Http.hpp:221
sf::Http::Response::Status::NotImplemented
@ NotImplemented
The server doesn't implement a requested feature.
Definition Http.hpp:225
sf::Http::Response::Status::BadRequest
@ BadRequest
The server couldn't understand the request (syntax error)
Definition Http.hpp:217
sf::Http::Response::Status::MovedTemporarily
@ MovedTemporarily
The requested page has temporarily moved to a new location.
Definition Http.hpp:213
sf::Http::Response::Status::MultipleChoices
@ MultipleChoices
The requested page can be accessed from several locations.
Definition Http.hpp:211
sf::Http::Response::Status::Ok
@ Ok
Most common code returned when operation was successful.
Definition Http.hpp:203
sf::Http::Response::Status::ConnectionFailed
@ ConnectionFailed
Connection with server failed.
Definition Http.hpp:233
sf::Http::Response::Status::NotModified
@ NotModified
For conditional requests, means the requested page hasn't changed and doesn't need to be refreshed.
Definition Http.hpp:214
sf::Http::Response::Status::NoContent
@ NoContent
The server didn't send any data in return.
Definition Http.hpp:206
sf::Http::Response::Status::VersionNotSupported
@ VersionNotSupported
The server doesn't support the requested HTTP version.
Definition Http.hpp:229
sf::Http::Response::Status::GatewayTimeout
@ GatewayTimeout
The gateway server couldn't receive a response from the source server.
Definition Http.hpp:228
sf::Http::Response::Status::Unauthorized
@ Unauthorized
The requested page needs an authentication to be accessed.
Definition Http.hpp:218
sf::Http::Response::Status::InternalServerError
@ InternalServerError
The server encountered an unexpected error.
Definition Http.hpp:224
sf::Http::Response::getMajorHttpVersion
unsigned int getMajorHttpVersion() const
Get the major HTTP version number of the response.
sf::Http::Response::Http
friend class Http
Definition Http.hpp:298
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::Http
Http(const Http &)=delete
Deleted copy constructor.
sf::Http::setHost
void setHost(const std::string &host, unsigned short port=0)
Set the target host.
sf::Http::operator=
Http & operator=(const Http &)=delete
Deleted copy assignment.
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
Default constructor.
sf::TcpSocket
Specialized socket using the TCP protocol.
Definition TcpSocket.hpp:54
sf::Time
Represents a time value.
Definition Time.hpp:42
sf::Time::Zero
static const Time Zero
Predefined "zero" time value.
Definition Time.hpp:110
sf
Definition AudioResource.hpp:36