98 void setField(
const std::string& field,
const std::string& value);
159 [[nodiscard]] std::string prepare()
const;
171 [[nodiscard]]
bool hasField(
const std::string& field)
const;
176 using FieldTable = std::map<std::string, std::string>;
184 unsigned int m_majorVersion{1};
185 unsigned int m_minorVersion{};
248 [[nodiscard]]
const std::string&
getField(
const std::string& field)
const;
295 [[nodiscard]]
const std::string&
getBody()
const;
309 void parse(
const std::string& data);
321 void parseFields(std::istream& in);
326 using FieldTable = std::map<std::string, std::string>;
333 unsigned int m_majorVersion{};
334 unsigned int m_minorVersion{};
358 Http(
const std::string& host,
unsigned short port = 0);
387 void setHost(
const std::string& host,
unsigned short port = 0);
414 std::optional<IpAddress> m_host;
415 std::string m_hostName;
416 unsigned short m_port{};
void setUri(const std::string &uri)
Set the requested URI.
Method
Enumerate the available HTTP methods for a request.
@ Post
Request in post mode, usually to send data to a page.
@ Head
Request a page's header only.
@ Get
Request in get mode, standard method to retrieve a page.
@ Put
Request in put mode, useful for a REST API.
@ Delete
Request in delete mode, useful for a REST API.
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.
Status getStatus() const
Get the response status code.
Status
Enumerate all the valid status codes for a response.
@ BadGateway
The gateway server has received an error from the source server.
@ Created
The resource has successfully been created.
@ ServiceNotAvailable
The server is temporarily unavailable (overloaded, in maintenance, ...)
@ MovedPermanently
The requested page has permanently moved to a new location.
@ InvalidResponse
Response is not a valid HTTP one.
@ Accepted
The request has been accepted, but will be processed later by the server.
@ NotFound
The requested page doesn't exist.
@ ResetContent
The server informs the client that it should clear the view (form) that caused the request to be sent...
@ Forbidden
The requested page cannot be accessed at all, even with authentication.
@ PartialContent
The server has sent a part of the resource, as a response to a partial GET request.
@ RangeNotSatisfiable
The server can't satisfy the partial GET request (with a "Range" header field)
@ NotImplemented
The server doesn't implement a requested feature.
@ BadRequest
The server couldn't understand the request (syntax error)
@ MovedTemporarily
The requested page has temporarily moved to a new location.
@ MultipleChoices
The requested page can be accessed from several locations.
@ Ok
Most common code returned when operation was successful.
@ ConnectionFailed
Connection with server failed.
@ NotModified
For conditional requests, means the requested page hasn't changed and doesn't need to be refreshed.
@ NoContent
The server didn't send any data in return.
@ VersionNotSupported
The server doesn't support the requested HTTP version.
@ GatewayTimeout
The gateway server couldn't receive a response from the source server.
@ Unauthorized
The requested page needs an authentication to be accessed.
@ InternalServerError
The server encountered an unexpected error.
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.
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.
static const Time Zero
Predefined "zero" time value.