lib/excon/response.rb in excon-0.38.0 vs lib/excon/response.rb in excon-0.39.0

- old
+ new

@@ -35,39 +35,42 @@ @data[:local_address] 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 + until status = socket.readline[9,11].to_i + end datum[:response] = { :body => '', :headers => Excon::Headers.new, - :status => status, - :remote_ip => socket.respond_to?(:remote_ip) && socket.remote_ip, - :local_port => socket.respond_to?(:local_port) && socket.local_port, - :local_address => socket.respond_to?(:local_address) && socket.local_address + :status => status } + unless datum[:scheme] == UNIX + datum[:response].merge!( + :remote_ip => socket.remote_ip, + :local_port => socket.local_port, + :local_address => socket.local_address + ) + end + parse_headers(socket, datum) unless (['HEAD', 'CONNECT'].include?(datum[:method].to_s.upcase)) || NO_ENTITY.include?(datum[:response][:status]) if key = datum[:response][:headers].keys.detect {|k| k.casecmp('Transfer-Encoding') == 0 } encodings = Utils.split_header_value(datum[:response][:headers][key]) if (encoding = encodings.last) && encoding.casecmp('chunked') == 0 transfer_encoding_chunked = true - encodings.pop - datum[:response][:headers][key] = encodings.join(', ') + if encodings.length == 1 + datum[:response][:headers].delete(key) + else + datum[:response][:headers][key] = encodings[0...-1].join(', ') + end end end - unless transfer_encoding_chunked - if key = datum[:response][:headers].keys.detect {|k| k.casecmp('Content-Length') == 0 } - content_length = datum[:response][:headers][key].to_i - end - end # use :response_block unless :expects would fail if response_block = datum[:response_block] if datum[:middlewares].include?(Excon::Middleware::Expects) && datum[:expects] && !Array(datum[:expects]).include?(datum[:response][:status]) @@ -100,32 +103,38 @@ new_line_size -= socket.read(new_line_size).length end end end parse_headers(socket, datum) # merge trailers into headers - elsif remaining = content_length - if response_block - while remaining > 0 - chunk = socket.read([datum[:chunk_size], remaining].min) - response_block.call(chunk, [remaining - chunk.bytesize, 0].max, content_length) - remaining -= chunk.bytesize - end - else - while remaining > 0 - chunk = socket.read([datum[:chunk_size], remaining].min) - datum[:response][:body] << chunk - remaining -= chunk.bytesize - end - end else - if response_block - while chunk = socket.read(datum[:chunk_size]) - response_block.call(chunk, nil, nil) + if key = datum[:response][:headers].keys.detect {|k| k.casecmp('Content-Length') == 0 } + content_length = datum[:response][:headers][key].to_i + end + + if remaining = content_length + if response_block + while remaining > 0 + chunk = socket.read([datum[:chunk_size], remaining].min) + response_block.call(chunk, [remaining - chunk.bytesize, 0].max, content_length) + remaining -= chunk.bytesize + end + else + while remaining > 0 + chunk = socket.read([datum[:chunk_size], remaining].min) + datum[:response][:body] << chunk + remaining -= chunk.bytesize + end end else - while chunk = socket.read(datum[:chunk_size]) - datum[:response][:body] << chunk + if response_block + while chunk = socket.read(datum[:chunk_size]) + response_block.call(chunk, nil, nil) + end + else + while chunk = socket.read(datum[:chunk_size]) + datum[:response][:body] << chunk + end end end end end datum @@ -166,9 +175,13 @@ end def params Excon.display_warning('Excon::Response#params is deprecated use Excon::Response#data instead.') data + end + + def pp + Excon::PrettyPrinter.pp($stdout, @data) end # Retrieve a specific header value. Header names are treated case-insensitively. # @param [String] name Header name def get_header(name)