File tree 3 files changed +35
-21
lines changed
3 files changed +35
-21
lines changed Original file line number Diff line number Diff line change @@ -93,6 +93,7 @@ def read
93
93
end
94
94
95
95
def discard
96
+ # It's safe to call close here because there is no underlying stream to close:
96
97
self . close
97
98
end
98
99
Original file line number Diff line number Diff line change @@ -31,7 +31,10 @@ def initialize(body, stream)
31
31
end
32
32
33
33
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
35
38
36
39
super
37
40
end
Original file line number Diff line number Diff line change @@ -16,30 +16,40 @@ def self.for(body, encoding = GZIP)
16
16
end
17
17
18
18
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
20
29
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
24
36
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
27
50
28
- break unless chunk &. empty?
51
+ return chunk
29
52
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
43
53
end
44
54
end
45
55
end
You can’t perform that action at this time.
0 commit comments