Skip to content

Commit

Permalink
Satisfy IO#readpartial API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
singpolyma-shopify committed Sep 22, 2020
1 parent a0f540f commit 8d362d7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/http/connection.rb
Expand Up @@ -83,17 +83,19 @@ def send_request(req)
#
# @return [String] data chunk
# @return [nil] when no more data left
def readpartial(size = BUFFER_SIZE)
def readpartial(size = BUFFER_SIZE, outbuf = nil)
return unless @pending_response

chunk = @parser.read(size)
return chunk if chunk

finished = (read_more(size) == :eof) || @parser.finished?
chunk = @parser.read(size)
finish_response if finished
unless chunk
finished = (read_more(size) == :eof) || @parser.finished?
chunk = @parser.read(size)
finish_response if finished
end

chunk || "".b
chunk ||= "".b
outbuf ? outbuf.replace(chunk) : chunk
end

# Reads data from socket up until headers are loaded
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/http/connection_spec.rb
Expand Up @@ -62,5 +62,13 @@
end
expect(buffer).to eq "1234567890"
end

it "fill outbuf when present" do
connection.read_headers!
outbuf = String.new
buffer = String.new
buffer << outbuf while connection.readpartial(2, outbuf)
expect(buffer).to eq "1234567890"
end
end
end

0 comments on commit 8d362d7

Please sign in to comment.