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

ruby-changes:15687

From: mame <ko1@a...>
Date: Tue, 4 May 2010 08:44:01 +0900 (JST)
Subject: [ruby-changes:15687] Ruby:r27605 (trunk): * lib/net/http.rb (Net::HTTPResponse#read_chunked): ensure to skip the

mame	2010-05-04 08:42:26 +0900 (Tue, 04 May 2010)

  New Revision: 27605

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

  Log:
    * lib/net/http.rb (Net::HTTPResponse#read_chunked): ensure to skip the
      last newline of chunk.  [ruby-core:29229]
    
    * test/net/http/utils.rb: add an option for chunked response test.
    
    * test/net/http/test_http.rb: add tests for chunked response.

  Modified files:
    trunk/ChangeLog
    trunk/lib/net/http.rb
    trunk/test/net/http/test_http.rb
    trunk/test/net/http/utils.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 27604)
+++ ChangeLog	(revision 27605)
@@ -1,3 +1,12 @@
+Tue May  4 07:52:33 2010  Yusuke Endoh  <mame@t...>
+
+	* lib/net/http.rb (Net::HTTPResponse#read_chunked): ensure to skip the
+	  last newline of chunk.  [ruby-core:29229]
+
+	* test/net/http/utils.rb: add an option for chunked response test.
+
+	* test/net/http/test_http.rb: add tests for chunked response.
+
 Tue May  4 03:37:54 2010  NARUSE, Yui  <naruse@r...>
 
 	* ext/nkf/nkf-utf8/nkf.c: Update nkf 2010-04-28.
Index: lib/net/http.rb
===================================================================
--- lib/net/http.rb	(revision 27604)
+++ lib/net/http.rb	(revision 27605)
@@ -2433,8 +2433,12 @@
             raise HTTPBadResponse, "wrong chunk size line: #{line}"
         len = hexlen.hex
         break if len == 0
-        @socket.read len, dest; total += len
-        @socket.read 2   # \r\n
+        begin
+          @socket.read len, dest
+        ensure
+          total += len
+          @socket.read 2   # \r\n
+        end
       end
       until @socket.readline.empty?
         # none
Index: test/net/http/test_http.rb
===================================================================
--- test/net/http/test_http.rb	(revision 27604)
+++ test/net/http/test_http.rb	(revision 27605)
@@ -17,7 +17,9 @@
       res = http.head('/')
       assert_kind_of Net::HTTPResponse, res
       assert_equal $test_net_http_data_type, res['Content-Type']
-      assert_equal $test_net_http_data.size, res['Content-Length'].to_i
+      unless self.is_a?(TestNetHTTP_v1_2_chunked)
+        assert_equal $test_net_http_data.size, res['Content-Length'].to_i
+      end
     }
   end
 
@@ -34,8 +36,10 @@
     assert_kind_of Net::HTTPResponse, res
     assert_kind_of String, res.body
     assert_kind_of String, body
-    assert_not_nil res['content-length']
-    assert_equal $test_net_http_data.size, res['content-length'].to_i
+    unless self.is_a?(TestNetHTTP_v1_2_chunked)
+      assert_not_nil res['content-length']
+      assert_equal $test_net_http_data.size, res['content-length'].to_i
+    end
     assert_equal $test_net_http_data_type, res['Content-Type']
     assert_equal $test_net_http_data.size, body.size
     assert_equal $test_net_http_data, body
@@ -49,8 +53,10 @@
     assert_kind_of Net::HTTPResponse, res
     # assert_kind_of String, res.body
     # assert_kind_of String, body
-    assert_not_nil res['content-length']
-    assert_equal $test_net_http_data.size, res['content-length'].to_i
+    unless self.is_a?(TestNetHTTP_v1_2_chunked)
+      assert_not_nil res['content-length']
+      assert_equal $test_net_http_data.size, res['content-length'].to_i
+    end
     assert_equal $test_net_http_data_type, res['Content-Type']
     assert_equal $test_net_http_data.size, buf.size
     assert_equal $test_net_http_data, buf
@@ -64,8 +70,10 @@
     assert_kind_of Net::HTTPResponse, res
     # assert_kind_of String, res.body
     # assert_kind_of String, body
-    assert_not_nil res['content-length']
-    assert_equal $test_net_http_data.size, res['content-length'].to_i
+    unless self.is_a?(TestNetHTTP_v1_2_chunked)
+      assert_not_nil res['content-length']
+      assert_equal $test_net_http_data.size, res['content-length'].to_i
+    end
     assert_equal $test_net_http_data_type, res['Content-Type']
     assert_equal $test_net_http_data.size, buf.size
     assert_equal $test_net_http_data, buf
@@ -89,7 +97,9 @@
     assert_kind_of Net::HTTPResponse, res
     assert_kind_of String, body
     assert_kind_of String, res.body
-    assert_not_nil res['content-length']
+    unless self.is_a?(TestNetHTTP_v1_2_chunked)
+      assert_not_nil res['content-length']
+    end
     assert_equal $test_net_http_data_type, res['Content-Type']
     assert_equal $test_net_http_data.size, res.body.size
     assert_equal $test_net_http_data, res.body
@@ -100,7 +110,9 @@
       http.get2('/') {|res|
         assert_kind_of Net::HTTPResponse, res
         assert_kind_of Net::HTTPResponse, res.header
-        assert_not_nil res['content-length']
+        unless self.is_a?(TestNetHTTP_v1_2_chunked)
+          assert_not_nil res['content-length']
+        end
         assert_equal $test_net_http_data_type, res['Content-Type']
         assert_kind_of String, res.body
         assert_kind_of String, res.entity
@@ -178,8 +190,10 @@
     http.request(req) {|res|
       assert_kind_of Net::HTTPResponse, res
       assert_kind_of String, res.body
-      assert_not_nil res['content-length']
-      assert_equal $test_net_http_data.size, res['content-length'].to_i
+      unless self.is_a?(TestNetHTTP_v1_2_chunked)
+        assert_not_nil res['content-length']
+        assert_equal $test_net_http_data.size, res['content-length'].to_i
+      end
       assert_equal $test_net_http_data.size, res.body.size
       assert_equal $test_net_http_data, res.body
     }
@@ -189,8 +203,10 @@
     req = Net::HTTP::Get.new('/')
     http.request(req) {|res|
       assert_kind_of Net::HTTPResponse, res
-      assert_not_nil res['content-length']
-      assert_equal $test_net_http_data.size, res['content-length'].to_i
+      unless self.is_a?(TestNetHTTP_v1_2_chunked)
+        assert_not_nil res['content-length']
+        assert_equal $test_net_http_data.size, res['content-length'].to_i
+      end
       f = StringIO.new("".force_encoding("ASCII-8BIT"))
       res.read_body f
       assert_equal $test_net_http_data.bytesize, f.string.bytesize
@@ -209,8 +225,10 @@
     req = Net::HTTP::Head.new('/')
     http.request(req) {|res|
       assert_kind_of Net::HTTPResponse, res
-      assert_not_nil res['content-length']
-      assert_equal $test_net_http_data.size, res['content-length'].to_i
+      unless self.is_a?(TestNetHTTP_v1_2_chunked)
+        assert_not_nil res['content-length']
+        assert_equal $test_net_http_data.size, res['content-length'].to_i
+      end
       assert_nil res.body
     }
   end
@@ -221,7 +239,9 @@
     req['Accept'] = $test_net_http_data_type
     http.request(req, data) {|res|
       assert_kind_of Net::HTTPResponse, res
-      assert_equal data.size, res['content-length'].to_i
+      unless self.is_a?(TestNetHTTP_v1_2_chunked)
+        assert_equal data.size, res['content-length'].to_i
+      end
       assert_kind_of String, res.body
       assert_equal data, res.body
     }
@@ -249,7 +269,9 @@
   def _test_send_request__GET(http)
     res = http.send_request('GET', '/')
     assert_kind_of Net::HTTPResponse, res
-    assert_equal $test_net_http_data.size, res['content-length'].to_i
+    unless self.is_a?(TestNetHTTP_v1_2_chunked)
+      assert_equal $test_net_http_data.size, res['content-length'].to_i
+    end
     assert_kind_of String, res.body
     assert_equal $test_net_http_data, res.body
   end
@@ -299,6 +321,38 @@
   end
 end
 
+class TestNetHTTP_v1_2_chunked < Test::Unit::TestCase
+  CONFIG = {
+    'host' => '127.0.0.1',
+    'port' => 10081,
+    'proxy_host' => nil,
+    'proxy_port' => nil,
+    'chunked' => true,
+  }
+
+  include TestNetHTTPUtils
+  include TestNetHTTP_version_1_1_methods
+  include TestNetHTTP_version_1_2_methods
+
+  def new
+    Net::HTTP.version_1_2
+    super
+  end
+
+  def test_chunked_break
+    i = 0
+    assert_nothing_raised("[ruby-core:29229]") {
+      start {|http|
+        http.request_get('/') {|res|
+          res.read_body {|chunk|
+            break
+          }
+        }
+      }
+    }
+  end
+end
+
 =begin
 class TestNetHTTP_proxy < Test::Unit::TestCase
   CONFIG = {
Index: test/net/http/utils.rb
===================================================================
--- test/net/http/utils.rb	(revision 27604)
+++ test/net/http/utils.rb	(revision 27605)
@@ -48,6 +48,7 @@
       :ShutdownSocketWithoutClose => true,
       :ServerType => Thread,
     }
+    server_config[:OutputBufferSize] = 4 if config('chunked')
     if defined?(OpenSSL) and config('ssl_enable')
       server_config.update({
         :SSLEnable      => true,
@@ -56,7 +57,7 @@
       })
     end
     @server = WEBrick::HTTPServer.new(server_config)
-    @server.mount('/', Servlet)
+    @server.mount('/', Servlet, config('chunked'))
     @server.start
     n_try_max = 5
     begin
@@ -75,15 +76,21 @@
   $test_net_http_data_type = 'application/octet-stream'
 
   class Servlet < WEBrick::HTTPServlet::AbstractServlet
+    def initialize(this, chunked = false)
+      @chunked = chunked
+    end
+
     def do_GET(req, res)
       res['Content-Type'] = $test_net_http_data_type
       res.body = $test_net_http_data
+      res.chunked = @chunked
     end
 
     # echo server
     def do_POST(req, res)
       res['Content-Type'] = req['Content-Type']
       res.body = req.body
+      res.chunked = @chunked
     end
   end
 

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

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