Skip to content

Commit ee2318e

Browse files
Fix handling of zero length reads.
1 parent d8b5f53 commit ee2318e

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

lib/protocol/http/body/stream.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module Reader
5252
# @parameter buffer [String] the buffer which will receive the data
5353
# @returns [String] a buffer containing the data
5454
def read(length = nil, buffer = nil)
55-
return "" if length == 0
55+
return (buffer ? buffer.clear : String.new) if length == 0
5656

5757
buffer ||= String.new.force_encoding(Encoding::BINARY)
5858

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Fix handling of `Stream#read(0)`, it must return a mutable string (or clear the given buffer).
6+
37
## v0.61.0
48

59
- Introduce `Protocol::HTTP::RefusedError` for indicating a stream or request was refused before processing and can be safely retried. `RequestRefusedError` is provided as an alias for backwards compatibility.

test/protocol/http/body/stream.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@
3838
expect(stream.read(0)).to be == ""
3939
end
4040

41+
it "returns a mutable string for zero-length read" do
42+
result = stream.read(0)
43+
expect(result).not.to be(:frozen?)
44+
end
45+
46+
it "populates the buffer on zero-length read" do
47+
buffer = String.new
48+
result = stream.read(0, buffer)
49+
expect(result).to be == ""
50+
expect(result).to be_equal(buffer)
51+
end
52+
4153
it "can read the entire input" do
4254
expect(stream.read).to be == "HelloWorld"
4355
end

0 commit comments

Comments
 (0)