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