From a288c21a5d46418e75c0f03eb12ff0782e51568d Mon Sep 17 00:00:00 2001 From: kvokka Date: Sat, 29 May 2021 11:29:39 +0100 Subject: [ruby/uri] Fix to support Ruby 3.0 Ractor https://github.com/ruby/uri/commit/1faa4fdc16 --- lib/uri/common.rb | 32 +++++++++++++++++++++++++------- lib/uri/file.rb | 2 -- lib/uri/ftp.rb | 1 - lib/uri/http.rb | 3 --- lib/uri/https.rb | 1 - lib/uri/ldap.rb | 2 -- lib/uri/ldaps.rb | 1 - lib/uri/mailto.rb | 2 -- lib/uri/ws.rb | 3 --- lib/uri/wss.rb | 1 - 10 files changed, 25 insertions(+), 23 deletions(-) (limited to 'lib/uri') diff --git a/lib/uri/common.rb b/lib/uri/common.rb index 915c0e9519..ab3234cb37 100644 --- a/lib/uri/common.rb +++ b/lib/uri/common.rb @@ -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 diff --git a/lib/uri/file.rb b/lib/uri/file.rb index 561ec703c4..45a7a8b666 100644 --- a/lib/uri/file.rb +++ b/lib/uri/file.rb @@ -89,6 +89,4 @@ module URI def set_password(v) end end - - @@schemes['FILE'] = File end diff --git a/lib/uri/ftp.rb b/lib/uri/ftp.rb index fb38481193..ec8b6e6fe7 100644 --- a/lib/uri/ftp.rb +++ b/lib/uri/ftp.rb @@ -262,5 +262,4 @@ module URI return str end end - @@schemes['FTP'] = FTP end diff --git a/lib/uri/http.rb b/lib/uri/http.rb index 70cfb2a1bf..ccd09ddd0c 100644 --- a/lib/uri/http.rb +++ b/lib/uri/http.rb @@ -81,7 +81,4 @@ module URI url.start_with?(?/.freeze) ? url : ?/ + url end end - - @@schemes['HTTP'] = HTTP - end diff --git a/lib/uri/https.rb b/lib/uri/https.rb index c481b1fe6d..98a08409f6 100644 --- a/lib/uri/https.rb +++ b/lib/uri/https.rb @@ -18,5 +18,4 @@ module URI # A Default port of 443 for URI::HTTPS DEFAULT_PORT = 443 end - @@schemes['HTTPS'] = HTTPS end diff --git a/lib/uri/ldap.rb b/lib/uri/ldap.rb index 14e6163292..3a0df23213 100644 --- a/lib/uri/ldap.rb +++ b/lib/uri/ldap.rb @@ -256,6 +256,4 @@ module URI false end end - - @@schemes['LDAP'] = LDAP end diff --git a/lib/uri/ldaps.rb b/lib/uri/ldaps.rb index 227e7fab35..fd3f7e875c 100644 --- a/lib/uri/ldaps.rb +++ b/lib/uri/ldaps.rb @@ -17,5 +17,4 @@ module URI # A Default port of 636 for URI::LDAPS DEFAULT_PORT = 636 end - @@schemes['LDAPS'] = LDAPS end diff --git a/lib/uri/mailto.rb b/lib/uri/mailto.rb index d08c2ae9da..214dea8a86 100644 --- a/lib/uri/mailto.rb +++ b/lib/uri/mailto.rb @@ -288,6 +288,4 @@ module URI end alias to_rfc822text to_mailtext end - - @@schemes['MAILTO'] = MailTo end diff --git a/lib/uri/ws.rb b/lib/uri/ws.rb index 2bfee59003..b846ba208a 100644 --- a/lib/uri/ws.rb +++ b/lib/uri/ws.rb @@ -78,7 +78,4 @@ module URI url.start_with?(?/.freeze) ? url : ?/ + url end end - - @@schemes['WS'] = WS - end diff --git a/lib/uri/wss.rb b/lib/uri/wss.rb index 1cfa133389..12a53c785e 100644 --- a/lib/uri/wss.rb +++ b/lib/uri/wss.rb @@ -18,5 +18,4 @@ module URI # A Default port of 443 for URI::WSS DEFAULT_PORT = 443 end - @@schemes['WSS'] = WSS end -- cgit v1.2.3