src/rfc/detail/rules.cpp

95.8% Lines (159/166) 100.0% List of functions (10/10)
rules.cpp
f(x) Functions (10)
Line TLA Hits 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
8 //
9
10 #include "src/rfc/detail/rules.hpp"
11
12 #include <boost/http/error.hpp>
13 #include <boost/http/detail/config.hpp>
14 #include <boost/http/rfc/token_rule.hpp>
15
16 #include <boost/core/detail/string_view.hpp>
17 #include <boost/url/grammar/delim_rule.hpp>
18 #include <boost/url/grammar/digit_chars.hpp>
19 #include <boost/url/grammar/error.hpp>
20 #include <boost/url/grammar/hexdig_chars.hpp>
21 #include <boost/url/grammar/lut_chars.hpp>
22 #include <boost/url/grammar/parse.hpp>
23 #include <boost/url/grammar/tuple_rule.hpp>
24
25 #include "src/rfc/detail/rules.hpp"
26
27 namespace boost {
28 namespace http {
29 namespace detail {
30
31 auto
32 28320x crlf_rule_t::
33 parse(
34 char const*& it,
35 char const* end) const noexcept ->
36 system::result<value_type>
37 {
38 28320x if(it == end)
39 6145x return grammar::error::need_more;
40 22175x if(*it != '\r')
41 29x return grammar::error::mismatch;
42 22146x ++it;
43 22146x if(it == end)
44 887x return grammar::error::need_more;
45 21259x if(*it != '\n')
46 51x return grammar::error::mismatch;
47 21208x ++it;
48 21208x return {};
49 }
50
51 //------------------------------------------------
52
53 auto
54 26900x version_rule_t::
55 parse(
56 char const*& it,
57 char const* end) const noexcept ->
58 system::result<value_type>
59 {
60 26900x value_type v = 0;
61 26900x if(it == end)
62 {
63 // expected "HTTP/"
64 1511x BOOST_HTTP_RETURN_EC(
65 grammar::error::need_more);
66 }
67 25389x if(end - it >= 5)
68 {
69 21083x if(std::memcmp(
70 it, "HTTP/", 5) != 0)
71 {
72 BOOST_HTTP_RETURN_EC(
73 grammar::error::mismatch);
74 }
75 21083x it += 5;
76 }
77 25389x if(it == end)
78 {
79 // expected DIGIT
80 991x BOOST_HTTP_RETURN_EC(
81 grammar::error::need_more);
82 }
83 24398x if(! grammar::digit_chars(*it))
84 {
85 // expected DIGIT
86 4306x BOOST_HTTP_RETURN_EC(
87 grammar::error::need_more);
88 }
89 20092x v = 10 * (*it++ - '0');
90 20092x if(it == end)
91 {
92 // expected "."
93 1119x BOOST_HTTP_RETURN_EC(
94 grammar::error::need_more);
95 }
96 18973x if(*it != '.')
97 {
98 // expected "."
99 BOOST_HTTP_RETURN_EC(
100 grammar::error::need_more);
101 }
102 18973x ++it;
103 18973x if(it == end)
104 {
105 // expected DIGIT
106 959x BOOST_HTTP_RETURN_EC(
107 grammar::error::need_more);
108 }
109 18014x if(! grammar::digit_chars(*it))
110 {
111 // expected DIGIT
112 BOOST_HTTP_RETURN_EC(
113 grammar::error::need_more);
114 }
115 18014x v += *it++ - '0';
116 18014x return v;
117 }
118
119 //------------------------------------------------
120
121 auto
122 7464x status_code_rule_t::
123 parse(
124 char const*& it,
125 char const* end) const noexcept ->
126 system::result<value_type>
127 {
128 auto const dig =
129 17380x [](char c) -> int
130 {
131 17380x unsigned char uc(c - '0');
132 17380x if(uc > 9)
133 return -1;
134 17380x return uc;
135 };
136
137 7464x if(it == end)
138 {
139 // end
140 846x BOOST_HTTP_RETURN_EC(
141 grammar::error::need_more);
142 }
143 6618x auto it0 = it;
144 6618x int v = dig(*it);
145 6618x if(v == -1)
146 {
147 // expected DIGIT
148 BOOST_HTTP_RETURN_EC(
149 grammar::error::mismatch);
150 }
151 6618x value_type t;
152 6618x t.v = 100 * v;
153 6618x ++it;
154 6618x if(it == end)
155 {
156 // end
157 830x BOOST_HTTP_RETURN_EC(
158 grammar::error::need_more);
159 }
160 5788x v = dig(*it);
161 5788x if(v == -1)
162 {
163 // expected DIGIT
164 BOOST_HTTP_RETURN_EC(
165 grammar::error::mismatch);
166 }
167 5788x t.v = t.v + (10 * v);
168 5788x ++it;
169 5788x if(it == end)
170 {
171 // end
172 814x BOOST_HTTP_RETURN_EC(
173 grammar::error::need_more);
174 }
175 4974x v = dig(*it);
176 4974x if(v == -1)
177 {
178 // expected DIGIT
179 BOOST_HTTP_RETURN_EC(
180 grammar::error::need_more);
181 }
182 4974x t.v = t.v + v;
183 4974x ++it;
184
185 4974x t.s = core::string_view(it0, it - it0);
186 4974x t.st = int_to_status(t.v);
187 4974x return t;
188 }
189
190 //------------------------------------------------
191
192 auto
193 4176x reason_phrase_rule_t::
194 parse(
195 char const*& it,
196 char const* end) const noexcept ->
197 system::result<value_type>
198 {
199 4176x auto begin = it;
200 4176x it = grammar::find_if_not(it, end, ws_vchars);
201 4176x return core::string_view(begin, it);
202 }
203
204 //------------------------------------------------
205
206 auto
207 25313x field_name_rule_t::
208 parse(
209 char const*& it,
210 char const* end) const noexcept ->
211 system::result<value_type>
212 {
213 25313x if( it == end )
214 1x BOOST_HTTP_RETURN_EC(
215 grammar::error::need_more);
216
217 25312x value_type v;
218
219 25312x auto begin = it;
220 25312x auto rv = grammar::parse(
221 it, end, token_rule);
222 25312x if( rv.has_error() || (it != end) )
223 {
224 15687x if( it != begin )
225 {
226 15621x v = core::string_view(begin, it - begin);
227 15621x return v;
228 }
229 66x return error::bad_field_name;
230 }
231
232 9625x v = core::string_view(begin, end - begin);
233 9625x return v;
234 }
235
236 auto
237 15890x field_value_rule_t::
238 parse(
239 char const*& it,
240 char const* end) const noexcept ->
241 system::result<value_type>
242 {
243 15890x value_type v;
244 15890x if( it == end )
245 {
246 651x v.value = core::string_view(it, 0);
247 651x return v;
248 }
249
250 // field-line = field-name ":" OWS field-value OWS
251 // field-value = *field-content
252 // field-content = field-vchar
253 // [ 1*( SP / HTAB / field-vchar ) field-vchar ]
254 // field-vchar = VCHAR / obs-text
255 // obs-text = %x80-FF
256 // VCHAR = %x21-7E
257 // ; visible (printing) characters
258
259 66277x auto is_field_vchar = [](unsigned char ch)
260 {
261 66277x return (ch >= 0x21 && ch <= 0x7e) || ch >= 0x80;
262 };
263
264 15239x char const* s0 = nullptr;
265 15239x char const* s1 = nullptr;
266
267 15239x bool has_crlf = false;
268 15239x bool has_obs_fold = false;
269
270 99363x while( it < end )
271 {
272 95981x auto ch = *it;
273 95981x if( ws(ch) )
274 {
275 17151x ++it;
276 17151x continue;
277 }
278
279 78830x if( ch == '\r' )
280 {
281 // too short to know if we have a potential obs-fold
282 // occurrence
283 12553x if( end - it < 2 )
284 558x BOOST_HTTP_RETURN_EC(
285 grammar::error::need_more);
286
287 11995x if( it[1] != '\n' )
288 53x goto done;
289
290 11942x if( end - it < 3 )
291 514x BOOST_HTTP_RETURN_EC(
292 grammar::error::need_more);
293
294 11428x if(! ws(it[2]) )
295 {
296 10698x has_crlf = true;
297 10698x goto done;
298 }
299
300 730x has_obs_fold = true;
301 730x it = it + 3;
302 730x continue;
303 730x }
304
305 66277x if(! is_field_vchar(ch) )
306 {
307 34x goto done;
308 }
309
310 66243x if(! s0 )
311 14176x s0 = it;
312
313 66243x ++it;
314 66243x s1 = it;
315 }
316
317 3382x done:
318 // later routines wind up doing pointer
319 // subtraction using the .data() member
320 // of the value so we need a valid 0-len range
321 14167x if(! s0 )
322 {
323 899x s0 = it;
324 899x s1 = s0;
325 }
326
327 14167x v.value = core::string_view(s0, s1 - s0);
328 14167x v.has_crlf = has_crlf;
329 14167x v.has_obs_fold = has_obs_fold;
330 14167x return v;
331 }
332
333 auto
334 36560x field_rule_t::
335 parse(
336 char const*& it,
337 char const* end) const noexcept ->
338 system::result<value_type>
339 {
340 36560x if(it == end)
341 {
342 907x BOOST_HTTP_RETURN_EC(
343 grammar::error::need_more);
344 }
345 // check for leading CRLF
346 35653x if(it[0] == '\r')
347 {
348 10581x ++it;
349 10581x if(it == end)
350 {
351 459x BOOST_HTTP_RETURN_EC(
352 grammar::error::need_more);
353 }
354 10122x if(*it != '\n')
355 {
356 21x BOOST_HTTP_RETURN_EC(
357 grammar::error::mismatch);
358 }
359 // end of fields
360 10101x ++it;
361 10101x BOOST_HTTP_RETURN_EC(
362 grammar::error::end_of_range);
363 }
364
365 25072x value_type v;
366 auto rv = grammar::parse(
367 25072x it, end, grammar::tuple_rule(
368 field_name_rule,
369 25072x grammar::delim_rule(':'),
370 field_value_rule,
371 25072x crlf_rule));
372
373 25072x if( rv.has_error() )
374 14390x return rv.error();
375
376 10682x auto val = rv.value();
377 10682x v.name = std::get<0>(val);
378 10682x v.value = std::get<2>(val).value;
379 10682x v.has_obs_fold = std::get<2>(val).has_obs_fold;
380
381 10682x return v;
382 }
383
384 //------------------------------------------------
385
386 void
387 244x remove_obs_fold(
388 char* it,
389 char const* const end) noexcept
390 {
391 2262x while(it != end)
392 {
393 2236x if(*it != '\r')
394 {
395 1637x ++it;
396 1637x continue;
397 }
398 599x if(end - it < 3)
399 218x break;
400 381x BOOST_ASSERT(it[1] == '\n');
401 762x if( it[1] == '\n' &&
402 381x ws(it[2]))
403 {
404 378x it[0] = ' ';
405 378x it[1] = ' ';
406 378x it += 3;
407 }
408 else
409 {
410 3x ++it;
411 }
412 }
413 244x }
414
415 } // detail
416 } // http
417 } // boost
418