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

ruby-changes:46731

From: normal <ko1@a...>
Date: Mon, 22 May 2017 16:36:05 +0900 (JST)
Subject: [ruby-changes:46731] normal:r58846 (trunk): lib/net/protocol.rb: account read_bytes before caller sees it

normal	2017-05-22 16:36:00 +0900 (Mon, 22 May 2017)

  New Revision: 58846

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58846

  Log:
    lib/net/protocol.rb: account read_bytes before caller sees it
    
    Users may modify the chunk yielded to them in Net::HTTPResponse#read_body.
    This will allow users to reduce memory usage by calling
    String#clear on the buffer once they're done using it.
    
    * lib/net/protocol.rb (read): increment read_bytes earlier
      (read_all): ditto
    * test/net/http/test_httpresponse.rb (test_read_body_block_mod): new test

  Modified files:
    trunk/lib/net/protocol.rb
    trunk/test/net/http/test_httpresponse.rb
Index: lib/net/protocol.rb
===================================================================
--- lib/net/protocol.rb	(revision 58845)
+++ lib/net/protocol.rb	(revision 58846)
@@ -119,12 +119,14 @@ module Net # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/protocol.rb#L119
       read_bytes = 0
       begin
         while read_bytes + @rbuf.size < len
-          dest << (s = rbuf_consume(@rbuf.size))
+          s = rbuf_consume(@rbuf.size)
           read_bytes += s.size
+          dest << s
           rbuf_fill
         end
-        dest << (s = rbuf_consume(len - read_bytes))
+        s = rbuf_consume(len - read_bytes)
         read_bytes += s.size
+        dest << s
       rescue EOFError
         raise unless ignore_eof
       end
@@ -137,8 +139,9 @@ module Net # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/protocol.rb#L139
       read_bytes = 0
       begin
         while true
-          dest << (s = rbuf_consume(@rbuf.size))
+          s = rbuf_consume(@rbuf.size)
           read_bytes += s.size
+          dest << s
           rbuf_fill
         end
       rescue EOFError
Index: test/net/http/test_httpresponse.rb
===================================================================
--- test/net/http/test_httpresponse.rb	(revision 58845)
+++ test/net/http/test_httpresponse.rb	(revision 58846)
@@ -76,6 +76,32 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpresponse.rb#L76
     assert_equal 'hello', body
   end
 
+  def test_read_body_block_mod
+    IO.pipe do |r, w|
+      buf = 'x' * 1024
+      buf.freeze
+      n = 1024
+      len = n * buf.size
+      th = Thread.new do
+        w.write("HTTP/1.1 200 OK\r\nContent-Length: #{len}\r\n\r\n")
+        n.times { w.write(buf) }
+        :ok
+      end
+      io = Net::BufferedIO.new(r)
+      res = Net::HTTPResponse.read_new(io)
+      nr = 0
+      res.reading_body io, true do
+        # should be allowed to modify the chunk given to them:
+        res.read_body do |chunk|
+          nr += chunk.size
+          chunk.clear
+        end
+      end
+      assert_equal len, nr
+      assert_equal :ok, th.value
+    end
+  end
+
   def test_read_body_content_encoding_deflate
     io = dummy_io(<<EOS)
 HTTP/1.1 200 OK

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

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