-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.yue
1384 lines (1176 loc) · 48.5 KB
/
url.yue
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
--[[
MIT License
Copyright (c) 2024 Pika Software
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--]]
import type, tostring, tonumber, rawget, rawset, getmetatable, pairs from _G
import byte, char, sub, gsub, lower, format from string
import rshift, lshift, band, bor from (bit or bit32)
import concat, remove from table
import floor from math
-- a quirky way to check if number have metatable
-- if it has, then we can use getmetatable and have even faster isnumber function
local isstring
if stringMeta := getmetatable("")
isstring = (v) -> getmetatable(v) == stringMeta
local isnumber
if numberMeta := getmetatable(0)
isnumber = (v) -> getmetatable(v) == numberMeta
-- Garry's Mod have is<type> functions, prefer them because they are faster than type
isstring = isstring or (v) -> type(v) == "string"
istable = istable or (v) -> type(v) == "table"
isnumber = isnumber or (v) -> type(v) == "number"
compileCharacterTable = (chars) ->
result = []
for v in *chars
if istable v
for i = byte(v[1]), byte(v[2])
result[i] = true
else
result[byte(v)] = true
return result
-- Use result from `compileCharacterTable` in `containsCharacter` as chars
containsCharacter = (str, chars, startPos = 1, endPos = #str) ->
for i = startPos, endPos
if chars[byte(str, i)]
return true
return false
PUNYCODE_PREFIX = [0x78, 0x6E, 0x2D, 0x2D] -- xn--
SPECIAL_SCHEMAS =
ftp: 21
file: true -- currently if value is true, then scheme will be treated as file: scheme in the parser
http: 80
https: 443
ws: 80
wss: 443
FORBIDDEN_HOST_CODE_POINTS = compileCharacterTable [
"\0", "\t", "\n", "\r"
" ", "#", "/", ":"
"<", ">", "?", "@"
"[", "\\", "]", "^", "|"
]
FORBIDDEN_DOMAIN_CODE_POINTS = compileCharacterTable [
"\0", "\t", "\n", "\r"
" ", "#", "/", ":"
"<", ">", "?", "@"
"[", "\\", "]", "^", "|"
["\0", "\x1F"], "%", "\x7F"
]
FILE_OTHERWISE_CODE_POINTS = compileCharacterTable [ "/", "\\", "?", "#" ]
DECODE_LOOKUP_TABLE = []
for i = 0x00, 0xFF
hex = bit.tohex(i, 2)
DECODE_LOOKUP_TABLE[hex] = char(i)
DECODE_LOOKUP_TABLE[hex\upper!] = char(i)
URI_DECODE_SET = {...DECODE_LOOKUP_TABLE}
for i in *[0x2D, 0x2E, 0x21, 0x7E, 0x2A, 0x27, 0x28, 0x29] -- decodeURI doesn't decode these characters: ; / ? : @ & = + $ , #
hex = bit.tohex(i, 2)
URI_DECODE_SET[hex] = nil
URI_DECODE_SET[hex\upper!] = nil
percentDecode = (s, decodeSet) ->
s = gsub(s, "%%(%x%x)", decodeSet)
return s
compilePercentEncodeSet = (encodeSet, ...) ->
-- Lookup table for decoding percent-encoded characters and encoding special characters
-- Using HEX_TABLE will result in a double speedup compared to using functions
encodeSet = {...encodeSet} -- copy given encodeSet, so we don't modify it
for ch in *{...}
if isstring ch
ch = byte(ch) -- if string then convert to byte
if isnumber ch -- just a single character
encodeSet[char(ch)] = "%" .. bit.tohex(ch, 2)\upper!
elseif istable ch -- range of characters
charStart = isstring(ch[1]) and byte(ch[1]) or ch[1]
charEnd = isstring(ch[2]) and byte(ch[2]) or ch[2]
for i = charStart, charEnd
encodeSet[char(i)] = "%" .. bit.tohex(i, 2)\upper!
return encodeSet
percentEncode = (s, encodeSet, spaceAsPlus) ->
old = nil
if spaceAsPlus == true
old = encodeSet[" "]
encodeSet[" "] = "+"
s = gsub(s, "%W", encodeSet)
if old
encodeSet[" "] = old
return s
C0_ENCODE_SET = compilePercentEncodeSet({}, [0x00, 0x1F], [0x7F, 0xFF])
FRAGMENT_ENCODE_SET = compilePercentEncodeSet(C0_ENCODE_SET, " ", "\"", "<", ">", "`")
QUERY_ENCODE_SET = compilePercentEncodeSet(C0_ENCODE_SET, " ", "\"", "#", "<", ">")
SPECIAL_QUERY_ENCODE_SET = compilePercentEncodeSet(QUERY_ENCODE_SET, "'")
PATH_ENCODE_SET = compilePercentEncodeSet(QUERY_ENCODE_SET, "?", "`", "{", "}")
USERINFO_ENCODE_SET = compilePercentEncodeSet(PATH_ENCODE_SET, "/", ":", ";", "=", "@", [0x5B, 0x5E], "|")
COMPONENT_ENCODE_SET = compilePercentEncodeSet(USERINFO_ENCODE_SET, [0x24, 0x26], "+", ",")
URLENCODED_ENCODE_SET = compilePercentEncodeSet(COMPONENT_ENCODE_SET, "!", [0x27, 0x29], "~")
URI_ENCODE_SET = compilePercentEncodeSet(C0_ENCODE_SET, 0x20, 0x22, 0x25, 0x3C, 0x3E, [0x42, 0x59], [0x5B, 0x5E], 0x60, [0x62, 0x79], [0x7B, 0x7D])
export encodeURI = (s) -> percentEncode(s, URI_ENCODE_SET)
export encodeURIComponent = (s) -> percentEncode(s, COMPONENT_ENCODE_SET, true)
export decodeURIComponent = (s) -> percentDecode(s, DECODE_LOOKUP_TABLE)
export decodeURI = (s) -> percentDecode(s, URI_DECODE_SET)
isLower = (ch) -> ch >= 0x61 --[['a']] and ch <= 0x7A --[['z']]
isUpper = (ch) -> ch >= 0x41 --[['A']] and ch <= 0x5A --[['Z']]
isAlpha = (ch) -> isLower(ch) or isUpper(ch)
isDigit = (ch) -> ch >= 0x30 --[['0']] and ch <= 0x39 --[['9']]
isHexDigit = (ch) -> isDigit(ch) or ch >= 0x41 --[['A']] and ch <= 0x46 --[['F']] or ch >= 0x61 --[['a']] and ch <= 0x66 --[['f']]
isSingleDot = (str) ->
return switch #str
when 1 then str == "."
when 3 then lower(str) == "%2e"
else false
isDoubleDot = (str) ->
return switch #str
when 2 then str == ".."
when 4
str = lower(str)
str == "%2e." or str == ".%2e"
when 6 then lower(str) == "%2e%2e"
else false
isWindowsDriveLetterCodePoints = (ch1, ch2, normalized) -> isAlpha(ch1) and (ch2 == 0x3A --[[':']] or (normalized == false and ch2 == 0x7C --[['|']]))
isWindowsDriveLetter = (str, normalized) -> return #str == 2 and isWindowsDriveLetterCodePoints(byte(str, 1), byte(str, 2), normalized)
startsWithWindowsDriveLetter = (str, startPos, endPos) ->
len = endPos - startPos + 1
return len >= 2 and
isWindowsDriveLetterCodePoints(byte(str, startPos), byte(str, startPos + 1), false) and
(len == 2 or FILE_OTHERWISE_CODE_POINTS[byte(str, startPos + 2)])
-- Converts character to digit,
-- if given non valid character it will return invalid number
charToDec = (ch) -> ch - 0x30
hexToDec = (ch) ->
if ch >= 0x61 --[['a']] then return ch - 0x61 + 10
elseif ch >= 0x41 --[['A']] then return ch - 0x41 + 10
else return charToDec(ch)
-- Finds nearest non whitespace character from startPos to endPos
-- And returns the position of that character
trimInput = (str, startPos, endPos) ->
for i = startPos, endPos, (startPos < endPos and 1 or -1)
ch = byte(str, i)
if not ch --[[ EOF ]] or ch > 0x20 --[[ C0 control or space ]]
return i
return endPos - 1
-- UTF-8 decoder from https://bjoern.hoehrmann.de/utf-8/decoder/dfa/
UTF8_DECODE_LOOKUP = []
do
UTF8_DECODE_LOOKUP_RULES = [
{0, 0x00, 0x7f}, {1, 0x80, 0x8f}, {9, 0x90, 0x9f}, {7, 0xa0, 0xbf}, -- 0x00 - 0xbf
{8, 0xc0, 0xc1}, {2, 0xc2, 0xdf}, {0xa, 0xe0}, {0x3, 0xe1, 0xef}, -- 0xc0 - 0xef
{0xb, 0xf0}, {0x6, 0xf1, 0xf3}, {0x5, 0xf4}, {0x8, 0xf5, 0xff}, -- 0xf0 - 0xff
{0x0, 0x100}, {0x1, 0x101}, {0x2, 0x102}, {0x3, 0x103}, {0x5, 0x104} -- 0x100 - 0x104
{0x8, 0x105}, {0x7, 0x106}, {0x1, 0x107, 0x10f}, {0x4, 0x10a}, {0x6, 0x10b}, -- 0x105 - 0x10f
{1, 0x110, 0x12f}, {0, 0x121}, {0, 0x127}, {0, 0x129}, -- 0x110 - 0x12f
{1, 0x130, 0x14f}, {2, 0x131}, {2, 0x137}, {2, 0x139}, {2, 0x147}, -- 0x130 - 0x14f
{1, 0x150, 0x16f}, {2, 0x151}, {2, 0x159}, {3, 0x167}, {3, 0x169}, -- 0x150 - 0x16f
{1, 0x170, 0x18f}, {3, 0x171}, {3, 0x177}, {3, 0x179}, {3, 0x181} -- 0x170 - 0x181
] -- {val, start, end} / {val, pos}
for rule in *UTF8_DECODE_LOOKUP_RULES do
if rule[3]
for i = rule[2], rule[3] do UTF8_DECODE_LOOKUP[i] = rule[1]
else UTF8_DECODE_LOOKUP[rule[2]] = rule[1]
utf8Decode = (str, startPos, endPos) ->
-- TODO add support for fullwidth utf8/utf16
output = []
count = state = codep = 0
for i = startPos, endPos
b = byte(str, i)
t = UTF8_DECODE_LOOKUP[b]
codep = (state != 0) and
bor( band(b, 0x3f), lshift(codep, 6) ) or
band( rshift(0xff, t), b )
state = UTF8_DECODE_LOOKUP[256 + state * 16 + t]
if state == 0
count += 1
output[count] = codep
if state != 0
error "Invalid URL: UTF-8 decoding error"
return output
-- RFC 3492 Punycode encode
punycodeEncode = (str, startPos, endPos) ->
const base = 36
const tMin = 1
const tMax = 26
const skew = 38
const damp = 700
const initialBias = 72
const initialN = 0x80
const delimiter = 0x2D --[['-']]
-- Initialize the state
n = initialN
input = utf8Decode(str, startPos, endPos)
inputLen = #input
output = []
delta = out = 0
bias = initialBias
-- Handle the basic code points
for ch in *input
if ch < 0x80
out += 1
output[out] = char(ch)
-- h is the number of code points that have been handled, b is the number of basic code points
-- that have been handled, and out is the number of characters that have been output.
h = b = out
if b > 0
out += 1
output[out] = char(delimiter)
-- Main encoding loop
while h < inputLen
-- All non-basic code points < n have been handled already. Find the next larger one
m = 0x7FFFFFFF
for ch in *input
if ch >= n and ch < m then m = ch
-- Increase delta enough to advance the decoder's <n,i> state to <m,0>, but guard against overflow
if m - n > (0x7FFFFFFF - delta) / (h + 1)
error "Invalid URL: Punycode overflow"
delta += (m - n) * (h + 1)
n = m
for ch in *input
-- Punycode does not need to check whether input[j] is basic:
if ch < n
delta += 1 -- Move this down incase of wrong answer
if delta + 1 > 0x7FFFFFFF
error "Invalid URL: Punycode overflow"
if ch == n
-- Represent delta as a generalized variable-length integer
q = delta
k = base
while true
t = k <= bias and tMin or
k >= bias + tMax and tMax or k - bias
if q < t then break
d = t + (q - t) % (base - t)
out += 1
output[out] = char(d + 22 + (d < 26 and 75 or 0))
q = floor((q - t) / (base - t))
k += base
out += 1
output[out] = char(q + 22 + (q < 26 and 75 or 0))
k = 0
delta = h == b and floor(delta / damp) or rshift(delta, 1)
delta += floor(delta / (h + 1))
while delta > ((base - tMin) * tMax) / 2
delta = floor(delta / (base - tMin))
k += base
bias = floor(k + (base - tMin + 1) * delta / (delta + skew))
delta = 0
h += 1
delta += 1
n += 1
return concat(output, "", 1, out)
parseIPv4InIPv6 = (str, pointer, endPos, address, pieceIndex) ->
numbersSeen = 0
while pointer <= endPos
ipv4Piece = nil
ch = byte(str, pointer)
if numbersSeen > 0
unless ch == 0x2E --[['.']] and numbersSeen < 4
error "Invalid URL: IPv4 in IPv6 invalid code point"
pointer += 1
ch = pointer <= endPos and byte(str, pointer)
while ch and isDigit(ch)
num = charToDec(ch)
unless ipv4Piece
ipv4Piece = num
elseif ipv4Piece == 0
error "Invalid URL: IPv4 in IPv6 invalid code point"
else
ipv4Piece = ipv4Piece * 10 + num
if ipv4Piece > 255
error "Invalid URL: IPv4 in IPv6 out of range part"
pointer += 1
ch = pointer <= endPos and byte(str, pointer)
unless ipv4Piece
error "Invalid URL: IPv4 in IPv6 invalid code point"
address[pieceIndex] = address[pieceIndex] * 0x100 + ipv4Piece
numbersSeen += 1
if numbersSeen == 2 or numbersSeen == 4
pieceIndex += 1
if numbersSeen != 4
error "Invalid URL: IPv4 in IPv6 too few parts"
return pieceIndex
parseIPv6 = (str, startPos, endPos) ->
address = [0, 0, 0, 0, 0, 0, 0, 0] -- ipv6 address
pointer = startPos
pieceIndex = 1
compress = nil
if byte(str, startPos) == 0x3A -- ':'
if startPos == endPos --[[ EOF ]] or byte(str, startPos + 1) != 0x3A -- ':'
error "Invalid URL: IPv6 invalid compression"
pointer += 2
pieceIndex = compress = 2
while pointer <= endPos
if pieceIndex == 9
error "Invalid URL: IPv6 too many pieces"
ch = byte(str, pointer)
if ch == 0x3A -- ':'
if compress
error "Invalid URL: IPv6 multiple compression"
pointer += 1
pieceIndex = compress = pieceIndex + 1
continue
value = length = 0
while length < 4 and ch and isHexDigit(ch)
value = value * 0x10 + hexToDec(ch)
pointer += 1
length += 1
ch = pointer <= endPos and byte(str, pointer)
if ch == 0x2E -- '.'
if length == 0
error "Invalud URL: IPv4 in IPv6 invalid code point"
pointer -= length
if pieceIndex > 7
error "Invalid URL: IPv4 in IPv6 too many pieces"
pieceIndex = parseIPv4InIPv6(str, pointer, endPos, address, pieceIndex)
break
elseif ch == 0x3A -- ':'
pointer += 1
if pointer > endPos -- EOF
error "Invalid URL: IPv6 invalid code point"
elseif pointer <= endPos
error "Invalid URL: IPv6 invalid code point"
address[pieceIndex] = value
pieceIndex += 1
if compress
swaps = pieceIndex - compress
pieceIndex = 8
while pieceIndex != 1 and swaps > 0
value = address[pieceIndex]
address[pieceIndex] = address[compress + swaps - 1]
address[compress + swaps - 1] = value
swaps -= 1
pieceIndex -= 1
elseif pieceIndex != 9
error "Invalid URL: IPv6 too few pieces"
return address
parseIPv4Number = (str) ->
if str == ""
return
radix = 10
ch1 = byte(str, 1)
ch2 = byte(str, 2)
if ch1 == 0x30 --[['0']] and (ch2 == 0x78 --[['x']] or ch2 == 0x58 --[['X']])
radix = 16
if #str == 2 then str = "0"
elseif ch1 == 0x30 --[['0']] and ch2
radix = 8
return tonumber(str, radix)
endsInANumberChecker = (str, startPos, endPos) ->
-- find a starting point for number
numStart = startPos
numEnd = endPos
for i = numEnd, numStart, -1
if byte(str, i) == 0x2E --[['.']]
if i == endPos -- if dot is at the end, then skip it
numEnd = i - 1
else -- dot was found, so after it must be a number
numStart = i + 1
break
-- sanity check, do not invoke parser if we have ONLY digits
for i = numStart, numEnd
if not isDigit byte(str, i)
-- welp, let us try at least parse it, what if it is a hex number
return parseIPv4Number sub(str, numStart, numEnd)
return numStart <= numEnd -- every charactar was a digit, yay!
parseIPv4 = (str, startPos, endPos) ->
numbers = []
pointer = startPos
while true
ch = pointer <= endPos and byte(str, pointer)
if not ch or ch == 0x2E -- '.'
num = parseIPv4Number sub(str, startPos, pointer - 1)
if not num
if pointer > endPos and #numbers > 0
break
error "Invalid URL: IPv4 non numeric part"
startPos = pointer + 1
numbers[] = num
if not ch
break
pointer += 1
if #numbers > 4
error "Invalid URL: IPv4 too many parts"
for i = 1, #numbers - 1
if numbers[i] > 255
error "Invalid URL: IPv4 out of range part"
if numbers[#numbers] >= 256 ^ (5 - #numbers)
error "Invalid URL: IPv4 out of range part"
ipv4 = numbers[#numbers]
counter = 0
for i = 1, #numbers - 1
ipv4 += numbers[i] * 256 ^ (3 - counter)
counter += 1
return ipv4
domainToASCII = (domain) ->
for i = 1, #domain
if byte(domain, i) > 0x7F
-- We are dealing with some complicated unicode domain name
-- Since I am lazy newbie who does not want to implement proper unicode
-- handling into lua, I'll just cover edge cases for tests
-- You can open issue on Github if it REALLY BOTHERS YOU
-- But if it REALLY BOTHERS YOU then feel free to make proper unicode support
-- yourself :)
-- Remove special symbols that are ignored
-- I probably really should implement some proper punycode
domain = gsub(domain, "\xC2\xAD", "") -- remove soft hyphen
domain = gsub(domain, "\xE3\x80\x82", ".") -- Ideographic full stop
-- remove space characters
domain = gsub(domain, "\xE2\x80\x8B", "")
domain = gsub(domain, "\xE2\x81\xA0", "")
domain = gsub(domain, "\xEF\xBB\xBF", "")
break
containsNonASCII = doLowerCase = false
punycodePrefix = 0
partStart = pointer = 1
parts = []
while true
ch = byte(domain, pointer)
if not ch or ch == 0x2E -- '.'
-- decode an find errors
if punycodePrefix == 4 and containsNonASCII
error "Invalid URL: Domain invalid code point"
domainPart = containsNonASCII and "xn--" .. punycodeEncode(domain, partStart, pointer - 1) or sub(domain, partStart, pointer - 1)
-- btw, punycode decode lowercases the domain, so we need to lowercase it
-- in ideal sutiation I should have written punycodeDecode, but I am not in the mood to write it
if doLowerCase
domainPart = lower(domainPart)
parts[] = domainPart
partStart = pointer + 1
containsNonASCII = doLowerCase = false
punycodePrefix = 0
if not ch
break
elseif ch > 0x7F
containsNonASCII = true
elseif PUNYCODE_PREFIX[pointer - partStart + 1] == ch
punycodePrefix += 1
elseif isUpper(ch)
doLowerCase = true
pointer += 1
return concat(parts, ".")
parseHostString = (str, startPos, endPos, isSpecial) ->
if byte(str, startPos) == 0x5B -- '['
if byte(str, endPos) != 0x5D -- ']'
error "Invalid URL: IPv6 unclosed"
return parseIPv6(str, startPos + 1, endPos - 1)
elseif not isSpecial
-- opaque host parsing
if containsCharacter(str, FORBIDDEN_HOST_CODE_POINTS, startPos, endPos)
error "Invalid URL: Host invalid code point"
return percentEncode(sub(str, startPos, endPos), C0_ENCODE_SET)
else
domain = percentDecode(sub(str, startPos, endPos), DECODE_LOOKUP_TABLE)
asciiDomain = domainToASCII(domain)
if containsCharacter(asciiDomain, FORBIDDEN_DOMAIN_CODE_POINTS)
error "Invalid URL: Domain invalid code point"
if endsInANumberChecker(asciiDomain, 1, #asciiDomain)
return parseIPv4(asciiDomain, 1, #asciiDomain)
return asciiDomain
-- Predefine locals so we can make functions in the order I want (as if you were reading specs)
local parseScheme, parseNoScheme, parseSpecialRelativeOrAuthority, parsePathOrAuthority
local parseRelative, parseRelativeSlash, parseSpecialAuthorityIgnoreSlashes, parseAuthority
local parseHost, parsePort, parseFile, parseFileSlash, parseFileHost, parsePathStart, parsePath, parseOpaquePath
local parseQuery, parseFragment
parseScheme = (str, startPos, endPos, base, stateOverride) =>
-- scheme start state
if startPos <= endPos and isAlpha byte(str, startPos)
-- scheme state
doLowerCase = false
scheme = nil
for i = startPos, endPos
ch = byte(str, i)
if ch == 0x3A -- ':'
scheme = sub(str, startPos, i - 1)
if doLowerCase
scheme = lower(scheme)
isSpecial = SPECIAL_SCHEMAS[scheme]
if stateOverride
isUrlSpecial = @scheme and SPECIAL_SCHEMAS[@scheme]
if isUrlSpecial and not isSpecial then return
if not isUrlSpecial and isSpecial then return
if @username or @password or @port and isSpecial == true then return
if isUrlSpecial == true and @hostname != "" then return
@scheme = scheme
if stateOverride
if @port == isSpecial
@port = nil
elseif isSpecial == true -- scheme is file:
-- file state
parseFile(@, str, i + 1, endPos, base)
elseif isSpecial and base and base.scheme == scheme
-- special relative or authority state
parseSpecialRelativeOrAuthority(@, str, i + 1, endPos, base, isSpecial)
elseif isSpecial
-- special authority slashes state
parseSpecialAuthorityIgnoreSlashes(@, str, i + 1, endPos, base, isSpecial) -- anyway state will ignore slashes
elseif byte(str, i + 1) == 0x2F --[['/']]
-- path or authority state
parsePathOrAuthority(@, str, i + 2, endPos, base)
else
-- opaque path state
parseOpaquePath(@, str, i + 1, endPos)
return
elseif isUpper(ch)
doLowerCase = true
elseif not isLower(ch) and not isDigit(ch) and ch != 0x2B --[['+']] and ch != 0x2D --[['-']] and ch != 0x2E --[['.']]
-- scheme have an invalid character, so it's not a scheme
break
if not stateOverride
-- no scheme state
parseNoScheme(@, str, startPos, endPos, base)
parseNoScheme = (str, startPos, endPos, base) =>
startsWithFragment = byte(str, startPos) == 0x23 --[['#']]
baseHasOpaquePath = base and isstring(base.path)
if not base or (baseHasOpaquePath and not startsWithFragment)
error "Invalid URL: Missing scheme"
if baseHasOpaquePath and startsWithFragment
@scheme = base.scheme
@path = base.path
@query = base.query
parseFragment(@, str, startPos + 1, endPos)
elseif base.scheme != "file"
parseRelative(@, str, startPos, endPos, base, SPECIAL_SCHEMAS[base.scheme])
else
@scheme = "file"
parseFile(@, str, startPos, endPos, base)
parseSpecialRelativeOrAuthority = (str, startPos, endPos, base, isSpecial) =>
if byte(str, startPos) == 0x2F --[['/']] and byte(str, startPos + 1) == 0x2F --[['/']]
-- special authority slashes state
parseSpecialAuthorityIgnoreSlashes(@, str, startPos + 2, endPos, base, isSpecial)
else
-- relative state
@scheme = base.scheme
parseRelative(@, str, startPos, endPos, base, isSpecial)
parsePathOrAuthority = (str, startPos, endPos, base) =>
if byte(str, startPos) == 0x2F --[['/']]
parseAuthority(@, str, startPos + 1, endPos)
else
parsePath(@, str, startPos, endPos)
parseRelative = (str, startPos, endPos, base, isSpecial) =>
@scheme = base.scheme
ch = startPos <= endPos and byte(str, startPos)
if ch == 0x2F --[['/']] or (isSpecial and ch == 0x5C --[['\']])
-- relative slash state
parseRelativeSlash(@, str, startPos + 1, endPos, base, isSpecial)
else
@username = base.username
@password = base.password
@hostname = base.hostname
@port = base.port
path = @path = {...(base.path or {})} -- clone path
if ch == 0x3F --[['?']]
parseQuery(@, str, startPos + 1, endPos)
elseif ch == 0x23 --[['#']]
@query = base.query
parseFragment(@, str, startPos + 1, endPos)
elseif ch -- not EOF
pathLen = #path
if pathLen != 1 or not isWindowsDriveLetter(path[1])
path[pathLen] = nil -- removing last path segment
parsePath(@, str, startPos, endPos, isSpecial, path)
parseRelativeSlash = (str, startPos, endPos, base, isSpecial) =>
ch = byte(str, startPos)
if isSpecial and (ch == 0x2F --[['/']] or ch == 0x5C --[['\']])
-- special authority ignore slashes state
parseSpecialAuthorityIgnoreSlashes(@, str, startPos + 1, endPos, base, isSpecial)
elseif ch == 0x2F --[['/']]
-- authority state
parseAuthority(@, str, startPos + 1, endPos, isSpecial)
else
@username = base.username
@password = base.password
@hostname = base.hostname
@port = base.port
parsePath(@, str, startPos, endPos, isSpecial)
parseSpecialAuthorityIgnoreSlashes = (str, startPos, endPos, base, isSpecial) =>
for i = startPos, endPos
ch = byte(str, i)
if ch != 0x2F --[['/']] and ch != 0x5C --[['\']]
parseAuthority(@, str, i, endPos, isSpecial)
break
parseAuthority = (str, startPos, endPos, isSpecial) =>
-- authority state
atSignSeen = false
passwordTokenSeen = false
pathEndPos = endPos
for i = startPos, endPos
ch = byte(str, i)
if ch == 0x2F --[['/']] or ch == 0x3F --[['?']] or ch == 0x23 --[['#']] or (isSpecial and ch == 0x5C --[['\']])
endPos = i - 1
break
elseif ch == 0x40 -- '@'
atSignSeen = i
elseif ch == 0x3A --[[':']] and not passwordTokenSeen and not atSignSeen
passwordTokenSeen = i
-- After @ there is no hostname
if atSignSeen == endPos
error "Invalid URL: Missing host"
if atSignSeen
if passwordTokenSeen
@username = percentEncode(sub(str, startPos, passwordTokenSeen - 1), USERINFO_ENCODE_SET)
@password = percentEncode(sub(str, passwordTokenSeen + 1, atSignSeen - 1), USERINFO_ENCODE_SET)
else
@username = percentEncode(sub(str, startPos, atSignSeen - 1), USERINFO_ENCODE_SET)
parseHost(@, str, atSignSeen and atSignSeen + 1 or startPos, endPos, isSpecial)
parsePathStart(@, str, endPos + 1, pathEndPos, isSpecial)
parseHost = (str, startPos, endPos, isSpecial, stateOverride) =>
if stateOverride and isSpecial == true
return parseFileHost(@, str, startPos, endPos, stateOverride)
insideBrackets = false
for i = startPos, endPos
ch = byte(str, i)
if ch == 0x3A --[[':']] and not insideBrackets
if i == startPos
error "Invalid URL: Missing host"
if stateOverride == "hostname"
return
parsePort(@, str, i + 1, endPos, isSpecial, stateOverride)
endPos = i - 1
break
elseif ch == 0x5B -- '['
insideBrackets = true
elseif ch == 0x5D -- ']'
insideBrackets = false
if isSpecial and startPos > endPos
error "Invalid URL: Missing host"
elseif stateOverride and startPos == endPos and (@username or @password or @port)
return
@hostname = parseHostString(str, startPos, endPos, isSpecial)
parsePort = (str, startPos, endPos, defaultPort, stateOverride) =>
if startPos > endPos
return
port = tonumber sub(str, startPos, endPos)
if not port or (port > 2 ^ 16 - 1) or port < 0
if stateOverride then
return
error "Invalid URL: Invalid port"
if port != defaultPort
@port = port
parseFile = (str, startPos, endPos, base) =>
@scheme = "file"
@hostname = ""
ch = startPos <= endPos and byte(str, startPos)
if ch == 0x2F --[['/']] or ch == 0x5C --[['\']]
parseFileSlash(@, str, startPos + 1, endPos, base)
elseif base and base.scheme == "file"
@hostname = base.hostname
path = @path = {...(base.path or {})}
if ch == 0x3F --[['?']]
parseQuery(@, str, startPos + 1, endPos)
elseif ch == 0x23 --[['#']]
@query = base.query
parseFragment(@, str, startPos + 1, endPos)
elseif ch -- not EOF
pathLen = #path
if not startsWithWindowsDriveLetter(str, startPos, endPos)
if pathLen != 1 or not isWindowsDriveLetter(path[1])
path[pathLen] = nil -- removing last path segment
else
path = nil
parsePath(@, str, startPos, endPos, true, path)
else
parsePath(@, str, startPos, endPos, true)
parseFileSlash = (str, startPos, endPos, base) =>
ch = byte(str, startPos)
if ch == 0x2F --[['/']] or ch == 0x5C --[['\']]
parseFileHost(@, str, startPos + 1, endPos)
else
path = {}
if base and base.scheme == "file"
@hostname = base.hostname
if not startsWithWindowsDriveLetter(str, startPos, endPos) and isWindowsDriveLetter(base.path[1], false)
path[1] = base.path[1]
parsePath(@, str, startPos, endPos, true, path)
parseFileHost = (str, startPos, endPos, stateOverride) =>
i = startPos
while true
ch = i <= endPos and byte(str, i)
if ch == 0x2F --[['/']] or ch == 0x5C --[['\']] or ch == 0x3F --[['?']] or ch == 0x23 --[['#']] or not ch -- EOF
hostLen = i - startPos
if not stateOverride and hostLen == 2 and isWindowsDriveLetterCodePoints(byte(str, startPos), byte(str, startPos + 1), false)
parsePath(@, str, startPos, endPos, true)
elseif hostLen == 0
@hostname = ""
if stateOverride
return
parsePathStart(@, str, i, endPos, true)
else
hostname = parseHostString(str, startPos, i - 1, true)
if hostname == "localhost"
hostname = ""
@hostname = hostname
if stateOverride
return
parsePathStart(@, str, i, endPos, true)
break
i += 1
parsePathStart = (str, startPos, endPos, isSpecial, stateOverride) =>
ch = startPos <= endPos and byte(str, startPos)
if isSpecial
if ch == 0x2F --[['/']] or ch == 0x5C --[['\']]
startPos += 1
parsePath(@, str, startPos, endPos, isSpecial, nil, stateOverride)
elseif not stateOverride and ch == 0x3F --[['?']]
parseQuery(@, str, startPos + 1, endPos)
elseif not stateOverride and ch == 0x23 --[['#']]
parseFragment(@, str, startPos + 1, endPos)
elseif ch -- not EOF
if ch == 0x2F --[['/']]
startPos += 1
parsePath(@, str, startPos, endPos, isSpecial, nil, stateOverride)
elseif stateOverride and not @hostname
@path[] = "" -- append empty string to path
parsePath = (str, startPos, endPos, isSpecial, segments={}, stateOverride) =>
segmentsCount = #segments
hasWindowsLetter = segmentsCount != 0 and isWindowsDriveLetter(segments[1], false)
segmentStart = startPos
i = startPos
while true
ch = i <= endPos and byte(str, i)
if ch == 0x2F --[['/']] or (isSpecial and ch == 0x5C --[['\']]) or (not stateOverride and (ch == 0x3F --[['?']] or ch == 0x23 --[['#']])) or not ch -- EOF
segment = percentEncode(sub(str, segmentStart, i - 1), PATH_ENCODE_SET)
segmentStart = i + 1
if isDoubleDot(segment)
if segmentsCount != 1 or not hasWindowsLetter
segments[segmentsCount] = nil
segmentsCount -= 1
if segmentsCount == -1 then segmentsCount = 0 -- do not allow underflow
if ch != 0x2F --[['/']] and (isSpecial and ch != 0x5C --[['\']])
segmentsCount += 1
segments[segmentsCount] = ""
elseif not isSingleDot(segment)
if isSpecial == true --[[is file scheme]] and segmentsCount == 0 and isWindowsDriveLetter(segment, false)
segment = gsub(segment, "|", ":")
hasWindowsLetter = true
segmentsCount += 1
segments[segmentsCount] = segment
elseif ch != 0x2F --[['/']] and (isSpecial and ch != 0x5C --[['\']])
segmentsCount += 1
segments[segmentsCount] = ""
if ch == 0x3F --[['?']]
parseQuery(@, str, i + 1, endPos)
break
elseif ch == 0x23 --[['#']]
parseFragment(@, str, i + 1, endPos)
break
elseif not ch
break
i += 1
@path = segments
parseOpaquePath = (str, startPos, endPos) =>
for i = startPos, endPos
ch = byte(str, i)
if ch == 0x3F --[['?']]
parseQuery(@, str, i + 1, endPos)
endPos = i - 1
break
elseif ch == 0x23 --[['#']]
parseFragment(@, str, i + 1, endPos)
endPos = i - 1
break
@path = percentEncode(sub(str, startPos, endPos), C0_ENCODE_SET)
parseQuery = (str, startPos, endPos, isSpecial, stateOverride) =>
for i = startPos, endPos
if not stateOverride and byte(str, i) == 0x23 --[['#']]
parseFragment(@, str, i + 1, endPos)
endPos = i - 1
break
encodeSet = isSpecial and SPECIAL_QUERY_ENCODE_SET or QUERY_ENCODE_SET
@query = percentEncode(sub(str, startPos, endPos), encodeSet)
-- This methods parses given query string to a list of name-value tuple
parseQueryString = (str, output=[]) ->
pointer = startPos = 1
name = value = nil
containsPlus = false
count = 0
while true
ch = byte(str, pointer)
if ch == 0x26 --[['&']] or not ch -- EOF
value = sub(str, startPos, pointer - 1)
if containsPlus
value = gsub(name, "%+", " ")
containsPlus = false
unless name
name = value
value = nil
if name != "" or value
name = percentDecode(name, DECODE_LOOKUP_TABLE)
value = value and percentDecode(value, DECODE_LOOKUP_TABLE) or nil
count += 1
output[count] = [name, value]
name = value = nil
startPos = pointer + 1
if not ch -- EOF
break
elseif ch == 0x3D -- '='
name = sub(str, startPos, pointer - 1)
startPos = pointer + 1
if containsPlus
name = gsub(name, "%+", " ")
containsPlus = false
elseif ch == 0x2B --[['+']]
containsPlus = true
pointer += 1
return output
parseFragment = (str, startPos, endPos) =>
@fragment = percentEncode(sub(str, startPos, endPos), FRAGMENT_ENCODE_SET)
export parse = (str, base) =>
unless isstring str
error "Invalid URL: URL must be a string"
if isstring base
-- yeah, we dont even need to full URL object for this
url = {}
parse(url, base)
base = url
str = gsub(str, "[\t\n\r]", "") -- remove all tab and newline characters
startPos = 1
endPos = #str
-- Trim leading and trailing whitespaces
startPos = trimInput(str, startPos, endPos)
endPos = trimInput(str, endPos, startPos)
parseScheme(@, str, startPos, endPos, base)
return @
serializeIPv6 = (address) ->
output = []
len = compress = compressLen = zeroStart = 0
-- Find first longest sequence of zeros
for i = 1, 8
if address[i] == 0
if zeroStart == 0
zeroStart = i
elseif i - zeroStart > compressLen
compress = zeroStart
compressLen = i - zeroStart
else
zeroStart = 0
ignore0 = false
for i = 1, 8