Line |
Branch |
Exec |
Source |
1 |
|
|
// |
2 |
|
|
// Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com) |
3 |
|
|
// Copyright (c) 2024 Christian Mazakas |
4 |
|
|
// |
5 |
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying |
6 |
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
7 |
|
|
// |
8 |
|
|
// Official repository: https://github.com/cppalliance/http_proto |
9 |
|
|
// |
10 |
|
|
|
11 |
|
|
#ifndef BOOST_HTTP_PROTO_RESPONSE_HPP |
12 |
|
|
#define BOOST_HTTP_PROTO_RESPONSE_HPP |
13 |
|
|
|
14 |
|
|
#include <boost/http_proto/detail/config.hpp> |
15 |
|
|
#include <boost/http_proto/message_base.hpp> |
16 |
|
|
#include <boost/http_proto/response_view.hpp> |
17 |
|
|
#include <boost/http_proto/status.hpp> |
18 |
|
|
|
19 |
|
|
namespace boost { |
20 |
|
|
namespace http_proto { |
21 |
|
|
|
22 |
|
|
/** Container for HTTP responses |
23 |
|
|
*/ |
24 |
|
|
class BOOST_SYMBOL_VISIBLE |
25 |
|
|
response |
26 |
|
|
: public message_base |
27 |
|
|
{ |
28 |
|
|
public: |
29 |
|
|
/** Constructor |
30 |
|
|
*/ |
31 |
|
|
BOOST_HTTP_PROTO_DECL |
32 |
|
|
response() noexcept; |
33 |
|
|
|
34 |
|
|
/** Constructor |
35 |
|
|
*/ |
36 |
|
|
BOOST_HTTP_PROTO_DECL |
37 |
|
|
explicit |
38 |
|
|
response( |
39 |
|
|
core::string_view s); |
40 |
|
|
|
41 |
|
|
/** Constructor |
42 |
|
|
*/ |
43 |
|
|
BOOST_HTTP_PROTO_DECL |
44 |
|
|
explicit |
45 |
|
|
response( |
46 |
|
|
std::size_t initial_size); |
47 |
|
|
|
48 |
|
|
/** Constructor |
49 |
|
|
*/ |
50 |
|
|
BOOST_HTTP_PROTO_DECL |
51 |
|
|
response( |
52 |
|
|
std::size_t initial_size, |
53 |
|
|
std::size_t max_capacity); |
54 |
|
|
|
55 |
|
|
/** Constructor |
56 |
|
|
|
57 |
|
|
The moved-from object will be |
58 |
|
|
left in the default-constructed |
59 |
|
|
state. |
60 |
|
|
*/ |
61 |
|
|
BOOST_HTTP_PROTO_DECL |
62 |
|
|
response(response&& other) noexcept; |
63 |
|
|
|
64 |
|
|
/** Constructor |
65 |
|
|
*/ |
66 |
|
|
BOOST_HTTP_PROTO_DECL |
67 |
|
|
response(response const& other); |
68 |
|
|
|
69 |
|
|
/** Constructor |
70 |
|
|
*/ |
71 |
|
|
BOOST_HTTP_PROTO_DECL |
72 |
|
|
response( |
73 |
|
|
response_view const& other); |
74 |
|
|
|
75 |
|
|
/** Assignment |
76 |
|
|
*/ |
77 |
|
|
BOOST_HTTP_PROTO_DECL |
78 |
|
|
response& |
79 |
|
|
operator=( |
80 |
|
|
response&& other) noexcept; |
81 |
|
|
|
82 |
|
|
/** Assignment |
83 |
|
|
*/ |
84 |
|
|
response& |
85 |
|
3 |
operator=( |
86 |
|
|
response const& other) |
87 |
|
|
{ |
88 |
|
3 |
copy_impl(*other.ph_); |
89 |
|
3 |
return *this; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
/** Assignment |
93 |
|
|
*/ |
94 |
|
|
response& |
95 |
|
1 |
operator=( |
96 |
|
|
response_view const& other) |
97 |
|
|
{ |
98 |
|
1 |
copy_impl(*other.ph_); |
99 |
|
1 |
return *this; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
/** Constructor |
103 |
|
|
*/ |
104 |
|
|
BOOST_HTTP_PROTO_DECL |
105 |
|
|
response( |
106 |
|
|
http_proto::status sc, |
107 |
|
|
http_proto::version v); |
108 |
|
|
|
109 |
|
|
/** Constructor |
110 |
|
|
* |
111 |
|
|
* The start-line of the response will contain the standard |
112 |
|
|
* text for the supplied status code and the HTTP version |
113 |
|
|
* will be defaulted to 1.1. |
114 |
|
|
*/ |
115 |
|
|
BOOST_HTTP_PROTO_DECL |
116 |
|
|
explicit |
117 |
|
|
response( |
118 |
|
|
http_proto::status sc); |
119 |
|
|
|
120 |
|
|
/** Return a read-only view to the response |
121 |
|
|
*/ |
122 |
|
5 |
operator |
123 |
|
|
response_view() const noexcept |
124 |
|
|
{ |
125 |
|
5 |
return response_view(ph_); |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
//-------------------------------------------- |
129 |
|
|
// |
130 |
|
|
// Observers |
131 |
|
|
// |
132 |
|
|
//-------------------------------------------- |
133 |
|
|
|
134 |
|
|
/** Return the reason string |
135 |
|
|
|
136 |
|
|
This field is obsolete in HTTP/1 |
137 |
|
|
and should only be used for display |
138 |
|
|
purposes. |
139 |
|
|
*/ |
140 |
|
|
core::string_view |
141 |
|
28 |
reason() const noexcept |
142 |
|
|
{ |
143 |
|
56 |
return core::string_view( |
144 |
|
28 |
ph_->cbuf + 13, |
145 |
|
28 |
ph_->prefix - 15); |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
/** Return the status code |
149 |
|
|
*/ |
150 |
|
|
http_proto::status |
151 |
|
28 |
status() const noexcept |
152 |
|
|
{ |
153 |
|
28 |
return ph_->res.status; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
/** Return the status code |
157 |
|
|
*/ |
158 |
|
|
unsigned short |
159 |
|
28 |
status_int() const noexcept |
160 |
|
|
{ |
161 |
|
28 |
return ph_->res.status_int; |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
/** Return the HTTP version |
165 |
|
|
*/ |
166 |
|
|
http_proto::version |
167 |
|
28 |
version() const noexcept |
168 |
|
|
{ |
169 |
|
28 |
return ph_->version; |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
//-------------------------------------------- |
173 |
|
|
// |
174 |
|
|
// Modifiers |
175 |
|
|
// |
176 |
|
|
//-------------------------------------------- |
177 |
|
|
|
178 |
|
|
/** Set the version, status code of the response |
179 |
|
|
|
180 |
|
|
The reason phrase will be set to the |
181 |
|
|
standard text for the specified status |
182 |
|
|
code. |
183 |
|
|
|
184 |
|
|
@par sc The status code. This must not be |
185 |
|
|
@ref http_proto::status::unknown. |
186 |
|
|
|
187 |
|
|
@par v The HTTP-version. |
188 |
|
|
*/ |
189 |
|
|
void |
190 |
|
13 |
set_start_line( |
191 |
|
|
http_proto::status sc, |
192 |
|
|
http_proto::version v = |
193 |
|
|
http_proto::version::http_1_1) |
194 |
|
|
{ |
195 |
|
13 |
set_impl( |
196 |
|
|
sc, |
197 |
|
|
static_cast< |
198 |
|
|
unsigned short>(sc), |
199 |
|
|
obsolete_reason(sc), |
200 |
|
|
v); |
201 |
|
13 |
} |
202 |
|
|
|
203 |
|
|
void |
204 |
|
6 |
set_start_line( |
205 |
|
|
unsigned short si, |
206 |
|
|
core::string_view reason, |
207 |
|
|
http_proto::version v) |
208 |
|
|
{ |
209 |
|
6 |
set_impl( |
210 |
|
|
int_to_status(si), |
211 |
|
|
si, |
212 |
|
|
reason, |
213 |
|
|
v); |
214 |
|
6 |
} |
215 |
|
|
|
216 |
|
|
/** Swap this with another instance |
217 |
|
|
*/ |
218 |
|
|
void |
219 |
|
6 |
swap(response& other) noexcept |
220 |
|
|
{ |
221 |
|
6 |
h_.swap(other.h_); |
222 |
|
6 |
} |
223 |
|
|
|
224 |
|
|
/** Swap two instances |
225 |
|
|
*/ |
226 |
|
|
// hidden friend |
227 |
|
|
friend |
228 |
|
|
void |
229 |
|
|
swap( |
230 |
|
|
response& t0, |
231 |
|
|
response& t1) noexcept |
232 |
|
|
{ |
233 |
|
|
t0.swap(t1); |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
private: |
237 |
|
|
BOOST_HTTP_PROTO_DECL |
238 |
|
|
void |
239 |
|
|
set_impl( |
240 |
|
|
http_proto::status sc, |
241 |
|
|
unsigned short si, |
242 |
|
|
core::string_view reason, |
243 |
|
|
http_proto::version v); |
244 |
|
|
}; |
245 |
|
|
|
246 |
|
|
} // http_proto |
247 |
|
|
} // boost |
248 |
|
|
|
249 |
|
|
#endif |
250 |
|
|
|