Skip to content

Commit fc99690

Browse files
committed
Ensure inflate reads all chunks when closing.
1 parent 1c63676 commit fc99690

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

lib/protocol/http/body/buffered.rb

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def read
9393
end
9494

9595
def discard
96+
# It's safe to call close here because there is no underlying stream to close:
9697
self.close
9798
end
9899

lib/protocol/http/body/deflate.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ def initialize(body, stream)
3131
end
3232

3333
def close(error = nil)
34-
@stream.close unless @stream.closed?
34+
if stream = @stream
35+
@stream = nil
36+
stream.close unless stream.closed?
37+
end
3538

3639
super
3740
end

lib/protocol/http/body/inflate.rb

+30-20
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,40 @@ def self.for(body, encoding = GZIP)
1616
end
1717

1818
def read
19-
return if @stream.finished?
19+
if stream = @stream
20+
# Read from the underlying stream and inflate it:
21+
while chunk = super
22+
@input_length += chunk.bytesize
23+
24+
# It's possible this triggers the stream to finish.
25+
chunk = stream.inflate(chunk)
26+
27+
break unless chunk&.empty?
28+
end
2029

21-
# The stream might have been closed while waiting for the chunk to come in.
22-
while chunk = super
23-
@input_length += chunk.bytesize
30+
if chunk
31+
@output_length += chunk.bytesize
32+
elsif !stream.closed?
33+
chunk = stream.finish
34+
@output_length += chunk.bytesize
35+
end
2436

25-
# It's possible this triggers the stream to finish.
26-
chunk = @stream.inflate(chunk)
37+
# If the stream is finished, we need to close it and potentially return nil:
38+
if stream.finished?
39+
@stream = nil
40+
stream.close
41+
42+
while super
43+
# There is data left in the stream, so we need to keep reading until it's all consumed.
44+
end
45+
46+
if chunk.empty?
47+
return nil
48+
end
49+
end
2750

28-
break unless chunk&.empty?
51+
return chunk
2952
end
30-
31-
if chunk
32-
@output_length += chunk.bytesize
33-
elsif !@stream.closed?
34-
chunk = @stream.finish
35-
@output_length += chunk.bytesize
36-
end
37-
38-
if chunk.empty? and @stream.finished?
39-
return nil
40-
end
41-
42-
return chunk
4353
end
4454
end
4555
end

0 commit comments

Comments
 (0)