9 #ifndef CPPSERVER_HTTP_HTTP_REQUEST_H
10 #define CPPSERVER_HTTP_HTTP_REQUEST_H
16 #include <string_view>
53 bool empty() const noexcept {
return _cache.empty(); }
55 bool error() const noexcept {
return _error; }
58 std::string_view
method() const noexcept {
return std::string_view(_cache.data() + _method_index, _method_size); }
60 std::string_view
url() const noexcept {
return std::string_view(_cache.data() + _url_index, _url_size); }
62 std::string_view
protocol() const noexcept {
return std::string_view(_cache.data() + _protocol_index, _protocol_size); }
64 size_t headers() const noexcept {
return _headers.size(); }
66 std::tuple<std::string_view, std::string_view>
header(
size_t i)
const noexcept;
68 size_t cookies() const noexcept {
return _cookies.size(); }
70 std::tuple<std::string_view, std::string_view>
cookie(
size_t i)
const noexcept;
72 std::string_view
body() const noexcept {
return std::string_view(_cache.data() + _body_index, _body_size); }
77 const std::string&
cache() const noexcept {
return _cache; }
80 std::string
string()
const { std::stringstream ss; ss << *
this;
return ss.str(); }
148 HTTPRequest&
MakePutRequest(std::string_view
url, std::string_view content, std::string_view content_type =
"text/plain; charset=UTF-8");
179 size_t _method_index;
185 size_t _protocol_index;
186 size_t _protocol_size;
188 std::vector<std::tuple<size_t, size_t, size_t, size_t>> _headers;
190 std::vector<std::tuple<size_t, size_t, size_t, size_t>> _cookies;
195 bool _body_length_provided;
202 bool IsPendingHeader()
const;
203 bool IsPendingBody()
const;
206 bool ReceiveHeader(
const void* buffer,
size_t size);
207 bool ReceiveBody(
const void* buffer,
size_t size);
210 std::string_view FastConvert(
size_t value,
char* buffer,
size_t size);
size_t body_length() const noexcept
Get the HTTP request body length.
friend void swap(HTTPRequest &request1, HTTPRequest &request2) noexcept
HTTPRequest & SetBody(std::string_view body="")
Set the HTTP request body.
const std::string & cache() const noexcept
Get the HTTP request cache content.
HTTPRequest & MakePutRequest(std::string_view url, std::string_view content, std::string_view content_type="text/plain; charset=UTF-8")
Make PUT request.
void swap(HTTPRequest &request) noexcept
Swap two instances.
std::string string() const
Get string from the current HTTP request.
HTTPRequest & MakeHeadRequest(std::string_view url)
Make HEAD request.
HTTPRequest & Clear()
Clear the HTTP request cache.
std::tuple< std::string_view, std::string_view > cookie(size_t i) const noexcept
Get the HTTP request cookie by index.
HTTPRequest & MakeDeleteRequest(std::string_view url)
Make DELETE request.
HTTPRequest & MakePostRequest(std::string_view url, std::string_view content, std::string_view content_type="text/plain; charset=UTF-8")
Make POST request.
HTTPRequest & AddCookie(std::string_view name, std::string_view value)
Add the HTTP request cookie.
HTTPRequest(const HTTPRequest &)=default
size_t cookies() const noexcept
Get the HTTP request cookies count.
friend std::ostream & operator<<(std::ostream &os, const HTTPRequest &request)
Output instance into the given output stream.
HTTPRequest & MakeOptionsRequest(std::string_view url)
Make OPTIONS request.
HTTPRequest & SetBodyLength(size_t length)
Set the HTTP request body length.
HTTPRequest & operator=(const HTTPRequest &)=default
HTTPRequest & SetBegin(std::string_view method, std::string_view url, std::string_view protocol="HTTP/1.1")
Set the HTTP request begin with a given method, URL and protocol.
HTTPRequest()
Initialize an empty HTTP request.
HTTPRequest & MakeGetRequest(std::string_view url)
Make GET request.
bool empty() const noexcept
Is the HTTP request empty?
HTTPRequest(HTTPRequest &&)=default
HTTPRequest & operator=(HTTPRequest &&)=default
size_t headers() const noexcept
Get the HTTP request headers count.
HTTPRequest & SetHeader(std::string_view key, std::string_view value)
Set the HTTP request header.
HTTPRequest(std::string_view method, std::string_view url, std::string_view protocol="HTTP/1.1")
Initialize a new HTTP request with a given method, URL and protocol.
std::string_view method() const noexcept
Get the HTTP request method.
bool error() const noexcept
Is the HTTP request error flag set?
std::tuple< std::string_view, std::string_view > header(size_t i) const noexcept
Get the HTTP request header by index.
HTTPRequest & SetCookie(std::string_view name, std::string_view value)
Set the HTTP request cookie.
HTTPRequest & MakeTraceRequest(std::string_view url)
Make TRACE request.
std::string_view url() const noexcept
Get the HTTP request URL.
std::string_view body() const noexcept
Get the HTTP request body.
std::string_view protocol() const noexcept
Get the HTTP request protocol version.
HTTP C++ Library definition.
HTTP request inline implementation.
C++ Server project definitions.