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 : /** Provides message metadata for HTTP responses
24 : */
25 : class response_base
26 : : public message_base
27 : {
28 : friend class response;
29 : template<std::size_t>
30 : friend class static_response;
31 :
32 106 : response_base() noexcept
33 : : fields_view_base(
34 : &this->fields_base::h_)
35 106 : , message_base(detail::kind::response)
36 : {
37 106 : }
38 :
39 4 : response_base(std::size_t storage_size)
40 : : fields_view_base(
41 : &this->fields_base::h_)
42 : , message_base(
43 : detail::kind::response,
44 4 : storage_size)
45 : {
46 4 : }
47 :
48 10 : response_base(
49 : std::size_t storage_size,
50 : std::size_t max_storage_size)
51 : : fields_view_base(
52 : &this->fields_base::h_)
53 : , message_base(
54 : detail::kind::response,
55 : storage_size,
56 10 : max_storage_size)
57 : {
58 6 : }
59 :
60 : explicit
61 97 : response_base(core::string_view s)
62 : : fields_view_base(
63 : &this->fields_base::h_)
64 97 : , message_base(detail::kind::response, s)
65 : {
66 96 : }
67 :
68 : explicit
69 4 : response_base(detail::header const& ph)
70 : : fields_view_base(
71 : &this->fields_base::h_)
72 4 : , message_base(ph)
73 : {
74 4 : }
75 :
76 4 : response_base(
77 : detail::header const& ph,
78 : char* storage,
79 : std::size_t storage_size)
80 : : fields_view_base(
81 : &this->fields_base::h_)
82 4 : , message_base(ph, storage, storage_size)
83 : {
84 4 : }
85 :
86 : public:
87 24 : response_base(
88 : char* storage,
89 : std::size_t storage_size) noexcept
90 : : fields_view_base(
91 : &this->fields_base::h_)
92 : , message_base(
93 24 : detail::kind::response, storage, storage_size)
94 : {
95 24 : }
96 :
97 3 : response_base(
98 : core::string_view s,
99 : char* storage,
100 : std::size_t storage_size)
101 : : fields_view_base(
102 : &this->fields_base::h_)
103 : , message_base(
104 3 : detail::kind::response, storage, storage_size, s)
105 : {
106 3 : }
107 :
108 : response_base(
109 : response_view const& other,
110 : char* storage,
111 : std::size_t storage_size)
112 : : fields_view_base(
113 : &this->fields_base::h_)
114 : , message_base(*other.ph_, storage, storage_size)
115 : {
116 : }
117 :
118 : /** Return a read-only view to the response
119 : */
120 82 : operator response_view() const noexcept
121 : {
122 82 : return response_view(ph_);
123 : }
124 :
125 : //--------------------------------------------
126 : //
127 : // Observers
128 : //
129 : //--------------------------------------------
130 :
131 : /** Return the reason string
132 :
133 : This field is obsolete in HTTP/1
134 : and should only be used for display
135 : purposes.
136 : */
137 : core::string_view
138 50 : reason() const noexcept
139 : {
140 100 : return core::string_view(
141 50 : ph_->cbuf + 13,
142 50 : ph_->prefix - 15);
143 : }
144 :
145 : /** Return the status code
146 : */
147 : http_proto::status
148 50 : status() const noexcept
149 : {
150 50 : return ph_->res.status;
151 : }
152 :
153 : /** Return the status code
154 : */
155 : unsigned short
156 50 : status_int() const noexcept
157 : {
158 50 : return ph_->res.status_int;
159 : }
160 :
161 : /** Return the HTTP version
162 : */
163 : http_proto::version
164 50 : version() const noexcept
165 : {
166 50 : return ph_->version;
167 : }
168 :
169 : //--------------------------------------------
170 : //
171 : // Modifiers
172 : //
173 : //--------------------------------------------
174 :
175 : /** Set the version, status code of the response
176 :
177 : The reason phrase will be set to the
178 : standard text for the specified status
179 : code.
180 :
181 : @par sc The status code. This must not be
182 : @ref http_proto::status::unknown.
183 :
184 : @par v The HTTP-version.
185 : */
186 : void
187 24 : set_start_line(
188 : http_proto::status sc,
189 : http_proto::version v =
190 : http_proto::version::http_1_1)
191 : {
192 24 : set_impl(
193 : sc,
194 : static_cast<
195 : unsigned short>(sc),
196 : obsolete_reason(sc),
197 : v);
198 24 : }
199 :
200 : void
201 12 : set_start_line(
202 : unsigned short si,
203 : core::string_view reason,
204 : http_proto::version v)
205 : {
206 12 : set_impl(
207 : int_to_status(si),
208 : si,
209 : reason,
210 : v);
211 12 : }
212 :
213 : private:
214 : BOOST_HTTP_PROTO_DECL
215 : void
216 : set_impl(
217 : http_proto::status sc,
218 : unsigned short si,
219 : core::string_view reason,
220 : http_proto::version v);
221 : };
222 :
223 : } // http_proto
224 : } // boost
225 :
226 : #endif
|