lib/excon/response.rb in excon-0.28.0 vs lib/excon/response.rb in excon-0.29.0
- old
+ new
@@ -28,17 +28,20 @@
def remote_ip
@data[:remote_ip]
end
def self.parse(socket, datum)
+ # this will discard any trailing lines from the previous response if any.
+ until match = /^HTTP\/\d+\.\d+\s(\d{3})\s/.match(socket.readline); end
+ status = match[1].to_i
+
datum[:response] = {
:body => '',
:headers => {},
- :status => socket.read(12)[9, 11].to_i,
+ :status => status,
:remote_ip => socket.respond_to?(:remote_ip) && socket.remote_ip
}
- socket.readline # read the rest of the status line and CRLF
until ((data = socket.readline).chop!).empty?
key, value = data.split(/:\s*/, 2)
datum[:response][:headers][key] = ([*datum[:response][:headers][key]] << value).compact.join(', ')
if key.casecmp('Content-Length') == 0
@@ -54,15 +57,15 @@
expected_status = !datum.has_key?(:expects) || [*datum[:expects]].include?(datum[:response][:status])
# if expects matched and there is a block, use it
if expected_status && datum.has_key?(:response_block)
if transfer_encoding_chunked
- # 2 == "/r/n".length
+ # 2 == "\r\n".length
while (chunk_size = socket.readline.chop!.to_i(16)) > 0
datum[:response_block].call(socket.read(chunk_size + 2).chop!, nil, nil)
end
- socket.read(2)
+ socket.read(2) # empty chunk-body
elsif remaining = content_length
while remaining > 0
datum[:response_block].call(socket.read([datum[:chunk_size], remaining].min), [remaining - datum[:chunk_size], 0].max, content_length)
remaining -= datum[:chunk_size]
end
@@ -71,14 +74,15 @@
datum[:response_block].call(remaining, remaining.length, content_length)
end
end
else # no block or unexpected status
if transfer_encoding_chunked
+ # 2 == "\r\n".length
while (chunk_size = socket.readline.chop!.to_i(16)) > 0
- datum[:response][:body] << socket.read(chunk_size + 2).chop! # 2 == "/r/n".length
+ datum[:response][:body] << socket.read(chunk_size + 2).chop!
end
- socket.read(2) # 2 == "/r/n".length
+ socket.read(2) # empty chunk-body
elsif remaining = content_length
while remaining > 0
datum[:response][:body] << socket.read([datum[:chunk_size], remaining].min)
remaining -= datum[:chunk_size]
end
@@ -104,10 +108,10 @@
def [](key)
@data[key]
end
def params
- Excon.display_warning("Excon::Response#params is deprecated use Excon::Response#data instead (#{caller.first})")
+ Excon.display_warning('Excon::Response#params is deprecated use Excon::Response#data instead.')
data
end
# Retrieve a specific header value. Header names are treated case-insensitively.
# @param [String] name Header name