Skip to content
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

Use RFC3986_Parser by default #107

Merged
merged 7 commits into from
Jul 19, 2024
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
37 changes: 25 additions & 12 deletions lib/uri/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,37 @@
require_relative "rfc3986_parser"

module URI
include RFC2396_REGEXP
RFC2396_PARSER = RFC2396_Parser.new
Ractor.make_shareable(RFC2396_PARSER) if defined?(Ractor)

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
DEFAULT_PARSER.pattern.each_pair do |sym, str|
unless REGEXP::PATTERN.const_defined?(sym)
REGEXP::PATTERN.const_set(sym, str)
DEFAULT_PARSER = RFC3986_PARSER
Ractor.make_shareable(DEFAULT_PARSER) if defined?(Ractor)

def self.parser=(parser = RFC3986_PARSER)
remove_const(:Parser) if defined?(Parser)
const_set("Parser", parser.class)

remove_const(:REGEXP) if defined?(REGEXP)
remove_const(:PATTERN) if defined?(PATTERN)
if Parser == RFC2396_Parser
const_set("REGEXP", URI::RFC2396_REGEXP)
const_set("PATTERN", URI::RFC2396_REGEXP::PATTERN)
Parser.new.pattern.each_pair do |sym, str|
unless REGEXP::PATTERN.const_defined?(sym)
REGEXP::PATTERN.const_set(sym, str)
end
end
end

Parser.new.regexp.each_pair do |sym, str|
remove_const(sym) if const_defined?(sym)
const_set(sym, str)
end
end
DEFAULT_PARSER.regexp.each_pair do |sym, str|
const_set(sym, str)
end
Ractor.make_shareable(DEFAULT_PARSER) if defined?(Ractor)
self.parser = RFC3986_PARSER

module Util # :nodoc:
def make_components_hash(klass, array_hash)
Expand Down
21 changes: 20 additions & 1 deletion lib/uri/rfc3986_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,31 @@ def parse(uri) # :nodoc:
URI.for(*self.split(uri), self)
end


def join(*uris) # :nodoc:
uris[0] = convert_to_uri(uris[0])
uris.inject :merge
end

# Compatibility for RFC2396 parser
def extract(str, schemes = nil, &block) # :nodoc:
RFC2396_PARSER.extract(str, schemes, &block)
end

# Compatibility for RFC2396 parser
def make_regexp(schemes = nil) # :nodoc:
RFC2396_PARSER.make_regexp(schemes)
end

# Compatibility for RFC2396 parser
def escape(str, unsafe = nil) # :nodoc:
unsafe ? RFC2396_PARSER.escape(str, unsafe) : RFC2396_PARSER.escape(str)
end

# Compatibility for RFC2396 parser
def unescape(str, escaped = nil) # :nodoc:
escaped ? RFC2396_PARSER.unescape(str, escaped) : RFC2396_PARSER.unescape(str)
end

@@to_s = Kernel.instance_method(:to_s)
if @@to_s.respond_to?(:bind_call)
def inspect
Expand Down
21 changes: 21 additions & 0 deletions test/uri/test_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ def setup
def teardown
end

def test_parser_switch
assert_equal(URI::Parser, URI::RFC3986_Parser)
refute defined?(URI::REGEXP)
refute defined?(URI::PATTERN)

URI.parser = URI::RFC2396_PARSER

assert_equal(URI::Parser, URI::RFC2396_Parser)
assert defined?(URI::REGEXP)
assert defined?(URI::PATTERN)
assert defined?(URI::PATTERN::ESCAPED)

URI.parser = URI::RFC3986_PARSER

assert_equal(URI::Parser, URI::RFC3986_Parser)
refute defined?(URI::REGEXP)
refute defined?(URI::PATTERN)
ensure
URI.parser = URI::RFC3986_PARSER
end

def test_extract
EnvUtil.suppress_warning do
assert_equal(['http://example.com'],
Expand Down
10 changes: 7 additions & 3 deletions test/uri/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def uri_to_ary(uri)
end

def test_inspect
assert_match(/URI::RFC2396_Parser/, URI::Parser.new.inspect)
assert_match(/URI::RFC3986_Parser/, URI::RFC3986_Parser.new.inspect)
assert_match(/URI::RFC2396_Parser/, URI::RFC2396_Parser.new.inspect)
assert_match(/URI::RFC3986_Parser/, URI::Parser.new.inspect)
end

def test_compare
Expand All @@ -33,7 +33,9 @@ def test_compare
assert(!u2.equal?(u3))
end

def test_parse
def test_parse_rfc2396_parser
URI.parser = URI::RFC2396_PARSER

escaped = URI::REGEXP::PATTERN::ESCAPED
hex = URI::REGEXP::PATTERN::HEX
p1 = URI::Parser.new(:ESCAPED => "(?:#{escaped}|%u[#{hex}]{4})")
Expand All @@ -43,6 +45,8 @@ def test_parse
u1.path = '/%uDCBA'
assert_equal(['http', nil, 'a', URI::HTTP.default_port, '/%uDCBA', nil, nil],
uri_to_ary(u1))
ensure
URI.parser = URI::DEFAULT_PARSER
end

def test_parse_query_pct_encoded
Expand Down