libs/http/include/boost/http/static_response.hpp
100.0% Lines (16/16)
100.0% Functions (4/4)
-% Branches (0/0)
libs/http/include/boost/http/static_response.hpp
| Line | Hits | Source Code |
|---|---|---|
| 1 | // | |
| 2 | // Copyright (c) 2025 Mohammad Nejati | |
| 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 | |
| 8 | // | |
| 9 | ||
| 10 | #ifndef BOOST_HTTP_STATIC_RESPONSE_HPP | |
| 11 | #define BOOST_HTTP_STATIC_RESPONSE_HPP | |
| 12 | ||
| 13 | #include <boost/http/response_base.hpp> | |
| 14 | ||
| 15 | namespace boost { | |
| 16 | namespace http { | |
| 17 | ||
| 18 | /** A modifiable static container for HTTP responses. | |
| 19 | ||
| 20 | This container uses an external memory | |
| 21 | storage with fixed capacity. | |
| 22 | The contents may be inspected and modified, | |
| 23 | and the implementation maintains a useful | |
| 24 | invariant: changes to the response always | |
| 25 | leave it in a valid state. | |
| 26 | ||
| 27 | @par Example | |
| 28 | @code | |
| 29 | char buf[1024]; | |
| 30 | static_response res(buf, sizeof(buf)); | |
| 31 | ||
| 32 | res.set_start_line(status::not_found); | |
| 33 | res.set(field::server, "Boost.HTTP"); | |
| 34 | res.set(field::content_type, "text/plain"); | |
| 35 | res.set_content_length(80); | |
| 36 | ||
| 37 | assert(res.buffer() == | |
| 38 | "HTTP/1.1 404 Not Found\r\n" | |
| 39 | "Server: Boost.HTTP\r\n" | |
| 40 | "Content-Type: text/plain\r\n" | |
| 41 | "Content-Length: 80\r\n" | |
| 42 | "\r\n"); | |
| 43 | @endcode | |
| 44 | ||
| 45 | @par Invariants | |
| 46 | @code | |
| 47 | this->capacity_in_bytes() == Capacity && this->max_capacity_in_bytes() == Capacity | |
| 48 | @endcode | |
| 49 | ||
| 50 | @tparam Capacity The maximum capacity in bytes. | |
| 51 | ||
| 52 | @see | |
| 53 | @ref response, | |
| 54 | @ref response_base. | |
| 55 | */ | |
| 56 | class static_response | |
| 57 | : public response_base | |
| 58 | { | |
| 59 | public: | |
| 60 | //-------------------------------------------- | |
| 61 | // | |
| 62 | // Special Members | |
| 63 | // | |
| 64 | //-------------------------------------------- | |
| 65 | ||
| 66 | /** Constructor. | |
| 67 | ||
| 68 | Constructs a response object that uses an | |
| 69 | external memory storage and does not perform | |
| 70 | any allocations during its lifetime. | |
| 71 | ||
| 72 | The caller is responsible for ensuring that the | |
| 73 | lifetime of the storage extends until the | |
| 74 | response object is destroyed. | |
| 75 | ||
| 76 | @par Postcondition | |
| 77 | @code | |
| 78 | this->capacity_in_bytes() == cap | |
| 79 | this->max_capacity_in_bytes() == cap | |
| 80 | @endcode | |
| 81 | ||
| 82 | @param storage The storage to use. | |
| 83 | @param cap The capacity of the storage. | |
| 84 | */ | |
| 85 | 6 | static_response( |
| 86 | void* storage, | |
| 87 | std::size_t cap) | |
| 88 | 6 | : response_base(storage, cap) |
| 89 | { | |
| 90 | 6 | } |
| 91 | ||
| 92 | /** Constructor (deleted) | |
| 93 | */ | |
| 94 | static_response( | |
| 95 | static_response const&) = delete; | |
| 96 | ||
| 97 | /** Constructor. | |
| 98 | ||
| 99 | The contents of `r` are transferred | |
| 100 | to the newly constructed object, | |
| 101 | which includes the underlying | |
| 102 | character buffer. | |
| 103 | After construction, the moved-from | |
| 104 | object has a valid but unspecified | |
| 105 | state where the only safe operation | |
| 106 | is destruction. | |
| 107 | ||
| 108 | @par Complexity | |
| 109 | Constant. | |
| 110 | ||
| 111 | @param r The response to move from. | |
| 112 | */ | |
| 113 | 1 | static_response( |
| 114 | static_response&& r) noexcept | |
| 115 | 1 | : response_base() |
| 116 | { | |
| 117 | 1 | h_.swap(r.h_); |
| 118 | 1 | external_storage_ = true; |
| 119 | 1 | max_cap_ = r.max_cap_; |
| 120 | 1 | r.max_cap_ = 0; |
| 121 | 1 | } |
| 122 | ||
| 123 | /** Assignment. | |
| 124 | ||
| 125 | The contents of `r` are copied and | |
| 126 | the previous contents of `this` are | |
| 127 | discarded. | |
| 128 | ||
| 129 | @par Postconditions | |
| 130 | @code | |
| 131 | this->buffer() == r.buffer() && this->buffer().data() != r.buffer().data() | |
| 132 | @endcode | |
| 133 | ||
| 134 | @par Complexity | |
| 135 | Linear in `r.size()`. | |
| 136 | ||
| 137 | @par Exception Safety | |
| 138 | Strong guarantee. | |
| 139 | Exception thrown if max capacity exceeded. | |
| 140 | ||
| 141 | @throw std::length_error | |
| 142 | Max capacity would be exceeded. | |
| 143 | ||
| 144 | @param r The response to copy. | |
| 145 | ||
| 146 | @return A reference to this object. | |
| 147 | */ | |
| 148 | static_response& | |
| 149 | 1 | operator=( |
| 150 | static_response const& r) | |
| 151 | { | |
| 152 | 1 | copy_impl(r.h_); |
| 153 | 1 | return *this; |
| 154 | } | |
| 155 | ||
| 156 | /** Assignment. | |
| 157 | ||
| 158 | The contents of `r` are copied and | |
| 159 | the previous contents of `this` are | |
| 160 | discarded. | |
| 161 | ||
| 162 | @par Postconditions | |
| 163 | @code | |
| 164 | this->buffer() == r.buffer() && this->buffer().data() != r.buffer().data() | |
| 165 | @endcode | |
| 166 | ||
| 167 | @par Complexity | |
| 168 | Linear in `r.size()`. | |
| 169 | ||
| 170 | @par Exception Safety | |
| 171 | Strong guarantee. | |
| 172 | Exception thrown if max capacity exceeded. | |
| 173 | ||
| 174 | @throw std::length_error | |
| 175 | Max capacity would be exceeded. | |
| 176 | ||
| 177 | @param r The response to copy. | |
| 178 | ||
| 179 | @return A reference to this object. | |
| 180 | */ | |
| 181 | static_response& | |
| 182 | 2 | operator=( |
| 183 | response_base const& r) | |
| 184 | { | |
| 185 | 2 | copy_impl(r.h_); |
| 186 | 2 | return *this; |
| 187 | } | |
| 188 | }; | |
| 189 | ||
| 190 | } // http | |
| 191 | } // boost | |
| 192 | ||
| 193 | #endif | |
| 194 |