Skip to content

Commit bc1bfa0

Browse files
committed
Add support for Body#discard + 100% test coverage.
1 parent 292c54e commit bc1bfa0

File tree

5 files changed

+62
-28
lines changed

5 files changed

+62
-28
lines changed

lib/protocol/http1/body/chunked.rb

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff 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:

lib/protocol/http1/body/fixed.rb

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff 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:

lib/protocol/http1/body/remainder.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff 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

test/protocol/http1/body/chunked.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,13 @@
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
8694
end

test/protocol/http1/body/fixed.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@
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
@@ -88,4 +89,14 @@
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
91102
end

0 commit comments

Comments
 (0)