aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/uri/parsefsm.rl6
blob: 7097723650383f7629aab1532a1c3ac8240b39a4 (plain) (blame)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#include <library/cpp/uri/parse.h>

#ifdef __clang__
    #pragma clang diagnostic ignored "-Wunused-variable"
#endif

%%{
    machine TParser;

    #================================================
    # RFC 3986 http://tools.ietf.org/html/rfc3986
    # with some modifications
    #================================================
    # The RegEx
    #
    # http://www.ics.uci.edu/pub/ietf/uri/#Related
    # ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
    #  12            3  4          5       6  7        8 9
    #results in the following subexpression matches:
    # $1 = http:
    # $2 = http
    # $3 = //www.ics.uci.edu
    # $4 = www.ics.uci.edu
    # $5 = /pub/ietf/uri/
    # $6 = <undefined>
    # $7 = <undefined>
    # $8 = #Related
    # $9 = Related
    #
    # So $2:scheme $4:authority $5:path $7:query $9:fragment
    #================================================


    #================================================
    # List of all ASCII characters and where they can be used
    #================================================

    #   0-31  x00-1F  cntrl  ext_cntrl
    #  32     x20     space  ext_space
    #  33     x21     !      sub_delims
    #  34     x22     "      ext_delims
    #  35     x23     #      gen_delims / f=frag
    #  36     x24     $      sub_delims
    #  37     x25     %      PCT
    #  38     x26     &      sub_delims
    #  39     x27     '      sub_delims
    #  40     x28     (      sub_delims
    #  41     x29     )      sub_delims
    #  42     x2A     *      sub_delims
    #  43     x2B     +      sub_delims
    #  44     x2C     ,      sub_delims
    #  45     x2D     -      unreserved
    #  46     x2E     .      unreserved
    #  47     x2F     /      gen_delims / f=path,qry,frag
    #  48-57  x30-39  0-9    unreserved
    #  58     x3A     :      gen_delims / f=pass,path,qry,frag
    #  59     x3B     ;      sub_delims
    #  60     x3C     <      ext_delims
    #  61     x3D     =      sub_delims
    #  62     x3E     >      ext_delims
    #  63     x3F     ?      gen_delims / f=qry,frag
    #  64     x40     @      gen_delims / f=path,qry,frag
    #  65-90  x41-5A  A-Z    unreserved
    #  91     x5B     [      gen_delims / ext_delims
    #  92     x5C     \      ext_delims
    #  93     x5D     ]      gen_delims / ext_delims
    #  94     x5E     ^      ext_delims
    #  95     x5F     _      unreserved
    #  96     x60     `      ext_delims
    #  97-122 x61-7A  a-z    unreserved
    # 123     x7B     {      ext_delims
    # 124     x7C     |      ext_delims
    # 125     x7D     }      ext_delims
    # 126     x7E     ~      unreserved
    # 127     x7F     DEL    ext_cntrl
    # 128-255 x80-FF         ext_ascii


    #================================================
    # Actions used in multiple definitions
    #================================================

    action act_req_enc_sql   { REQ(fpc, FeatureEncodeForSQL) }

    # REQ must apply to a char in range but not after the range has been reset
    action act_req_pathop    { REQ(fpc - 1, FeaturePathOperation) }

    action act_clr_scheme    { CLR(fpc, Scheme) }
    action act_clr_user      { CLR(fpc, User)   }
    action act_clr_host      { CLR(fpc, Host)   }
    action act_beg_host      { BEG(fpc, Host)   }
    action act_end_host      { END(fpc, Host)   }
    action act_beg_path      { BEG(fpc, Path)   }
    action act_end_path      { END(fpc, Path)   }


    #================================================
    # RFC 3986 ABNFs
    #================================================

    DIGIT = digit;

    ALPHA = ( upper >{ REQ(fpc, FeatureToLower) } ) |
                  lower;

    ALNUM = ALPHA | DIGIT;

    PCT   = "%" >{ PctBeg(fpc); } ;

    HEXDIG = (
        DIGIT   >{ HexDigit(fpc, fc); }
        | [A-F] >{ HexUpper(fpc, fc); }
        | [a-f] >{ HexLower(fpc, fc); }
    );

    # HexSet sets REQ so must apply in range
    HEXNUM = ( HEXDIG HEXDIG ) %{ HexSet(fpc - 1); };

    pct_encoded   = PCT HEXNUM;

    unreserved    = ALNUM | "-" | "." | "_" | "~";

    gen_delims    = ":" | "/" | "?" | "#" | "[" | "]" | "@";

    sub_delims    = "!" | "$" | "&" | "(" | ")"
                  | "*" | "+" | "," | ";" | "="
                  | ( ['] >act_req_enc_sql );


    #================================================
    # Local ABNFs
    #================================================

    VALID    = ^(cntrl | space) | " ";

    # safe character sequences
    safe          = unreserved | pct_encoded | sub_delims;

    # MOD: Yandex extensions

    ext_ascii     = (VALID - ascii) >{ REQ(fpc, FeatureEncodeExtendedASCII) };
    ext_delims    = ( "[" | "]" | "|" |  "{" | "}" | "`" | "^" | "<" | ">"
                  | ( ["\\] >act_req_enc_sql )
                  ) >{ REQ(fpc, FeatureEncodeExtendedDelim) }; # " fix hilite
    ext_space     = " " >{ REQ(fpc, FeatureEncodeSpace) };
    ext_cntrl     = cntrl >{ REQ(fpc, FeatureEncodeCntrl) };

    pct_maybe_encoded = PCT (HEXDIG | HEXNUM)? ;
    ext_safe      = unreserved
                  | pct_maybe_encoded
                  | sub_delims
                  | ext_delims
                  | ext_space
                  | ext_cntrl
                  | ext_ascii;

    # pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
    # uric (RFC 2396)
    # MOD: extension to format, add extended delimiters and 8-bit ascii

    pchar_nc      = ext_safe | "@";
    pchar         = pchar_nc | ":";
    path_sep      = "/";
    uric          = pchar | path_sep | "?";


    #================================================
    # Fields
    #================================================
    # Single fields use fXXX as machine definitions


    #================================================
    # Scheme
    # scheme        = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
    #================================================

    scheme   = ( ALPHA ( ALPHA | DIGIT | "+" | "-" | "." )** );
    fscheme  = scheme >{ BEG(fpc, Scheme) } %{ END(fpc, Scheme) };


    #================================================
    # UserInfo
    # userinfo      = *( unreserved / pct-encoded / sub-delims / ":" )
    #================================================

    # MOD: split into a pair of sections: username and password

    fuser    = ( ext_safe       )** >{ BEG(fpc, User) }   %{ END(fpc, User) };
    fpass    = ( ext_safe | ":" )** >{ BEG(fpc, Pass) }   %{ END(fpc, Pass) };
    userinfo = ( fuser ( ":" fpass )? ) ( "@" %act_clr_host @^act_clr_user );


    #================================================
    # Hostname
    # host          = IP-literal / IPv4address / reg-name
    #================================================

    # MOD: simplify IP-literal for now
    IPv6address   = (HEXDIG | ":" | ".")+;
    IP_literal    = "[" IPv6address "]";

    # IPv4address   = dec-octet "." dec-octet "." dec-octet "." dec-octet
    # MOD: simplify dec-octet which originally matches only 0-255

    dec_octet     = DIGIT+;
    IPv4address   = dec_octet "." dec_octet "." dec_octet "." dec_octet;

    # MOD: non-empty; will use host?
    # reg-name      = *( unreserved / pct-encoded / sub-delims )
    ### todo: allow ':' (need to fix grammar to disambiguate port)
    achar         = any - (0x00 .. 0x20) - '/' - '#' - '?' - ':' - '%';
    upperhalf     = any - (0x00 .. 0x7F);
    hostname      = (((achar | pct_encoded)+) & (any* (alnum | upperhalf) any*));
    reg_name      = hostname - IPv4address - IP_literal;

    # uses first-match-wins approach
    host          = IP_literal | IPv4address | (reg_name - IPv4address);
    fhost         = host?    >act_beg_host   %act_end_host;
    fhost_nempty  = host     >act_beg_host   %act_end_host;


    #================================================
    # Port
    # port          = *DIGIT
    #================================================

    # MOD: use fport? for empty
    fport         = DIGIT+   >{ BEG(fpc, Port) }   %{ END(fpc, Port) };


    #================================================
    # Authority
    # authority     = [ userinfo "@" ] host [ ":" port ]
    #================================================

    authority = userinfo? fhost ( ":" fport? )? ;


    #================================================
    # Path
    #================================================
    # path          = path-abempty    ; begins with "/" or is empty
    #               / path-absolute   ; begins with "/" but not "//"
    #               / path-noscheme   ; begins with a non-colon segment
    #               / path-rootless   ; begins with a segment
    #               / path-empty      ; zero characters
    #================================================

    # checkPath rules

    checkPathHead =
        "." ( "."? path_sep VALID* )? %act_req_pathop ;

    checkPathTail =
        VALID*
        ( path_sep "."{1,2} ) %act_req_pathop ;

    checkPathMid = VALID*
        ( path_sep "."{,2} path_sep ) %act_req_pathop
        VALID*;

    checkAbsPath = checkPathMid | checkPathTail | VALID*;
    checkRelPath = checkPathHead | checkAbsPath;

    # segment       = *pchar
    segment        = pchar**;

    # segment-nz    = 1*pchar
    segment_nz     = pchar+;

    # segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
    segment_nz_nc  = pchar_nc+;

    sep_segment    = path_sep segment;

    # non-standard definitions

    fpath_abnempty =
        (
            ( sep_segment+ )
            & checkAbsPath
        )
        >act_beg_path %act_end_path
    ;

    fpath_relative =
        (
            "."
            ( "."? sep_segment+ )?
        )
        >act_beg_path %act_req_pathop %act_end_path
    ;

    # standard definitions

    # do not save empty paths, they behave differently in relative resolutions
    fpath_empty = zlen;

    fpath_abempty = fpath_abnempty?;

    fpath_absolute =
        (
            ( path_sep ( segment_nz sep_segment* )? )
            & checkAbsPath
        )
        >act_beg_path %act_end_path
    ;

    fpath_noscheme =
        (
            ( segment_nz_nc sep_segment* )
            & checkRelPath
        )
        >act_beg_path %act_end_path
    ;

    fpath_rootless =
        (
            ( segment_nz sep_segment* )
        )
        >act_beg_path %act_end_path
    ;

    #================================================
    # Query and fragment
    # query         = *( pchar / "/" / "?" )
    # fragment      = *( pchar / "/" / "?" )
    #================================================

    # MOD: fragment allows '#' characters

    fquery     = (uric      )** >{ BEG(fpc, Query) }  %{ END(fpc, Query) };
    ffrag      = (uric | "#")** >{ BEG(fpc, Frag) }   %{ END(fpc, Frag) };
    query_frag = ("?" fquery)? ("#" ffrag)? ;


    #================================================
    # final ABNFs
    # URI-reference = URI / relative-ref
    #================================================
    # URI           = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
    # hier-part     = "//" authority path-abempty
    #               / path-absolute
    #               / path-rootless
    #               / path-empty
    # relative-ref  = relative-part [ "?" query ] [ "#" fragment ]
    # relative-part = "//" authority path-abempty
    #               / path-absolute
    #               / path-noscheme
    #               / path-empty

    net_path = "//" authority fpath_abempty;

    URI =
        fscheme ":"
        (
            net_path
            | fpath_absolute
            | fpath_rootless
            | fpath_empty
        )
        $^act_clr_scheme
        query_frag
    ;

    relative_ref =
        (
            net_path
            | fpath_absolute
            | fpath_noscheme
            | fpath_empty
        )
        %act_clr_scheme
        query_frag
    ;

    # non-standard definitions

    URI_no_rootless =
        fscheme ":"
        (
            net_path
            | fpath_absolute
            | fpath_empty
        )
        $^act_clr_scheme
        query_frag
    ;

    host_path =
        (
               fhost_nempty                     fpath_abempty
            | (fhost_nempty - scheme) ":" fport fpath_abempty
        )
        @^act_clr_host
    ;

    # no userinfo, path absolute, empty or clearly relative, starting with "./" | "../"
    relative_ref_host_pabem =
        (
            net_path
            | host_path
            | fpath_absolute
            | fpath_relative
            | fpath_empty
        )
        %act_clr_scheme
        query_frag
    ;

    # port must be non-empty, to avoid clash with "scheme:/..."
    auth_path =
        (
                fhost_nempty ( ":" fport  )? fpath_abempty
            | userinfo fhost ( ":" fport? )? fpath_abempty
        )
        @^act_clr_host
        @^act_clr_user
    ;

    # userinfo, path absolute, empty or clearly relative, starting with "./" | "../"
    relative_ref_auth_pabem =
        (
            net_path
            | auth_path
            | fpath_absolute
            | fpath_relative
            | fpath_empty
        )
        %act_clr_scheme
        query_frag
    ;


    # machine instantiations

    URI_ref_no_rootless :=
        (
            URI_no_rootless
            # scheme://user@host preferred over user://pass@host/path
            | relative_ref_auth_pabem
        )
    ;

    URI_ref_no_relpath :=
        (
            relative_ref_host_pabem
            # host:port/path preferred over scheme:path/rootless
            | (URI - relative_ref_host_pabem)
        )
    ;

    URI_ref :=
        (
            relative_ref
            | URI
        )
    ;

    write data;

}%%

namespace NUri {

bool TParser::doParse(const char* str_beg, size_t length)
{
    const char* p = str_beg;
    const char* pe = str_beg + length;
    const char* eof = pe;
    int cs;

#define BEG(ptr, fld) startSection (ptr, TField::Field ## fld);
#define END(ptr, fld) finishSection(ptr, TField::Field ## fld);
#define SET(val, fld) storeSection(val, TField::Field ## fld);
#define CLR(ptr, fld) ResetSection (TField::Field ## fld, ptr);
#define REQ(ptr, req) setRequirement(ptr, TFeature :: req);

    %% write init nocs;

    if (0 == (Flags & TFeature::FeatureNoRelPath)) {
        cs = TParser_en_URI_ref;
    } else if (0 == (Flags & TFeature::FeatureAllowRootless)) {
        cs = TParser_en_URI_ref_no_rootless;
    } else {
        cs = TParser_en_URI_ref_no_relpath;
    }

    %% write exec;

#undef BEG
#undef END
#undef SET
#undef CLR
#undef REQ

    return cs >= TParser_first_final;
}

}