-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathurl.src
2199 lines (1725 loc) · 76.6 KB
/
url.src
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<pre class="metadata">
Title: URL Standard
#ifdef WEBSPECS
Group: WebSpecs
#else
Group: WHATWG
#endif
H1: URL
Shortname: url
Status: LS
No Editor: true
Abstract: The URL Standard defines URLs, domains, IP addresses, the <code title>application/x-www-form-urlencoded</code> format, and their APIs.
#ifdef WHATWG
Logo: https://resources.whatwg.org/logo-url.svg
!Version History: <a href="https://github.com/whatwg/url/commits">https://github.com/whatwg/url/commits</a> <a href="https://twitter.com/urlstandard">@urlstandard</a>
!Participate: <a href="https://www.w3.org/Bugs/Public/enter_bug.cgi?product=WHATWG&component=URL">file a bug</a> (<a href="https://www.w3.org/Bugs/Public/buglist.cgi?product=WHATWG&component=URL&resolution=---">open bugs</a>)
!Participate: <a href="https://whatwg.org/mailing-list">[email protected]</a> (<a href="https://whatwg.org/mailing-list#specs">archives</a>)
!Participate: <a href="http://wiki.whatwg.org/wiki/IRC">IRC: #whatwg on Freenode</a>
#endif
Indent: 2
</pre>
<style>
.grammar-rule a, .grammar-rule span {
color: black;
background-color: hsl(120, 100%, 90%);
font-weight: bold;
padding: 2px;
border: 2px solid black;
}
#ifdef WEBSPECS
.big-issue, .XXX { color: #E50000; background: white; border: solid red; padding: 0.5em; margin: 1em 0; }
.big-issue, .XXX { border-radius: 0.5em }
.big-issue > :first-child, .XXX > :first-child { margin-top: 0; }
p .big-issue, p .XXX { line-height: 3em; }
table { border-collapse: collapse; border-style: hidden hidden none hidden; }
table thead, table tbody { border-bottom: solid; }
table tbody th { text-align: left; }
table tbody th:first-child { border-left: solid; }
table td, table th { border-left: solid; border-right: solid; border-bottom:
solid thin; vertical-align: top; padding: 0.2em; }
.dfnPanel {
display: inline;
position: absolute;
z-index: 35;
height: auto;
width: auto;
padding: 0.5em 0.75em;
font: small Helvetica Neue, sans-serif, Droid Sans Fallback;
background: #DDDDDD;
color: black;
border: outset 0.2em;
}
.dfnPanel * { margin: 0; padding: 0; font: inherit; text-indent: 0; }
.dfnPanel :link, .dfnPanel :visited { color: black; }
.dfnPanel p:not(.spec-link) { font-weight: bolder; }
.dfnPanel * + p { margin-top: 0.25em; }
.dfnPanel li { list-style-position: inside; }
#endif
</style>
<h2 id=goals class=no-num>Goals</h2>
<p>The URL standard takes the following approach towards making URLs fully interoperable:
<ul>
<li><p>Align RFC 3986 and RFC 3987 with contemporary implementations and
obsolete them in the process. (E.g. spaces, other "illegal" code points,
query encoding, equality, canonicalization, are all concepts not entirely
shared, or defined.) URL parsing needs to become as solid as HTML parsing.
[[RFC3986]]
[[RFC3987]]
<li><p>Standardize on the term URL. URI and IRI are just confusing. In
practice a single algorithm is used for both so keeping them distinct is
not helping anyone. URL also easily wins the
<a href="http://www.googlefight.com/index.php?word1=url&word2=uri">search result popularity contest</a>.
<li><p>Supplanting <a href="https://tools.ietf.org/html/rfc6454#section-4">Origin of a URI [sic]</a>.
[[RFC6454]]
<li><p>Define URL's existing JavaScript API in full detail and add
enhancements to make it easier to work with. Add a new <code><a interface>URL</a></code>
object as well for URL manipulation without usage of HTML elements. (Useful
for JavaScript worker environments.)
</ul>
<p class=note>As the editors learn more about the subject matter the goals
might increase in scope somewhat.
<h2 id=urls>URLs</h2>
<!-- History behind URL as term:
http://lists.w3.org/Archives/Public/uri/2012Oct/0080.html -->
<p>A <dfn id=concept-url title='URL'>URL</dfn> is a universal identifier.
<p>A <a>URL</a> consists of components, namely a
<a title='URL scheme'>scheme</a>,
<a title='URL scheme data'>scheme data</a>,
<a title='URL username'>username</a>,
<a title='URL password'>password</a>,
<a title='URL host'>host</a>,
<a title='URL port'>port</a>,
<a title='URL path'>path</a>,
<a title='URL query'>query</a>, and
<a title='URL fragment'>fragment</a>.
<p class="example">
<code>http://username:[email protected]:8000/path?query#fragment</code>
contains values for all <a>URL</a> components except
<a title="URL scheme data">scheme data</a>.
</p>
<p class="example">
<code>javascript:doSomething()</code> contains values for only
<a title='URL scheme'>scheme</a> and
<a title='URL scheme data'>scheme data</a>.
</p>
<p>A <a>URL</a>'s <dfn id=concept-url-scheme title='URL scheme'>scheme</dfn> is
a string that identifies the type of <a>URL</a> and can be used to
dispatch a <a>URL</a> for further processing after
<a title='URL parser'>parsing</a>. It is initially null.
<p>A <a>URL</a>'s
<dfn id=concept-url-scheme-data title='URL scheme data'>scheme data</dfn> is a string holding the contents of a
<a>URL</a>. It is initially null.
<p class="note no-backref">A <a>URL</a>'s
<a title='URL scheme data'>scheme data</a> will be null if its initial
<a title='URL scheme'>scheme</a> is a <a>relative scheme</a>, and
otherwise will be the only component other than <a title='URL
scheme'>scheme</a> that differs from its initial value.
<p>A <a>URL</a>'s <dfn id=concept-url-username title='URL username'>username</dfn>
is a string identifying a user. It is initially the empty string.
<p>A <a>URL</a>'s <dfn id=concept-url-password title='URL password'>password</dfn>
is either null or a string identifying a user's credentials. It is initially null.
<p>A <a>URL</a>'s <dfn id=concept-url-host title='URL host'>host</dfn> is
either null or a <a>host</a>. It is initially null.
<p>A <a>URL</a>'s <dfn id=concept-url-port title='URL port'>port</dfn> is a
string that identifies a networking port. It is initially the empty string.
<p>A <a>URL</a>'s <dfn id=concept-url-path title='URL path'>path</dfn> is a
list of zero or more strings holding data, usually identifying a location in hierarchical
form. It is initially the empty list.
<p>A <a>URL</a>'s <dfn id=concept-url-query title='URL query'>query</dfn> is
either null or a string holding data. It is initially null.
<p>A <a>URL</a>'s <dfn id=concept-url-fragment title='URL fragment'>fragment</dfn>
is either null or a string holding data that can be used for further processing on the
resource the <a>URL</a>'s other components identify.
It is initially null.
<p>A <a>URL</a> also has an associated
<dfn id=concept-url-object title='URL object'>object</dfn> that is either null or a
<code><a>Blob</a></code>. It is initially null.
[[!FILEAPI]]
<p class="note">At this point this is used primarily to support "<code>blob</code>"
URLs, but others can be added going forward, hence "object".
<p>A <dfn>relative scheme</dfn> is a
<a title='URL scheme'>scheme</a> listed in the first column of
the following table. A <dfn>default port</dfn> is a
<a>relative scheme</a>'s optional corresponding
<a title='URL port'>port</a> and is listed in the second column
on the same row.
<table>
<tr><th><a title='URL scheme'>scheme</a>
<th><a title='URL port'>port</a>
<tr><td>"<code>ftp</code>"<td>"<code>21</code>"
<tr><td>"<code>file</code>"<td>
<tr><td>"<code>gopher</code>"<td>"<code>70</code>"
<tr><td>"<code>http</code>"<td>"<code>80</code>"
<tr><td>"<code>https</code>"<td>"<code>443</code>"
<tr><td>"<code>ws</code>"<td>"<code>80</code>"
<tr><td>"<code>wss</code>"<td>"<code>443</code>"
</table>
<!-- The best reason I have for listing "gopher" is Google does it too:
https://code.google.com/p/google-url/source/browse/trunk/src/url_canon_stdurl.cc#120
It seems fine to remain compatible on that front, no need to support it
elsewhere though. -->
<p>A <a>URL</a>
<dfn title="include credentials">includes credentials</dfn> if either its
<a title='URL username'>username</a> is not the empty string or its
<a title='URL password'>password</a> is non-null.
<!-- used by Fetch -->
<p>A <a>URL</a> can be designated as
<dfn id=concept-base-url title='base URL'>base URL</dfn>.
<p class="note no-backref">A <a>base URL</a> is useful for
the <a>URL parser</a> when the input is potentially a
<a>relative URL</a>.
<h3 id=url-writing>Authoring Requirements</h3>
Two types of parse errors are defined. <dfn title='parse exception'>Parse
exceptions</dfn> terminate parsing and must be implemented by all conforming
implementations. By contrast, user agents are encouraged, but not required,
to expose <dfn title='conformance error'>conformance errors</dfn> somehow.
If parsing a given input results in the detection of multiple
<a>conformance errors</a>, user agents may chose to only report a subset of
the errors detected.
<!-- http://tantek.com/2011/238/b1/many-ways-slice-url-name-pieces -->
<p>A <a>URL</a> must be written as either a
<a>relative URL</a> or an
<a>absolute URL</a>, optionally followed by
"<code>#</code>" and a
<a title='URL fragment'>fragment</a>.
<p>An <dfn id=concept-absolute-url title='absolute URL'>absolute URL</dfn> must be a
<a title='URL scheme'>scheme</a>, followed by
"<code>:</code>", followed by either a
<a>scheme-relative URL</a>, if
<a title='URL scheme'>scheme</a> is a <a>relative scheme</a>, or
<a title='URL scheme data'>scheme data</a> otherwise, optionally followed
by "<code>?</code>" and a <a title='URL query'>query</a>.
<p>A <a title='URL scheme'>scheme</a> must be one
<a>ASCII alpha</a>, followed by zero or more of
<a>ASCII alphanumeric</a>, "<code>+</code>",
"<code>-</code>", and "<code>.</code>". A
<a title='URL scheme'>scheme</a> must be registered
<span class=XXX>...</span>.
<p>The syntax of <a title='URL scheme data'>scheme data</a>
depends on the <a title='URL scheme'>scheme</a> and is typically
defined alongside it. Standards must define
<a title='URL scheme data'>scheme data</a> within the constraints of zero or
more <a>URL units</a>, excluding "<code>?</code>".
<p>A <dfn id=concept-relative-url title='relative URL'>relative URL</dfn> must be either a
<a>scheme-relative URL</a>, an
<a>absolute-path-relative URL</a>,
or a <a>path-relative URL</a> that
does not start with a <a title='URL scheme'>scheme</a> and
"<code>:</code>", optionally followed by a "<code>?</code>" and
a <a title='URL query'>query</a>.
<p>At the point where a <a>relative URL</a> is
<a title='URL parser'>parsed</a>, a
<a>base URL</a> must be in scope.
<p>A <dfn id=concept-scheme-relative-url title='scheme-relative URL'>scheme-relative URL</dfn> must be
"<code>//</code>", optionally followed by
<a title='URL Userinfo'>userinfo</a> and "<code>@</code>",
followed by a <a>host</a>, optionally followed
by "<code>:</code>" and a <a title='URL port'>port</a>,
optionally followed by an
<a>absolute-path-relative URL</a>.
<p><dfn id=concept-url-userinfo title='URL Userinfo'>Userinfo</dfn> must be a
<a title='URL username'>username</a>, optionally followed by a
"<code>:</code>" and a
<a title='URL password'>password</a>.
<p>A <a title='URL username'>username</a> must be zero or more
<a>URL units</a>, excluding "<code>/</code>",
"<code>:</code>, "<code>?</code>", and "<code>@</code>".
<!-- password without ":" (sorted on ASCII position) -->
<p>A <a title='URL password'>password</a> must be zero or more
<a>URL units</a>, excluding "<code>/</code>",
"<code>?</code>", and "<code>@</code>".
<p>A <a>host</a> must be either a <a>domain</a>,
or an <a title='IPv4'>IPv4 address</a>,
or "<code>[</code>" followed
by an <a title='IPv6'>IPv6 address</a> followed by
"<code>]</code>".
<p>A <a>domain</a> must be a string that is a
<a>valid domain</a>.
<p class=XXX>Textual representation of <a title='IPv4'>IPv4 address</a> does
not appear to be defined by an RFC. See
<a href="https://tools.ietf.org/html/draft-main-ipaddr-text-rep-00">Textual
Representation of IPv4 and IPv6 Addresses</a> for some history.
<p>An <a title='IPv6'>IPv6 address</a> is defined in the
<a href="http://tools.ietf.org/html/rfc4291#section-2.2">"Text Representation of Addresses" chapter of IP Version 6 Addressing Architecture</a>.
[[!RFC4291]]
<!-- http://tools.ietf.org/html/rfc5952 updates that RFC, but it seems as
far as what developers can do we should be liberal
XXX should we define the format inline instead just like STD 66? -->
<p>A <a title='URL port'>port</a> must be zero or more
<a>ASCII digits</a>.
<p>An
<dfn id=concept-absolute-path-relative-url title='absolute-path-relative URL'>absolute-path-relative URL</dfn>
must be "<code>/</code>", followed by a
<a>path-relative URL</a> that does not
start with "<code>/</code>".
<p>A <dfn id=concept-path-relative-url title='path-relative URL'>path-relative URL</dfn> must be zero or
more <a title="path segment">path segments</a> separated from each
other by a "<code>/</code>". The first segment (if any) of a <a>path-relative
URL</a> must not contain a colon (<code>U+003A</code>).
<p>A <dfn>path segment</dfn> must be zero or more <a>URL units</a>,
excluding "<code>/</code>" and "<code>?</code>".
<p>A <a title='URL query'>query</a> must be zero or more
<a>URL units</a>.
<p>A <a title='URL fragment'>fragment</a> must be zero or more
<a>URL units</a>.
<p>The <dfn>URL code points</dfn> are <a>ASCII alphanumeric</a>,
"<code>!</code>",<!-- 0x21, sub-delims -->
"<code>$</code>",<!-- 0x24, sub-delims -->
"<code>&</code>",<!-- 0x26, sub-delims -->
"<code>'</code>",<!-- 0x27, sub-delims -->
"<code>(</code>",<!-- 0x28, sub-delims -->
"<code>)</code>",<!-- 0x29, sub-delims -->
"<code>*</code>",<!-- 0x2A, sub-delims -->
"<code>+</code>",<!-- 0x2B, sub-delims -->
"<code>,</code>",<!-- 0x2C, sub-delims -->
"<code>-</code>",<!-- 0x2D, iunreserved -->
"<code>.</code>",<!-- 0x2E, iunreserved -->
"<code>/</code>",<!-- 0x2F, iquery/ifragment -->
"<code>:</code>",<!-- 0x3A, ipchar -->
"<code>;</code>",<!-- 0x3B, sub-delims -->
"<code>=</code>",<!-- 0x3D, sub-delims -->
"<code>?</code>",<!-- 0x3F, iquery/ifragment -->
"<code>@</code>",<!-- 0x40, ipchar -->
"<code>_</code>",<!-- 0x5F, iunreserved -->
"<code>~</code>",<!-- 0x7E, iunreserved -->
and code points in the ranges
U+00A0 to U+D7FF,
U+E000 to <!--U+F8FF,
U+F900 to -->U+FDCF,
U+FDF0 to U+FFFD,<!-- changed relative to IRI from U+FFEF to U+FFFD to align with HTML-->
U+10000 to U+1FFFD,
U+20000 to U+2FFFD,
U+30000 to U+3FFFD,
U+40000 to U+4FFFD,
U+50000 to U+5FFFD,
U+60000 to U+6FFFD,
U+70000 to U+7FFFD,
U+80000 to U+8FFFD,
U+90000 to U+9FFFD,
U+A0000 to U+AFFFD,
U+B0000 to U+BFFFD,
U+C0000 to U+CFFFD,
U+D0000 to U+DFFFD,
U+E0000 to U+EFFFD,<!-- changed relative to IRI from E1000 to E0000 to align with HTML-->
U+F0000 to U+FFFFD,
U+100000 to U+10FFFD.
<p class=note>Code points higher than U+009F will be converted to
<a title="percent-encoded byte">percent-encoded bytes</a> by the
<a>URL parser</a>, except for code points appearing in
<a title="URL fragment">fragments</a>.
<p>The <dfn>URL units</dfn> are <a>URL code points</a> and
<a title="percent-encoded byte">percent-encoded bytes</a>.
<h3 id=url-parsing>Parsers</h3>
<p>The <dfn id=concept-url-parser title='URL parser'>URL parser</dfn> takes a string
<var>input</var>, optionally with a
<a>base URL</a> <var>base</var>, and
optionally with an <a>encoding</a>
<var>encoding override</var>, and then runs these steps:
<ol>
<li><p>Let <var>url</var> be the result of running the
<a>basic URL parser</a> on <var>input</var>
with <var>base</var>, and <var>encoding override</var> as provided.
<li><p>If <var>url</var> is failure, return failure.
<li><p>If <var>url</var>'s <a title='URL scheme'>scheme</a> is not
"<code>blob</code>", return <var>url</var>.
<li><p>If <var>url</var>'s <a title='URL scheme data'>scheme data</a>
is not in the <a>blob URL store</a>, return
<var>url</var>. [[!FILEAPI]]
<li><p>Set <var>url</var>'s <a title='URL object'>object</a> to a
<a>structured clone</a> of the entry in the
<a>blob URL store</a> corresponding to
<var>url</var>'s <a title='URL scheme data'>scheme data</a>.
[[!HTML]]
<li><p>Return <var>url</var>.
</ol>
<hr>
<p>The <dfn id=concept-basic-url-parser title='basic URL parser'>basic URL parser</dfn> takes a string
<var>input</var>, optionally with a
<a>base URL</a> <var>base</var>,
optionally with an <a>encoding</a>
<var>encoding override</var>, optionally with an
<a>URL</a> <var>url</var> and a state override
<var>state override</var>, and then runs these steps:
<div class="note no-backref">
<p>The <var>encoding override</var> argument is a legacy concept only relevant for
HTML. The <var>url</var> and <var>state override</var> arguments are only for
use by methods of objects implementing the <code><a interface>URLUtils</a></code> interface.
[[!HTML]]
<p>When the <var>url</var> and <var>state override</var> arguments are not
passed the <a>basic URL parser</a> returns either a
<a>URL</a> or failure. If they are passed the
algorithm simply modifies the passed <var>url</var> and can terminate without
returning anything.
</div>
<ol>
<li>
<p>If <var>url</var> is not given:
<ol>
<li><p>Set <var>url</var> to a new <a>URL</a>.
<li><p>If leading or trailing <a>ASCII whitespace</a> codepoints are
present in the <var>input</var>:</p>
<ol>
<li><p>Indicate a <a>conformance error</a>.</p></li>
<li><p>Remove leading and trailing <a>ASCII whitespace</a> from
<var>input</var>.<p></li>
</ol>
</ol>
<li><p>If <var>base</var> is not given, set it a new <a>URL</a> with
<code>scheme</code> set to <code>about</code>.
<li><p>If <var>encoding override</var> is not given, set it to
<a>utf-8</a>.
<li>Invoke <code class=grammar-rule><a href=#url>url</a></code>
on the <var>input</var> passing <var>url</var>, <var>base</var> and
<var>encoding override</var>.
<li><p>Return <var>url</var>.
</ol>
Parsing Rules {#parsing-rules}
---
These railroad diagrams, as modified by the accompanying text, define grammar
production rules for URLs. They are to be evaluated sequentially, first
left-to-right then top-to-bottom, backtracking as necessary, until a complete
match against the input provided is found.
Each rule defines a function that can be invoked individually. Rules can
invoke one another.
<dfn title="parse input">Parsing a given input</dfn> according to a railroad
diagram produces a number of intermediate values that can be referenced
individually as local variables within the function. The names of those local
variables isn't specified by this specification, instead the names used in the
railroad diagrams are referenced.
If a given input doesn't match the railroad diagram, failure is returned
instead. If failure is returned by an alternative, evaluation continues
with the next alternative.
The following conventions are used in descriptions of parsing logic for
clarity and conciseness:
* The phrase "<code class=grammar-rule><span>x</span></code>
<dfn>is present</dfn>" is
to be interpreted to mean "the grammar rule for
<code class=grammar-rule><span>x</code><span> matches some part of the
<code>input</code> when that <code>input</code> is parsed according to
the given railroad diagram". Note that if the railroad diagram contains
alternatives where multiple alternatives could potentially match the
input, only the first matching input is considered to be present.
* The phrase "<dfn>value of</dfn>
<code class=grammar-rule><span>x</span></code>" is to be
interpreted to mean "the value returned by the function
<code>x(input)</code> when the function <code>x</code> is passed some
part of the original <code>input</code> during the parsing of that
original <code>input</code> according to the given railroad diagram".
Note: extracting the railroad diagrams from this specification and
interpreting them in isolation will produce incorrect results. In particular,
the definitions provided in the prose modifies these parsing algorithms in
important ways including returning failure and early termination.
#include <url.pegjs>
Common Functions {#common-functions}
---
<p>To <dfn>percent encode</dfn> a <var>byte</var> into a
<a>percent-encoded byte</a>, return a string consisting of
"<code>%</code>", followed by a double-digit, uppercase, hexadecimal
representation of <var>byte</var>.
<p>To <dfn>percent decode</dfn> a byte sequence <var>input</var>, run these steps:
<p class=warning>Using anything but a <a>utf-8 decoder</a>
when the <var>input</var> contains bytes outside the range 0x00 to 0x7F might be
insecure and is not recommended.
<ol>
<li><p>Let <var>output</var> be an empty byte sequence.
<li>
<p>For each byte <var>byte</var> in <var>input</var>, run these steps:
<ol>
<li><p>If <var>byte</var> is not `<code>%</code>`, append
<var>byte</var> to <var>output</var>.
<li><p>Otherwise, if <var>byte</var> is `<code>%</code>` and the next two
bytes after <var>byte</var> in <var>input</var> are not in the ranges
0x30 to 0x39, 0x41 to 0x46, and 0x61 to 0x66, append <var>byte</var> to
<var>output</var>.
<li>
<p>Otherwise, run these substeps:
<ol>
<li><p>Let <var>bytePoint</var> be the two bytes after <var>byte</var> in
<var>input</var>,
<a title="utf-8 decode without BOM">decoded</a>, and
then interpreted as hexadecimal number.
<!-- We should have a definition for this that is saner. -->
<li><p>Append a byte whose value is <var>bytePoint</var> to
<var>output</var>.
<li><p>Skip the next two bytes in <var>input</var>.
</ol>
</ol>
<li><p>Return <var>output</var>.
</ol>
<p>To <dfn>utf-8 percent encode</dfn> a <var>code point</var>, using
an <var>encode set</var>, run these steps:
<ol>
<li><p>If <var>code point</var> is not in
<var>encode set</var>, return <var>code point</var>.
<li><p>Let <var>bytes</var> be the result of running
<a>utf-8 encode</a> on
<var>code point</var>.
<li><p><a>Percent encode</a> each byte in <var>bytes</var>, and
then return them concatenated, in the same order.
</ol>
<p>The <dfn id=concept-domain-to-ascii title='domain to ASCII'>domain to
ASCII</dfn> function given a <a>domain</a> <var>domain</var>, runs these steps:
<ol>
<li><p>Let <var>result</var> be the result of running
<a title=ToASCII>Unicode ToASCII</a> with
<i>domain_name</i> set to <var>domain</var>,
<i>UseSTD3ASCIIRules</i> set to false, <i>processing_option</i> set to
<i>Transitional_Processing</i>, and <i>VerifyDnsLength</i> set to false.
<li><p>If <var>result</var> is a failure value, return failure.
<li><p>Return <var>result</var>.
</ol>
<p>The <dfn id=concept-domain-to-unicode title='domain to Unicode'>domain to
Unicode</dfn> function given a <a>domain</a> <var>domain</var>, runs these
steps:
<ol>
<li><p>Let <var>result</var> be the result of running
<a title=ToUnicode>Unicode ToUnicode</a> with
<i>domain_name</i> set to <var>domain</var>,
<i>UseSTD3ASCIIRules</i> set to false.
<li>
<p>Return <var>result</var>, ignoring any returned errors.
<p class=note>User agents are encouraged to report errors through a developer console.
</ol>
<p>A <var>domain</var> is a <dfn>valid domain</dfn> if these steps return success:
<ol>
<li><p>Let <var>result</var> be the result of running
<a title=ToASCII>Unicode ToASCII</a> with
<i>domain_name</i> set to <var>domain</var>,
<i>UseSTD3ASCIIRules</i> set to true, <i>processing_option</i> set to
<i>Nontransitional_Processing</i>, and <i>VerifyDnsLength</i> set to true.
<li><p>If <var>result</var> is a failure value, return failure.
<li><p>Set <var>result</var> to the result of running
<a title=ToUnicode>Unicode ToUnicode</a> with
<i>domain_name</i> set to <var>result</var>,
<i>UseSTD3ASCIIRules</i> set to true.
<li><p>If <var>result</var> contains any errors, return failure.
<li><p>Return success.
</ol>
<p class=XXX>Ideally we define this in terms of a sequence of code points that make up a
<a>valid domain</a> rather than through a whack-a-mole:
<a href=https://www.w3.org/Bugs/Public/show_bug.cgi?id=25334>bug 25334</a>.
<p>The <dfn id=concept-host-parser title='host parser'>host parser</dfn> takes
a string <var>input</var> and then runs these steps:
<ol>
<li><p>If <var>input</var> is the empty string, return failure.
<!-- Otherwise parsing URLs would not be idempotent:
http://@/example.org/ -> http:///example.org/ -> http://example.org/
Could potentially move this check to the URL parser if deemed problematic. -->
<li><p>Let <var>domain</var> be the result of
<a>utf-8 decode without BOM</a> on the
<a title="percent decode">percent decoding</a> of
<a>utf-8 encode</a> on <var>input</var>.
<!-- https://bugzilla.mozilla.org/show_bug.cgi?id=309671 -->
<li><p>Let <var>asciiDomain</var> be the result of running
<a>domain to ASCII</a> on <var>domain</var>.
<li><p>If <var>asciiDomain</var> is failure, return failure.
<li>
<p>If <var>asciiDomain</var> contains one of
U+0000,
U+0009,
U+000A,
U+000D,
U+0020,
"<code>#</code>",<!-- 23 -->
"<code>%</code>",<!-- 25 -->
"<code>/</code>",<!-- 2F -->
"<code>:</code>",<!-- 3A -->
"<code>?</code>",<!-- 3F -->
"<code>@</code>",<!-- 40 -->
"<code>[</code>",<!-- 5B -->
"<code>\</code>",<!-- 5C -->
and
"<code>]</code>",<!-- 5D -->
return failure.
<li><p>Return <var>asciiDomain</var>.
</ol>
To <dfn>cleanse</dfn> a string given an <var>encode set</var>, run these steps:
<ol>
<li>If any character in the string not a <a>URL code point</a>
or a percent sign (<code>U+0025</code>), indicate a <a>conformance
error</a>.
<li>If the name includes a percent sign (<code>U+0025</code>) that is not
immediately followed by two hexadecimal characters, indicate a
<a>conformance error</a>.
<li>If any <code>U+0009</code>, <code>U+000A</code> or <code>U+000D</code>
characters are present in the string, remove those characters and indicate
a <a>conformance error</a>.
<li>if the <var>encode set</var> is non-null,
first <a>encode</a> the
result using the <code>encoding override</code>, then
<a>percent encode</a>
that result using the provided <var>encode set</var>.
<li>Return the result as a string.
</ol>
To do <dfn>path concatenation</dfn> given a <var>base</var> array of path
names, and a <var>path</path> array of names, run the following steps:
<ol>
<li>If <var>base</var> is null, set <var>base</var> to an empty array.
Otherwise make a local copy of the <var>base</var> array.
<li>If the first element on <var>path</var> is ".", remove this first element
from <var>path</var> as well as the last element (if any) of
<var>base</var>.
<li>If <var>path.length</var> is one, and the first and only element on the
<var>path</var> is an empty string, set <var>path</var> to the value of
<var>base</var>.
<li>Otherwise if <var>path.length</var> is greater than one, and the first
element on the <var>path</var> is the empty string, remove the first
element from the <var>path</var>.
<li>Otherwise, remove the last element (if any) of <var>base</var> and then
prepend the values of the <var>base</var> array to the <var>path</var>
array.
</ol>
<h3 id=url-serializing>Serializers</h3>
<p>The <dfn id=concept-url-serializer title='URL serializer'>URL serializer</dfn> takes a
<a>URL</a> <var>url</var>,
optionally an <i title>exclude fragment flag</i>, and then runs these steps:
<ol>
<li><p>Let <var>output</var> be <var>url</var>'s
<a title='URL scheme'>scheme</a> and
"<code>:</code>" concatenated.
<li>
<p>If <var>url</var>'s <a title='URL scheme data'>scheme data</a> is unset:
<ol>
<li><p>Append "<code>//</code>" to <var>output</var>.
<li>
<p>If <var>url</var>'s
<a title='URL username'>username</a> is not the empty string
or <var>url</var>'s
<a title='URL password'>password</a> is non-null, run these
substeps:
<ol>
<li><p>Append <var>url</var>'s
<a title='URL username'>username</a> to
<var>output</var>.
<li><p>If <var>url</var>'s
<a title='URL password'>password</a> is non-null, append
"<code>:</code>" concatenated with <var>url</var>'s
<a title='URL password'>password</a> to
<var>output</var>.
<li><p>Append "<code>@</code>" to <var>output</var>.
</ol>
<li><p>Append <var>url</var>'s
<a title='URL host'>host</a>,
<a title='host serializer'>serialized</a>, to
<var>output</var>.
<li><p>If <var>url</var>'s <a title='URL port'>port</a>
is not the empty string, append "<code>:</code>" concatenated with
<var>url</var>'s <a title='URL port'>port</a> to
<var>output</var>.
<li><p>Append "<code>/</code>" concatenated with the strings in
<var>url</var>'s <a title='URL path'>path</a>
(including empty strings), separated from each other by
"<code>/</code>" to <var>output</var>.
</ol>
<li><p>Otherwise, append <var>url</var>'s
<a title='URL scheme data'>scheme data</a> to
<var>output</var>.
<li><p>If <var>url</var>'s <a title='URL query'>query</a> is non-null,
append "<code>?</code>" concatenated with <var>url</var>'s
<a title='URL query'>query</a> to <var>output</var>.
<li><p>If the <i title>exclude fragment flag</i> is unset and
<var>url</var>'s <a title='URL fragment'>fragment</a> is
non-null, append "<code>#</code>" concatenated with
<var>url</var>'s <a title='URL fragment'>fragment</a> to
<var>output</var>.
<li><p>Return <var>output</var>.
</ol>
<p>The <dfn id=concept-host-serializer title='host serializer'>host serializer</dfn> takes null or a
<a>host</a> <var>host</var> and then runs
these steps:
<ol>
<li><p>If <var>host</var> is null, return the empty string.
<li><p>If <var>host</var> is an
<a title='IPv6'>IPv6 address</a>, return
"<code>[</code>", followed by the result of running the
<a>IPv6 serializer</a> on <var>host</var>,
followed by "<code>]</code>".
<li><p>Otherwise, <var>host</var> is a <a>domain</a> or an
<a title='IPv4'>IPv4 address</a>,
return <var>host</var>.
</ol>
<p>The <dfn id=concept-ipv6-serializer title='IPv6 serializer'>IPv6 serializer</dfn> takes an
<a title='IPv6'>IPv6 address</a> <var>address</var> and
then runs these steps:
<ol>
<li><p>Let <var>output</var> be the empty string.
<li>
<p>Let <var>compress pointer</var> be a pointer to the first
<a title='IPv6 piece'>16-bit piece</a> in the first longest
sequences of <var>address</var>'s
<a title='IPv6 piece'>16-bit pieces</a> that are 0.
<p class=example>In <code>0:f:0:0:f:f:0:0</code> it would point to
the second 0.
<li><p>If there is no sequence of <var>address</var>'s
<a title='IPv6 piece'>16-bit pieces</a> that are 0 longer than
one, set <var>compress pointer</var> to null.
<li>
<p>For each <var>piece</var> in <var>address</var>'s
<a title='IPv6 piece'>pieces</a>, run these substeps:
<ol>
<li><p>If <var>compress pointer</var> points to
<var>piece</var>, append "<code>::</code>" to
<var>output</var> if <var>piece</var> is
<var>address</var>'s first <a title='IPv6 piece'>piece</a> and append
"<code>:</code>" otherwise, and then run these substeps again with all
subsequent <a title='IPv6 piece'>pieces</a> in
<var>address</var>'s <a title='IPv6 piece'>pieces</a>
that are 0 skipped or go the next step in the overall set of steps if
that leaves no <a title='IPv6 piece'>pieces</a>.
<li><p>Append <var>piece</var>, represented as the shortest
possible lowercase hexadecimal number, to <var>output</var>.
<li><p>If <var>piece</var> is not
<var>address</var>'s last <a title='IPv6 piece'>piece</a>,
append "<code>:</code>" to <var>output</var>.
</ol>
<li><p>Return <var>output</var>.
</ol>
<p class=note>This algorithm requires the recommendation from
A Recommendation for IPv6 Address Text Representation.
[[RFC5952]]
<!-- Safari/Gecko/Opera do not normalize IPv6. Chrome does. This algorithm
follows Chrome because we normalize domains too. -->
<h3 id=origin>Origin</h3>
<!-- Still need to watch the final bits -->
<p class=note>See <a spec=html>origin</a>'s definition in HTML for the
necessary background information. [[!HTML]]
<p>A <a>URL</a>'s <dfn id=concept-url-origin title='URL origin'>origin</dfn> is
the <a spec=html>origin</a> returned by running these steps, switching
on <a>URL</a>'s <a title='URL scheme'>scheme</a>:
<dl class=switch>
<dt>"<code>blob</code>"
<dd>
<p>Let <var>url</var> be the result of
<a title='basic URL parser'>parsing</a>
<a>URL</a>'s
<a title='URL scheme data'>scheme data</a>.
<p>If <var>url</var> is failure, return a new globally unique identifier.
Otherwise, return <var>url</var>'s <a title='URL origin'>origin</a>.
<!-- Did you mean: recursion -->
<p class="example no-backref">The <a title='URL origin'>origin</a> of
<code>blob:https://whatwg.org/d0360e2f-caee-469f-9a2f-87d5b0456f6f</code> is
the tuple
(<code>https</code>, <code>whatwg.org</code>, <code>443</code>).
<dt>"<code>ftp</code>"
<dt>"<code>gopher</code>"
<dt>"<code>http</code>"
<dt>"<code>https</code>"
<dt>"<code>ws</code>"
<dt>"<code>wss</code>"
<dd><p>Return a tuple consisting of <a>URL</a>'s
<a title='URL scheme'>scheme</a>, its
<a title='URL host'>host</a>, and its <a>default port</a> if its
<a title='URL port'>port</a> is the empty string, and its
<a title='URL port'>port</a> otherwise.
<dt>"<code>file</code>"
<dd><p>Unfortunate as it is, this is left as an exercise to the reader. When in doubt,
return a new globally unique identifier.
<dt>Otherwise
<dd><p>Return a new globally unique identifier.
</dl>
<h2 id=api>APIs</h2>
<!-- XXX https://www.w3.org/Bugs/Public/show_bug.cgi?id=20159 -->
<pre class=idl>[Constructor(USVString url, optional USVString base = "about:blank"),
Exposed=(Window,Worker)]
interface URL {
static USVString domainToASCII(USVString domain);
static USVString domainToUnicode(USVString domain);
};
URL implements URLUtils;
[NoInterfaceObject,
Exposed=(Window,Worker)]
interface URLUtils {
stringifier attribute USVString href;
readonly attribute USVString origin;
attribute USVString protocol;
attribute USVString username;
attribute USVString password;
attribute USVString host;
attribute USVString hostname;
attribute USVString port;
attribute USVString pathname;
attribute USVString search;
attribute URLSearchParams searchParams;
attribute USVString hash;
};
[NoInterfaceObject,
Exposed=(Window,Worker)]
interface URLUtilsReadOnly {
stringifier readonly attribute USVString href;
readonly attribute USVString origin;
readonly attribute USVString protocol;<!--
readonly attribute USVString username;
readonly attribute USVString password;-->
readonly attribute USVString host;
readonly attribute USVString hostname;
readonly attribute USVString port;
readonly attribute USVString pathname;
readonly attribute USVString search;<!--
readonly attribute URLSearchParams searchParams;-->
readonly attribute USVString hash;
};</pre>
<p>Except where different objects implementing <code><a interface>URLUtilsReadOnly</a></code> are identical
to objects implementing <code><a interface>URLUtils</a></code>.
<p class=note>Since all members are readonly and certain members from
<code><a interface>URLUtils</a></code> are not exposed a number of potential optimizations is possible
compared to objects implementing <code><a interface>URLUtils</a></code>. These are left as an exercise to
the reader.
<!-- XXX Ideas:
boolean isEqual(URL, optional URLEqualOptions options)
attribute URLPath segments;
dictionary URLEqualOptions {
boolean percentEncoding = false;
boolean ignoreHash = false;
boolean ignoreDomainDot = false;
...
};
URLPath would be a subclassed Array? -->
<p>Specifications defining objects implementing <code><a interface>URLUtils</a></code> or
<code><a interface>URLUtilsReadOnly</a></code> must define a
<dfn id=concept-URLUtils-get-the-base title='URLUtils get the base'>get the base</dfn> algorithm, which must return the
appropriate <a>base URL</a> for the object.
<p>Specifications defining objects implementing <code><a interface>URLUtils</a></code> may
define <dfn id=concept-URLUtils-update title='URLUtils update'>update steps</dfn> to make it possible for an
underlying string (such as an
<a title=concept-attribute-value>attribute value</a>)
to be updated. The <a title='URLUtils update'>update steps</a> are passed a string
<var>value</var> for this purpose.
<p>An object implementing <code><a interface>URLUtils</a></code> or <code><a interface>URLUtilsReadOnly</a></code> has an