| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) | ||
| 3 | // | ||
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| 6 | // | ||
| 7 | // Official repository: https://github.com/cppalliance/http_proto | ||
| 8 | // | ||
| 9 | |||
| 10 | #ifndef BOOST_HTTP_PROTO_RESPONSE_VIEW_HPP | ||
| 11 | #define BOOST_HTTP_PROTO_RESPONSE_VIEW_HPP | ||
| 12 | |||
| 13 | #include <boost/http_proto/detail/config.hpp> | ||
| 14 | #include <boost/http_proto/message_view_base.hpp> | ||
| 15 | #include <boost/core/detail/string_view.hpp> | ||
| 16 | |||
| 17 | namespace boost { | ||
| 18 | namespace http_proto { | ||
| 19 | |||
| 20 | /** A view to a valid HTTP response. | ||
| 21 | |||
| 22 | Objects of this type represent a view to | ||
| 23 | a HTTP response container. That is, it acts | ||
| 24 | like a `core::string_view` in terms of | ||
| 25 | ownership. The caller is responsible for | ||
| 26 | ensuring that the lifetime of the underlying | ||
| 27 | buffer extends until it is no | ||
| 28 | longer referenced. | ||
| 29 | |||
| 30 | @see | ||
| 31 | @ref response, | ||
| 32 | @ref static_response, | ||
| 33 | @ref response_parser, | ||
| 34 | */ | ||
| 35 | class response_view | ||
| 36 | : public message_view_base | ||
| 37 | { | ||
| 38 | friend class response_base; | ||
| 39 | friend class response_parser; | ||
| 40 | |||
| 41 | explicit | ||
| 42 | 63 | response_view( | |
| 43 | detail::header const* ph) noexcept | ||
| 44 | 63 | : fields_view_base(ph) | |
| 45 | { | ||
| 46 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
|
63 | BOOST_ASSERT(ph_->kind == |
| 47 | detail::kind::response); | ||
| 48 | 63 | } | |
| 49 | |||
| 50 | public: | ||
| 51 | |||
| 52 | //-------------------------------------------- | ||
| 53 | // | ||
| 54 | // Special Members | ||
| 55 | // | ||
| 56 | //-------------------------------------------- | ||
| 57 | |||
| 58 | /** Constructor. | ||
| 59 | |||
| 60 | A default-constructed response view refer | ||
| 61 | to a valid HTTP 200 OK response with no | ||
| 62 | headers, which always remains valid. | ||
| 63 | |||
| 64 | @par Example | ||
| 65 | @code | ||
| 66 | response_view resv; | ||
| 67 | @endcode | ||
| 68 | |||
| 69 | @par Postconditions | ||
| 70 | @code | ||
| 71 | this->buffer() == "HTTP/1.1 200 OK\r\n\r\n" | ||
| 72 | @endcode | ||
| 73 | |||
| 74 | @par Complexity | ||
| 75 | Constant. | ||
| 76 | */ | ||
| 77 | 4 | response_view() noexcept | |
| 78 | 4 | : fields_view_base( | |
| 79 | detail::header::get_default( | ||
| 80 | 4 | detail::kind::response)) | |
| 81 | { | ||
| 82 | 4 | } | |
| 83 | |||
| 84 | /** Constructor. | ||
| 85 | |||
| 86 | After construction, both response views | ||
| 87 | reference the same underlying buffer. | ||
| 88 | Ownership is not transferred. | ||
| 89 | |||
| 90 | @par Postconditions | ||
| 91 | @code | ||
| 92 | this->buffer().data() == other.buffer().data() | ||
| 93 | @endcode | ||
| 94 | |||
| 95 | @par Complexity | ||
| 96 | Constant. | ||
| 97 | |||
| 98 | @param other The other view. | ||
| 99 | */ | ||
| 100 | 1 | response_view( | |
| 101 | response_view const& other) noexcept = default; | ||
| 102 | |||
| 103 | /** Assignment. | ||
| 104 | |||
| 105 | After assignment, both response views | ||
| 106 | reference the same underlying buffer. | ||
| 107 | Ownership is not transferred. | ||
| 108 | |||
| 109 | @par Postconditions | ||
| 110 | @code | ||
| 111 | this->buffer().data() == other.buffer().data() | ||
| 112 | @endcode | ||
| 113 | |||
| 114 | @par Complexity | ||
| 115 | Constant. | ||
| 116 | |||
| 117 | @param other The other view. | ||
| 118 | @return A reference to this object. | ||
| 119 | */ | ||
| 120 | response_view& | ||
| 121 | 1 | operator=( | |
| 122 | response_view const& other) noexcept = default; | ||
| 123 | |||
| 124 | /** Destructor | ||
| 125 | |||
| 126 | Any reference, iterators, or other views | ||
| 127 | which reference the same underlying | ||
| 128 | buffer remain valid. | ||
| 129 | */ | ||
| 130 | ~response_view() = default; | ||
| 131 | |||
| 132 | //-------------------------------------------- | ||
| 133 | // | ||
| 134 | // Observers | ||
| 135 | // | ||
| 136 | //-------------------------------------------- | ||
| 137 | |||
| 138 | /** Return the reason string | ||
| 139 | |||
| 140 | This field is obsolete in `HTTP/1.1` | ||
| 141 | and should only be used for display | ||
| 142 | purposes. | ||
| 143 | */ | ||
| 144 | core::string_view | ||
| 145 | 4 | reason() const noexcept | |
| 146 | { | ||
| 147 | 8 | return core::string_view( | |
| 148 | 4 | ph_->cbuf + 13, | |
| 149 | 4 | ph_->prefix - 15); | |
| 150 | } | ||
| 151 | |||
| 152 | /** Return the status code. | ||
| 153 | */ | ||
| 154 | http_proto::status | ||
| 155 | 4 | status() const noexcept | |
| 156 | { | ||
| 157 | 4 | return ph_->res.status; | |
| 158 | } | ||
| 159 | |||
| 160 | /** Return the status code integer. | ||
| 161 | */ | ||
| 162 | unsigned short | ||
| 163 | 4 | status_int() const noexcept | |
| 164 | { | ||
| 165 | 4 | return ph_->res.status_int; | |
| 166 | } | ||
| 167 | |||
| 168 | //-------------------------------------------- | ||
| 169 | |||
| 170 | /** Swap. | ||
| 171 | |||
| 172 | Exchanges the view with that of `other`. | ||
| 173 | All iterators and references remain valid. | ||
| 174 | |||
| 175 | If `this == &other`, this function call has no effect. | ||
| 176 | |||
| 177 | @par Example | ||
| 178 | @code | ||
| 179 | response r1(status::ok); | ||
| 180 | response r2(status::bad_request); | ||
| 181 | response_view v1 = r1; | ||
| 182 | response_view v2 = r2; | ||
| 183 | v1.swap(v2); | ||
| 184 | assert(v1.buffer() == "HTTP/1.1 400 Bad Request\r\n\r\n" ); | ||
| 185 | assert(v2.buffer() == "HTTP/1.1 200 OK\r\n\r\n" ); | ||
| 186 | @endcode | ||
| 187 | |||
| 188 | @par Complexity | ||
| 189 | Constant. | ||
| 190 | |||
| 191 | @param other The object to swap with. | ||
| 192 | */ | ||
| 193 | void | ||
| 194 | swap(response_view& other) noexcept | ||
| 195 | { | ||
| 196 | auto ph = ph_; | ||
| 197 | ph_ = other.ph_; | ||
| 198 | ph_ = ph; | ||
| 199 | } | ||
| 200 | |||
| 201 | /** Swap. | ||
| 202 | |||
| 203 | Exchanges the view of `v0` with | ||
| 204 | another `v1`. All iterators and | ||
| 205 | references remain valid. | ||
| 206 | |||
| 207 | If `&v0 == &v1`, this function call has no effect. | ||
| 208 | |||
| 209 | @par Example | ||
| 210 | @code | ||
| 211 | response r1(status::ok); | ||
| 212 | response r2(status::bad_request); | ||
| 213 | response_view v1 = r1; | ||
| 214 | response_view v2 = r2; | ||
| 215 | std::swap(v1, v2); | ||
| 216 | assert(v1.buffer() == "HTTP/1.1 400 Bad Request\r\n\r\n" ); | ||
| 217 | assert(v2.buffer() == "HTTP/1.1 200 OK\r\n\r\n" ); | ||
| 218 | @endcode | ||
| 219 | |||
| 220 | @par Effects | ||
| 221 | @code | ||
| 222 | v0.swap(v1); | ||
| 223 | @endcode | ||
| 224 | |||
| 225 | @par Complexity | ||
| 226 | Constant. | ||
| 227 | |||
| 228 | @param v0 The first object to swap. | ||
| 229 | @param v1 The second object to swap. | ||
| 230 | |||
| 231 | @see | ||
| 232 | @ref response_view::swap. | ||
| 233 | */ | ||
| 234 | friend | ||
| 235 | void | ||
| 236 | swap( | ||
| 237 | response_view& v0, | ||
| 238 | response_view& v1) noexcept | ||
| 239 | { | ||
| 240 | v0.swap(v1); | ||
| 241 | } | ||
| 242 | }; | ||
| 243 | |||
| 244 | } // http_proto | ||
| 245 | } // boost | ||
| 246 | |||
| 247 | #endif | ||
| 248 |