TLA 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
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 HIT 28894 : crlf_rule_t::
33 : parse(
34 : char const*& it,
35 : char const* end) const noexcept ->
36 : system::result<value_type>
37 : {
38 28894 : if(it == end)
39 6535 : return grammar::error::need_more;
40 22359 : if(*it != '\r')
41 29 : return grammar::error::mismatch;
42 22330 : ++it;
43 22330 : if(it == end)
44 961 : return grammar::error::need_more;
45 21369 : if(*it != '\n')
46 51 : return grammar::error::mismatch;
47 21318 : ++it;
48 21318 : return {};
49 : }
50 :
51 : //------------------------------------------------
52 :
53 : auto
54 28514 : version_rule_t::
55 : parse(
56 : char const*& it,
57 : char const* end) const noexcept ->
58 : system::result<value_type>
59 : {
60 28514 : value_type v = 0;
61 28514 : if(it == end)
62 : {
63 : // expected "HTTP/"
64 1625 : return grammar::error::need_more;
65 : }
66 26889 : if(end - it >= 5)
67 : {
68 22179 : if(std::memcmp(
69 : it, "HTTP/", 5) != 0)
70 : {
71 MIS 0 : return grammar::error::mismatch;
72 : }
73 HIT 22179 : it += 5;
74 : }
75 26889 : if(it == end)
76 : {
77 : // expected DIGIT
78 1087 : return grammar::error::need_more;
79 : }
80 25802 : if(! grammar::digit_chars(*it))
81 : {
82 : // expected DIGIT
83 4710 : return grammar::error::need_more;
84 : }
85 21092 : v = 10 * (*it++ - '0');
86 21092 : if(it == end)
87 : {
88 : // expected "."
89 1213 : return grammar::error::need_more;
90 : }
91 19879 : if(*it != '.')
92 : {
93 : // expected "."
94 MIS 0 : return grammar::error::need_more;
95 : }
96 HIT 19879 : ++it;
97 19879 : if(it == end)
98 : {
99 : // expected DIGIT
100 1051 : return grammar::error::need_more;
101 : }
102 18828 : if(! grammar::digit_chars(*it))
103 : {
104 : // expected DIGIT
105 MIS 0 : return grammar::error::need_more;
106 : }
107 HIT 18828 : v += *it++ - '0';
108 18828 : return v;
109 : }
110 :
111 : //------------------------------------------------
112 :
113 : auto
114 8186 : status_code_rule_t::
115 : parse(
116 : char const*& it,
117 : char const* end) const noexcept ->
118 : system::result<value_type>
119 : {
120 : auto const dig =
121 19026 : [](char c) -> int
122 : {
123 19026 : unsigned char uc(c - '0');
124 19026 : if(uc > 9)
125 MIS 0 : return -1;
126 HIT 19026 : return uc;
127 : };
128 :
129 8186 : if(it == end)
130 : {
131 : // end
132 934 : return grammar::error::need_more;
133 : }
134 7252 : auto it0 = it;
135 7252 : int v = dig(*it);
136 7252 : if(v == -1)
137 : {
138 : // expected DIGIT
139 MIS 0 : return grammar::error::mismatch;
140 : }
141 HIT 7252 : value_type t;
142 7252 : t.v = 100 * v;
143 7252 : ++it;
144 7252 : if(it == end)
145 : {
146 : // end
147 916 : return grammar::error::need_more;
148 : }
149 6336 : v = dig(*it);
150 6336 : if(v == -1)
151 : {
152 : // expected DIGIT
153 MIS 0 : return grammar::error::mismatch;
154 : }
155 HIT 6336 : t.v = t.v + (10 * v);
156 6336 : ++it;
157 6336 : if(it == end)
158 : {
159 : // end
160 898 : return grammar::error::need_more;
161 : }
162 5438 : v = dig(*it);
163 5438 : if(v == -1)
164 : {
165 : // expected DIGIT
166 MIS 0 : return grammar::error::need_more;
167 : }
168 HIT 5438 : t.v = t.v + v;
169 5438 : ++it;
170 :
171 5438 : t.s = core::string_view(it0, it - it0);
172 5438 : t.st = int_to_status(t.v);
173 5438 : return t;
174 : }
175 :
176 : //------------------------------------------------
177 :
178 : auto
179 4558 : reason_phrase_rule_t::
180 : parse(
181 : char const*& it,
182 : char const* end) const noexcept ->
183 : system::result<value_type>
184 : {
185 4558 : auto begin = it;
186 4558 : it = grammar::find_if_not(it, end, ws_vchars);
187 4558 : return core::string_view(begin, it);
188 : }
189 :
190 : //------------------------------------------------
191 :
192 : auto
193 26368 : field_name_rule_t::
194 : parse(
195 : char const*& it,
196 : char const* end) const noexcept ->
197 : system::result<value_type>
198 : {
199 26368 : if( it == end )
200 1 : return grammar::error::need_more;
201 :
202 26367 : value_type v;
203 :
204 26367 : auto begin = it;
205 26367 : auto rv = grammar::parse(
206 : it, end, token_rule);
207 26367 : if( rv.has_error() || (it != end) )
208 : {
209 15943 : if( it != begin )
210 : {
211 15877 : v = core::string_view(begin, it - begin);
212 15877 : return v;
213 : }
214 132 : return make_error_code(
215 66 : error::bad_field_name);
216 : }
217 :
218 10424 : v = core::string_view(begin, end - begin);
219 10424 : return v;
220 : }
221 :
222 : auto
223 16148 : field_value_rule_t::
224 : parse(
225 : char const*& it,
226 : char const* end) const noexcept ->
227 : system::result<value_type>
228 : {
229 16148 : value_type v;
230 16148 : if( it == end )
231 : {
232 693 : v.value = core::string_view(it, 0);
233 693 : return v;
234 : }
235 :
236 : // field-line = field-name ":" OWS field-value OWS
237 : // field-value = *field-content
238 : // field-content = field-vchar
239 : // [ 1*( SP / HTAB / field-vchar ) field-vchar ]
240 : // field-vchar = VCHAR / obs-text
241 : // obs-text = %x80-FF
242 : // VCHAR = %x21-7E
243 : // ; visible (printing) characters
244 :
245 66600 : auto is_field_vchar = [](unsigned char ch)
246 : {
247 66600 : return (ch >= 0x21 && ch <= 0x7e) || ch >= 0x80;
248 : };
249 :
250 15455 : char const* s0 = nullptr;
251 15455 : char const* s1 = nullptr;
252 :
253 15455 : bool has_crlf = false;
254 15455 : bool has_obs_fold = false;
255 :
256 100116 : while( it < end )
257 : {
258 96618 : auto ch = *it;
259 96618 : if( ws(ch) )
260 : {
261 17365 : ++it;
262 17365 : continue;
263 : }
264 :
265 79253 : if( ch == '\r' )
266 : {
267 : // too short to know if we have a potential obs-fold
268 : // occurrence
269 12653 : if( end - it < 2 )
270 592 : return grammar::error::need_more;
271 :
272 12061 : if( it[1] != '\n' )
273 53 : goto done;
274 :
275 12008 : if( end - it < 3 )
276 546 : return grammar::error::need_more;
277 :
278 11462 : if(! ws(it[2]) )
279 : {
280 10732 : has_crlf = true;
281 10732 : goto done;
282 : }
283 :
284 730 : has_obs_fold = true;
285 730 : it = it + 3;
286 730 : continue;
287 : }
288 :
289 66600 : if(! is_field_vchar(ch) )
290 : {
291 34 : goto done;
292 : }
293 :
294 66566 : if(! s0 )
295 14352 : s0 = it;
296 :
297 66566 : ++it;
298 66566 : s1 = it;
299 : }
300 :
301 3498 : done:
302 : // later routines wind up doing pointer
303 : // subtraction using the .data() member
304 : // of the value so we need a valid 0-len range
305 14317 : if(! s0 )
306 : {
307 939 : s0 = it;
308 939 : s1 = s0;
309 : }
310 :
311 14317 : v.value = core::string_view(s0, s1 - s0);
312 14317 : v.has_crlf = has_crlf;
313 14317 : v.has_obs_fold = has_obs_fold;
314 14317 : return v;
315 : }
316 :
317 : auto
318 37748 : field_rule_t::
319 : parse(
320 : char const*& it,
321 : char const* end) const noexcept ->
322 : system::result<value_type>
323 : {
324 37748 : if(it == end)
325 : {
326 979 : return grammar::error::need_more;
327 : }
328 : // check for leading CRLF
329 36769 : if(it[0] == '\r')
330 : {
331 10643 : ++it;
332 10643 : if(it == end)
333 : {
334 489 : return grammar::error::need_more;
335 : }
336 10154 : if(*it != '\n')
337 : {
338 21 : return grammar::error::mismatch;
339 : }
340 : // end of fields
341 10133 : ++it;
342 10133 : return grammar::error::end_of_range;
343 : }
344 :
345 26126 : value_type v;
346 : auto rv = grammar::parse(
347 26126 : it, end, grammar::tuple_rule(
348 : field_name_rule,
349 26126 : grammar::delim_rule(':'),
350 : field_value_rule,
351 26126 : crlf_rule));
352 :
353 26126 : if( rv.has_error() )
354 15410 : return rv.error();
355 :
356 10716 : auto val = rv.value();
357 10716 : v.name = std::get<0>(val);
358 10716 : v.value = std::get<2>(val).value;
359 10716 : v.has_obs_fold = std::get<2>(val).has_obs_fold;
360 :
361 10716 : return v;
362 : }
363 :
364 : //------------------------------------------------
365 :
366 : void
367 244 : remove_obs_fold(
368 : char* it,
369 : char const* const end) noexcept
370 : {
371 2262 : while(it != end)
372 : {
373 2236 : if(*it != '\r')
374 : {
375 1637 : ++it;
376 1637 : continue;
377 : }
378 599 : if(end - it < 3)
379 218 : break;
380 381 : BOOST_ASSERT(it[1] == '\n');
381 762 : if( it[1] == '\n' &&
382 381 : ws(it[2]))
383 : {
384 378 : it[0] = ' ';
385 378 : it[1] = ' ';
386 378 : it += 3;
387 : }
388 : else
389 : {
390 3 : ++it;
391 : }
392 : }
393 244 : }
394 :
395 : } // detail
396 : } // http
397 : } // boost
|