Skip to content

Commit

Permalink
Fix #readpartial not respecting max length argument
Browse files Browse the repository at this point in the history
  • Loading branch information
janko committed Feb 12, 2018
1 parent 145b2bc commit 06e3604
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/http/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ def send_request(req)
def readpartial(size = BUFFER_SIZE)
return unless @pending_response

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

finished = (read_more(size) == :eof) || @parser.finished?
chunk = @parser.chunk

finish_response if finished

chunk.to_s
Expand Down
16 changes: 12 additions & 4 deletions lib/http/response/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module HTTP
class Response
class Parser
attr_reader :headers
attr_reader :headers, :chunk

def initialize
@parser = HTTP::Parser.new(self)
Expand Down Expand Up @@ -43,9 +43,17 @@ def on_body(chunk)
end
end

def chunk
chunk = @chunk
@chunk = nil
def get_chunk(size)
return if @chunk.nil?

if @chunk.bytesize <= size
chunk = @chunk
@chunk = nil
else
chunk = @chunk.byteslice(0, size)
@chunk[0, size] = ""
end

chunk
end

Expand Down

0 comments on commit 06e3604

Please sign in to comment.