Line data Source code
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_FIELDS_VIEW_BASE_HPP
11 : #define BOOST_HTTP_PROTO_FIELDS_VIEW_BASE_HPP
12 :
13 : #include <boost/http_proto/detail/config.hpp>
14 : #include <boost/http_proto/detail/header.hpp>
15 : #include <boost/url/grammar/recycled.hpp>
16 : #include <boost/url/grammar/type_traits.hpp>
17 : #include <boost/core/detail/string_view.hpp>
18 : #include <iterator>
19 : #include <memory>
20 : #include <string>
21 :
22 : namespace boost {
23 : namespace http_proto {
24 :
25 : /** A read-only, bidirectional range of HTTP fields
26 :
27 : This is a mix-in used to add common
28 : functionality to derived classes.
29 : */
30 : class fields_view_base
31 : {
32 : detail::header const* ph_;
33 :
34 : friend class fields;
35 : template<std::size_t>
36 : friend class static_fields;
37 : friend class fields_base;
38 : friend class fields_view;
39 : friend class message_base;
40 : friend class message_view_base;
41 : friend class request_base;
42 : friend class request;
43 : template<std::size_t>
44 : friend class static_request;
45 : friend class request_view;
46 : friend class response_base;
47 : friend class response;
48 : template<std::size_t>
49 : friend class static_response;
50 : friend class response_view;
51 : friend class serializer;
52 :
53 : explicit
54 1341 : fields_view_base(
55 : detail::header const* ph) noexcept
56 1341 : : ph_(ph)
57 : {
58 1341 : }
59 :
60 : fields_view_base(
61 : fields_view_base const&) = default;
62 : fields_view_base&
63 : operator=(fields_view_base const&) = default;
64 :
65 : public:
66 : //--------------------------------------------
67 : //
68 : // Types
69 : //
70 : //--------------------------------------------
71 :
72 : /** A field
73 : */
74 : /**@{*/
75 : struct reference
76 : {
77 : field const id;
78 : core::string_view const name;
79 : core::string_view const value;
80 :
81 : #ifndef BOOST_HTTP_PROTO_DOCS
82 : reference const*
83 2077 : operator->() const noexcept
84 : {
85 2077 : return this;
86 : }
87 : #endif
88 : };
89 :
90 : typedef reference const_reference;
91 : /**@}*/
92 :
93 : /** A type which can represent a field as a value
94 :
95 : This type allows for making a copy of
96 : a field where ownership is retained
97 : in the copy.
98 : */
99 : struct value_type
100 : {
101 : field id;
102 : std::string name;
103 : std::string value;
104 :
105 : BOOST_HTTP_PROTO_DECL
106 : value_type(
107 : reference const& other);
108 :
109 : operator reference() const noexcept;
110 : };
111 :
112 : /** An unsigned integer type
113 : */
114 : using size_type = std::size_t;
115 :
116 : /** A signed integer type
117 : */
118 : using difference_type =
119 : std::ptrdiff_t;
120 :
121 : /** A bidirectional iterator to HTTP fields
122 : */
123 : /**@{*/
124 : #ifdef BOOST_HTTP_PROTO_DOCS
125 : using iterator = __see_below__;
126 : #else
127 : class iterator;
128 : #endif
129 :
130 : using const_iterator = iterator;
131 : /**@}*/
132 :
133 : /** A bidirectional reverse iterator to HTTP fields
134 : */
135 : /**@{*/
136 : #ifdef BOOST_HTTP_PROTO_DOCS
137 : using reverse_iterator = __see_below__;
138 : #else
139 : class reverse_iterator;
140 : #endif
141 :
142 : using const_reverse_iterator = reverse_iterator;
143 : /**@}*/
144 :
145 : /** A forward range of matching fields
146 :
147 : Objects of this type are returned by
148 : the function @ref find_all.
149 : */
150 : #ifdef BOOST_HTTP_PROTO_DOCS
151 : using subrange = __see_below__;
152 : #else
153 : class subrange;
154 : #endif
155 :
156 : //--------------------------------------------
157 : //
158 : // Observers
159 : //
160 : //--------------------------------------------
161 :
162 : /** Returns the largest possible serialized message
163 : */
164 : static
165 : constexpr
166 : std::size_t
167 : max_size() noexcept
168 : {
169 : return max_offset;
170 : }
171 :
172 : /** Return an iterator to the beginning
173 : */
174 : iterator
175 : begin() const noexcept;
176 :
177 : /** Return an iterator to the end
178 : */
179 : iterator
180 : end() const noexcept;
181 :
182 : /** Return a reverse iterator to the beginning
183 : */
184 : reverse_iterator
185 : rbegin() const noexcept;
186 :
187 : /** Return a reverse iterator to the end
188 : */
189 : reverse_iterator
190 : rend() const noexcept;
191 :
192 : //---
193 :
194 : /** Return a string representing the serialized data
195 : */
196 : core::string_view
197 669 : buffer() const noexcept
198 : {
199 1338 : return core::string_view(
200 669 : ph_->cbuf, ph_->size);
201 : }
202 :
203 : /** Returns the number of fields in the container
204 : */
205 : std::size_t
206 211 : size() const noexcept
207 : {
208 211 : return ph_->count;
209 : }
210 :
211 : /** Return true if a field exists
212 : */
213 : BOOST_HTTP_PROTO_DECL
214 : bool
215 : exists(field id) const noexcept;
216 :
217 : /** Return true if a field exists
218 : */
219 : BOOST_HTTP_PROTO_DECL
220 : bool
221 : exists(
222 : core::string_view name) const noexcept;
223 :
224 : /** Return the number of matching fields
225 : */
226 : BOOST_HTTP_PROTO_DECL
227 : std::size_t
228 : count(field id) const noexcept;
229 :
230 : /** Return the number of matching fields
231 : */
232 : BOOST_HTTP_PROTO_DECL
233 : std::size_t
234 : count(
235 : core::string_view name) const noexcept;
236 :
237 : /** Returns an iterator to the matching element if it exists
238 : */
239 : BOOST_HTTP_PROTO_DECL
240 : iterator
241 : find(field id) const noexcept;
242 :
243 : /** Returns an iterator to the matching element if it exists
244 :
245 : If `name` refers to a known field, it is faster
246 : to call @ref find with a field id instead of a
247 : string.
248 : */
249 : BOOST_HTTP_PROTO_DECL
250 : iterator
251 : find(
252 : core::string_view name) const noexcept;
253 :
254 : /** Returns an iterator to the matching element if it exists
255 : */
256 : BOOST_HTTP_PROTO_DECL
257 : iterator
258 : find(
259 : iterator from,
260 : field id) const noexcept;
261 :
262 : /** Returns an iterator to the matching element if it exists
263 : */
264 : BOOST_HTTP_PROTO_DECL
265 : iterator
266 : find(
267 : iterator from,
268 : core::string_view name) const noexcept;
269 :
270 : /** Returns an iterator to the matching element if it exists
271 : */
272 : BOOST_HTTP_PROTO_DECL
273 : iterator
274 : find_last(
275 : iterator before,
276 : field id) const noexcept;
277 :
278 : /** Returns an iterator to the matching element if it exists
279 : */
280 : BOOST_HTTP_PROTO_DECL
281 : iterator
282 : find_last(
283 : iterator before,
284 : core::string_view name) const noexcept;
285 :
286 : /** Return the value of a field
287 : */
288 : BOOST_HTTP_PROTO_DECL
289 : core::string_view
290 : value_or(
291 : field id,
292 : core::string_view s) const noexcept;
293 :
294 : /** Return the value of a field
295 : */
296 : BOOST_HTTP_PROTO_DECL
297 : core::string_view
298 : value_or(
299 : core::string_view name,
300 : core::string_view s) const noexcept;
301 :
302 : //---
303 :
304 : /** Return a forward range containing values for all matching fields
305 : */
306 : BOOST_HTTP_PROTO_DECL
307 : subrange
308 : find_all(field id) const noexcept;
309 :
310 : /** Return a forward range containing values for all matching fields
311 : */
312 : BOOST_HTTP_PROTO_DECL
313 : subrange
314 : find_all(
315 : core::string_view name) const noexcept;
316 : };
317 :
318 : } // http_proto
319 : } // boost
320 :
321 : #include <boost/http_proto/impl/fields_view_base.hpp>
322 :
323 : #endif
|