CppServer 1.0.5.0
C++ Server Library
Loading...
Searching...
No Matches
http_request.h
Go to the documentation of this file.
1
9#ifndef CPPSERVER_HTTP_HTTP_REQUEST_H
10#define CPPSERVER_HTTP_HTTP_REQUEST_H
11
12#include "http.h"
13
14#include <sstream>
15#include <string>
16#include <string_view>
17#include <tuple>
18#include <vector>
19
20namespace CppServer {
21namespace HTTP {
22
24
31{
32 friend class HTTPSession;
33 friend class HTTPSSession;
34
35public:
39
44 HTTPRequest(std::string_view method, std::string_view url, std::string_view protocol = "HTTP/1.1") { SetBegin(method, url, protocol); }
45 HTTPRequest(const HTTPRequest&) = default;
47 ~HTTPRequest() = default;
48
49 HTTPRequest& operator=(const HTTPRequest&) = default;
51
53 bool empty() const noexcept { return _cache.empty(); }
55 bool error() const noexcept { return _error; }
56
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); }
74 size_t body_length() const noexcept { return _body_length; }
75
77 const std::string& cache() const noexcept { return _cache; }
78
80 std::string string() const { std::stringstream ss; ss << *this; return ss.str(); }
81
84
86
91 HTTPRequest& SetBegin(std::string_view method, std::string_view url, std::string_view protocol = "HTTP/1.1");
93
97 HTTPRequest& SetHeader(std::string_view key, std::string_view value);
99
103 HTTPRequest& SetCookie(std::string_view name, std::string_view value);
105
109 HTTPRequest& AddCookie(std::string_view name, std::string_view value);
111
114 HTTPRequest& SetBody(std::string_view body = "");
116
119 HTTPRequest& SetBodyLength(size_t length);
120
122
126 HTTPRequest& MakeHeadRequest(std::string_view url);
128
132 HTTPRequest& MakeGetRequest(std::string_view url);
134
140 HTTPRequest& MakePostRequest(std::string_view url, std::string_view content, std::string_view content_type = "text/plain; charset=UTF-8");
142
148 HTTPRequest& MakePutRequest(std::string_view url, std::string_view content, std::string_view content_type = "text/plain; charset=UTF-8");
150
154 HTTPRequest& MakeDeleteRequest(std::string_view url);
156
160 HTTPRequest& MakeOptionsRequest(std::string_view url);
162
166 HTTPRequest& MakeTraceRequest(std::string_view url);
167
169 friend std::ostream& operator<<(std::ostream& os, const HTTPRequest& request);
170
172 void swap(HTTPRequest& request) noexcept;
173 friend void swap(HTTPRequest& request1, HTTPRequest& request2) noexcept { request1.swap(request2); }
174
175private:
176 // HTTP request error flag
177 bool _error;
178 // HTTP request method
179 size_t _method_index;
180 size_t _method_size;
181 // HTTP request URL
182 size_t _url_index;
183 size_t _url_size;
184 // HTTP request protocol
185 size_t _protocol_index;
186 size_t _protocol_size;
187 // HTTP request headers
188 std::vector<std::tuple<size_t, size_t, size_t, size_t>> _headers;
189 // HTTP request cookies
190 std::vector<std::tuple<size_t, size_t, size_t, size_t>> _cookies;
191 // HTTP request body
192 size_t _body_index;
193 size_t _body_size;
194 size_t _body_length;
195 bool _body_length_provided;
196
197 // HTTP request cache
198 std::string _cache;
199 size_t _cache_size;
200
201 // Is pending parts of HTTP response
202 bool IsPendingHeader() const;
203 bool IsPendingBody() const;
204
205 // Receive parts of HTTP response
206 bool ReceiveHeader(const void* buffer, size_t size);
207 bool ReceiveBody(const void* buffer, size_t size);
208
209 // Fast convert integer value to the corresponding string representation
210 std::string_view FastConvert(size_t value, char* buffer, size_t size);
211};
212
213} // namespace HTTP
214} // namespace CppServer
215
216#include "http_request.inl"
217
218#endif // CPPSERVER_HTTP_HTTP_REQUEST_H
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.
HTTPRequest & MakePutRequest(std::string_view url, std::string_view content, std::string_view content_type="text/plain; charset=UTF-8")
Make PUT request.
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.
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
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.
HTTPRequest & operator=(HTTPRequest &&)=default
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.
const std::string & cache() const noexcept
Get the HTTP request cache content.
friend std::ostream & operator<<(std::ostream &os, const HTTPRequest &request)
Output instance into the given output stream.
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.
Definition asio.h:56