SFML - Simple and Fast Multimedia Library

SFML 3.1.0
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-2026 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#include <vector>
42
43
44namespace sf
45{
50 class SFML_NETWORK_API Http
51{
52public:
57 class SFML_NETWORK_API Request
58 {
59 public:
64 enum class Method
65 {
66 Get,
67 Post,
68 Head,
69 Put,
70 Delete
71 };
72
84 Request(const std::string& uri = "/", Method method = Method::Get, const std::string& body = "");
85
99 void setField(const std::string& field, const std::string& value);
100
111 void setMethod(Method method);
112
123 void setUri(const std::string& uri);
124
134 void setHttpVersion(unsigned int major, unsigned int minor);
135
146 void setBody(const std::string& body);
147
148 private:
149 friend class Http;
150
160 [[nodiscard]] std::string prepare() const;
161
172 [[nodiscard]] bool hasField(const std::string& field) const;
173
175 // Types
177 using FieldTable = std::map<std::string, std::string>; // Use an ordered map for predictable payloads
178
180 // Member data
182 FieldTable m_fields;
183 Method m_method;
184 std::string m_uri;
185 unsigned int m_majorVersion{1};
186 unsigned int m_minorVersion{};
187 std::string m_body;
188 };
189
194 class SFML_NETWORK_API Response
195 {
196 public:
201 enum class 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
249 [[nodiscard]] const std::string& getField(const std::string& field) const;
250
262 [[nodiscard]] Status getStatus() const;
263
272 [[nodiscard]] unsigned int getMajorHttpVersion() const;
273
282 [[nodiscard]] unsigned int getMinorHttpVersion() const;
283
296 [[nodiscard]] const std::string& getBody() const;
297
298 private:
299 friend class Http;
300
310 void parse(const std::string& data);
311
312
322 void parseFields(std::istream& in);
323
325 // Types
327 using FieldTable = std::map<std::string, std::string>; // Use an ordered map for predictable payloads
328
330 // Member data
332 FieldTable m_fields;
333 Status m_status{Status::ConnectionFailed};
334 unsigned int m_majorVersion{};
335 unsigned int m_minorVersion{};
336 std::string m_body;
337 };
338
343 Http() = default;
344
360 Http(const std::string& host, unsigned short port = 0, std::optional<IpAddress::Type> addressType = std::nullopt);
361
366 Http(const Http&) = delete;
367
372 Http& operator=(const Http&) = delete;
373
393 bool setHost(const std::string& host, unsigned short port = 0, std::optional<IpAddress::Type> addressType = std::nullopt);
394
414 [[nodiscard]] Response sendRequest(const Request& request, Time timeout = Time::Zero, bool verifyServer = true) const;
415
416private:
418 // Member data
420 std::vector<IpAddress> m_hosts;
421 std::optional<IpAddress::Type> m_addressType;
422 std::string m_hostName;
423 unsigned short m_port{};
424 bool m_https{};
425};
426
427} // namespace sf
428
429
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:58
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:65
sf::Http::Request::Method::Get
@ Get
Request in get mode, standard method to retrieve a page.
Definition Http.hpp:66
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:149
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:195
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::Status::ConnectionFailed
@ ConnectionFailed
Connection with server failed.
Definition Http.hpp:234
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:299
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::setHost
bool setHost(const std::string &host, unsigned short port=0, std::optional< IpAddress::Type > addressType=std::nullopt)
Set the target host.
sf::Http::Http
Http(const Http &)=delete
Deleted copy constructor.
sf::Http::sendRequest
Response sendRequest(const Request &request, Time timeout=Time::Zero, bool verifyServer=true) const
Send a HTTP request and return the server's response.
sf::Http::operator=
Http & operator=(const Http &)=delete
Deleted copy assignment.
sf::Http::Http
Http(const std::string &host, unsigned short port=0, std::optional< IpAddress::Type > addressType=std::nullopt)
Construct the HTTP client with the target host.
sf::Http::Http
Http()=default
Default constructor.
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