Line data Source code
1 : //
2 : // Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
3 : // Copyright (c) 2024 Christian Mazakas
4 : // Copyright (c) 2025 Mohammad Nejati
5 : //
6 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
7 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 : //
9 : // Official repository: https://github.com/cppalliance/http_proto
10 : //
11 :
12 : #ifndef BOOST_HTTP_PROTO_RESPONSE_BASE_HPP
13 : #define BOOST_HTTP_PROTO_RESPONSE_BASE_HPP
14 :
15 : #include <boost/http_proto/detail/config.hpp>
16 : #include <boost/http_proto/message_base.hpp>
17 : #include <boost/http_proto/status.hpp>
18 :
19 : namespace boost {
20 : namespace http_proto {
21 :
22 : /** Mixin for modifing HTTP responses.
23 :
24 : @see
25 : @ref message_base,
26 : @ref response,
27 : @ref static_response.
28 : */
29 : class response_base
30 : : public message_base
31 : {
32 : friend class response;
33 : friend class static_response;
34 :
35 100 : response_base() noexcept
36 100 : : message_base(detail::kind::response)
37 : {
38 100 : }
39 :
40 : explicit
41 100 : response_base(core::string_view s)
42 100 : : message_base(detail::kind::response, s)
43 : {
44 99 : }
45 :
46 6 : response_base(
47 : void* storage,
48 : std::size_t cap) noexcept
49 6 : : message_base(
50 6 : detail::kind::response, storage, cap)
51 : {
52 6 : }
53 :
54 : public:
55 : //--------------------------------------------
56 : //
57 : // Observers
58 : //
59 : //--------------------------------------------
60 :
61 : /** Return the reason string.
62 :
63 : This field is obsolete in HTTP/1
64 : and should only be used for display
65 : purposes.
66 : */
67 : core::string_view
68 31 : reason() const noexcept
69 : {
70 62 : return core::string_view(
71 31 : h_.cbuf + 13,
72 31 : h_.prefix - 15);
73 : }
74 :
75 : /** Return the status code.
76 : */
77 : http_proto::status
78 32 : status() const noexcept
79 : {
80 32 : return h_.res.status;
81 : }
82 :
83 : /** Return the status code as an integral.
84 : */
85 : unsigned short
86 32 : status_int() const noexcept
87 : {
88 32 : return h_.res.status_int;
89 : }
90 :
91 : //--------------------------------------------
92 : //
93 : // Modifiers
94 : //
95 : //--------------------------------------------
96 :
97 : /** Set the status code and version of the response.
98 :
99 : The reason-phrase will be set to the
100 : standard text for the specified status
101 : code.
102 :
103 : This is more efficient than setting the
104 : properties individually.
105 :
106 : @par Exception Safety
107 : Strong guarantee.
108 : Calls to allocate may throw.
109 : Exception thrown if max capacity exceeded.
110 :
111 : @throw std::length_error
112 : Max capacity would be exceeded.
113 :
114 : @param sc The status code to set. This
115 : must not be @ref status::unknown.
116 :
117 : @param v The version to set.
118 : */
119 : void
120 13 : set_start_line(
121 : http_proto::status sc,
122 : http_proto::version v =
123 : http_proto::version::http_1_1)
124 : {
125 13 : set_start_line_impl(
126 : sc,
127 : static_cast<
128 : unsigned short>(sc),
129 : obsolete_reason(sc),
130 : v);
131 13 : }
132 :
133 : /** Set the status code and version of the response.
134 :
135 : The reason-phrase will be set to the
136 : standard text for the specified status
137 : code.
138 :
139 : This is more efficient than setting the
140 : properties individually.
141 :
142 : @par Exception Safety
143 : Strong guarantee.
144 : Calls to allocate may throw.
145 : Exception thrown on invalid input.
146 : Exception thrown if max capacity exceeded.
147 :
148 : @throw system_error
149 : Input is invalid.
150 :
151 : @throw std::length_error
152 : Max capacity would be exceeded.
153 :
154 : @param si An integral representing the
155 : status code to set.
156 :
157 : @param reason A string view representing the
158 : reason string to set.
159 :
160 : @param v The version to set.
161 : */
162 : void
163 8 : set_start_line(
164 : unsigned short si,
165 : core::string_view reason,
166 : http_proto::version v =
167 : http_proto::version::http_1_1)
168 : {
169 8 : set_start_line_impl(
170 : int_to_status(si),
171 : si,
172 : reason,
173 : v);
174 7 : }
175 :
176 : private:
177 : BOOST_HTTP_PROTO_DECL
178 : void
179 : set_start_line_impl(
180 : http_proto::status sc,
181 : unsigned short si,
182 : core::string_view reason,
183 : http_proto::version v);
184 : };
185 :
186 : } // http_proto
187 : } // boost
188 :
189 : #endif
|