-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfc1738.ts
754 lines (559 loc) · 15.8 KB
/
rfc1738.ts
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
import {
createParser,
setParser,
regex,
seq,
rep,
alt,
ch,
opt,
end
} from "../src/parser";
const genericurl = createParser();
const url = createParser();
const _scheme = createParser();
const scheme = createParser();
const schemepart = createParser();
const ipSchemepart = createParser();
const login = createParser();
const hostport = createParser();
const host = createParser();
const hostname = createParser();
const domainlabel = createParser();
const toplabel = createParser();
const alphadigit = createParser();
const hostnumber = createParser();
const port = createParser();
const otherurl = createParser();
const user = createParser();
const password = createParser();
const urlpath = createParser();
const ftpurl = createParser();
const newsurl = createParser();
const nntpurl = createParser();
const telneturl = createParser();
const gopherurl = createParser();
const waisurl = createParser();
const mailtourl = createParser();
const prosperourl = createParser();
const fpath = createParser();
const fsegment = createParser();
const ftptype = createParser();
const fileurl = createParser();
const httpurl = createParser();
const hpath = createParser();
const hsegment = createParser();
const search = createParser();
const lowalpha = createParser();
const hialpha = createParser();
const alpha = createParser();
const digit = createParser();
const safe = createParser();
const extra = createParser();
const national = createParser();
const punctuation = createParser();
const reserved = createParser();
const hex = createParser();
const escape = createParser();
const unreserved = createParser();
const uchar = createParser();
const xchar = createParser();
const digits = createParser();
const gtype = createParser();
const selector = createParser();
const gopher_string = createParser();
const encoded822addr = createParser();
const grouppart = createParser();
const group = createParser();
const _articlePart = createParser();
const article = createParser();
const waisdatabase = createParser();
const waisindex = createParser();
const waisdoc = createParser();
const database = createParser();
const wpath = createParser();
const wtype = createParser();
const ppath = createParser();
const fieldspec = createParser();
const psegment = createParser();
const fieldname = createParser();
const fieldvalue = createParser();
// const fieldspec = createParser();
// ; The generic form of a URL is:
// genericurl = scheme ":" schemepart
setParser(genericurl, seq(scheme, ch(":"), schemepart));
// ; Specific predefined schemes are defined here; new schemes
// ; may be registered with IANA
// url = httpurl | ftpurl | newsurl |
// nntpurl | telneturl | gopherurl |
// waisurl | mailtourl | fileurl |
// prosperourl | otherurl
setParser(
url,
alt(
httpurl,
ftpurl,
newsurl,
nntpurl,
telneturl,
gopherurl,
waisurl,
mailtourl,
fileurl,
prosperourl,
otherurl,
)
);
// ; new schemes follow the general syntax
// otherurl = genericurl
setParser(otherurl, genericurl);
// ; the scheme is in lower case; interpreters should use case-ignore
// scheme = 1*[ lowalpha | digit | "+" | "-" | "." ]
setParser(_scheme, alt(lowalpha, digit, ch("+"), ch("-"), ch(".")));
setParser(scheme, seq(_scheme, rep(_scheme)));
// schemepart = *xchar | ip-schemepart
setParser(schemepart, alt(rep(xchar), ipSchemepart));
// ; URL schemeparts for ip based protocols:
// ip-schemepart = "//" login [ "/" urlpath ]
setParser(ipSchemepart, seq(ch("//"), login, opt(seq(ch("/"), urlpath))));
// login = [ user [ ":" password ] "@" ] hostport
setParser(
login,
seq(opt(seq(user, opt(seq(ch(":"), password)), ch("@"))), hostport)
);
// hostport = host [ ":" port ]
setParser(hostport, seq(host, opt(seq(ch(":"), port))));
// host = hostname | hostnumber
setParser(host, alt(hostname, hostnumber));
// hostname = *[ domainlabel "." ] toplabel
setParser(
hostname,
seq(
rep(
seq(
domainlabel,
ch(".")
)
),
toplabel
)
);
// domainlabel = alphadigit | alphadigit *[ alphadigit | "-" ] alphadigit
setParser(
domainlabel,
alt(alphadigit, seq(alphadigit, rep(alt(alphadigit, ch("-"))), alphadigit))
);
// toplabel = alpha | alpha *[ alphadigit | "-" ] alphadigit
setParser(
toplabel,
alt(alpha, seq(alpha, rep(alt(alphadigit, ch("-"))), alphadigit))
);
// alphadigit = alpha | digit
setParser(alphadigit, alt(alpha, digit));
// hostnumber = digits "." digits "." digits "." digits
setParser(
hostnumber,
seq(digits, ch("."), digits, ch("."), digits, ch("."), digits)
);
// port = digits
// If set directly, it will be [[ undefined ]]
setParser(port, seq(digit, rep(digit)));
// user = *[ uchar | ";" | "?" | "&" | "=" ]
setParser(user, rep(alt(uchar, ch(";"), ch("?"), ch("&"), ch("="))));
// password = *[ uchar | ";" | "?" | "&" | "=" ]
setParser(password, rep(alt(uchar, ch(";"), ch("?"), ch("&"), ch("="))));
// urlpath = *xchar ; depends on protocol see section 3.1
setParser(urlpath, rep(xchar));
// ; The predefined schemes:
// ; FTP (see also RFC959)
// ftpurl = "ftp://" login [ "/" fpath [ ";type=" ftptype ]]
setParser(
ftpurl,
seq(
ch("ftp://"),
login,
opt(
seq(
ch("/"),
fpath,
opt(
seq(
ch(";type="),
ftptype
)
)
)
)
)
);
// fpath = fsegment *[ "/" fsegment ]
setParser(fpath, seq(fsegment, rep(seq(ch("/"), fsegment))));
// fsegment = *[ uchar | "?" | ":" | "@" | "&" | "=" ]
setParser(
fsegment,
rep(alt(uchar, ch("?"), ch(":"), ch("@"), ch("&"), ch("=")))
);
// ftptype = "A" | "I" | "D" | "a" | "i" | "d"
setParser(ftptype, alt(ch("A"), ch("I"), ch("D"), ch("a"), ch("i"), ch("d")));
// ; FILE
// fileurl = "file://" [ host | "localhost" ] "/" fpath
setParser(
ftpurl,
seq(ch("file://"), opt(alt(host, ch("localhost"))), ch("/"), fpath)
);
// ; HTTP
// httpurl = "http://" hostport [ "/" hpath [ "?" search ]]
setParser(
httpurl,
seq(
ch("http://"),
hostport,
opt(seq(ch("/"), hpath, opt(seq(ch("?"), search))))
)
);
// hpath = hsegment *[ "/" hsegment ]
setParser(hpath, seq(hsegment, rep(seq(ch("/"), hsegment))));
// hsegment = *[ uchar | ";" | ":" | "@" | "&" | "=" ]
setParser(
hsegment,
rep(alt(uchar, ch(";"), ch(":"), ch("@"), ch("&"), ch("=")))
);
// search = *[ uchar | ";" | ":" | "@" | "&" | "=" ]
setParser(search, rep(alt(uchar, ch(";"), ch(":"), ch("@"), ch("&"), ch("="))));
// ; GOPHER (see also RFC1436)
// gopherurl = "gopher://" hostport [ / [ gtype [ selector
// [ "%09" search [ "%09" gopher+_string ] ] ] ] ]
// "gopher://" hostport [ / [ gtype [ selector [ "%09" search [ "%09" gopher+_string ] ] ] ] ]
setParser(
gopherurl,
seq(
ch("gopher://"),
hostport,
opt(
seq(
ch("/"),
opt(
seq(
gtype,
opt(
seq(
selector,
opt(
seq(
ch("%09"),
search,
opt(
seq(
ch("%09"),
gopher_string
)
)
)
)
)
)
)
)
)
)
)
);
// gtype = xchar
setParser(gtype, alt(unreserved, reserved, escape));
// selector = *xchar
setParser(selector, rep(xchar));
// gopher+_string = *xchar
setParser(gopher_string, rep(xchar));
// ; MAILTO (see also RFC822)
// mailtourl = "mailto:" encoded822addr
setParser(mailtourl, seq(ch("mailto:"), encoded822addr));
// encoded822addr = 1*xchar ; further defined in RFC822
setParser(encoded822addr, seq(xchar, rep(xchar)));
// ; NEWS (see also RFC1036)
// newsurl = "news:" grouppart
setParser(newsurl, seq(ch("news:"), grouppart));
// grouppart = "*" | group | article
setParser(grouppart, alt(ch("*"), group, article));
// group = alpha *[ alpha | digit | "-" | "." | "+" | "_" ]
setParser(group, seq(alpha, rep(alt(
alpha,
digit,
ch("-"),
ch("."),
ch("+"),
ch("_"),
))));
// article = 1*[ uchar | ";" | "/" | "?" | ":" | "&" | "=" ] "@" host
setParser(
_articlePart,
alt(
uchar,
ch(";"),
ch("/"),
ch("?"),
ch(":"),
ch("&"),
ch("="),
)
);
setParser(
article,
seq(
_articlePart,
rep(_articlePart),
ch("@"),
host
)
);
// ; NNTP (see also RFC977)
// nntpurl = "nntp://" hostport "/" group [ "/" digits ]
setParser(nntpurl, seq(ch("nntp:"), hostport, ch("/"), group, opt(seq(ch("/"), digits))));
// ; TELNET
// telneturl = "telnet://" login [ "/" ]
setParser(telneturl, seq(ch("telnet:"), login, opt(ch("/"))));
// ; WAIS (see also RFC1625)
// waisurl = waisdatabase | waisindex | waisdoc
setParser(
waisurl,
alt(
waisdatabase,
waisindex,
waisdoc
)
);
// waisdatabase = "wais://" hostport "/" database
setParser(waisdatabase, seq(ch("wais://"), hostport, ch("/"), database));
// waisindex = "wais://" hostport "/" database "?" search
setParser(waisindex, seq(ch("wais://"), hostport, ch("/"), database, ch("?"), search));
// waisdoc = "wais://" hostport "/" database "/" wtype "/" wpath
setParser(waisindex, seq(ch("wais://"), hostport, ch("/"), database, ch("?"), search));
// database = *uchar
setParser(database, rep(uchar));
// wtype = *uchar
setParser(wtype, rep(uchar));
// wpath = *uchar
setParser(wpath, rep(uchar));
// ; PROSPERO
// prosperourl = "prospero://" hostport "/" ppath *[ fieldspec ]
setParser(prosperourl, seq(ch("prospero://"), hostport, ch("/"), ppath, rep(fieldspec)));
// ppath = psegment *[ "/" psegment ]
setParser(ppath, seq(psegment, rep(seq(ch("/"), psegment))));
// psegment = *[ uchar | "?" | ":" | "@" | "&" | "=" ]
setParser(psegment, rep(
alt(
uchar,
ch("?"),
ch(":"),
ch("@"),
ch("&"),
ch("="),
)
));
// fieldspec = ";" fieldname "=" fieldvalue
setParser(fieldspec, seq(ch(";"), fieldname, ch("="), fieldvalue));
// fieldname = *[ uchar | "?" | ":" | "@" | "&" ]
setParser(fieldname, rep(
alt(
uchar,
ch("?"),
ch(":"),
ch("@"),
ch("&")
)
));
// fieldvalue = *[ uchar | "?" | ":" | "@" | "&" ]
setParser(fieldvalue, rep(
alt(
uchar,
ch("?"),
ch(":"),
ch("@"),
ch("&")
)
));
// ; Miscellaneous definitions
// lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" |
// "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" |
// "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" |
// "y" | "z"
setParser(lowalpha, regex("[a-z]"));
// hialpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |
// "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |
// "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
setParser(hialpha, regex("[A-Z]"));
// alpha = lowalpha | hialpha
setParser(alpha, alt(lowalpha, hialpha));
// digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
// "8" | "9"
setParser(digit, regex("[0-9]"));
// safe = "$" | "-" | "_" | "." | "+"
setParser(safe, alt(ch("$"), ch("-"), ch("_"), ch("."), ch("+")));
// extra = "!" | "*" | "'" | "(" | ")" | ","
setParser(extra, alt(ch("!"), ch("*"), ch("'"), ch("("), ch(")"), ch(",")));
// national = "{" | "}" | "|" | "\" | "^" | "~" | "[" | "]" | "`"
setParser(
national,
alt(
ch("{"),
ch("}"),
ch("|"),
ch("\\"),
ch("^"),
ch("~"),
ch("["),
ch("]"),
ch("`")
)
);
// punctuation = "<" | ">" | "#" | "%" | <">
setParser(punctuation, alt(ch("<"), ch(">"), ch("#"), ch("%"), ch('"')));
// reserved = ";" | "/" | "?" | ":" | "@" | "&" | "="
setParser(
reserved,
alt(ch(";"), ch("/"), ch("?"), ch(":"), ch("@"), ch("&"), ch("="))
);
// hex = digit | "A" | "B" | "C" | "D" | "E" | "F" |
// "a" | "b" | "c" | "d" | "e" | "f"
setParser(hex, alt(digit, regex("[A-F]"), regex("[a-f]")));
// escape = "%" hex hex
setParser(escape, seq(ch("%"), hex, hex));
// unreserved = alpha | digit | safe | extra
setParser(unreserved, alt(alpha, digit, safe, extra));
// uchar = unreserved | escape
setParser(uchar, alt(unreserved, escape));
// xchar = unreserved | reserved | escape
setParser(xchar, alt(unreserved, reserved, escape));
// digits = 1*digit
setParser(digits, seq(digit, rep(digit)));
const genericurlParser = createParser();
setParser(genericurlParser, seq(genericurl, end()));
function isGenericurl(input: string) {
const result = genericurlParser[0](input);
if (result.length) {
return true;
}
return false;
}
const urlParser = createParser();
setParser(urlParser, seq(url, end()));
function isUrl(input: string) {
const result = urlParser[0](input);
if (result.length) {
return true;
}
return false;
}
const httpurlParser = createParser();
setParser(httpurlParser, seq(httpurl, end()));
function isHttpurl(input: string) {
const result = httpurlParser[0](input);
if (result.length) {
return true;
}
return false;
}
const ftpurlParser = createParser();
setParser(ftpurlParser, seq(ftpurl, end()));
function isFtpurl(input: string) {
const result = ftpurlParser[0](input);
if (result.length) {
return true;
}
return false;
}
const newsurlParser = createParser();
setParser(newsurlParser, seq(newsurl, end()));
function isNewsurl(input: string) {
const result = newsurlParser[0](input);
if (result.length) {
return true;
}
return false;
}
const nntpurlParser = createParser();
setParser(nntpurlParser, seq(nntpurl, end()));
function isNntpurl(input: string) {
const result = nntpurlParser[0](input);
if (result.length) {
return true;
}
return false;
}
const telneturlParser = createParser();
setParser(telneturlParser, seq(telneturl, end()));
function isTelneturl(input: string) {
const result = telneturlParser[0](input);
if (result.length) {
return true;
}
return false;
}
const gopherurlParser = createParser();
setParser(gopherurlParser, seq(gopherurl, end()));
function isGopherurl(input: string) {
const result = gopherurlParser[0](input);
if (result.length) {
return true;
}
return false;
}
const waisurlParser = createParser();
setParser(waisurlParser, seq(waisurl, end()));
function isWaisurl(input: string) {
const result = waisurlParser[0](input);
if (result.length) {
return true;
}
return false;
}
const mailtourlParser = createParser();
setParser(mailtourlParser, seq(mailtourl, end()));
function isMailtourl(input: string) {
const result = mailtourlParser[0](input);
if (result.length) {
return true;
}
return false;
}
const fileurlParser = createParser();
setParser(fileurlParser, seq(fileurl, end()));
function isFileurl(input: string) {
const result = fileurlParser[0](input);
if (result.length) {
return true;
}
return false;
}
const prosperourlParser = createParser();
setParser(prosperourlParser, seq(prosperourl, end()));
function isProsperourl(input: string) {
const result = prosperourlParser[0](input);
if (result.length) {
return true;
}
return false;
}
const otherurlParser = createParser();
setParser(otherurlParser, seq(otherurl, end()));
function isOtherurl(input: string) {
const result = otherurlParser[0](input);
if (result.length) {
return true;
}
return false;
}
export {
isGenericurl,
isUrl,
isHttpurl,
isFtpurl,
isNewsurl,
isNntpurl,
isTelneturl,
isGopherurl,
isWaisurl,
isMailtourl,
isFileurl,
isProsperourl,
isOtherurl,
};