Skip to content

Commit 28b60b8

Browse files
Satisfy IO#readpartial API
IO#readpartial allows for a second "outbuf" param which some streaming usages expect, so support it here to allow using response bodies anywhere IO can be.
1 parent a0f540f commit 28b60b8

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/http/connection.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def send_request(req)
8383
#
8484
# @return [String] data chunk
8585
# @return [nil] when no more data left
86-
def readpartial(size = BUFFER_SIZE)
86+
def readpartial(size = BUFFER_SIZE, outbuf = nil)
8787
return unless @pending_response
8888

8989
chunk = @parser.read(size)
@@ -93,7 +93,8 @@ def readpartial(size = BUFFER_SIZE)
9393
chunk = @parser.read(size)
9494
finish_response if finished
9595

96-
chunk || "".b
96+
chunk ||= "".b
97+
outbuf ? outbuf.replace(chunk) : chunk
9798
end
9899

99100
# Reads data from socket up until headers are loaded

spec/lib/http/connection_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,13 @@
6262
end
6363
expect(buffer).to eq "1234567890"
6464
end
65+
66+
it "fill outbuf when present" do
67+
connection.read_headers!
68+
outbuf = String.new
69+
buffer = String.new
70+
buffer << outbuf while connection.readpartial(3, outbuf)
71+
expect(buffer).to eq "1234567890"
72+
end
6573
end
6674
end

0 commit comments

Comments
 (0)