diff --git a/ChangeLog b/ChangeLog index edf8e65b1f..6f0f896a43 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Tue May 4 07:52:33 2010 Yusuke Endoh + + * 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 * ext/nkf/nkf-utf8/nkf.c: Update nkf 2010-04-28. diff --git a/lib/net/http.rb b/lib/net/http.rb index ebe51496f6..f39451591f 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -2433,8 +2433,12 @@ module Net #:nodoc: 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 diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index 271c50e802..167eda933e 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -17,7 +17,9 @@ module TestNetHTTP_version_1_1_methods 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 @@ module TestNetHTTP_version_1_1_methods 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 @@ module TestNetHTTP_version_1_1_methods 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 @@ module TestNetHTTP_version_1_1_methods 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 @@ module TestNetHTTP_version_1_1_methods 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 @@ module TestNetHTTP_version_1_1_methods 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 @@ module TestNetHTTP_version_1_2_methods 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 @@ module TestNetHTTP_version_1_2_methods 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 @@ module TestNetHTTP_version_1_2_methods 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 @@ module TestNetHTTP_version_1_2_methods 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 @@ module TestNetHTTP_version_1_2_methods 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 @@ class TestNetHTTP_v1_2 < Test::Unit::TestCase 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 = { diff --git a/test/net/http/utils.rb b/test/net/http/utils.rb index e685a8ff61..10c88d70d7 100644 --- a/test/net/http/utils.rb +++ b/test/net/http/utils.rb @@ -48,6 +48,7 @@ module TestNetHTTPUtils :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 @@ module TestNetHTTPUtils }) 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 @@ module TestNetHTTPUtils $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