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 {
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
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
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
void setHttpVersion(unsigned int major, unsigned int minor)
Set the HTTP version for the request.
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
unsigned int getMajorHttpVersion() const
Get the major HTTP version number of the response.
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.
A HTTP client.
Definition Http.hpp:50
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