Project

General

Profile

Bug #14275 ยป net-http-readbody.rb

normalperson (Eric Wong), 01/03/2018 01:52 AM

 
1
require 'net/http'
2
require 'digest/md5'
3
Thread.abort_on_exception = true
4
s = TCPServer.new('127.0.0.1', 0)
5
len = 1024 * 1024 * 1024 * 1
6
th = Thread.new do
7
  c = s.accept
8
  c.readpartial(16384).clear
9
  c.write("HTTP/1.0 200 OK\r\nContent-Length: #{len}\r\n\r\n")
10
  IO.copy_stream('/dev/zero', c, len)
11
  c.close
12
end
13

    
14
addr = s.addr
15
Net::HTTP.start(addr[3], addr[1]) do |http|
16
  http.request_get('/') do |res|
17
    dig = Digest::MD5.new
18
    res.read_body { |buf|
19
      dig.update(buf)
20
      # memory use nearly increases 10x without String#clear,
21
      # maybe GC could be more aggressive?
22
      # buf.clear
23
    }
24
    puts dig.hexdigest
25
  end
26
end