mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
parent
82191da2a2
commit
a288c21a5d
12 changed files with 45 additions and 24 deletions
|
@ -29,8 +29,8 @@
|
|||
# module URI
|
||||
# class RSYNC < Generic
|
||||
# DEFAULT_PORT = 873
|
||||
# URI.refresh_scheme_list
|
||||
# end
|
||||
# @@schemes['RSYNC'] = RSYNC
|
||||
# end
|
||||
# #=> URI::RSYNC
|
||||
#
|
||||
|
|
|
@ -16,6 +16,7 @@ module URI
|
|||
REGEXP = RFC2396_REGEXP
|
||||
Parser = RFC2396_Parser
|
||||
RFC3986_PARSER = RFC3986_Parser.new
|
||||
Ractor.make_shareable(RFC3986_PARSER) if defined?(Ractor)
|
||||
|
||||
# URI::Parser.new
|
||||
DEFAULT_PARSER = Parser.new
|
||||
|
@ -27,6 +28,7 @@ module URI
|
|||
DEFAULT_PARSER.regexp.each_pair do |sym, str|
|
||||
const_set(sym, str)
|
||||
end
|
||||
Ractor.make_shareable(DEFAULT_PARSER) if defined?(Ractor)
|
||||
|
||||
module Util # :nodoc:
|
||||
def make_components_hash(klass, array_hash)
|
||||
|
@ -62,10 +64,30 @@ module URI
|
|||
|
||||
include REGEXP
|
||||
|
||||
@@schemes = {}
|
||||
SCHEME_LIST_MUTEX = Mutex.new
|
||||
private_constant :SCHEME_LIST_MUTEX
|
||||
|
||||
# Returns a Hash of the defined schemes.
|
||||
# The list is lazily calculated.
|
||||
def self.scheme_list
|
||||
@@schemes
|
||||
return const_get(:SCHEMES) if defined?(SCHEMES)
|
||||
|
||||
SCHEME_LIST_MUTEX.synchronize do
|
||||
const_set(:SCHEMES, ObjectSpace.
|
||||
each_object(Class).
|
||||
select { |klass| klass < URI::Generic }.
|
||||
each_with_object({}) { |klass, acc| acc[klass.name.split('::').last.upcase] = klass }.
|
||||
freeze)
|
||||
end
|
||||
end
|
||||
|
||||
# Re-calculate scheme list
|
||||
def self.refresh_scheme_list
|
||||
SCHEME_LIST_MUTEX.synchronize do
|
||||
remove_const(:SCHEMES) if defined?(SCHEMES)
|
||||
end
|
||||
|
||||
scheme_list
|
||||
end
|
||||
|
||||
#
|
||||
|
@ -73,11 +95,7 @@ module URI
|
|||
# from +URI.scheme_list+.
|
||||
#
|
||||
def self.for(scheme, *arguments, default: Generic)
|
||||
if scheme
|
||||
uri_class = @@schemes[scheme.upcase] || default
|
||||
else
|
||||
uri_class = default
|
||||
end
|
||||
uri_class = scheme_list[scheme.to_s.upcase] || default
|
||||
|
||||
return uri_class.new(scheme, *arguments)
|
||||
end
|
||||
|
|
|
@ -89,6 +89,4 @@ module URI
|
|||
def set_password(v)
|
||||
end
|
||||
end
|
||||
|
||||
@@schemes['FILE'] = File
|
||||
end
|
||||
|
|
|
@ -262,5 +262,4 @@ module URI
|
|||
return str
|
||||
end
|
||||
end
|
||||
@@schemes['FTP'] = FTP
|
||||
end
|
||||
|
|
|
@ -81,7 +81,4 @@ module URI
|
|||
url.start_with?(?/.freeze) ? url : ?/ + url
|
||||
end
|
||||
end
|
||||
|
||||
@@schemes['HTTP'] = HTTP
|
||||
|
||||
end
|
||||
|
|
|
@ -18,5 +18,4 @@ module URI
|
|||
# A Default port of 443 for URI::HTTPS
|
||||
DEFAULT_PORT = 443
|
||||
end
|
||||
@@schemes['HTTPS'] = HTTPS
|
||||
end
|
||||
|
|
|
@ -256,6 +256,4 @@ module URI
|
|||
false
|
||||
end
|
||||
end
|
||||
|
||||
@@schemes['LDAP'] = LDAP
|
||||
end
|
||||
|
|
|
@ -17,5 +17,4 @@ module URI
|
|||
# A Default port of 636 for URI::LDAPS
|
||||
DEFAULT_PORT = 636
|
||||
end
|
||||
@@schemes['LDAPS'] = LDAPS
|
||||
end
|
||||
|
|
|
@ -288,6 +288,4 @@ module URI
|
|||
end
|
||||
alias to_rfc822text to_mailtext
|
||||
end
|
||||
|
||||
@@schemes['MAILTO'] = MailTo
|
||||
end
|
||||
|
|
|
@ -78,7 +78,4 @@ module URI
|
|||
url.start_with?(?/.freeze) ? url : ?/ + url
|
||||
end
|
||||
end
|
||||
|
||||
@@schemes['WS'] = WS
|
||||
|
||||
end
|
||||
|
|
|
@ -18,5 +18,4 @@ module URI
|
|||
# A Default port of 443 for URI::WSS
|
||||
DEFAULT_PORT = 443
|
||||
end
|
||||
@@schemes['WSS'] = WSS
|
||||
end
|
||||
|
|
|
@ -33,6 +33,25 @@ class TestCommon < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_ractor
|
||||
return unless defined?(Ractor)
|
||||
r = Ractor.new { URI.parse("https://ruby-lang.org/").inspect }
|
||||
assert_equal(URI.parse("https://ruby-lang.org/").inspect, r.take)
|
||||
end
|
||||
|
||||
def test_register_scheme
|
||||
assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS"].sort, URI.scheme_list.keys.sort)
|
||||
|
||||
begin
|
||||
URI::Generic.const_set :FOOBAR, Class.new(URI::Generic)
|
||||
URI.refresh_scheme_list
|
||||
assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "FOOBAR"].sort, URI.scheme_list.keys.sort)
|
||||
ensure
|
||||
URI::Generic.send(:remove_const, :FOOBAR)
|
||||
URI.refresh_scheme_list
|
||||
end
|
||||
end
|
||||
|
||||
def test_regexp
|
||||
EnvUtil.suppress_warning do
|
||||
assert_instance_of Regexp, URI.regexp
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue