SFML 3.0.0

- Main Page
- Topics
- Namespaces
- Classes
- Files

- File List
- File Members

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-2024 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::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::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::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:50
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
Definition AudioResource.hpp:36