9 #ifndef CPPSERVER_HTTP_HTTP_RESPONSE_H
10 #define CPPSERVER_HTTP_HTTP_RESPONSE_H
14 #include "time/time.h"
18 #include <string_view>
20 #include <unordered_map>
62 bool empty() const noexcept {
return _cache.empty(); }
64 bool error() const noexcept {
return _error; }
67 int status() const noexcept {
return _status; }
69 std::string_view
status_phrase() const noexcept {
return std::string_view(_cache.data() + _status_phrase_index, _status_phrase_size); }
71 std::string_view
protocol() const noexcept {
return std::string_view(_cache.data() + _protocol_index, _protocol_size); }
73 size_t headers() const noexcept {
return _headers.size(); }
75 std::tuple<std::string_view, std::string_view>
header(
size_t i)
const noexcept;
77 std::string_view
body() const noexcept {
return std::string_view(_cache.data() + _body_index, _body_size); }
82 const std::string&
cache() const noexcept {
return _cache; }
85 std::string
string()
const { std::stringstream ss; ss << *
this;
return ss.str(); }
125 HTTPResponse&
SetCookie(std::string_view name, std::string_view value,
size_t max_age = 86400, std::string_view path =
"", std::string_view domain =
"",
bool secure =
true,
bool strict =
true,
bool http_only =
true);
196 size_t _status_phrase_index;
197 size_t _status_phrase_size;
199 size_t _protocol_index;
200 size_t _protocol_size;
202 std::vector<std::tuple<size_t, size_t, size_t, size_t>> _headers;
207 bool _body_length_provided;
214 static const std::unordered_map<std::string, std::string> _mime_table;
217 bool IsPendingHeader()
const;
218 bool IsPendingBody()
const;
221 bool ReceiveHeader(
const void* buffer,
size_t size);
222 bool ReceiveBody(
const void* buffer,
size_t size);
225 std::string_view FastConvert(
size_t value,
char* buffer,
size_t size);
HTTPResponse & SetBody(std::string_view body="")
Set the HTTP response body.
const std::string & cache() const noexcept
Get the HTTP response cache content.
HTTPResponse(HTTPResponse &&)=default
HTTPResponse(const HTTPResponse &)=default
HTTPResponse & MakeHeadResponse()
Make HEAD response.
HTTPResponse & MakeOptionsResponse(std::string_view allow="HEAD,GET,POST,PUT,DELETE,OPTIONS,TRACE")
Make OPTIONS response.
HTTPResponse & MakeGetResponse(std::string_view content="", std::string_view content_type="text/plain; charset=UTF-8")
Make GET response.
size_t body_length() const noexcept
Get the HTTP response body length.
friend std::ostream & operator<<(std::ostream &os, const HTTPResponse &response)
Output instance into the given output stream.
HTTPResponse & SetHeader(std::string_view key, std::string_view value)
Set the HTTP response header.
HTTPResponse(int status, std::string_view status_phrase, std::string_view protocol)
Initialize a new HTTP response with a given status, status phrase and protocol.
bool error() const noexcept
Is the HTTP response error flag set?
void swap(HTTPResponse &response) noexcept
Swap two instances.
HTTPResponse & MakeOKResponse(int status=200)
Make OK response.
HTTPResponse(int status, std::string_view protocol="HTTP/1.1")
Initialize a new HTTP response with a given status and protocol.
HTTPResponse & SetCookie(std::string_view name, std::string_view value, size_t max_age=86400, std::string_view path="", std::string_view domain="", bool secure=true, bool strict=true, bool http_only=true)
Set the HTTP response cookie.
HTTPResponse & Clear()
Clear the HTTP response cache.
size_t headers() const noexcept
Get the HTTP response headers count.
HTTPResponse()
Initialize an empty HTTP response.
int status() const noexcept
Get the HTTP response status.
HTTPResponse & SetBodyLength(size_t length)
Set the HTTP response body length.
std::string_view protocol() const noexcept
Get the HTTP response protocol version.
HTTPResponse & MakeErrorResponse(std::string_view content="", std::string_view content_type="text/plain; charset=UTF-8")
Make ERROR response.
HTTPResponse & MakeTraceResponse(std::string_view request)
Make TRACE response.
std::string_view status_phrase() const noexcept
Get the HTTP response status phrase.
HTTPResponse & SetBegin(int status, std::string_view protocol="HTTP/1.1")
Set the HTTP response begin with a given status and protocol.
bool empty() const noexcept
Is the HTTP response empty?
HTTPResponse & SetContentType(std::string_view extension)
Set the HTTP response content type.
friend void swap(HTTPResponse &response1, HTTPResponse &response2) noexcept
std::tuple< std::string_view, std::string_view > header(size_t i) const noexcept
Get the HTTP response header by index.
std::string_view body() const noexcept
Get the HTTP response body.
HTTPResponse & operator=(const HTTPResponse &)=default
std::string string() const
Get string from the current HTTP response.
HTTPResponse & operator=(HTTPResponse &&)=default
HTTP C++ Library definition.
HTTP response inline implementation.
C++ Server project definitions.