aboutsummaryrefslogtreecommitdiffstats
path: root/lib/uri/common.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/uri/common.rb')
-rw-r--r--lib/uri/common.rb32
1 files changed, 25 insertions, 7 deletions
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