| Line | Branch | Exec | Source |
|---|---|---|---|
| 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_BASE_HPP | ||
| 13 | #define BOOST_HTTP_PROTO_RESPONSE_BASE_HPP | ||
| 14 | |||
| 15 | #include <boost/http_proto/detail/config.hpp> | ||
| 16 | #include <boost/http_proto/message_base.hpp> | ||
| 17 | #include <boost/http_proto/status.hpp> | ||
| 18 | |||
| 19 | namespace boost { | ||
| 20 | namespace http_proto { | ||
| 21 | |||
| 22 | /** Mixin for modifing HTTP responses. | ||
| 23 | |||
| 24 | @see | ||
| 25 | @ref message_base, | ||
| 26 | @ref response, | ||
| 27 | @ref static_response. | ||
| 28 | */ | ||
| 29 | class response_base | ||
| 30 | : public message_base | ||
| 31 | { | ||
| 32 | friend class response; | ||
| 33 | friend class static_response; | ||
| 34 | |||
| 35 | 103 | response_base() noexcept | |
| 36 | 103 | : message_base(detail::kind::response) | |
| 37 | { | ||
| 38 | 103 | } | |
| 39 | |||
| 40 | explicit | ||
| 41 | 100 | response_base(core::string_view s) | |
| 42 | 100 | : message_base(detail::kind::response, s) | |
| 43 | { | ||
| 44 | 99 | } | |
| 45 | |||
| 46 | 6 | response_base( | |
| 47 | void* storage, | ||
| 48 | std::size_t cap) noexcept | ||
| 49 | 6 | : message_base( | |
| 50 | 6 | detail::kind::response, storage, cap) | |
| 51 | { | ||
| 52 | 6 | } | |
| 53 | |||
| 54 | public: | ||
| 55 | //-------------------------------------------- | ||
| 56 | // | ||
| 57 | // Observers | ||
| 58 | // | ||
| 59 | //-------------------------------------------- | ||
| 60 | |||
| 61 | /** Return the reason string. | ||
| 62 | |||
| 63 | This field is obsolete in HTTP/1 | ||
| 64 | and should only be used for display | ||
| 65 | purposes. | ||
| 66 | */ | ||
| 67 | core::string_view | ||
| 68 | 31 | reason() const noexcept | |
| 69 | { | ||
| 70 | 62 | return core::string_view( | |
| 71 | 31 | h_.cbuf + 13, | |
| 72 | 31 | h_.prefix - 15); | |
| 73 | } | ||
| 74 | |||
| 75 | /** Return the status code. | ||
| 76 | */ | ||
| 77 | http_proto::status | ||
| 78 | 32 | status() const noexcept | |
| 79 | { | ||
| 80 | 32 | return h_.res.status; | |
| 81 | } | ||
| 82 | |||
| 83 | /** Return the status code as an integral. | ||
| 84 | */ | ||
| 85 | unsigned short | ||
| 86 | 32 | status_int() const noexcept | |
| 87 | { | ||
| 88 | 32 | return h_.res.status_int; | |
| 89 | } | ||
| 90 | |||
| 91 | //-------------------------------------------- | ||
| 92 | // | ||
| 93 | // Modifiers | ||
| 94 | // | ||
| 95 | //-------------------------------------------- | ||
| 96 | |||
| 97 | /** Set the status code and version of the response. | ||
| 98 | |||
| 99 | The reason-phrase will be set to the | ||
| 100 | standard text for the specified status | ||
| 101 | code. | ||
| 102 | |||
| 103 | This is more efficient than setting the | ||
| 104 | properties individually. | ||
| 105 | |||
| 106 | @par Exception Safety | ||
| 107 | Strong guarantee. | ||
| 108 | Calls to allocate may throw. | ||
| 109 | Exception thrown if max capacity exceeded. | ||
| 110 | |||
| 111 | @throw std::length_error | ||
| 112 | Max capacity would be exceeded. | ||
| 113 | @throw std::invalid_argument | ||
| 114 | `sc == status::unknown` | ||
| 115 | |||
| 116 | @param sc The status code to set. This | ||
| 117 | must not be @ref status::unknown. | ||
| 118 | |||
| 119 | @param v The version to set. | ||
| 120 | */ | ||
| 121 | void | ||
| 122 | 13 | set_start_line( | |
| 123 | http_proto::status sc, | ||
| 124 | http_proto::version v = | ||
| 125 | http_proto::version::http_1_1) | ||
| 126 | { | ||
| 127 | 13 | set_start_line_impl(sc, | |
| 128 | static_cast<unsigned short>(sc), | ||
| 129 | obsolete_reason(sc), v); | ||
| 130 | 13 | } | |
| 131 | |||
| 132 | /** Set the HTTP version of the response | ||
| 133 | |||
| 134 | @par Exception Safety | ||
| 135 | Strong guarantee. | ||
| 136 | Calls to allocate may throw. | ||
| 137 | Exception thrown if maximum capacity exceeded. | ||
| 138 | |||
| 139 | @throw std::length_error | ||
| 140 | Maximum capacity would be exceeded. | ||
| 141 | |||
| 142 | @param v The version to set. | ||
| 143 | */ | ||
| 144 | BOOST_HTTP_PROTO_DECL | ||
| 145 | void | ||
| 146 | set_version( | ||
| 147 | http_proto::version v); | ||
| 148 | |||
| 149 | /** Set the status code of the response. | ||
| 150 | |||
| 151 | The reason-phrase will be set to the | ||
| 152 | standard text for the specified status | ||
| 153 | code. The version will remain unchanged. | ||
| 154 | |||
| 155 | @par Exception Safety | ||
| 156 | Strong guarantee. | ||
| 157 | Calls to allocate may throw. | ||
| 158 | Exception thrown if maximum capacity exceeded. | ||
| 159 | |||
| 160 | @throw std::length_error | ||
| 161 | Maximum capacity would be exceeded. | ||
| 162 | @throw std::invalid_argument | ||
| 163 | `sc == status::unknown` | ||
| 164 | |||
| 165 | @param sc The status code to set. This | ||
| 166 | must not be @ref status::unknown. | ||
| 167 | */ | ||
| 168 | void | ||
| 169 | 3 | set_status( | |
| 170 | http_proto::status sc) | ||
| 171 | { | ||
| 172 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if(sc == http_proto::status::unknown) |
| 173 | ✗ | detail::throw_invalid_argument(); | |
| 174 | 3 | set_start_line_impl(sc, | |
| 175 | static_cast<unsigned short>(sc), | ||
| 176 | obsolete_reason(sc), | ||
| 177 | version()); | ||
| 178 | 3 | } | |
| 179 | |||
| 180 | /** Set the status code and version of the response. | ||
| 181 | |||
| 182 | The reason-phrase will be set to the | ||
| 183 | standard text for the specified status | ||
| 184 | code. | ||
| 185 | |||
| 186 | This is more efficient than setting the | ||
| 187 | properties individually. | ||
| 188 | |||
| 189 | @par Exception Safety | ||
| 190 | Strong guarantee. | ||
| 191 | Calls to allocate may throw. | ||
| 192 | Exception thrown on invalid input. | ||
| 193 | Exception thrown if max capacity exceeded. | ||
| 194 | |||
| 195 | @throw system_error | ||
| 196 | Input is invalid. | ||
| 197 | |||
| 198 | @throw std::length_error | ||
| 199 | Max capacity would be exceeded. | ||
| 200 | |||
| 201 | @param si An integral representing the | ||
| 202 | status code to set. | ||
| 203 | |||
| 204 | @param reason A string view representing the | ||
| 205 | reason string to set. | ||
| 206 | |||
| 207 | @param v The version to set. | ||
| 208 | */ | ||
| 209 | void | ||
| 210 | 8 | set_start_line( | |
| 211 | unsigned short si, | ||
| 212 | core::string_view reason, | ||
| 213 | http_proto::version v = | ||
| 214 | http_proto::version::http_1_1) | ||
| 215 | { | ||
| 216 | 8 | set_start_line_impl( | |
| 217 | int_to_status(si), | ||
| 218 | si, | ||
| 219 | reason, | ||
| 220 | v); | ||
| 221 | 7 | } | |
| 222 | |||
| 223 | private: | ||
| 224 | BOOST_HTTP_PROTO_DECL | ||
| 225 | void | ||
| 226 | set_start_line_impl( | ||
| 227 | http_proto::status sc, | ||
| 228 | unsigned short si, | ||
| 229 | core::string_view reason, | ||
| 230 | http_proto::version v); | ||
| 231 | }; | ||
| 232 | |||
| 233 | } // http_proto | ||
| 234 | } // boost | ||
| 235 | |||
| 236 | #endif | ||
| 237 |