Skip to content

Commit 800c581

Browse files
jankotarcieri
authored andcommitted
Fix #readpartial not respecting max length argument
1 parent 1469007 commit 800c581

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/http/connection.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ def send_request(req)
8686
def readpartial(size = BUFFER_SIZE)
8787
return unless @pending_response
8888

89+
chunk = @parser.get_chunk(size)
90+
return chunk if chunk
91+
8992
finished = (read_more(size) == :eof) || @parser.finished?
9093
chunk = @parser.chunk
91-
9294
finish_response if finished
9395

9496
chunk.to_s

lib/http/response/parser.rb

+12-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module HTTP
44
class Response
55
class Parser
6-
attr_reader :headers
6+
attr_reader :headers, :chunk
77

88
def initialize
99
@parser = HTTP::Parser.new(self)
@@ -43,9 +43,17 @@ def on_body(chunk)
4343
end
4444
end
4545

46-
def chunk
47-
chunk = @chunk
48-
@chunk = nil
46+
def get_chunk(size)
47+
return if @chunk.nil?
48+
49+
if @chunk.bytesize <= size
50+
chunk = @chunk
51+
@chunk = nil
52+
else
53+
chunk = @chunk.byteslice(0, size)
54+
@chunk[0, size] = ""
55+
end
56+
4957
chunk
5058
end
5159

0 commit comments

Comments
 (0)