From 2f95f39b1363c3a4b11a08b7351c61ac80d21355 Mon Sep 17 00:00:00 2001 From: Gareth Adams Date: Tue, 10 Jan 2023 05:07:39 +0000 Subject: [PATCH] Update URI::Generic.build/build2 to use RFC3986_PARSER In bb83f32, a new URI parser was introduced to support RFC3986, where previously support was limited to RFC2396. The default parser used by URI() was updated to point to the new parser. However, the parser used by URI::Generic.build and build2 still points at the old parser. Among other things this means that hostnames with underscores can't correctly be parsed by this method. This commit switches to explicitly use the newer parser for these methods, and adds a previously failing test that now passes. --- lib/uri/generic.rb | 12 ++++++------ test/uri/test_generic.rb | 4 ++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index 69698c4e2d..b397bc769e 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -158,7 +158,7 @@ def self.build(args) # +fragment+:: # Part of the URI after '#' character. # +parser+:: - # Parser for internal use [URI::DEFAULT_PARSER by default]. + # Parser for internal use [URI::RFC3986_PARSER by default]. # +arg_check+:: # Check arguments [false by default]. # @@ -171,7 +171,7 @@ def initialize(scheme, path, opaque, query, fragment, - parser = DEFAULT_PARSER, + parser = RFC3986_PARSER, arg_check = false) @scheme = nil @user = nil @@ -182,7 +182,7 @@ def initialize(scheme, @query = nil @opaque = nil @fragment = nil - @parser = parser == DEFAULT_PARSER ? nil : parser + @parser = parser == RFC3986_PARSER ? nil : parser if arg_check self.scheme = scheme @@ -284,13 +284,13 @@ def registry # :nodoc: # Returns the parser to be used. # - # Unless a URI::Parser is defined, DEFAULT_PARSER is used. + # Unless a URI::Parser is defined, RFC3986_PARSER is used. # def parser if !defined?(@parser) || !@parser - DEFAULT_PARSER + RFC3986_PARSER else - @parser || DEFAULT_PARSER + @parser || RFC3986_PARSER end end diff --git a/test/uri/test_generic.rb b/test/uri/test_generic.rb index 3897c3d6ee..603593841c 100644 --- a/test/uri/test_generic.rb +++ b/test/uri/test_generic.rb @@ -830,6 +830,10 @@ def test_build assert_equal(":5432", u.to_s) assert_equal(5432, u.port) + u = URI::Generic.build(:host => "underscore_host.test") + assert_equal("//underscore_host.test", u.to_s) + assert_equal("underscore_host.test", u.host) + u = URI::Generic.build(:scheme => "http", :host => "::1", :path => "/bar/baz") assert_equal("http://[::1]/bar/baz", u.to_s) assert_equal("[::1]", u.host) -- 2.33.1