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
31
34
35#include <SFML/System/Time.hpp>
36
37#include <iosfwd>
38#include <map>
39#include <optional>
40#include <string>
41
42
43namespace sf
44{
50{
51public:
57 {
58 public:
63 enum class Method
64 {
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
194 {
195 public:
200 enum class Status
201 {
202 // 2xx: success
203 Ok = 200,
204 Created = 201,
205 Accepted = 202,
206 NoContent = 204,
209
210 // 3xx: redirection
215
216 // 4xx: client error
219 Forbidden = 403,
220 NotFound = 404,
222
223 // 5xx: server error
230
231 // 10xx: SFML custom codes
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;
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
409private:
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
#define SFML_NETWORK_API
HTTP request.
Definition Http.hpp:57
void setUri(const std::string &uri)
Set the requested URI.
Method
Enumerate the available HTTP methods for a request.
Definition Http.hpp:64
@ Post
Request in post mode, usually to send data to a page.
Definition Http.hpp:66
@ Head
Request a page's header only.
Definition Http.hpp:67
@ Get
Request in get mode, standard method to retrieve a page.
Definition Http.hpp:65
@ Put
Request in put mode, useful for a REST API.
Definition Http.hpp:68
@ Delete
Request in delete mode, useful for a REST API.
Definition Http.hpp:69
void setHttpVersion(unsigned int major, unsigned int minor)
Set the HTTP version for the request.
friend class Http
Definition Http.hpp:148
void setMethod(Method method)
Set the request method.
Request(const std::string &uri="/", Method method=Method::Get, const std::string &body="")
Default constructor.
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.
HTTP response.
Definition Http.hpp:194
Status getStatus() const
Get the response status code.
Status
Enumerate all the valid status codes for a response.
Definition Http.hpp:201
@ BadGateway
The gateway server has received an error from the source server.
Definition Http.hpp:226
@ Created
The resource has successfully been created.
Definition Http.hpp:204
@ ServiceNotAvailable
The server is temporarily unavailable (overloaded, in maintenance, ...)
Definition Http.hpp:227
@ MovedPermanently
The requested page has permanently moved to a new location.
Definition Http.hpp:212
@ InvalidResponse
Response is not a valid HTTP one.
Definition Http.hpp:232
@ Accepted
The request has been accepted, but will be processed later by the server.
Definition Http.hpp:205
@ NotFound
The requested page doesn't exist.
Definition Http.hpp:220
@ ResetContent
The server informs the client that it should clear the view (form) that caused the request to be sent...
Definition Http.hpp:207
@ Forbidden
The requested page cannot be accessed at all, even with authentication.
Definition Http.hpp:219
@ PartialContent
The server has sent a part of the resource, as a response to a partial GET request.
Definition Http.hpp:208
@ RangeNotSatisfiable
The server can't satisfy the partial GET request (with a "Range" header field)
Definition Http.hpp:221
@ NotImplemented
The server doesn't implement a requested feature.
Definition Http.hpp:225
@ BadRequest
The server couldn't understand the request (syntax error)
Definition Http.hpp:217
@ MovedTemporarily
The requested page has temporarily moved to a new location.
Definition Http.hpp:213
@ MultipleChoices
The requested page can be accessed from several locations.
Definition Http.hpp:211
@ Ok
Most common code returned when operation was successful.
Definition Http.hpp:203
@ ConnectionFailed
Connection with server failed.
Definition Http.hpp:233
@ NotModified
For conditional requests, means the requested page hasn't changed and doesn't need to be refreshed.
Definition Http.hpp:214
@ NoContent
The server didn't send any data in return.
Definition Http.hpp:206
@ VersionNotSupported
The server doesn't support the requested HTTP version.
Definition Http.hpp:229
@ GatewayTimeout
The gateway server couldn't receive a response from the source server.
Definition Http.hpp:228
@ Unauthorized
The requested page needs an authentication to be accessed.
Definition Http.hpp:218
@ InternalServerError
The server encountered an unexpected error.
Definition Http.hpp:224
unsigned int getMajorHttpVersion() const
Get the major HTTP version number of the response.
friend class Http
Definition Http.hpp:298
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.
Http(const Http &)=delete
Deleted copy constructor.
void setHost(const std::string &host, unsigned short port=0)
Set the target host.
Http & operator=(const Http &)=delete
Deleted copy assignment.
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
Default constructor.
Specialized socket using the TCP protocol.
Definition TcpSocket.hpp:54
Represents a time value.
Definition Time.hpp:42
static const Time Zero
Predefined "zero" time value.
Definition Time.hpp:110