File tree Expand file tree Collapse file tree 5 files changed +62
-28
lines changed Expand file tree Collapse file tree 5 files changed +62
-28
lines changed Original file line number Diff line number Diff line change @@ -26,15 +26,19 @@ def empty?
2626 @stream . nil?
2727 end
2828
29- def close ( error = nil )
30- if @stream
29+ def discard
30+ if stream = @stream
31+ @stream = nil
32+
3133 # We only close the connection if we haven't completed reading the entire body:
3234 unless @finished
33- @ stream. close_read
35+ stream . close_read
3436 end
35-
36- @stream = nil
3737 end
38+ end
39+
40+ def close ( error = nil )
41+ self . discard
3842
3943 super
4044 end
@@ -67,13 +71,18 @@ def read
6771 # Read trailing CRLF:
6872 chunk = @stream . read ( length + 2 )
6973
70- # ...and chomp it off:
71- chunk . chomp! ( CRLF )
72-
73- @length += length
74- @count += 1
75-
76- return chunk
74+ if chunk . bytesize == length + 2
75+ # ...and chomp it off:
76+ chunk . chomp! ( CRLF )
77+
78+ @length += length
79+ @count += 1
80+
81+ return chunk
82+ else
83+ # The stream has been closed before we have read the requested length:
84+ self . discard
85+ end
7786 end
7887
7988 # If the stream has been closed before we have read the final chunk, raise an error:
Original file line number Diff line number Diff line change @@ -23,15 +23,18 @@ def empty?
2323 @stream . nil? or @remaining == 0
2424 end
2525
26- def close ( error = nil )
27- if @stream
28- # If we are closing the body without fully reading it, the underlying connection is now in an undefined state.
26+ def discard
27+ if stream = @stream
28+ @stream = nil
29+
2930 if @remaining != 0
30- @ stream. close_read
31+ stream . close_read
3132 end
32-
33- @stream = nil
3433 end
34+ end
35+
36+ def close ( error = nil )
37+ self . discard
3538
3639 super
3740 end
@@ -41,11 +44,11 @@ def read
4144 if @remaining > 0
4245 if @stream
4346 # `readpartial` will raise `EOFError` if the stream is finished, or `IOError` if the stream is closed.
44- if chunk = @stream . readpartial ( @remaining )
45- @remaining -= chunk . bytesize
46-
47- return chunk
48- end
47+ chunk = @stream . readpartial ( @remaining )
48+
49+ @remaining -= chunk . bytesize
50+
51+ return chunk
4952 end
5053
5154 # If the stream has been closed before we have read the expected length, raise an error:
Original file line number Diff line number Diff line change @@ -21,12 +21,15 @@ def empty?
2121 @stream . nil?
2222 end
2323
24- def close ( error = nil )
25- if @stream
26- # We can't really do anything in this case except close the connection.
27- @stream . close_read
24+ def discard
25+ if stream = @stream
2826 @stream = nil
27+ stream . close_read
2928 end
29+ end
30+
31+ def close ( error = nil )
32+ self . discard
3033
3134 super
3235 end
Original file line number Diff line number Diff line change 8282 expect { body . read } . to raise_exception ( Protocol ::HTTP1 ::BadHeader )
8383 end
8484 end
85+
86+ with "invalid content length" do
87+ let ( :buffer ) { StringIO . new ( "#{ ( content . bytesize + 1 ) . to_s ( 16 ) } \r \n #{ content } " ) }
88+
89+ it "raises error" do
90+ expect { body . read } . to raise_exception ( EOFError )
91+ end
92+ end
8593 end
8694end
Original file line number Diff line number Diff line change 6464 let ( :body ) { subject . new ( buffer , 20 ) }
6565
6666 it "retrieves content up to provided length" do
67+ body . read
68+
6769 expect do
6870 body . read
69- body . read
7071 end . to raise_exception ( EOFError )
7172 end
7273 end
8889 )
8990 end
9091 end
92+
93+ with "#discard" do
94+ it "causes #read to raise EOFError" do
95+ body . discard
96+
97+ expect do
98+ body . read
99+ end . to raise_exception ( EOFError )
100+ end
101+ end
91102end
You can’t perform that action at this time.
0 commit comments