Skip to content

Commit cdfb3f5

Browse files
authored
Merge pull request #26 from eregon/fix-ractor-support
Add proper Ractor support to URI
2 parents 5ea2356 + bbf8b44 commit cdfb3f5

File tree

16 files changed

+876
-43
lines changed

16 files changed

+876
-43
lines changed

Rakefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ require "bundler/gem_tasks"
22
require "rake/testtask"
33

44
Rake::TestTask.new(:test) do |t|
5-
t.libs << "test" << "test/lib"
6-
t.libs << "lib"
5+
t.libs << "test/lib"
6+
t.ruby_opts << "-rhelper"
77
t.test_files = FileList["test/**/test_*.rb"]
88
end
99

10+
task :sync_tool do
11+
require 'fileutils'
12+
FileUtils.cp "../ruby/tool/lib/test/unit/core_assertions.rb", "./test/lib"
13+
FileUtils.cp "../ruby/tool/lib/envutil.rb", "./test/lib"
14+
FileUtils.cp "../ruby/tool/lib/find_executable.rb", "./test/lib"
15+
end
16+
1017
task :default => :test

lib/uri.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
# module URI
3030
# class RSYNC < Generic
3131
# DEFAULT_PORT = 873
32-
# URI.refresh_scheme_list
3332
# end
33+
# register_scheme 'RSYNC', RSYNC
3434
# end
3535
# #=> URI::RSYNC
3636
#
@@ -100,3 +100,9 @@ module URI
100100
require_relative 'uri/ldap'
101101
require_relative 'uri/ldaps'
102102
require_relative 'uri/mailto'
103+
104+
module URI
105+
INITIAL_SCHEMES = scheme_list
106+
private_constant :INITIAL_SCHEMES
107+
Ractor.make_shareable(INITIAL_SCHEMES) if defined?(Ractor)
108+
end

lib/uri/common.rb

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,38 +64,33 @@ def make_components_hash(klass, array_hash)
6464

6565
include REGEXP
6666

67-
SCHEME_LIST_MUTEX = Mutex.new
68-
private_constant :SCHEME_LIST_MUTEX
69-
70-
# Returns a Hash of the defined schemes.
71-
# The list is lazily calculated.
72-
def self.scheme_list
73-
return const_get(:SCHEMES) if defined?(SCHEMES)
74-
75-
SCHEME_LIST_MUTEX.synchronize do
76-
const_set(:SCHEMES, ObjectSpace.
77-
each_object(Class).
78-
select { |klass| klass < URI::Generic }.
79-
each_with_object({}) { |klass, acc| acc[klass.name.split('::').last.upcase] = klass }.
80-
freeze)
81-
end
67+
module Schemes
8268
end
69+
private_constant :Schemes
8370

84-
# Re-calculate scheme list
85-
def self.refresh_scheme_list
86-
SCHEME_LIST_MUTEX.synchronize do
87-
remove_const(:SCHEMES) if defined?(SCHEMES)
88-
end
71+
def self.register_scheme(scheme, klass)
72+
Schemes.const_set(scheme, klass)
73+
end
8974

90-
scheme_list
75+
# Returns a Hash of the defined schemes.
76+
def self.scheme_list
77+
Schemes.constants.map { |name|
78+
[name.to_s.upcase, Schemes.const_get(name)]
79+
}.to_h
9180
end
9281

9382
#
9483
# Construct a URI instance, using the scheme to detect the appropriate class
9584
# from +URI.scheme_list+.
9685
#
9786
def self.for(scheme, *arguments, default: Generic)
98-
uri_class = scheme_list[scheme.to_s.upcase] || default
87+
const_name = scheme.to_s.upcase
88+
89+
uri_class = INITIAL_SCHEMES[const_name]
90+
if !uri_class && !const_name.empty? && Schemes.const_defined?(const_name, false)
91+
uri_class = Schemes.const_get(const_name, false)
92+
end
93+
uri_class ||= default
9994

10095
return uri_class.new(scheme, *arguments)
10196
end
@@ -671,6 +666,7 @@ def self.decode_www_form(str, enc=Encoding::UTF_8, separator: '&', use__charset_
671666
"utf-16"=>"utf-16le",
672667
"utf-16le"=>"utf-16le",
673668
} # :nodoc:
669+
Ractor.make_shareable(WEB_ENCODINGS_) if defined?(Ractor)
674670

675671
# :nodoc:
676672
# return encoding or nil

lib/uri/file.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,6 @@ def set_user(v)
8989
def set_password(v)
9090
end
9191
end
92+
93+
register_scheme 'FILE', File
9294
end

lib/uri/ftp.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,6 @@ def to_s
262262
return str
263263
end
264264
end
265+
266+
register_scheme 'FTP', FTP
265267
end

lib/uri/http.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,6 @@ def request_uri
8181
url.start_with?(?/.freeze) ? url : ?/ + url
8282
end
8383
end
84+
85+
register_scheme 'HTTP', HTTP
8486
end

lib/uri/https.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ class HTTPS < HTTP
1818
# A Default port of 443 for URI::HTTPS
1919
DEFAULT_PORT = 443
2020
end
21+
22+
register_scheme 'HTTPS', HTTPS
2123
end

lib/uri/ldap.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,6 @@ def hierarchical?
256256
false
257257
end
258258
end
259+
260+
register_scheme 'LDAP', LDAP
259261
end

lib/uri/ldaps.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ class LDAPS < LDAP
1717
# A Default port of 636 for URI::LDAPS
1818
DEFAULT_PORT = 636
1919
end
20+
21+
register_scheme 'LDAPS', LDAPS
2022
end

lib/uri/mailto.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,6 @@ def to_mailtext
288288
end
289289
alias to_rfc822text to_mailtext
290290
end
291+
292+
register_scheme 'MAILTO', MailTo
291293
end

0 commit comments

Comments
 (0)