]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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 | ||
8 | #ifndef BEAST_HTTP_RFC7230_HPP | |
9 | #define BEAST_HTTP_RFC7230_HPP | |
10 | ||
11 | #include <beast/config.hpp> | |
12 | #include <beast/http/detail/rfc7230.hpp> | |
13 | #include <beast/http/detail/basic_parsed_list.hpp> | |
14 | ||
15 | namespace beast { | |
16 | namespace http { | |
17 | ||
18 | /** A list of parameters in a HTTP extension field value. | |
19 | ||
20 | This container allows iteration of the parameter list in a HTTP | |
21 | extension. The parameter list is a series of name/value pairs | |
22 | with each pair starting with a semicolon. The value is optional. | |
23 | ||
24 | If a parsing error is encountered while iterating the string, | |
25 | the behavior of the container will be as if a string containing | |
26 | only characters up to but excluding the first invalid character | |
27 | was used to construct the list. | |
28 | ||
29 | @par BNF | |
30 | @code | |
31 | param-list = *( OWS ";" OWS param ) | |
32 | param = token OWS [ "=" OWS ( token / quoted-string ) ] | |
33 | @endcode | |
34 | ||
35 | To use this class, construct with the string to be parsed and | |
36 | then use @ref begin and @ref end, or range-for to iterate each | |
37 | item: | |
38 | ||
39 | @par Example | |
40 | @code | |
41 | for(auto const& param : param_list{";level=9;no_context_takeover;bits=15"}) | |
42 | { | |
43 | std::cout << ";" << param.first; | |
44 | if(! param.second.empty()) | |
45 | std::cout << "=" << param.second; | |
46 | std::cout << "\n"; | |
47 | } | |
48 | @endcode | |
49 | */ | |
50 | class param_list | |
51 | { | |
52 | boost::string_ref s_; | |
53 | ||
54 | public: | |
55 | /** The type of each element in the list. | |
56 | ||
57 | The first string in the pair is the name of the parameter, | |
58 | and the second string in the pair is its value (which may | |
59 | be empty). | |
60 | */ | |
61 | using value_type = | |
62 | std::pair<boost::string_ref, boost::string_ref>; | |
63 | ||
64 | /// A constant iterator to the list | |
65 | #if BEAST_DOXYGEN | |
66 | using const_iterator = implementation_defined; | |
67 | #else | |
68 | class const_iterator; | |
69 | #endif | |
70 | ||
71 | /// Default constructor. | |
72 | param_list() = default; | |
73 | ||
74 | /** Construct a list. | |
75 | ||
76 | @param s A string containing the list contents. The string | |
77 | must remain valid for the lifetime of the container. | |
78 | */ | |
79 | explicit | |
80 | param_list(boost::string_ref const& s) | |
81 | : s_(s) | |
82 | { | |
83 | } | |
84 | ||
85 | /// Return a const iterator to the beginning of the list | |
86 | const_iterator begin() const; | |
87 | ||
88 | /// Return a const iterator to the end of the list | |
89 | const_iterator end() const; | |
90 | ||
91 | /// Return a const iterator to the beginning of the list | |
92 | const_iterator cbegin() const; | |
93 | ||
94 | /// Return a const iterator to the end of the list | |
95 | const_iterator cend() const; | |
96 | }; | |
97 | ||
98 | //------------------------------------------------------------------------------ | |
99 | ||
100 | /** A list of extensions in a comma separated HTTP field value. | |
101 | ||
102 | This container allows iteration of the extensions in a HTTP | |
103 | field value. The extension list is a comma separated list of | |
104 | token parameter list pairs. | |
105 | ||
106 | If a parsing error is encountered while iterating the string, | |
107 | the behavior of the container will be as if a string containing | |
108 | only characters up to but excluding the first invalid character | |
109 | was used to construct the list. | |
110 | ||
111 | @par BNF | |
112 | @code | |
113 | ext-list = *( "," OWS ) ext *( OWS "," [ OWS ext ] ) | |
114 | ext = token param-list | |
115 | param-list = *( OWS ";" OWS param ) | |
116 | param = token OWS [ "=" OWS ( token / quoted-string ) ] | |
117 | @endcode | |
118 | ||
119 | To use this class, construct with the string to be parsed and | |
120 | then use @ref begin and @ref end, or range-for to iterate each | |
121 | item: | |
122 | ||
123 | @par Example | |
124 | @code | |
125 | for(auto const& ext : ext_list{"none, 7z;level=9, zip;no_context_takeover;bits=15"}) | |
126 | { | |
127 | std::cout << ext.first << "\n"; | |
128 | for(auto const& param : ext.second) | |
129 | { | |
130 | std::cout << ";" << param.first; | |
131 | if(! param.second.empty()) | |
132 | std::cout << "=" << param.second; | |
133 | std::cout << "\n"; | |
134 | } | |
135 | } | |
136 | @endcode | |
137 | */ | |
138 | class ext_list | |
139 | { | |
140 | using iter_type = boost::string_ref::const_iterator; | |
141 | ||
142 | boost::string_ref s_; | |
143 | ||
144 | public: | |
145 | /** The type of each element in the list. | |
146 | ||
147 | The first element of the pair is the extension token, and the | |
148 | second element of the pair is an iterable container holding the | |
149 | extension's name/value parameters. | |
150 | */ | |
151 | using value_type = std::pair<boost::string_ref, param_list>; | |
152 | ||
153 | /// A constant iterator to the list | |
154 | #if BEAST_DOXYGEN | |
155 | using const_iterator = implementation_defined; | |
156 | #else | |
157 | class const_iterator; | |
158 | #endif | |
159 | ||
160 | /** Construct a list. | |
161 | ||
162 | @param s A string containing the list contents. The string | |
163 | must remain valid for the lifetime of the container. | |
164 | */ | |
165 | explicit | |
166 | ext_list(boost::string_ref const& s) | |
167 | : s_(s) | |
168 | { | |
169 | } | |
170 | ||
171 | /// Return a const iterator to the beginning of the list | |
172 | const_iterator begin() const; | |
173 | ||
174 | /// Return a const iterator to the end of the list | |
175 | const_iterator end() const; | |
176 | ||
177 | /// Return a const iterator to the beginning of the list | |
178 | const_iterator cbegin() const; | |
179 | ||
180 | /// Return a const iterator to the end of the list | |
181 | const_iterator cend() const; | |
182 | ||
183 | /** Find a token in the list. | |
184 | ||
185 | @param s The token to find. A case-insensitive comparison is used. | |
186 | ||
187 | @return An iterator to the matching token, or `end()` if no | |
188 | token exists. | |
189 | */ | |
190 | template<class T> | |
191 | const_iterator | |
192 | find(T const& s); | |
193 | ||
194 | /** Return `true` if a token is present in the list. | |
195 | ||
196 | @param s The token to find. A case-insensitive comparison is used. | |
197 | */ | |
198 | template<class T> | |
199 | bool | |
200 | exists(T const& s); | |
201 | }; | |
202 | ||
203 | //------------------------------------------------------------------------------ | |
204 | ||
205 | /** A list of tokens in a comma separated HTTP field value. | |
206 | ||
207 | This container allows iteration of a list of items in a | |
208 | header field value. The input is a comma separated list of | |
209 | tokens. | |
210 | ||
211 | If a parsing error is encountered while iterating the string, | |
212 | the behavior of the container will be as if a string containing | |
213 | only characters up to but excluding the first invalid character | |
214 | was used to construct the list. | |
215 | ||
216 | @par BNF | |
217 | @code | |
218 | token-list = *( "," OWS ) token *( OWS "," [ OWS token ] ) | |
219 | @endcode | |
220 | ||
221 | To use this class, construct with the string to be parsed and | |
222 | then use @ref begin and @ref end, or range-for to iterate each | |
223 | item: | |
224 | ||
225 | @par Example | |
226 | @code | |
227 | for(auto const& token : token_list{"apple, pear, banana"}) | |
228 | std::cout << token << "\n"; | |
229 | @endcode | |
230 | */ | |
231 | class token_list | |
232 | { | |
233 | using iter_type = boost::string_ref::const_iterator; | |
234 | ||
235 | boost::string_ref s_; | |
236 | ||
237 | public: | |
238 | /// The type of each element in the token list. | |
239 | using value_type = boost::string_ref; | |
240 | ||
241 | /// A constant iterator to the list | |
242 | #if BEAST_DOXYGEN | |
243 | using const_iterator = implementation_defined; | |
244 | #else | |
245 | class const_iterator; | |
246 | #endif | |
247 | ||
248 | /** Construct a list. | |
249 | ||
250 | @param s A string containing the list contents. The string | |
251 | must remain valid for the lifetime of the container. | |
252 | */ | |
253 | explicit | |
254 | token_list(boost::string_ref const& s) | |
255 | : s_(s) | |
256 | { | |
257 | } | |
258 | ||
259 | /// Return a const iterator to the beginning of the list | |
260 | const_iterator begin() const; | |
261 | ||
262 | /// Return a const iterator to the end of the list | |
263 | const_iterator end() const; | |
264 | ||
265 | /// Return a const iterator to the beginning of the list | |
266 | const_iterator cbegin() const; | |
267 | ||
268 | /// Return a const iterator to the end of the list | |
269 | const_iterator cend() const; | |
270 | ||
271 | /** Return `true` if a token is present in the list. | |
272 | ||
273 | @param s The token to find. A case-insensitive comparison is used. | |
274 | */ | |
275 | template<class T> | |
276 | bool | |
277 | exists(T const& s); | |
278 | }; | |
279 | ||
280 | /** A list of tokens in a comma separated HTTP field value. | |
281 | ||
282 | This container allows iteration of a list of items in a | |
283 | header field value. The input is a comma separated list of | |
284 | tokens. | |
285 | ||
286 | If a parsing error is encountered while iterating the string, | |
287 | the behavior of the container will be as if a string containing | |
288 | only characters up to but excluding the first invalid character | |
289 | was used to construct the list. | |
290 | ||
291 | @par BNF | |
292 | @code | |
293 | token-list = *( "," OWS ) token *( OWS "," [ OWS token ] ) | |
294 | @endcode | |
295 | ||
296 | To use this class, construct with the string to be parsed and | |
297 | then use `begin` and `end`, or range-for to iterate each item: | |
298 | ||
299 | @par Example | |
300 | @code | |
301 | for(auto const& token : token_list{"apple, pear, banana"}) | |
302 | std::cout << token << "\n"; | |
303 | @endcode | |
304 | */ | |
305 | using opt_token_list = | |
306 | detail::basic_parsed_list< | |
307 | detail::opt_token_list_policy>; | |
308 | ||
309 | /** Returns `true` if a parsed list is parsed without errors. | |
310 | ||
311 | This function iterates a single pass through a parsed list | |
312 | and returns `true` if there were no parsing errors, else | |
313 | returns `false`. | |
314 | */ | |
315 | template<class Policy> | |
316 | bool | |
317 | validate_list(detail::basic_parsed_list< | |
318 | Policy> const& list); | |
319 | ||
320 | } // http | |
321 | } // beast | |
322 | ||
323 | #include <beast/http/impl/rfc7230.ipp> | |
324 | ||
325 | #endif |