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 <boost/http/rfc/quoted_token_rule.hpp>
11 : #include <boost/http/rfc/token_rule.hpp>
12 : #include <boost/url/grammar/charset.hpp>
13 : #include <boost/url/grammar/error.hpp>
14 : #include <boost/url/grammar/lut_chars.hpp>
15 : #include <boost/url/grammar/parse.hpp>
16 : #include <boost/url/grammar/vchars.hpp>
17 :
18 : namespace boost {
19 : namespace http {
20 :
21 : namespace {
22 :
23 : struct obs_text
24 : {
25 : constexpr
26 : bool
27 : operator()(char ch) const noexcept
28 : {
29 : return static_cast<
30 : unsigned char>(ch) >= 0x80;
31 : }
32 : };
33 :
34 : struct qdtext
35 : {
36 : constexpr
37 : bool
38 : operator()(char ch) const noexcept
39 : {
40 : return
41 : ch == '\t' ||
42 : ch == ' ' ||
43 : ch == 0x21 ||
44 : (ch >= 0x23 && ch <= 0x5b) ||
45 : (ch >= 0x5d && ch <= 0x7e) ||
46 : static_cast<unsigned char>(ch) >= 0x80;
47 : }
48 : };
49 :
50 : // qdtext = HTAB / SP /%x21 / %x23-5B / %x5D-7E / obs-text
51 : constexpr grammar::lut_chars qdtext_chars(qdtext{});
52 :
53 : // qpchars = ( HTAB / SP / VCHAR / obs-text )
54 : constexpr auto qpchars =
55 : grammar::lut_chars(grammar::vchars) +
56 : grammar::lut_chars(obs_text{}) + '\t' + ' ';
57 :
58 : } // namespace
59 :
60 :
61 : namespace implementation_defined {
62 : auto
63 HIT 22 : quoted_token_rule_t::
64 : parse(
65 : char const*& it,
66 : char const* end) const noexcept ->
67 : system::result<value_type>
68 : {
69 22 : if(it == end)
70 : {
71 1 : return grammar::error::need_more;
72 : }
73 21 : if(*it != '\"')
74 : {
75 : // token
76 15 : auto rv = grammar::parse(
77 : it, end, token_rule);
78 15 : if(rv.has_value())
79 15 : return quoted_token_view(*rv);
80 MIS 0 : return rv.error();
81 : }
82 : // quoted-string
83 HIT 6 : auto const it0 = it++;
84 6 : std::size_t n = 0;
85 : for(;;)
86 : {
87 10 : auto it1 = it;
88 10 : it = grammar::find_if_not(
89 : it, end, qdtext_chars);
90 10 : if(it == end)
91 : {
92 MIS 0 : return grammar::error::need_more;
93 : }
94 HIT 10 : n += static_cast<std::size_t>(it - it1);
95 10 : if(*it == '\"')
96 6 : break;
97 4 : if(*it != '\\')
98 : {
99 MIS 0 : return grammar::error::syntax;
100 : }
101 HIT 4 : ++it;
102 4 : if(it == end)
103 : {
104 MIS 0 : return grammar::error::need_more;
105 : }
106 HIT 4 : if(! qpchars(*it))
107 : {
108 MIS 0 : return grammar::error::syntax;
109 : }
110 HIT 4 : ++it;
111 4 : ++n;
112 4 : }
113 12 : return value_type(core::string_view(
114 12 : it0, ++it - it0), n);
115 : }
116 :
117 : } // implementation_defined
118 : } // http
119 : } // boost
|