-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfc3986.rs
382 lines (340 loc) · 11.2 KB
/
rfc3986.rs
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
// Draft implementation attempt at ABNF cming from IETF's RFC 3986.
// (https://www.ietf.org/rfc/rfc3986.txt)
// intent is to parse the inpt string for URI only, no AST/extra processing needed...
// TODO - one_of() vs alt() for various chars? (consume 1 vs greedy more???)
use nom::{
branch::alt,
bytes::complete::{tag},
character::{
complete::{anychar, char, one_of},
is_alphabetic, is_digit,
},
combinator::{not, opt, recognize, verify},
multi::{count, many0, many1, many_m_n},
sequence::{pair, tuple},
IResult,
};
// URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
pub fn uri(input: &str) -> IResult<&str, &str> {
recognize(tuple((
scheme,
recognize(char(':')),
hier_part,
opt(pair(char('?'), query)),
opt(pair(char('#'), fragment)),
)))(input)
}
// hier-part = "//" authority path-abempty
// / path-absolute
// / path-rootless
// / path-empty
fn hier_part(input: &str) -> IResult<&str, &str> {
alt((
recognize(tuple((tag("//"), authority, path_abempty))),
path_absolute,
path_rootless,
path_empty,
))(input)
}
// URI-reference = URI / relative-ref
pub fn uri_reference(input: &str) -> IResult<&str, &str> {
alt((uri, relative_ref))(input)
}
// absolute-URI = scheme ":" hier-part [ "?" query ]
pub fn absolute_uri(input: &str) -> IResult<&str, &str> {
recognize(tuple((
scheme,
char(':'),
hier_part,
opt(pair(char('?'), query)),
)))(input)
}
// relative-ref = relative-part [ "?" query ] [ "#" fragment ]
fn relative_ref(input: &str) -> IResult<&str, &str> {
recognize(tuple((
relative_part,
opt(pair(char('?'), query)),
opt(pair(char('#'), fragment)),
)))(input)
}
// relative-part = "//" authority path-abempty
// / path-absolute
// / path-noscheme
// / path-empty
fn relative_part(input: &str) -> IResult<&str, &str> {
recognize(alt((
recognize(tuple((tag("//"), authority, path_abempty))),
path_absolute,
path_noscheme,
path_empty,
)))(input)
}
// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
fn scheme(input: &str) -> IResult<&str, &str> {
recognize(pair(
alpha,
many0(alt((alpha, digit, recognize(one_of("+-."))))),
))(input)
}
// authority = [ userinfo "@" ] host [ ":" port ]
pub fn authority(input: &str) -> IResult<&str, &str> {
recognize(tuple((
opt(pair(userinfo, char('@'))),
tag("host"),
opt(pair(char(':'), port)),
)))(input)
}
// userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
pub fn userinfo(input: &str) -> IResult<&str, &str> {
recognize(many0(alt((
unreserved,
pct_encoded,
sub_delims,
recognize(char(':')),
))))(input)
}
// host = IP-literal / IPv4address / reg-name
pub fn host(input: &str) -> IResult<&str, &str> {
alt((ip_literal, ipv4address, reg_name))(input)
}
// port = *DIGIT
pub fn port(input: &str) -> IResult<&str, &str> {
recognize(many0(digit))(input)
}
// IP-literal = "[" ( IPv6address / IPvFuture ) "]"
fn ip_literal(input: &str) -> IResult<&str, &str> {
recognize(tuple((tag("["), alt((ipv6address, ipvfuture)), tag("]"))))(input)
}
// IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
fn ipvfuture(input: &str) -> IResult<&str, &str> {
recognize(tuple((
char('v'),
many1(hexdig),
char('.'),
many1(alt((unreserved, sub_delims, recognize(char(':'))))),
)))(input)
}
// IPv6address = 6( h16 ":" ) ls32
// / "::" 5( h16 ":" ) ls32
// / [ h16 ] "::" 4( h16 ":" ) ls32
// / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
// / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
// / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
// / [ *4( h16 ":" ) h16 ] "::" ls32
// / [ *5( h16 ":" ) h16 ] "::" h16
// / [ *6( h16 ":" ) h16 ] "::"
fn ipv6address(input: &str) -> IResult<&str, &str> {
let v1 = tuple((count(pair(h16, char(':')), 6), ls32));
let v2 = tuple((tag("::"), count(pair(h16, char(':')), 5), ls32));
let v3 = tuple((opt(h16), tag("::"), count(pair(h16, char(':')), 4), ls32));
let v4 = tuple((
opt(pair(many_m_n(0, 1, pair(h16, char(':'))), h16)),
tag("::"),
count(pair(h16, char(':')), 3),
ls32,
));
let v5 = tuple((
opt(pair(many_m_n(0, 2, pair(h16, char(':'))), h16)),
tag("::"),
count(pair(h16, char(':')), 2),
ls32,
));
let v6 = tuple((
opt(pair(many_m_n(0, 3, pair(h16, char(':'))), h16)),
tag("::"),
h16,
char(':'),
ls32,
));
let v7 = tuple((
opt(pair(many_m_n(0, 4, pair(h16, char(':'))), h16)),
tag("::"),
ls32,
));
let v8 = tuple((
opt(pair(many_m_n(0, 5, pair(h16, char(':'))), h16)),
tag("::"),
h16,
));
let v9 = tuple((
opt(pair(many_m_n(0, 6, pair(h16, char(':'))), h16)),
tag("::"),
));
recognize(alt((
recognize(v1),
recognize(v2),
recognize(v3),
recognize(v4),
recognize(v5),
recognize(v6),
recognize(v7),
recognize(v8),
recognize(v9),
)))(input)
}
// h16 = 1*4HEXDIG
fn h16(input: &str) -> IResult<&str, &str> {
recognize(many_m_n(1, 4, hexdig))(input)
}
// ls32 = ( h16 ":" h16 ) / IPv4address
fn ls32(input: &str) -> IResult<&str, &str> {
alt((recognize(tuple((h16, char(':'), h16))), ipv4address))(input)
}
// IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
fn ipv4address(input: &str) -> IResult<&str, &str> {
recognize(tuple((
dec_octet,
char('.'),
dec_octet,
char('.'),
dec_octet,
char('.'),
dec_octet,
)))(input)
}
// dec-octet = DIGIT ; 0-9
// / %x31-39 DIGIT ; 10-99
// / "1" 2DIGIT ; 100-199
// / "2" %x30-34 DIGIT ; 200-249
// / "25" %x30-35 ; 250-255
fn dec_octet(input: &str) -> IResult<&str, &str> {
alt((
digit,
recognize(pair(recognize(one_of("123456789")), digit)),
recognize(pair(recognize(char('1')), count(digit, 2))),
recognize(tuple((char('2'), one_of("01234"), digit))),
recognize(pair(tag("25"), one_of("012345"))),
))(input)
}
// reg-name = *( unreserved / pct-encoded / sub-delims )
fn reg_name(input: &str) -> IResult<&str, &str> {
recognize(many0(alt((unreserved, pct_encoded, sub_delims))))(input)
}
// 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
fn path(input: &str) -> IResult<&str, &str> {
alt((
path_abempty,
path_absolute,
path_noscheme,
path_rootless,
path_empty,
))(input)
}
// path-abempty = *( "/" segment )
pub fn path_abempty(input: &str) -> IResult<&str, &str> {
recognize(many0(pair(char('/'), segment)))(input)
}
// path-absolute = "/" [ segment-nz *( "/" segment ) ]
pub fn path_absolute(input: &str) -> IResult<&str, &str> {
recognize(pair(
recognize(char('/')),
recognize(opt(pair(
segment_nz,
recognize(many0(pair(recognize(char('/')), segment))),
))),
))(input)
}
// path-noscheme = segment-nz-nc *( "/" segment )
fn path_noscheme(input: &str) -> IResult<&str, &str> {
recognize(pair(
segment_nz_nc,
recognize(many0(pair(char('/'), segment))),
))(input)
}
// path-rootless = segment-nz *( "/" segment )
fn path_rootless(input: &str) -> IResult<&str, &str> {
recognize(pair(segment_nz, recognize(many0(pair(char('/'), segment)))))(input)
}
// path-empty = 0<pchar>
fn path_empty(input: &str) -> IResult<&str, &str> {
recognize(not(pchar))(input)
}
// segment = *pchar
fn segment(input: &str) -> IResult<&str, &str> {
recognize(many0(pchar))(input)
}
// segment-nz = 1*pchar
fn segment_nz(input: &str) -> IResult<&str, &str> {
recognize(many1(pchar))(input)
}
// segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
// ; non-zero-length segment without any colon ":"
fn segment_nz_nc(input: &str) -> IResult<&str, &str> {
recognize(many1(alt((
unreserved,
pct_encoded,
sub_delims,
recognize(char('@')),
))))(input)
}
// pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
fn pchar(input: &str) -> IResult<&str, &str> {
alt((
unreserved,
pct_encoded,
sub_delims,
recognize(char(':')),
recognize(char('@')),
))(input)
}
// query = *( pchar / "/" / "?" )
fn query(input: &str) -> IResult<&str, &str> {
recognize(many0(alt((
pchar,
recognize(char('/')),
recognize(char('?')),
))))(input)
}
// fragment = *( pchar / "/" / "?" )
fn fragment(input: &str) -> IResult<&str, &str> {
recognize(many0(alt((
pchar,
recognize(char('/')),
recognize(char('?')),
))))(input)
}
// pct-encoded = "%" HEXDIG HEXDIG
fn pct_encoded(input: &str) -> IResult<&str, &str> {
recognize(tuple((char('%'), hexdig, hexdig)))(input)
}
// HEXDIG from ABNF RFC 2234
fn hexdig(input: &str) -> IResult<&str, &str> {
// HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
alt((digit, recognize(one_of("ABCDEF"))))(input)
}
// unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
fn unreserved(input: &str) -> IResult<&str, &str> {
alt((alpha, digit, recognize(one_of("-._~"))))(input)
}
// ALPHA from ABNF RFC 2234
fn alpha(input: &str) -> IResult<&str, &str> {
// ALPHA = %x41-5A / %x61-7A
// let r = (0x41..=0x5A).map(char::from).collect::<Vec<_>>();
// let s = String::from_iter(r);
recognize(verify(anychar, |c| is_alphabetic(*c as u8)))(input)
}
// DIGIT from ABNF RFC 2234
fn digit(input: &str) -> IResult<&str, &str> {
// DIGIT = %x30-39
// let r = (0x30..=0x39).map(char::from).collect::<Vec<_>>();
// let s = String::from_iter(r);
// recognize(one_of(s.as_str()))(input)
recognize(verify(anychar, |c| is_digit(*c as u8)))(input)
}
// reserved = gen-delims / sub-delims
fn reserved(input: &str) -> IResult<&str, &str> {
alt((gen_delims, sub_delims))(input)
}
// gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
fn gen_delims(input: &str) -> IResult<&str, &str> {
recognize(one_of(":/?#[]@"))(input)
}
// sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
// / "*" / "+" / "," / ";" / "="
fn sub_delims(input: &str) -> IResult<&str, &str> {
recognize(one_of("!$&'()*+,;="))(input)
}