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/response_view.hpp>
18 : #include <boost/http_proto/status.hpp>
19 :
20 : namespace boost {
21 : namespace http_proto {
22 :
23 : /** Mixin for modifing HTTP responses.
24 :
25 : @see
26 : @ref message_base,
27 : @ref response,
28 : @ref static_response.
29 : */
30 : class response_base
31 : : public message_base
32 : {
33 : friend class response;
34 : template<std::size_t>
35 : friend class static_response;
36 :
37 98 : response_base() noexcept
38 : : fields_view_base(
39 : &this->fields_base::h_)
40 98 : , message_base(detail::kind::response)
41 : {
42 98 : }
43 :
44 : explicit
45 100 : response_base(core::string_view s)
46 : : fields_view_base(
47 : &this->fields_base::h_)
48 100 : , message_base(detail::kind::response, s)
49 : {
50 99 : }
51 :
52 : explicit
53 4 : response_base(detail::header const& ph)
54 : : fields_view_base(
55 : &this->fields_base::h_)
56 4 : , message_base(ph)
57 : {
58 4 : }
59 :
60 4 : response_base(
61 : detail::header const& ph,
62 : char* storage,
63 : std::size_t cap)
64 : : fields_view_base(
65 : &this->fields_base::h_)
66 4 : , message_base(ph, storage, cap)
67 : {
68 4 : }
69 :
70 : public:
71 20 : response_base(
72 : char* storage,
73 : std::size_t cap) noexcept
74 : : fields_view_base(
75 : &this->fields_base::h_)
76 : , message_base(
77 20 : detail::kind::response, storage, cap)
78 : {
79 20 : }
80 :
81 3 : response_base(
82 : core::string_view s,
83 : char* storage,
84 : std::size_t cap)
85 : : fields_view_base(
86 : &this->fields_base::h_)
87 : , message_base(
88 3 : detail::kind::response, storage, cap, s)
89 : {
90 3 : }
91 :
92 : response_base(
93 : response_view const& other,
94 : char* storage,
95 : std::size_t cap)
96 : : fields_view_base(
97 : &this->fields_base::h_)
98 : , message_base(*other.ph_, storage, cap)
99 : {
100 : }
101 :
102 : /** Conversion.
103 :
104 : @see
105 : @ref response_view.
106 :
107 : @return A view of the response.
108 : */
109 62 : operator response_view() const noexcept
110 : {
111 62 : return response_view(ph_);
112 : }
113 :
114 : //--------------------------------------------
115 : //
116 : // Observers
117 : //
118 : //--------------------------------------------
119 :
120 : /** Return the reason string.
121 :
122 : This field is obsolete in HTTP/1
123 : and should only be used for display
124 : purposes.
125 : */
126 : core::string_view
127 50 : reason() const noexcept
128 : {
129 100 : return core::string_view(
130 50 : ph_->cbuf + 13,
131 50 : ph_->prefix - 15);
132 : }
133 :
134 : /** Return the status code.
135 : */
136 : http_proto::status
137 50 : status() const noexcept
138 : {
139 50 : return ph_->res.status;
140 : }
141 :
142 : /** Return the status code as an integral.
143 : */
144 : unsigned short
145 50 : status_int() const noexcept
146 : {
147 50 : return ph_->res.status_int;
148 : }
149 :
150 : //--------------------------------------------
151 : //
152 : // Modifiers
153 : //
154 : //--------------------------------------------
155 :
156 : /** Set the status code and version of the response.
157 :
158 : The reason-phrase will be set to the
159 : standard text for the specified status
160 : code.
161 :
162 : This is more efficient than setting the
163 : properties individually.
164 :
165 : @par Exception Safety
166 : Strong guarantee.
167 : Calls to allocate may throw.
168 : Exception thrown if max capacity exceeded.
169 :
170 : @throw std::length_error
171 : Max capacity would be exceeded.
172 :
173 : @param sc The status code to set. This
174 : must not be @ref status::unknown.
175 :
176 : @param v The version to set.
177 : */
178 : void
179 25 : set_start_line(
180 : http_proto::status sc,
181 : http_proto::version v =
182 : http_proto::version::http_1_1)
183 : {
184 25 : set_start_line_impl(
185 : sc,
186 : static_cast<
187 : unsigned short>(sc),
188 : obsolete_reason(sc),
189 : v);
190 25 : }
191 :
192 : /** Set the status code and version of the response.
193 :
194 : The reason-phrase will be set to the
195 : standard text for the specified status
196 : code.
197 :
198 : This is more efficient than setting the
199 : properties individually.
200 :
201 : @par Exception Safety
202 : Strong guarantee.
203 : Calls to allocate may throw.
204 : Exception thrown on invalid input.
205 : Exception thrown if max capacity exceeded.
206 :
207 : @throw system_error
208 : Input is invalid.
209 :
210 : @throw std::length_error
211 : Max capacity would be exceeded.
212 :
213 : @param si An integral representing the
214 : status code to set.
215 :
216 : @param reason A string view representing the
217 : reason string to set.
218 :
219 : @param v The version to set.
220 : */
221 : void
222 14 : set_start_line(
223 : unsigned short si,
224 : core::string_view reason,
225 : http_proto::version v =
226 : http_proto::version::http_1_1)
227 : {
228 14 : set_start_line_impl(
229 : int_to_status(si),
230 : si,
231 : reason,
232 : v);
233 13 : }
234 :
235 : private:
236 : BOOST_HTTP_PROTO_DECL
237 : void
238 : set_start_line_impl(
239 : http_proto::status sc,
240 : unsigned short si,
241 : core::string_view reason,
242 : http_proto::version v);
243 : };
244 :
245 : } // http_proto
246 : } // boost
247 :
248 : #endif
|