Class: Excon::Middleware::Decompress

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

Instance Method Summary collapse

Methods inherited from Base

#error_call, #initialize

Constructor Details

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

Instance Method Details

#request_call(datum) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/excon/middlewares/decompress.rb', line 5

def request_call(datum)
  unless datum.has_key?(:response_block)
    key = datum[:headers].keys.detect {|k| k.to_s.casecmp('Accept-Encoding') == 0 } || 'Accept-Encoding'
    if datum[:headers][key].to_s.empty?
      datum[:headers][key] = 'deflate, gzip'
    end
  end
  @stack.request_call(datum)
end

#response_call(datum) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/excon/middlewares/decompress.rb', line 15

def response_call(datum)
  body = datum[:response][:body]
  unless datum.has_key?(:response_block) || body.nil? || body.empty?
    if key = datum[:response][:headers].keys.detect {|k| k.casecmp('Content-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(body)
          encodings.pop
        elsif encoding.casecmp('gzip') == 0 || encoding.casecmp('x-gzip') == 0
          datum[:response][:body] = Zlib::GzipReader.new(StringIO.new(body)).read
          encodings.pop
        end
        datum[:response][:headers][key] = encodings.join(', ')
      end
    end
  end
  @stack.response_call(datum)
end