CppServer  1.0.4.0
C++ Server Library
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 
23 namespace CppServer {
24 namespace HTTP {
25 
27 
34 {
35  friend class HTTPClient;
36  friend class HTTPSClient;
37 
38 public:
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;
55  HTTPResponse(HTTPResponse&&) = default;
56  ~HTTPResponse() = default;
57 
58  HTTPResponse& operator=(const HTTPResponse&) = default;
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 
142  HTTPResponse& MakeOKResponse(int status = 200);
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 
190 private:
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.
const std::string & cache() const noexcept
Get the HTTP response cache content.
Definition: http_response.h:82
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.
Definition: http_response.h:79
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.
Definition: http_response.h:53
bool error() const noexcept
Is the HTTP response error flag set?
Definition: http_response.h:64
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.
Definition: http_response.h:46
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.
Definition: http_response.h:73
HTTPResponse()
Initialize an empty HTTP response.
Definition: http_response.h:40
int status() const noexcept
Get the HTTP response status.
Definition: http_response.h:67
HTTPResponse & SetBodyLength(size_t length)
Set the HTTP response body length.
std::string_view protocol() const noexcept
Get the HTTP response protocol version.
Definition: http_response.h:71
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.
Definition: http_response.h:69
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?
Definition: http_response.h:62
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.
Definition: http_response.h:77
HTTPResponse & operator=(const HTTPResponse &)=default
std::string string() const
Get string from the current HTTP response.
Definition: http_response.h:85
HTTPResponse & operator=(HTTPResponse &&)=default
HTTP C++ Library definition.
HTTP response inline implementation.
C++ Server project definitions.
Definition: asio.h:56