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