Skip to content

Commit

Permalink
Revert "Fix to support Ruby 3.0 Ractor"
Browse files Browse the repository at this point in the history
* This reverts commit 1faa4fd.
* It has too many problems, see #22 for discussion.
  • Loading branch information
eregon committed Jun 25, 2021
1 parent 5ea2356 commit b959da2
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 45 deletions.
2 changes: 1 addition & 1 deletion lib/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
# module URI
# class RSYNC < Generic
# DEFAULT_PORT = 873
# URI.refresh_scheme_list
# end
# @@schemes['RSYNC'] = RSYNC
# end
# #=> URI::RSYNC
#
Expand Down
32 changes: 7 additions & 25 deletions lib/uri/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ 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
Expand All @@ -28,7 +27,6 @@ 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)
Expand Down Expand Up @@ -64,38 +62,22 @@ def make_components_hash(klass, array_hash)

include REGEXP

SCHEME_LIST_MUTEX = Mutex.new
private_constant :SCHEME_LIST_MUTEX

@@schemes = {}
# Returns a Hash of the defined schemes.
# The list is lazily calculated.
def self.scheme_list
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
@@schemes
end

#
# Construct a URI instance, using the scheme to detect the appropriate class
# from +URI.scheme_list+.
#
def self.for(scheme, *arguments, default: Generic)
uri_class = scheme_list[scheme.to_s.upcase] || default
if scheme
uri_class = @@schemes[scheme.upcase] || default
else
uri_class = default
end

return uri_class.new(scheme, *arguments)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/uri/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,6 @@ def set_user(v)
def set_password(v)
end
end

@@schemes['FILE'] = File
end
1 change: 1 addition & 0 deletions lib/uri/ftp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,5 @@ def to_s
return str
end
end
@@schemes['FTP'] = FTP
end
3 changes: 3 additions & 0 deletions lib/uri/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,7 @@ def request_uri
url.start_with?(?/.freeze) ? url : ?/ + url
end
end

@@schemes['HTTP'] = HTTP

end
1 change: 1 addition & 0 deletions lib/uri/https.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ class HTTPS < HTTP
# A Default port of 443 for URI::HTTPS
DEFAULT_PORT = 443
end
@@schemes['HTTPS'] = HTTPS
end
2 changes: 2 additions & 0 deletions lib/uri/ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,6 @@ def hierarchical?
false
end
end

@@schemes['LDAP'] = LDAP
end
1 change: 1 addition & 0 deletions lib/uri/ldaps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ class LDAPS < LDAP
# A Default port of 636 for URI::LDAPS
DEFAULT_PORT = 636
end
@@schemes['LDAPS'] = LDAPS
end
2 changes: 2 additions & 0 deletions lib/uri/mailto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,6 @@ def to_mailtext
end
alias to_rfc822text to_mailtext
end

@@schemes['MAILTO'] = MailTo
end
3 changes: 3 additions & 0 deletions lib/uri/ws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,7 @@ def request_uri
url.start_with?(?/.freeze) ? url : ?/ + url
end
end

@@schemes['WS'] = WS

end
1 change: 1 addition & 0 deletions lib/uri/wss.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ class WSS < WS
# A Default port of 443 for URI::WSS
DEFAULT_PORT = 443
end
@@schemes['WSS'] = WSS
end
19 changes: 0 additions & 19 deletions test/uri/test_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,6 @@ def test_extract
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
Expand Down

0 comments on commit b959da2

Please sign in to comment.