include/boost/burl/response_head.hpp
100.0% Lines (9/9)
100.0% List of functions (3/3)
100.0% Branches (1/1)
Functions (3)
Function
Calls
Lines
Branches
Blocks
boost::burl::response_head::response_head(boost::http::status, boost::http::version)
:139
53x
100.0%
100.0%
80.0%
boost::burl::swap(boost::burl::response_head&, boost::burl::response_head&)
:251
1x
100.0%
–
100.0%
boost::burl::response_head::static_() const
:304
512x
100.0%
–
100.0%
| Line | Branch | TLA | Hits | Source Code |
|---|---|---|---|---|
| 1 | // | |||
| 2 | // Copyright (c) 2021 Vinnie Falco ([email protected]) | |||
| 3 | // Copyright (c) 2026 Mohammad Nejati | |||
| 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/burl | |||
| 9 | // | |||
| 10 | ||||
| 11 | #ifndef BOOST_BURL_RESPONSE_HEAD_HPP | |||
| 12 | #define BOOST_BURL_RESPONSE_HEAD_HPP | |||
| 13 | ||||
| 14 | #include <boost/burl/detail/config.hpp> | |||
| 15 | #include <boost/burl/response_head_base.hpp> | |||
| 16 | ||||
| 17 | #include <cstddef> | |||
| 18 | ||||
| 19 | namespace boost | |||
| 20 | { | |||
| 21 | namespace burl | |||
| 22 | { | |||
| 23 | ||||
| 24 | /** A dynamic container for an HTTP response header. | |||
| 25 | ||||
| 26 | This container builds a response header in a single | |||
| 27 | allocation which grows as needed, mirroring | |||
| 28 | @ref fields for the field section while adding the | |||
| 29 | status line. It owns its storage and never runs | |||
| 30 | out of room. | |||
| 31 | ||||
| 32 | A default-constructed object holds the default | |||
| 33 | status line ("HTTP/1.1 200 OK") and no fields; | |||
| 34 | the status line and fields are filled in through | |||
| 35 | the inherited @ref response_head_base interface. | |||
| 36 | Like @ref fields, a default-constructed object is a | |||
| 37 | non-owning view over a shared buffer and does not | |||
| 38 | allocate until it is first modified. | |||
| 39 | ||||
| 40 | @ref message_head_base::buffer returns the complete | |||
| 41 | header bytes ready for the wire. | |||
| 42 | ||||
| 43 | @see | |||
| 44 | @ref static_response_head, | |||
| 45 | @ref response_head_base. | |||
| 46 | */ | |||
| 47 | class response_head : public response_head_base | |||
| 48 | { | |||
| 49 | public: | |||
| 50 | /** Constructor. | |||
| 51 | ||||
| 52 | The header holds the default status line and | |||
| 53 | no fields. No allocation is performed until the | |||
| 54 | header is first modified. | |||
| 55 | ||||
| 56 | @par Exception Safety | |||
| 57 | No-throw guarantee. | |||
| 58 | */ | |||
| 59 | BOOST_BURL_DECL | |||
| 60 | response_head() noexcept; | |||
| 61 | ||||
| 62 | /** Destructor. | |||
| 63 | ||||
| 64 | Releases the allocation. All views obtained | |||
| 65 | from the header are invalidated. | |||
| 66 | */ | |||
| 67 | BOOST_BURL_DECL | |||
| 68 | ~response_head(); | |||
| 69 | ||||
| 70 | /** Constructor. | |||
| 71 | ||||
| 72 | The container acquires ownership of the | |||
| 73 | contents of `other`, which is left in a | |||
| 74 | valid but unspecified state and must not be | |||
| 75 | used except to be destroyed or assigned to. | |||
| 76 | ||||
| 77 | @par Complexity | |||
| 78 | Constant. | |||
| 79 | */ | |||
| 80 | BOOST_BURL_DECL | |||
| 81 | response_head(response_head&& other) noexcept; | |||
| 82 | ||||
| 83 | /** Constructor. | |||
| 84 | ||||
| 85 | The contents of `other` are copied into an | |||
| 86 | exact-fit allocation. | |||
| 87 | ||||
| 88 | @par Complexity | |||
| 89 | Linear in `other.buffer().size()`. | |||
| 90 | ||||
| 91 | @par Exception Safety | |||
| 92 | Strong guarantee. Calls to allocate may throw. | |||
| 93 | */ | |||
| 94 | BOOST_BURL_DECL | |||
| 95 | response_head(response_head const& other); | |||
| 96 | ||||
| 97 | /** Constructor. | |||
| 98 | ||||
| 99 | The contents of `other` are copied into an | |||
| 100 | exact-fit allocation, taking ownership of a | |||
| 101 | header that may otherwise refer to external | |||
| 102 | storage, such as the one produced by | |||
| 103 | @ref head_parser. | |||
| 104 | ||||
| 105 | The conversion is lossless and implicit: an | |||
| 106 | owning @ref response_head carries exactly the | |||
| 107 | state of a @ref response_head_base. | |||
| 108 | ||||
| 109 | @par Complexity | |||
| 110 | Linear in `other.buffer().size()`. | |||
| 111 | ||||
| 112 | @par Exception Safety | |||
| 113 | Strong guarantee. Calls to allocate may throw. | |||
| 114 | ||||
| 115 | @param other The header to copy. | |||
| 116 | */ | |||
| 117 | BOOST_BURL_DECL | |||
| 118 | response_head(response_head_base const& other); | |||
| 119 | ||||
| 120 | /** Constructor. | |||
| 121 | ||||
| 122 | The header holds a status line with the given | |||
| 123 | status code and version, and no fields. The | |||
| 124 | reason-phrase is set to the standard text for | |||
| 125 | the status code. | |||
| 126 | ||||
| 127 | @par Exception Safety | |||
| 128 | Strong guarantee. Calls to allocate may throw. | |||
| 129 | ||||
| 130 | @throw std::length_error | |||
| 131 | The storage cannot accommodate the status line. | |||
| 132 | ||||
| 133 | @param sc The status code. Must have an | |||
| 134 | integer value in [100, 999]. | |||
| 135 | ||||
| 136 | @param v The version. | |||
| 137 | */ | |||
| 138 | explicit | |||
| 139 | 53x | response_head( | ||
| 140 | http::status sc, | |||
| 141 | http::version v = | |||
| 142 | http::version::http_1_1) | |||
| 143 | 53x | : response_head() | ||
| 144 | { | |||
| 145 |
1/1✓ Branch 1 taken 53 times.
|
53x | set_start_line(sc, v); | |
| 146 | 53x | } | ||
| 147 | ||||
| 148 | /** Assignment. | |||
| 149 | ||||
| 150 | The container acquires ownership of the | |||
| 151 | contents of `other`, which is left in a | |||
| 152 | valid but unspecified state. The previous | |||
| 153 | contents are released. | |||
| 154 | ||||
| 155 | @par Complexity | |||
| 156 | Constant. | |||
| 157 | */ | |||
| 158 | BOOST_BURL_DECL | |||
| 159 | response_head& | |||
| 160 | operator=(response_head&& other) noexcept; | |||
| 161 | ||||
| 162 | /** Assignment. | |||
| 163 | ||||
| 164 | The contents are replaced with a copy of | |||
| 165 | `other`. | |||
| 166 | ||||
| 167 | @par Complexity | |||
| 168 | Linear in `other.buffer().size()`. | |||
| 169 | ||||
| 170 | @par Exception Safety | |||
| 171 | Strong guarantee. Calls to allocate may throw. | |||
| 172 | */ | |||
| 173 | BOOST_BURL_DECL | |||
| 174 | response_head& | |||
| 175 | operator=(response_head const& other); | |||
| 176 | ||||
| 177 | /** Assignment. | |||
| 178 | ||||
| 179 | The contents are replaced with a copy of | |||
| 180 | `other`, taking ownership of a header that | |||
| 181 | may otherwise refer to external storage. | |||
| 182 | ||||
| 183 | @par Complexity | |||
| 184 | Linear in `other.buffer().size()`. | |||
| 185 | ||||
| 186 | @par Exception Safety | |||
| 187 | Strong guarantee. Calls to allocate may throw. | |||
| 188 | ||||
| 189 | @param other The header to copy. | |||
| 190 | */ | |||
| 191 | BOOST_BURL_DECL | |||
| 192 | response_head& | |||
| 193 | operator=(response_head_base const& other); | |||
| 194 | ||||
| 195 | /** Swap the contents. | |||
| 196 | ||||
| 197 | The contents of the two headers are | |||
| 198 | exchanged. No allocation occurs and no | |||
| 199 | bytes are copied. | |||
| 200 | ||||
| 201 | Views obtained from either header remain | |||
| 202 | valid; they follow the contents into the | |||
| 203 | other header. Iterators are invalidated: an | |||
| 204 | iterator stays bound to the header it was | |||
| 205 | obtained from, which now holds different | |||
| 206 | fields. | |||
| 207 | ||||
| 208 | If `this == &other`, this function call has | |||
| 209 | no effect. | |||
| 210 | ||||
| 211 | @par Complexity | |||
| 212 | Constant. | |||
| 213 | ||||
| 214 | @par Exception Safety | |||
| 215 | No-throw guarantee. | |||
| 216 | ||||
| 217 | @param other The header to swap with. | |||
| 218 | */ | |||
| 219 | BOOST_BURL_DECL | |||
| 220 | void | |||
| 221 | swap(response_head& other) noexcept; | |||
| 222 | ||||
| 223 | /** Swap the contents. | |||
| 224 | ||||
| 225 | The contents of the two headers are | |||
| 226 | exchanged. No allocation occurs and no | |||
| 227 | bytes are copied. | |||
| 228 | ||||
| 229 | If `&v0 == &v1`, this function call has no | |||
| 230 | effect. | |||
| 231 | ||||
| 232 | @par Effects | |||
| 233 | @code | |||
| 234 | v0.swap(v1); | |||
| 235 | @endcode | |||
| 236 | ||||
| 237 | @par Complexity | |||
| 238 | Constant. | |||
| 239 | ||||
| 240 | @par Exception Safety | |||
| 241 | No-throw guarantee. | |||
| 242 | ||||
| 243 | @param v0 The first header to swap. | |||
| 244 | ||||
| 245 | @param v1 The second header to swap. | |||
| 246 | ||||
| 247 | @see @ref response_head::swap | |||
| 248 | */ | |||
| 249 | friend | |||
| 250 | void | |||
| 251 | 1x | swap( | ||
| 252 | response_head& v0, | |||
| 253 | response_head& v1) noexcept | |||
| 254 | { | |||
| 255 | 1x | v0.swap(v1); | ||
| 256 | 1x | } | ||
| 257 | ||||
| 258 | /** Reserve storage. | |||
| 259 | ||||
| 260 | Ensures the field section can grow to `bytes` | |||
| 261 | bytes and the number of fields to `count` | |||
| 262 | without reallocating. Has no effect if the | |||
| 263 | current allocation is already sufficient. | |||
| 264 | ||||
| 265 | The status line shares the allocation with the | |||
| 266 | field section, so a status line larger than the | |||
| 267 | one currently stored may still reallocate. | |||
| 268 | ||||
| 269 | All views are invalidated when a reallocation | |||
| 270 | occurs. | |||
| 271 | ||||
| 272 | @par Exception Safety | |||
| 273 | Strong guarantee. Calls to allocate may throw. | |||
| 274 | ||||
| 275 | @throw std::length_error | |||
| 276 | `bytes` exceeds @ref max_buffer_size, or | |||
| 277 | `count` exceeds @ref max_field_count. | |||
| 278 | ||||
| 279 | @param bytes The serialized field-section size, | |||
| 280 | as returned by `fields_base::buffer().size()`. | |||
| 281 | ||||
| 282 | @param count The number of fields. | |||
| 283 | */ | |||
| 284 | BOOST_BURL_DECL | |||
| 285 | void | |||
| 286 | reserve( | |||
| 287 | std::size_t bytes, | |||
| 288 | std::size_t count); | |||
| 289 | ||||
| 290 | /** Remove excess capacity. | |||
| 291 | ||||
| 292 | All views are invalidated when a reallocation | |||
| 293 | occurs. | |||
| 294 | ||||
| 295 | @par Exception Safety | |||
| 296 | Strong guarantee. Calls to allocate may throw. | |||
| 297 | */ | |||
| 298 | BOOST_BURL_DECL | |||
| 299 | void | |||
| 300 | shrink_to_fit(); | |||
| 301 | ||||
| 302 | private: | |||
| 303 | bool | |||
| 304 | 512x | static_() const noexcept override | ||
| 305 | { | |||
| 306 | 512x | return false; | ||
| 307 | } | |||
| 308 | }; | |||
| 309 | ||||
| 310 | } // namespace burl | |||
| 311 | } // namespace boost | |||
| 312 | ||||
| 313 | #endif | |||
| 314 |