[前][次][番号順一覧][スレッド一覧]

ruby-changes:13125

From: akr <ko1@a...>
Date: Sun, 13 Sep 2009 01:18:18 +0900 (JST)
Subject: [ruby-changes:13125] Ruby:r24875 (trunk): * lib/open-uri.rb (URI::FTP#buffer_open): fix the %2F handling.

akr	2009-09-13 01:18:03 +0900 (Sun, 13 Sep 2009)

  New Revision: 24875

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=24875

  Log:
    * lib/open-uri.rb (URI::FTP#buffer_open): fix the %2F handling.

  Modified files:
    trunk/ChangeLog
    trunk/lib/open-uri.rb
    trunk/test/open-uri/test_open-uri.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 24874)
+++ ChangeLog	(revision 24875)
@@ -1,3 +1,7 @@
+Sun Sep 13 01:15:49 2009  Tanaka Akira  <akr@f...>
+
+	* lib/open-uri.rb (URI::FTP#buffer_open): fix the %2F handling.
+
 Sun Sep 13 00:46:39 2009  Nobuyoshi Nakada  <nobu@r...>
 
 	* vm_method.c (CALL_METHOD_HOOK): refined with macro.
Index: lib/open-uri.rb
===================================================================
--- lib/open-uri.rb	(revision 24874)
+++ lib/open-uri.rb	(revision 24875)
@@ -778,8 +778,9 @@
       end
       require 'net/ftp'
 
-      directories = self.path.split(%r{/}, -1)
-      directories.shift if directories[0] == '' # strip a field before leading slash
+      path = self.path
+      path = path.sub(%r{\A/}, '%2F') # re-encode the beginning slash because uri library decodes it.
+      directories = path.split(%r{/}, -1)
       directories.each {|d|
         d.gsub!(/%([0-9A-Fa-f][0-9A-Fa-f])/) { [$1].pack("H2") }
       }
Index: test/open-uri/test_open-uri.rb
===================================================================
--- test/open-uri/test_open-uri.rb	(revision 24874)
+++ test/open-uri/test_open-uri.rb	(revision 24875)
@@ -446,6 +446,15 @@
     }
   end
 
+  def test_ftp_invalid_request
+    assert_raise(ArgumentError) { URI("ftp://127.0.0.1/").read }
+    assert_raise(ArgumentError) { URI("ftp://127.0.0.1/a%0Db").read }
+    assert_raise(ArgumentError) { URI("ftp://127.0.0.1/a%0Ab").read }
+    assert_raise(ArgumentError) { URI("ftp://127.0.0.1/a%0Db/f").read }
+    assert_raise(ArgumentError) { URI("ftp://127.0.0.1/a%0Ab/f").read }
+    assert_raise(URI::InvalidComponentError) { URI("ftp://127.0.0.1/d/f;type=x") }
+  end
+
   def test_ftp
     TCPServer.open("127.0.0.1", 0) {|serv|
       _, port, _, host = serv.addr
@@ -487,6 +496,56 @@
     }
   end
 
+  def test_ftp_ascii
+    TCPServer.open("127.0.0.1", 0) {|serv|
+      _, port, _, host = serv.addr
+      th = Thread.new {
+        s = serv.accept
+        begin
+          content = "content"
+          s.print "220 Test FTP Server\r\n"
+          assert_equal("USER anonymous\r\n", s.gets); s.print "331 name ok\r\n"
+          assert_match(/\APASS .*\r\n/, s.gets); s.print "230 logged in\r\n"
+          assert_equal("TYPE I\r\n", s.gets); s.print "200 type set to I\r\n"
+          assert_equal("CWD /foo\r\n", s.gets); s.print "250 CWD successful\r\n"
+          assert_equal("TYPE A\r\n", s.gets); s.print "200 type set to A\r\n"
+          assert_equal("SIZE bar\r\n", s.gets); s.print "213 #{content.bytesize}\r\n"
+          assert_equal("PASV\r\n", s.gets)
+          TCPServer.open("127.0.0.1", 0) {|data_serv|
+            _, data_serv_port, _, data_serv_host = data_serv.addr
+            hi = data_serv_port >> 8
+            lo = data_serv_port & 0xff
+            s.print "227 Entering Passive Mode (127,0,0,1,#{hi},#{lo}).\r\n"
+            assert_equal("RETR bar\r\n", s.gets); s.print "150 file okay\r\n"
+            data_sock = data_serv.accept
+            begin
+              data_sock << content
+            ensure
+              data_sock.close
+            end
+            s.print "226 transfer complete\r\n"
+            assert_nil(s.gets)
+          }
+        ensure
+          s.close if s
+        end
+      }
+      begin
+        length = []
+        progress = []
+        content = URI("ftp://#{host}:#{port}/%2Ffoo/b%61r;type=a").read(
+         :content_length_proc => lambda {|n| length << n },
+         :progress_proc => lambda {|n| progress << n })
+        assert_equal("content", content)
+        assert_equal([7], length)
+        assert_equal(7, progress.inject(&:+))
+      ensure
+        Thread.kill(th)
+        th.join
+      end
+    }
+  end
+
   def test_ftp_over_http_proxy
     TCPServer.open("127.0.0.1", 0) {|proxy_serv|
       proxy_port = proxy_serv.addr[1]

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]