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_HPP
13 : #define BOOST_HTTP_PROTO_RESPONSE_HPP
14 :
15 : #include <boost/http_proto/response_base.hpp>
16 :
17 : namespace boost {
18 : namespace http_proto {
19 :
20 : /** Container for HTTP responses
21 : */
22 : class response
23 : : public response_base
24 : {
25 : public:
26 : /** Constructor
27 : */
28 : BOOST_HTTP_PROTO_DECL
29 : response() noexcept;
30 :
31 : /** Constructor
32 : */
33 : BOOST_HTTP_PROTO_DECL
34 : explicit
35 : response(
36 : core::string_view s);
37 :
38 : /** Constructor
39 :
40 : Construct a response container which allocates
41 : `storage_size` bytes for storing the header.
42 : Attempting to grow the container beyond
43 : this amount will result in an exception.
44 : The storage is also used internally to store
45 : instances of an implementation-defined type.
46 : The requested number of bytes will be aligned
47 : accordingly (currently the alignment requirement is 4).
48 :
49 : <br/>
50 :
51 : This constructor is useful when an upper-bound size
52 : of the response is known ahead of time and we want
53 : to prevent reallocations.
54 :
55 : <br/>
56 :
57 : Passing an initial storage size of `0` does not
58 : throw and the maximum capacity is set to an
59 : implementation-defined limit observable via
60 : @ref max_capacity_in_bytes().
61 :
62 : @param storage_size The initial and final size of
63 : the storage.
64 :
65 : @code
66 : boost::http_proto::response
67 : make_response(std::string_view server)
68 : {
69 : std::size_t size = 4096;
70 : // res.buffer() is now stable
71 : boost::http_proto::response res(size);
72 : BOOST_ASSERT(
73 : res.max_capacity_in_bytes(), 4096);
74 :
75 : // uses spare capacity so that reallocations
76 : // are avoided
77 : res.append(
78 : boost::http_proto::field::server, server);
79 : res.append(
80 : boost::http_proto::field::connection, "close");
81 : return res;
82 : }
83 : @endcode
84 : */
85 : BOOST_HTTP_PROTO_DECL
86 : explicit
87 : response(
88 : std::size_t storage_size);
89 :
90 : /** Constructor
91 :
92 : Construct a response container which allocates
93 : `storage_size` bytes for storing the header, with an
94 : upper limit of `max_storage_size`. Attempting to
95 : grow the container beyond its maximum will result in
96 : an exception. The storage is also used internally to
97 : store instances of an implementation-defined type.
98 : Both values will be aligned accordingly (currently
99 : the alignment requirement is 4).
100 :
101 : <br/>
102 :
103 : This constructor is useful when there's a best-fit
104 : guess for an initial header size but we still wish
105 : to permit reallocating and growing the container to
106 : some upper limit.
107 :
108 : <br/>
109 :
110 : Passing an initial size of `0` does not throw.
111 :
112 : @param storage_size The initial size of the storage.
113 :
114 : @param max_storage_size The maximum size of the
115 : allocated storage. Any operation that attempts to
116 : grow the container beyond this value throws
117 : `std::length_error`.
118 :
119 : @throws std::length_error Thrown if `size > max_size`
120 :
121 : @code
122 : boost::http_proto::response
123 : make_response(std::string_view host)
124 : {
125 : std::size_t size = 4096;
126 : boost::http_proto::response res(size, 2 * size);
127 : BOOST_ASSERT(
128 : res.max_capacity_in_bytes(), 2 * 4096);
129 :
130 : // uses spare capacity so that reallocations
131 : // are avoided
132 : res.append(
133 : boost::http_proto::field::host, host);
134 : res.append(
135 : boost::http_proto::field::connection, "close");
136 : return res;
137 : }
138 : @endcode
139 : */
140 : BOOST_HTTP_PROTO_DECL
141 : response(
142 : std::size_t storage_size,
143 : std::size_t max_storage_size);
144 :
145 : /** Constructor
146 : */
147 : BOOST_HTTP_PROTO_DECL
148 : response(
149 : http_proto::status sc,
150 : http_proto::version v);
151 :
152 : /** Constructor
153 : *
154 : * The start-line of the response will contain the standard
155 : * text for the supplied status code and the HTTP version
156 : * will be defaulted to 1.1.
157 : */
158 : BOOST_HTTP_PROTO_DECL
159 : explicit
160 : response(
161 : http_proto::status sc);
162 :
163 : /** Constructor
164 :
165 : The moved-from object will be
166 : left in the default-constructed
167 : state.
168 : */
169 : BOOST_HTTP_PROTO_DECL
170 : response(response&& other) noexcept;
171 :
172 : /** Constructor
173 : */
174 : BOOST_HTTP_PROTO_DECL
175 : response(response const& other);
176 :
177 : /** Constructor
178 : */
179 : BOOST_HTTP_PROTO_DECL
180 : response(
181 : response_view const& other);
182 :
183 : /** Assignment
184 : */
185 : BOOST_HTTP_PROTO_DECL
186 : response&
187 : operator=(
188 : response&& other) noexcept;
189 :
190 : /** Assignment
191 : */
192 : response&
193 1 : operator=(
194 : response const& other)
195 : {
196 1 : copy_impl(*other.ph_);
197 1 : return *this;
198 : }
199 :
200 : /** Assignment
201 : */
202 : response&
203 1 : operator=(
204 : response_view const& other)
205 : {
206 1 : copy_impl(*other.ph_);
207 1 : return *this;
208 : }
209 :
210 : /** Swap this with another instance
211 : */
212 : void
213 4 : swap(response& other) noexcept
214 : {
215 4 : h_.swap(other.h_);
216 4 : }
217 :
218 : /** Swap two instances
219 : */
220 : // hidden friend
221 : friend
222 : void
223 : swap(
224 : response& t0,
225 : response& t1) noexcept
226 : {
227 : t0.swap(t1);
228 : }
229 : };
230 :
231 : } // http_proto
232 : } // boost
233 :
234 : #endif
|