Class: Excon::Middleware::ResponseParser

Inherits:
Base
  • Object
show all
Defined in:
lib/excon/middlewares/response_parser.rb

Instance Method Summary collapse

Methods inherited from Base

#error_call, #initialize, #request_call

Constructor Details

This class inherits a constructor from Excon::Middleware::Base

Instance Method Details

#response_call(datum) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/excon/middlewares/response_parser.rb', line 4

def response_call(datum)
  unless datum.has_key?(:response)
    datum = Excon::Response.parse(datum[:connection].send(:socket), datum)

    # only requests without a :response_block add 'deflate, gzip' to the TE header.
    unless datum[:response_block]
      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
          if encoding.casecmp('deflate') == 0
            # assume inflate omits header
            datum[:response][:body] = Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(datum[:response][:body])
            encodings.pop
          elsif encoding.casecmp('gzip') == 0 || encoding.casecmp('x-gzip') == 0
            datum[:response][:body] = Zlib::GzipReader.new(StringIO.new(datum[:response][:body])).read
            encodings.pop
          end
          datum[:response][:headers][key] = encodings.join(', ')
        end
      end
    end
  end
  @stack.response_call(datum)
end