Project

General

Profile

Bug #13882 ยป http_gzip_hang.rb

wjordan (Will Jordan), 05/29/2020 11:41 PM

 
1
# Repro https://bugs.ruby-lang.org/issues/13882 using Net::HTTP
2
# by exiting in the middle of reading gzip-encoded content.
3

    
4
require 'net/http'
5

    
6
HOST = '127.0.0.1'
7
PORT = 1234
8

    
9
server = nil
10
# Server
11
t = Thread.new do
12
  server = TCPServer.new(HOST, PORT)
13
  c = server.accept
14
  c.print "HTTP/1.1 200\r\n"
15
  c.print "Content-Type: text/plain\r\n"
16
  c.print "Content-Encoding: gzip\r\n"
17
  s = StringIO.new
18
  z = Zlib::GzipWriter.new(s)
19
  z.write 'Hello World!'
20
  z.close
21
  c.print "Content-Length: #{s.length}\r\n\r\n"
22

    
23
  # Write partial gzip content
24
  c.write s.string.byteslice(0..-2)
25
end
26

    
27
Thread.pass until server
28

    
29
# Client
30
Thread.new do
31
  begin
32
    puts Net::HTTP.get(HOST, '/', PORT)
33
  rescue Zlib::BufError => e
34
    puts "Caught error: #{e.inspect}"
35
  end
36
  puts "Sleeping"
37
  sleep
38
end
39

    
40
at_exit {puts 'Exiting'}
41

    
42
t.join
43
sleep 0.1