From: "akira yamada / やまだあきら" Date: 2004-06-06T17:42:31+09:00 Subject: [ruby-dev:23670] Re: [uri.rb] non-hierarchical URI with a fragment >>>>> In [ruby-dev : No.23669] >>>>> akira yamada / やまだあきら wrote: > この二件について、こういう感じでどうでしょうか: -のほうがうまくなかったのでもう一度考えてみました。 さっきのパッチを破棄して、こっちでどうでしょうか: diff -ruN ruby-1.8.1.orig/lib/uri/generic.rb ruby-1.8.1/lib/uri/generic.rb --- ruby-1.8.1.orig/lib/uri/generic.rb 2004-03-24 21:20:32.000000000 +0900 +++ ruby-1.8.1/lib/uri/generic.rb 2004-06-06 17:40:38.000000000 +0900 @@ -726,7 +726,12 @@ # # => # # def merge(oth) - base, rel = merge0(oth) + begin + base, rel = merge0(oth) + rescue + raise $!.class, $!.message + end + if base == rel return base end @@ -734,7 +739,7 @@ authority = rel.userinfo || rel.host || rel.port # RFC2396, Section 5.2, 2) - if rel.path.empty? && !authority && !rel.query + if (rel.path.nil? || rel.path.empty?) && !authority && !rel.query base.set_fragment(rel.fragment) if rel.fragment return base end @@ -744,10 +749,10 @@ # RFC2396, Section 5.2, 4) if !authority - base.set_path(merge_path(base.path, rel.path)) + base.set_path(merge_path(base.path, rel.path)) if base.path && rel.path else # RFC2396, Section 5.2, 4) - base.set_path(rel.path) + base.set_path(rel.path) if rel.path end # RFC2396, Section 5.2, 7) @@ -785,14 +790,6 @@ return oth, oth end - if !self.hierarchical? - raise BadURIError, - "not hierarchical URI: #{self}" - elsif !oth.hierarchical? - raise BadURIError, - "not hierarchical URI: #{oth}" - end - if self.absolute? return self.dup, oth else @@ -861,12 +858,8 @@ "relative URI: #{oth}" end - if !self.hierarchical? || !oth.hierarchical? - return self, self.dup - end - if self.scheme != oth.scheme - return oth, oth.dup + return self, self.dup end rel = URI::Generic.new(nil, # it is relative URI self.userinfo, self.host, self.port, @@ -876,6 +869,9 @@ if rel.userinfo != oth.userinfo || rel.host.to_s.downcase != oth.host.to_s.downcase || rel.port != oth.port + if self.userinfo.nil? && self.host.nil? + return self, self.dup + end rel.set_port(nil) if rel.port == oth.default_port return rel, rel end @@ -883,10 +879,14 @@ rel.set_host(nil) rel.set_port(nil) - if rel.path == oth.path + if rel.path && rel.path == oth.path rel.set_path('') rel.set_query(nil) if rel.query == oth.query return rel, rel + elsif rel.opaque && rel.opaque == oth.opaque + rel.set_opaque('') + rel.set_query(nil) if rel.query == oth.query + return rel, rel end # you can modify `rel', but can not `oth'. @@ -913,7 +913,11 @@ # def route_from(oth) # you can modify `rel', but can not `oth'. - oth, rel = route_from0(oth) + begin + oth, rel = route_from0(oth) + rescue + raise $!.class, $!.message + end if oth == rel return rel end @@ -1045,6 +1049,14 @@ end end + def hash + self.component_ary.hash + end + + def eql?(oth) + self.hash == oth.hash + end + =begin --- URI::Generic#===(oth) diff -ruN ruby-1.8.1.orig/test/uri/test_generic.rb ruby-1.8.1/test/uri/test_generic.rb --- ruby-1.8.1.orig/test/uri/test_generic.rb 2003-12-23 14:17:00.000000000 +0900 +++ ruby-1.8.1/test/uri/test_generic.rb 2004-06-06 17:39:37.000000000 +0900 @@ -158,6 +158,11 @@ assert_equal('http://foo/bar/', u.to_s) assert(nil != u.merge!("../baz")) assert_equal('http://foo/baz', u.to_s) + + # [ruby-dev:23628] + u0 = URI.parse('mailto:foo@example.com') + u1 = URI.parse('mailto:foo@example.com#bar') + assert_equal(uri_to_ary(u0 + '#bar'), uri_to_ary(u1)) end def test_route @@ -180,6 +185,15 @@ url = URI.parse('file:///a/b/').route_to('file:///a/b/') assert_equal('', url.to_s) + + url = URI.parse('mailto:foo@example.com').route_to('mailto:foo@example.com#bar') + assert_equal('#bar', url.to_s) + + url = URI.parse('mailto:foo@example.com#bar').route_to('mailto:foo@example.com') + assert_equal('', url.to_s) + + url = URI.parse('mailto:foo@example.com').route_to('mailto:foo@example.com') + assert_equal('', url.to_s) end def test_rfc2396_examples -- やまだあきら Vine Caves, Ltd.