Skip to content

Add proper Ractor support to URI #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ require "bundler/gem_tasks"
require "rake/testtask"

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

task :sync_tool do
require 'fileutils'
FileUtils.cp "../ruby/tool/lib/test/unit/core_assertions.rb", "./test/lib"
FileUtils.cp "../ruby/tool/lib/envutil.rb", "./test/lib"
FileUtils.cp "../ruby/tool/lib/find_executable.rb", "./test/lib"
end

task :default => :test
8 changes: 7 additions & 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
# register_scheme 'RSYNC', RSYNC
# end
# #=> URI::RSYNC
#
Expand Down Expand Up @@ -100,3 +100,9 @@ module URI
require_relative 'uri/ldap'
require_relative 'uri/ldaps'
require_relative 'uri/mailto'

module URI
INITIAL_SCHEMES = scheme_list
private_constant :INITIAL_SCHEMES
Ractor.make_shareable(INITIAL_SCHEMES) if defined?(Ractor)
end
40 changes: 18 additions & 22 deletions lib/uri/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,38 +64,33 @@ def make_components_hash(klass, array_hash)

include REGEXP

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
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
module Schemes
end
private_constant :Schemes

# Re-calculate scheme list
def self.refresh_scheme_list
SCHEME_LIST_MUTEX.synchronize do
remove_const(:SCHEMES) if defined?(SCHEMES)
end
def self.register_scheme(scheme, klass)
Schemes.const_set(scheme, klass)
end

scheme_list
# Returns a Hash of the defined schemes.
def self.scheme_list
Schemes.constants.map { |name|
[name.to_s.upcase, Schemes.const_get(name)]
}.to_h
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
const_name = scheme.to_s.upcase

uri_class = INITIAL_SCHEMES[const_name]
if !uri_class && !const_name.empty? && Schemes.const_defined?(const_name, false)
uri_class = Schemes.const_get(const_name, false)
end
uri_class ||= default

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

# :nodoc:
# return encoding or nil
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

register_scheme 'FILE', File
end
2 changes: 2 additions & 0 deletions lib/uri/ftp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,6 @@ def to_s
return str
end
end

register_scheme 'FTP', FTP
end
2 changes: 2 additions & 0 deletions lib/uri/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ def request_uri
url.start_with?(?/.freeze) ? url : ?/ + url
end
end

register_scheme 'HTTP', HTTP
end
2 changes: 2 additions & 0 deletions lib/uri/https.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ class HTTPS < HTTP
# A Default port of 443 for URI::HTTPS
DEFAULT_PORT = 443
end

register_scheme '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

register_scheme 'LDAP', LDAP
end
2 changes: 2 additions & 0 deletions lib/uri/ldaps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ class LDAPS < LDAP
# A Default port of 636 for URI::LDAPS
DEFAULT_PORT = 636
end

register_scheme '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

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

register_scheme 'WS', WS
end
2 changes: 2 additions & 0 deletions lib/uri/wss.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ class WSS < WS
# A Default port of 443 for URI::WSS
DEFAULT_PORT = 443
end

register_scheme 'WSS', WSS
end
Loading