-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathurl.rb
511 lines (428 loc) · 12.4 KB
/
url.rb
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
class Url
# https://url.spec.whatwg.org/#percent-encoded-bytes
BASE_ENCODE_SET = '([\uD800-\uDBFF][\uDC00-\uDFFF])|[^\u0020-\u007E]'
SIMPLE_ENCODE_SET = RegExp.new(BASE_ENCODE_SET, 'g')
PASSWORD_ENCODE_SET = RegExp.new(BASE_ENCODE_SET + "|[\\@/]", 'g')
USERNAME_ENCODE_SET = RegExp.new(BASE_ENCODE_SET + "|[\\@/:]", 'g')
DEFAULT_ENCODE_SET = RegExp.new(BASE_ENCODE_SET + '|[\u0020"#<>?]', 'g')
QUERY_ENCODE_SET = RegExp.new(BASE_ENCODE_SET + '|[\x20\x23\x3C\x3E\x60]', 'g')
# https://url.spec.whatwg.org/#relative-scheme
DEFAULT_PORT = {
"ftp" => "21",
"file" => nil,
"gopher" => "70",
"http" => "80",
"https" => "443",
"ws" => "80",
"wss" => "443"
}
RELATIVE_SCHEME = DEFAULT_PORT.keys()
# https://url.spec.whatwg.org/#url-code-points
URL_CODE_POINTS = /[a-zA-Z0-9
!$&'()*+,\-.\/:;=?@_~
\u00A0-\uD7FF \uE000-\uFDCF \uFDF0-\uFFFD
\u{10000}-\u{1FFFD} \u{20000}-\u{2FFFD} \u{30000}-\u{3FFFD}
\u{40000}-\u{4FFFD} \u{50000}-\u{5FFFD} \u{60000}-\u{6FFFD}
\u{70000}-\u{7FFFD} \u{80000}-\u{8FFFD} \u{90000}-\u{9FFFD}
\u{A0000}-\u{AFFFD} \u{B0000}-\u{BFFFD} \u{C0000}-\u{CFFFD}
\u{D0000}-\u{DFFFD} \u{E0000}-\u{EFFFD} \u{F0000}-\u{FFFFD}
\u{100000}-\u{10FFFD}
]/x
def self.utf8_percent_encode(codepoint)
# TODO: compare against WHATWG spec
enc = nil
c1 = codepoint.charCodeAt(0)
if c1 < 128
enc = [c1]
elsif c1 > 127 && c1 < 2048
enc = [
(c1 >> 6) | 192, (c1 & 63) | 128
]
elsif (c1 & 0xF800) != 0xD800
enc = [
(c1 >> 12) | 224, ((c1 >> 6) & 63) | 128, (c1 & 63) | 128
]
else
# surrogate pairs
c2 = codepoint.charCodeAt(1)
if (c1 & 0xFC00) != 0xD800 or (c2 & 0xFC00) != 0xDC00
# unmatched surrogate pair
enc = [
(c1 >> 12) | 224, ((c1 >> 6) & 63) | 128, (c1 & 63) | 128
]
else
c1 = ((c1 & 0x3FF) << 10) + (c2 & 0x3FF) + 0x10000
enc = [
(c1 >> 18) | 240, ((c1 >> 12) & 63) | 128,
((c1 >> 6) & 63) | 128, (c1 & 63) | 128
]
end
end
result = ""
if enc != nil
for i in 0...enc.length
result += "%" + (Math.floor(enc[i]/16)).toString(16) +
(enc[i]%16).toString(16)
end
end
return result.toUpperCase()
end
# https://url.spec.whatwg.org/#percent-decode
def self.percent_decode(input)
warn = null;
if input =~ /%($|[^0-9a-fA-F]|.$|.[^0-9a-fA-F])/
warn = 'Percent sign ("%") not followed by two hexadecimal digits'
end
# collapsed steps 1..3
result = input.gsub(/%[0-9a-fA-F]{2}/) {|c| c[1..-1].to_i(16).chr}
if warn
result = String.new(result)
result.exception = warn
end
return result
end
def self.utf8_percent_decode(input)
# TODO compare against WHATWG encoding
return input.gsub(/(%[0-9a-fA-F]{2})+/) do |chars|
bytes = []
(0...chars.length).step(3) do |i|
bytes << chars[i+1..i+2].to_i(16)
end
chars = ''
while bytes.length>0
if bytes[0] < 0x80
chars += String.fromCharCode(bytes.shift())
elsif bytes[0] < 0xC2
chars += '%' + bytes.shift().to_s(16).upcase() # error
elsif bytes[0] < 0xE0
if bytes.length == 1
chars += '%' + bytes.shift().to_s(16).upcase() # error
elsif (bytes[1] & 0xC0) != 0x80
chars += '%' + bytes.shift().to_s(16).upcase() # error
else
chars += String.fromCharCode((bytes.shift() << 6) +
bytes.shift() - 0x3080)
end
elsif bytes[0] < 0xF0
if bytes.length <= 2
chars += '%' + bytes.shift().to_s(16).upcase() # error
elsif (bytes[1] & 0xC0) != 0x80
chars += '%' + bytes.shift().to_s(16).upcase() # error
elsif (bytes[1] & 0xC0) != 0x80
chars += '%' + bytes.shift().to_s(16).upcase() # error
elsif bytes[0] == 0xE0 && bytes[1] < 0xA0
chars += '%' + bytes.shift().to_s(16).upcase() # error
elsif (bytes[2] & 0xC0) != 0x80
chars += '%' + bytes.shift().to_s(16).upcase() # error
else
chars += String.fromCharCode((bytes.shift() << 12) +
(bytes.shift() << 6) + bytes.shift() - 0xE2080)
end
elsif bytes[0] < 0xF5
if bytes.length <= 3
chars += '%' + bytes.shift().to_s(16).upcase() # error
elsif (bytes[1] & 0xC0) != 0x80
chars += '%' + bytes.shift().to_s(16).upcase() # error
elsif bytes[0] == 0xF0 && bytes[1] < 0x90
chars += '%' + bytes.shift().to_s(16).upcase() # error
elsif bytes[0] == 0xF4 && bytes[1] >= 0x90
chars += '%' + bytes.shift().to_s(16).upcase() # error
elsif (bytes[2] & 0xC0) != 0x80
chars += '%' + bytes.shift().to_s(16).upcase() # error
elsif (bytes[3] & 0xC0) != 0x80
chars += '%' + bytes.shift().to_s(16).upcase() # error
else
chars += String.fromCharCode((bytes.shift() << 18) +
(bytes.shift() << 12) + (bytes.shift() << 6) +
bytes.shift() - 0x3C82080)
end
else
chars += '%' + bytes.shift().to_s(16).upcase() # error
end
end
chars
end
end
# https://url.spec.whatwg.org/#utf-8-percent-encode
def self.percent_encode(string, encode_set)
return string.gsub encode_set do |code_point|
Url.utf8_percent_encode(code_point)
end
end
def self.path_concat(base, path)
base = (base ? base.slice(0) : [])
if path[0] == '.'
path.shift()
base.pop()
end
if path.length == 1 and path[0] == ''
path=base
elsif path.length > 1 and path[0] == ''
path.shift()
else
base.pop()
path = base.concat(path)
end
return path
end
# https://url.spec.whatwg.org/#host-parsing
def host_parser(input, unicode_flag=nil)
# 1
raise Failure, 'empty host' if input == ''
# 2
if input.start_with? '['
# 2.1
if not input.end_with? ']'
@parse_error = true
raise Failure, 'unmatched brackets in host'
end
# 2.2
return IPAddr.new(input[1...-1])
end
# 3
domain = percent_decode(input.encode(Encoding::UTF_8)).
encode(Encoding::UTF_8)
begin
# 4
uri = Addressable::URI.new
uri.host = domain
asciiDomain = uri.normalized_host
rescue => e
# 5
raise Failure, "invalid domain - #{e}"
end
# 6
if
asciiDomain.chars.any? do |c|
"\u0000\u0009\u000A\u0020#%/:?@[\\]".include? c
end
then
raise Failure, 'invalid domain - reserved characters'
end
# 7
if unicode_flag
return asciiDomain
else
return asciiDomain # TODO:
# https://url.spec.whatwg.org/#concept-domain-to-unicode
end
end
# https://url.spec.whatwg.org/#concept-host-serializer
def host_serializer(host)
# 1
return '' if host.nil?
# 2
# return "[#{host.to_s}]" if IPAddr === host
# 3
return host
end
# https://url.spec.whatwg.org/#concept-ipv6-serializer
# http://tools.ietf.org/html/rfc5952#section-4
def self.canonicalize_ipv6(pre, post=[], ipv4=nil)
slots = (ipv4 ? 6 : 8)
pre << '0' while pre.length + post.length < slots
pre = pre.concat post
pre.map! {|n| n.to_i(16).to_s(16).upcase()}
zero = nil
for i in 0 .. slots-2
if pre[i] == '0' and pre[i] == '0'
zero = i
break
end
end
if not zero
post = nil
else
post = pre[zero+1..-1]
pre = pre.first(zero)
post.shift() while post.length > 1 and post[0] == '0'
post = nil if ipv4 and post.length == 1 and post[0] == '0'
end
result = pre.join(':')
result += '::' + post.join(':') if post
result += '::' + ipv4 if ipv4
return result
end
attr_accessor :scheme, :scheme_data, :host
attr_accessor :path, :query, :fragment, :exception
def initialize(input, base)
begin
base = UrlParser.parse(base) if base
if input =~ /^[\u0009\u000A\u000C\u000D\u0020] |
[\u0009\u000A\u000C\u000D\u0020]$/x
then
this._exception = 'invalid leading/trailing whitespace characters'
input.sub! /^[\u0009\u000A\u000C\u000D\u0020]+/, ''
input.sub! /[\u0009\u000A\u000C\u000D\u0020]+$/, ''
end
url = UrlParser.parse(input, base: base)
@scheme = nil
@scheme_data = nil
@username = ''
@password = nil
@host = nil
@port = ''
@path = []
@query = nil
@fragment = nil
for property in url
this["_#{property}"] = url[property]
end
rescue => e
@href = input
@exception = e.message
end
end
# https://url.spec.whatwg.org/#url-serializing
def serializer(exclude_fragment=false)
# 1
output = "#@scheme:"
# 2
if not @scheme_data
# 2.1
output += '//'
# 2.2
if @username != '' or @password != nil
# 2.2.1
output += @username || ''
# 2.2.2
output += ":#@password" unless @password == nil
# 2.2.3
output += '@'
end
# 2.3
output += host_serializer(@host)
# 2.4
output += ":#@port" unless @port.empty?
# 2.5
output += "/" + @path.join('/')
# 3
else
output += @scheme_data
end
# 4
output += "?#@query" unless @query.nil?
# 5
output += "##@fragment" if not @fragment.nil? and not exclude_fragment
# 6
return output
end
# https://url.spec.whatwg.org/#dom-url-href
def href
@href || self.serializer()
end
def href=(value)
begin
oldhref = @href
oldexception = @exception
Url.apply(this, [value, null]);
ensure
# @href is only set when there is an error. If there previously was
# an error, restore it. If there is a new error, ignore it.
@href = oldhref
@exception = oldexception
end
end
# https://url.spec.whatwg.org/#dom-url-protocol
def protocol
@scheme ? "#@scheme:" : ':'
end
def protocol=(value)
begin
UrlParser.parse(value, url: this, startRule: 'setProtocol')
rescue => e
this.exception = e.message
end
end
# https://url.spec.whatwg.org/#dom-url-username
def username
@username ? @username : ''
end
def username=(value)
begin
UrlParser.parse(value, url: this, startRule: 'setUsername')
rescue => e
this.exception = e.message
end
end
# https://url.spec.whatwg.org/#dom-url-password
def password
@password ? @password : ''
end
def password=(value)
begin
UrlParser.parse(value, url: this, startRule: 'setPassword')
rescue => e
this.exception = e.message
end
end
# https://url.spec.whatwg.org/#dom-urlutils-host
def host
"#{self.hostname}:#{self.port}"
end
def host=(value)
begin
UrlParser.parse(value, url: this, startRule: 'setHost')
rescue => e
this.exception = e.message
end
end
# https://url.spec.whatwg.org/#dom-url-hostname
def hostname
self.host_serializer(@host)
end
def hostname=(value)
begin
UrlParser.parse(value, url: this, startRule: 'setHostname')
rescue => e
this.exception = e.message
end
end
# https://url.spec.whatwg.org/#dom-url-port
def port
@port ? @port : ''
end
def port=(value)
begin
UrlParser.parse(value.toString(), url: this, startRule: 'setPort')
rescue => e
this.exception = e.message
end
end
# https://url.spec.whatwg.org/#dom-url-pathname
def pathname
return '' unless @scheme
@scheme_data || ('/' + @path.join('/'))
end
def pathname=(value)
begin
UrlParser.parse(value.toString(), url: this, startRule: 'setPathname')
rescue => e
this.exception = e.message
end
end
# https://url.spec.whatwg.org/#dom-url-search
def search
return '' if @query.nil? or @query.empty?
return "?#@query"
end
def search=(value)
begin
UrlParser.parse(value.toString(), url: this, startRule: 'setSearch')
rescue => e
this.exception = e.message
end
end
# https://url.spec.whatwg.org/#dom-url-hash
def hash
return '' if @fragment.nil? or @fragment.empty?
return "##@fragment"
end
def hash=(value)
begin
UrlParser.parse(value, url: this, startRule: 'setHash')
rescue => e
this.exception = e.message
end
end
end