File: rfc7230.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (361 lines) | stat: -rw-r--r-- 9,229 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//

// Test that header file is self-contained.
#include <boost/beast/http/rfc7230.hpp>

#include <boost/beast/http/detail/rfc7230.hpp>
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <string>
#include <vector>

namespace boost {
namespace beast {
namespace http {

class rfc7230_test : public beast::unit_test::suite
{
public:
    static
    std::string
    fmt(std::string const& s)
    {
        return '\'' + s + '\'';
    }

    static
    std::string
    str(string_view s)
    {
        return std::string(s.data(), s.size());
    }

    static
    std::string
    str(param_list const& c)
    {
        std::string s;
        for(auto const& p : c)
        {
            s.push_back(';');
            s.append(str(p.first));
            if(! p.second.empty())
            {
                s.push_back('=');
                s.append(str(p.second));
            }
        }
        return s;
    }

    void
    testParamList()
    {
        auto const ce =
            [&](std::string const& s)
            {
                auto const got = str(param_list{s});
                BEAST_EXPECTS(got == s, fmt(got));
            };
        auto const cs =
            [&](std::string const& s, std::string const& answer)
            {
                ce(answer);
                auto const got = str(param_list{s});
                ce(got);
                BEAST_EXPECTS(got == answer, fmt(got));
            };
        auto const cq =
            [&](std::string const& s, std::string const& answer)
            {
                auto const got = str(param_list{s});
                BEAST_EXPECTS(got == answer, fmt(got));
            };

        ce("");
        ce(";x");
        ce(";xy");
        ce(";x;y");

        ce("");
        cs(" ;\t i =\t 1 \t", ";i=1");
        cq("\t; \t xyz=1 ; ijk=\"q\\\"t\"", ";xyz=1;ijk=q\"t");
        ce(";x;y");

		ce(";chunked;a=b;i=j;gzip;windowBits=12");
		ce(";chunked;a=b;i=j;gzip;windowBits=12;permessage-deflate");

        // invalid strings
        cs(";", "");
        cs(";,", "");
        cq(";x=,", "");
        cq(";xy=\"", "");
        cq(";xy=\"\x7f", "");
        cq(";xy=\"\\", "");
        cq(";xy=\"\\\x01\"", "");

    }

    static
    std::string
    str(ext_list const& ex)
    {
        std::string s;
        for(auto const& e : ex)
        {
            if(! s.empty())
                s += ',';
            s.append(str(e.first));
            s += str(e.second);
        }
        return s;
    }

    void
    testExtList()
    {
        auto const ce =
            [&](std::string const& s)
            {
                auto const got = str(ext_list{s});
                BEAST_EXPECTS(got == s, fmt(got));
            };
        auto const cs =
            [&](std::string const& s, std::string const& good)
            {
                ce(good);
                auto const got = str(ext_list{s});
                ce(got);
                BEAST_EXPECTS(got == good, fmt(got));
            };
        auto const cq =
            [&](std::string const& s, std::string const& good)
            {
                auto const got = str(ext_list{s});
                BEAST_EXPECTS(got == good, fmt(got));
            };
    /*
        ext-basic_parsed_list    = *( "," OWS ) ext *( OWS "," [ OWS ext ] )
        ext         = token param-basic_parsed_list
        param-basic_parsed_list  = *( OWS ";" OWS param )
        param       = token OWS "=" OWS ( token / quoted-string )
    */
        cs(",", "");
        cs(", ", "");
        cs(",\t", "");
        cs(", \t", "");
        cs(" ", "");
        cs(" ,", "");
        cs("\t,", "");
        cs("\t , \t", "");
        cs(",,", "");
        cs(" , \t,, \t,", "");
        cs( "permessage-deflate; client_no_context_takeover; client_max_window_bits",
            "permessage-deflate;client_no_context_takeover;client_max_window_bits");

        ce("a");
        ce("ab");
        ce("a,b");
        cs(" a ", "a");
        cs("\t a, b\t  ,  c\t", "a,b,c");
        ce("a;b");
        ce("a;b;c");

        cs("a; \t i\t=\t \t1\t ", "a;i=1");
        ce("a;i=1;j=2;k=3");
        ce("a;i=1;j=2;k=3,b;i=4;j=5;k=6");

        cq("ab;x=\" \"", "ab;x= ");
        cq("ab;x=\"\\\"\"", "ab;x=\"");

        BEAST_EXPECT(ext_list{"a,b;i=1,c;j=2;k=3"}.exists("A"));
        BEAST_EXPECT(ext_list{"a,b;i=1,c;j=2;k=3"}.exists("b"));
        BEAST_EXPECT(! ext_list{"a,b;i=1,c;j=2;k=3"}.exists("d"));

        // invalid strings
        cs("i j", "i");
        cs(";", "");
    }

    static
    std::string
    str(token_list const& c)
    {
        bool first = true;
        std::string s;
        for(auto const& p : c)
        {
            if(! first)
                s.push_back(',');
            s.append(str(p));
            first = false;
        }
        return s;
    }

    void
    testTokenList()
    {
        auto const ce =
            [&](std::string const& s)
            {
                auto const got = str(token_list{s});
                BEAST_EXPECTS(got == s, fmt(got));
            };
        auto const cs =
            [&](std::string const& s, std::string const& good)
            {
                ce(good);
                auto const got = str(token_list{s});
                ce(got);
                BEAST_EXPECTS(got == good, fmt(got));
            };

        cs("", "");
        cs(" ", "");
        cs("  ", "");
        cs("\t", "");
        cs(" \t ", "");
        cs(",", "");
        cs(",,", "");
        cs(" ,", "");
        cs(" , ,", "");
        cs(" x", "x");
        cs(" \t x", "x");
        cs("x ", "x");
        cs("x \t", "x");
        cs(" \t x \t ", "x");
        ce("x,y");
        cs("x ,\ty ", "x,y");
        cs("x, y, z", "x,y,z");

        BEAST_EXPECT(token_list{"a,b,c"}.exists("A"));
        BEAST_EXPECT(token_list{"a,b,c"}.exists("b"));
        BEAST_EXPECT(! token_list{"a,b,c"}.exists("d"));

        // invalid
        cs("x y", "x");
    }

    template<class Policy>
    static
    std::vector<std::string>
    to_vector(string_view in)
    {
        std::vector<std::string> v;
        detail::basic_parsed_list<Policy> list{in};
        for(auto const& s :
                detail::basic_parsed_list<Policy>{in})
            v.emplace_back(s.data(), s.size());
        return v;
    }

    template<class Policy>
    void
    validate(string_view in,
        std::vector<std::string> const& v)
    {
        BEAST_EXPECT(to_vector<Policy>(in) == v);
    }

    template<class Policy>
    void
    good(string_view in)
    {
        BEAST_EXPECT(validate_list(
            detail::basic_parsed_list<Policy>{in}));
    }

    template<class Policy>
    void
    good(string_view in,
        std::vector<std::string> const& v)
    {
        BEAST_EXPECT(validate_list(
            detail::basic_parsed_list<Policy>{in}));
        validate<Policy>(in, v);
    }

    template<class Policy>
    void
    bad(string_view in)
    {
        BEAST_EXPECT(! validate_list(
            detail::basic_parsed_list<Policy>{in}));
    }

    void
    testOptTokenList()
    {
    /*
        #token = [ ( "," / token )   *( OWS "," [ OWS token ] ) ]
    */
        using type = detail::opt_token_list_policy;

        good<type>("",          {});
        good<type>(" ",         {});
        good<type>("\t",        {});
        good<type>(" \t",       {});
        good<type>(",",         {});
        good<type>(",,",        {});
        good<type>(", ,",       {});
        good<type>(",\t,",      {});
        good<type>(", \t,",     {});
        good<type>(", \t, ",    {});
        good<type>(", \t,\t",   {});
        good<type>(", \t, \t",  {});

        good<type>("x",         {"x"});
        good<type>(" x",        {"x"});
        good<type>("x,,",       {"x"});
        good<type>("x, ,",      {"x"});
        good<type>("x,, ",      {"x"});
        good<type>("x,,,",      {"x"});

        good<type>("x,y",       {"x","y"});
        good<type>("x ,y",      {"x","y"});
        good<type>("x\t,y",     {"x","y"});
        good<type>("x \t,y",    {"x","y"});
        good<type>(" x,y",      {"x","y"});
        good<type>(" x,y ",     {"x","y"});
        good<type>(",x,y",      {"x","y"});
        good<type>("x,y,",      {"x","y"});
        good<type>(",,x,y",     {"x","y"});
        good<type>(",x,,y",     {"x","y"});
        good<type>(",x,y,",     {"x","y"});
        good<type>("x ,, y",    {"x","y"});
        good<type>("x , ,y",    {"x","y"});

        good<type>("x,y,z",     {"x","y","z"});

        bad<type>("(");
        bad<type>("x(");
        bad<type>("(x");
        bad<type>(",(");
        bad<type>("(,");
        bad<type>("x,(");
        bad<type>("(,x");
        bad<type>("x y");
    }

    void
    run()
    {
        testOptTokenList();
        testParamList();
        testExtList();
        testTokenList();
    }
};

BEAST_DEFINE_TESTSUITE(beast,http,rfc7230);

} // http
} // beast
} // boost