Line data Source code
1 : #ifndef HTTP_RESPONSE_HPP
2 : #define HTTP_RESPONSE_HPP
3 :
4 : #include <string>
5 : #include <vector>
6 : #include <boost/asio.hpp>
7 : #include <boost/asio/buffer.hpp>
8 : #include <boost/lexical_cast.hpp>
9 : #include "../data/out_data.hpp"
10 : #include "headers.hpp"
11 :
12 : namespace thinger::http {
13 :
14 : class http_response : public headers {
15 :
16 : public:
17 :
18 : // the status of the http_response.
19 : enum class status {
20 : ok = 200,
21 : created = 201,
22 : accepted = 202,
23 : no_content = 204,
24 : partial_content = 206,
25 : multiple_choices = 300,
26 : moved_permanently = 301,
27 : moved_temporarily = 302,
28 : not_modified = 304,
29 : temporary_redirect = 307,
30 : permanent_redirect = 308,
31 : bad_request = 400,
32 : unauthorized = 401,
33 : forbidden = 403,
34 : not_found = 404,
35 : not_allowed = 405,
36 : timed_out = 408,
37 : conflict = 409,
38 : payload_too_large = 413,
39 : unsupported_media_type = 415,
40 : range_not_satisfiable = 416,
41 : expectation_failed = 417,
42 : misdirected_request = 421,
43 : request_header_fields_too_large = 431,
44 : upgrade_required = 426,
45 : too_many_requests = 429,
46 : internal_server_error = 500,
47 : not_implemented = 501,
48 : bad_gateway = 502,
49 : service_unavailable = 503,
50 : gateway_timeout = 504,
51 : switching_protocols = 101
52 : } ;
53 :
54 : // constructor
55 : http_response();
56 335727 : ~http_response() override = default;
57 : http_response(const http_response&) = default;
58 2597 : http_response(http_response&&) = default;
59 : http_response& operator=(const http_response&) = default;
60 : http_response& operator=(http_response&&) = default;
61 :
62 : // response to buffer
63 : void to_buffer(std::vector<boost::asio::const_buffer>&buffer) const override;
64 :
65 : // some setters
66 : void set_content(std::string content);
67 : void set_content(std::string content, std::string content_type);
68 : void set_content_length(size_t content_length);
69 : void set_content_type(std::string && content_type);
70 :
71 : void set_content_type(const std::string &content_type);
72 :
73 : void set_status(uint16_t status_code);
74 : void set_status(status status_code);
75 : // Reason phrase of the status line, for statuses without a standard one (e.g. those
76 : // received from another server); only printable characters are sent
77 : void set_reason_phrase(const std::string& reason);
78 :
79 : // some getters (inline: read on every response)
80 1548 : const std::string& get_content() const { return content_; }
81 50234 : std::string& get_content() { return content_; }
82 18 : size_t get_content_size() const { return content_.size(); }
83 : size_t get_size() override;
84 40008 : status get_status() const { return status_; }
85 74873 : int get_status_code() const { return static_cast<int>(status_); }
86 1226 : const std::string& get_reason() const { return reason_phrase_; }
87 : bool is_ok() const;
88 : bool is_redirect_response() const;
89 :
90 : // log
91 : void log(const char* scope, const body_logging& options = {}) const override;
92 :
93 : // factory methods
94 : static std::shared_ptr<http_response> stock_http_reply(http_response::status status);
95 :
96 : // Standard reason phrase of a status ("Not Found" for 404)
97 : static std::string get_reason_phrase(http_response::status status);
98 :
99 : private:
100 : std::string content_;
101 : status status_ = status::ok;
102 : std::string reason_phrase_;
103 : // Status line of a status without a standard reason phrase, built when sent
104 : mutable std::string status_line_;
105 : };
106 :
107 : }
108 :
109 : #endif
|