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