CppServer 1.0.5.0
C++ Server Library
Loading...
Searching...
No Matches
http_response.h
Go to the documentation of this file.
1
9#ifndef CPPSERVER_HTTP_HTTP_RESPONSE_H
10#define CPPSERVER_HTTP_HTTP_RESPONSE_H
11
12#include "http.h"
13
14#include "time/time.h"
15
16#include <sstream>
17#include <string>
18#include <string_view>
19#include <tuple>
20#include <unordered_map>
21#include <vector>
22
23namespace CppServer {
24namespace HTTP {
25
27
34{
35 friend class HTTPClient;
36 friend class HTTPSClient;
37
38public:
42
46 HTTPResponse(int status, std::string_view protocol = "HTTP/1.1") { SetBegin(status, protocol); }
48
53 HTTPResponse(int status, std::string_view status_phrase, std::string_view protocol) { SetBegin(status, status_phrase, protocol); }
54 HTTPResponse(const HTTPResponse&) = default;
56 ~HTTPResponse() = default;
57
60
62 bool empty() const noexcept { return _cache.empty(); }
64 bool error() const noexcept { return _error; }
65
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); }
79 size_t body_length() const noexcept { return _body_length; }
80
82 const std::string& cache() const noexcept { return _cache; }
83
85 std::string string() const { std::stringstream ss; ss << *this; return ss.str(); }
86
89
91
95 HTTPResponse& SetBegin(int status, std::string_view protocol = "HTTP/1.1");
97
102 HTTPResponse& SetBegin(int status, std::string_view status_phrase, std::string_view protocol);
104
107 HTTPResponse& SetContentType(std::string_view extension);
109
113 HTTPResponse& SetHeader(std::string_view key, std::string_view value);
115
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);
127
130 HTTPResponse& SetBody(std::string_view body = "");
132
135 HTTPResponse& SetBodyLength(size_t length);
136
138
144
149 HTTPResponse& MakeErrorResponse(std::string_view content = "", std::string_view content_type = "text/plain; charset=UTF-8") { return MakeErrorResponse(500, content, content_type); }
151
157 HTTPResponse& MakeErrorResponse(int status, std::string_view content = "", std::string_view content_type = "text/plain; charset=UTF-8");
159
164
169 HTTPResponse& MakeGetResponse(std::string_view content = "", std::string_view content_type = "text/plain; charset=UTF-8");
171
175 HTTPResponse& MakeOptionsResponse(std::string_view allow = "HEAD,GET,POST,PUT,DELETE,OPTIONS,TRACE");
177
181 HTTPResponse& MakeTraceResponse(std::string_view request);
182
184 friend std::ostream& operator<<(std::ostream& os, const HTTPResponse& response);
185
187 void swap(HTTPResponse& response) noexcept;
188 friend void swap(HTTPResponse& response1, HTTPResponse& response2) noexcept { response1.swap(response2); }
189
190private:
191 // HTTP response error flag
192 bool _error;
193 // HTTP response status
194 int _status;
195 // HTTP response status phrase
196 size_t _status_phrase_index;
197 size_t _status_phrase_size;
198 // HTTP response protocol
199 size_t _protocol_index;
200 size_t _protocol_size;
201 // HTTP response headers
202 std::vector<std::tuple<size_t, size_t, size_t, size_t>> _headers;
203 // HTTP response body
204 size_t _body_index;
205 size_t _body_size;
206 size_t _body_length;
207 bool _body_length_provided;
208
209 // HTTP response cache
210 std::string _cache;
211 size_t _cache_size;
212
213 // HTTP response mime table
214 static const std::unordered_map<std::string, std::string> _mime_table;
215
216 // Is pending parts of HTTP response
217 bool IsPendingHeader() const;
218 bool IsPendingBody() const;
219
220 // Receive parts of HTTP response
221 bool ReceiveHeader(const void* buffer, size_t size);
222 bool ReceiveBody(const void* buffer, size_t size);
223
224 // Fast convert integer value to the corresponding string representation
225 std::string_view FastConvert(size_t value, char* buffer, size_t size);
226};
227
228} // namespace HTTP
229} // namespace CppServer
230
231#include "http_response.inl"
232
233#endif // CPPSERVER_HTTP_HTTP_RESPONSE_H
HTTPResponse & SetBody(std::string_view body="")
Set the HTTP response body.
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.
HTTPResponse & SetHeader(std::string_view key, std::string_view value)
Set the HTTP response header.
const std::string & cache() const noexcept
Get the HTTP response cache content.
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?
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 & operator=(HTTPResponse &&)=default
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.
friend std::ostream & operator<<(std::ostream &os, const HTTPResponse &response)
Output instance into the given output stream.
std::string string() const
Get string from the current HTTP response.
HTTPResponse & operator=(const HTTPResponse &)=default
HTTP C++ Library definition.
HTTP response inline implementation.
C++ Server project definitions.
Definition asio.h:56