[#39863] forループの速度 — Masahiro Sato <msato@...>

15 messages 2004/07/20

[#39868] イテレータとfor文 — OOTANI TAKASHI <otn@...5.so-net.ne.jp>

大谷と申します。

31 messages 2004/07/20
[#39886] Re: イテレータとfor文 — Tietew <tietew-ml-ruby-list@...> 2004/07/21

[ruby-list:39840] Re: URI#merge and relative references

From: akira yamada / やまだあきら <akira@...>
Date: 2004-07-07 14:58:41 UTC
List: ruby-list #39840
>>>>> In [ruby-list : No.39838] 
>>>>>	sheepman <sheepman@tcn.zaq.ne.jp> wrote:
> URI ライブラリで、
> p URI.parse("http://www.example.com/foo/..") + "./"
> => #<URI::HTTP:0x2010db84 URL:http://www.example.com/foo/>
> となります。この
> http://www.example.com/foo/
> は
> http://www.example.com/
> となるべきなのではないでしょうか。

こんな感じでどうでしょうか。

--- lib/uri/generic.rb	9 Jun 2004 09:05:41 -0000	1.13.2.2
+++ lib/uri/generic.rb	7 Jul 2004 14:57:14 -0000
@@ -630,15 +630,19 @@
         base_path = split_path(base)
         rel_path  = split_path(rel)
 
-        if base_path.empty?
-          base_path = [''] # XXX
-        end
-
         # RFC2396, Section 5.2, 6), a)
-        base_path.pop unless base_path.size == 1
+	base_path << '' if base_path.last == '..'
+	while i = base_path.index('..')
+	  base_path.slice!(i - 1, 2)
+	end
+        if base_path.empty?
+          base_path = [''] # keep '/' for root directory
+	else
+	  base_path.pop
+	end
 
         # RFC2396, Section 5.2, 6), c)
-         # RFC2396, Section 5.2, 6), d)
+      	# RFC2396, Section 5.2, 6), d)
         rel_path.push('') if rel_path.last == '.'
         rel_path.delete('.')
 

テストはこんな感じで足りてますでしょうか。

--- test/uri/test_generic.rb	9 Jun 2004 09:05:41 -0000	1.5.2.1
+++ test/uri/test_generic.rb	7 Jul 2004 14:57:14 -0000
@@ -163,6 +163,23 @@
     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))
+
+    # [ruby-list:39838]
+    u0 = URI.parse('http://www.example.com/')
+    u1 = URI.parse('http://www.example.com/foo/..') + './'
+    assert_equal(u0, u1)
+    u0 = URI.parse('http://www.example.com/foo/')
+    u1 = URI.parse('http://www.example.com/foo/bar/..') + './'
+    assert_equal(u0, u1)
+    u0 = URI.parse('http://www.example.com/foo/bar/')
+    u1 = URI.parse('http://www.example.com/foo/bar/baz/..') + './'
+    assert_equal(u0, u1)
+    u0 = URI.parse('http://www.example.com/')
+    u1 = URI.parse('http://www.example.com/foo/bar/../..') + './'
+    assert_equal(u0, u1)
+    u0 = URI.parse('http://www.example.com/foo/')
+    u1 = URI.parse('http://www.example.com/foo/bar/baz/../..') + './'
+    assert_equal(u0, u1)
   end
 
   def test_route

-- 
やまだあきら       <URL:http://arika.org>
Vine Caves, Ltd.   <URL:http://vinecaves.com>

In This Thread