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 BOOST_HTTP_PROTO_DECL response_base
30 : : public message_base
31 : {
32 : friend class response;
33 : friend class static_response;
34 :
35 104 : response_base() noexcept
36 104 : : message_base(detail::kind::response)
37 : {
38 104 : }
39 :
40 : explicit
41 101 : response_base(core::string_view s)
42 101 : : message_base(detail::kind::response, s)
43 : {
44 100 : }
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 : @throw std::invalid_argument
114 : `sc == status::unknown`
115 :
116 : @param sc The status code to set. This
117 : must not be @ref status::unknown.
118 :
119 : @param v The version to set.
120 : */
121 : void
122 13 : set_start_line(
123 : http_proto::status sc,
124 : http_proto::version v =
125 : http_proto::version::http_1_1)
126 : {
127 13 : set_start_line_impl(sc,
128 : static_cast<unsigned short>(sc),
129 : to_string(sc), v);
130 13 : }
131 :
132 : /** Set the HTTP version of the response
133 :
134 : @par Exception Safety
135 : Strong guarantee.
136 : Calls to allocate may throw.
137 : Exception thrown if maximum capacity exceeded.
138 :
139 : @throw std::length_error
140 : Maximum capacity would be exceeded.
141 :
142 : @param v The version to set.
143 : */
144 : void
145 : set_version(
146 : http_proto::version v);
147 :
148 : /** Set the status code of the response.
149 :
150 : The reason-phrase will be set to the
151 : standard text for the specified status
152 : code. The version will remain unchanged.
153 :
154 : @par Exception Safety
155 : Strong guarantee.
156 : Calls to allocate may throw.
157 : Exception thrown if maximum capacity exceeded.
158 :
159 : @throw std::length_error
160 : Maximum capacity would be exceeded.
161 : @throw std::invalid_argument
162 : `sc == status::unknown`
163 :
164 : @param sc The status code to set. This
165 : must not be @ref status::unknown.
166 : */
167 : void
168 3 : set_status(
169 : http_proto::status sc)
170 : {
171 3 : if(sc == http_proto::status::unknown)
172 0 : detail::throw_invalid_argument();
173 3 : set_start_line_impl(sc,
174 : static_cast<unsigned short>(sc),
175 : to_string(sc),
176 : version());
177 3 : }
178 :
179 : /** Set the status code and version of the response.
180 :
181 : The reason-phrase will be set to the
182 : standard text for the specified status
183 : code.
184 :
185 : This is more efficient than setting the
186 : properties individually.
187 :
188 : @par Exception Safety
189 : Strong guarantee.
190 : Calls to allocate may throw.
191 : Exception thrown on invalid input.
192 : Exception thrown if max capacity exceeded.
193 :
194 : @throw system_error
195 : Input is invalid.
196 :
197 : @throw std::length_error
198 : Max capacity would be exceeded.
199 :
200 : @param si An integral representing the
201 : status code to set.
202 :
203 : @param reason A string view representing the
204 : reason string to set.
205 :
206 : @param v The version to set.
207 : */
208 : void
209 8 : set_start_line(
210 : unsigned short si,
211 : core::string_view reason,
212 : http_proto::version v =
213 : http_proto::version::http_1_1)
214 : {
215 8 : set_start_line_impl(
216 : int_to_status(si),
217 : si,
218 : reason,
219 : v);
220 7 : }
221 :
222 : private:
223 :
224 : void
225 : set_start_line_impl(
226 : http_proto::status sc,
227 : unsigned short si,
228 : core::string_view reason,
229 : http_proto::version v);
230 : };
231 :
232 : } // http_proto
233 : } // boost
234 :
235 : #endif
|