~ubuntu-branches/ubuntu/utopic/ruby-excon/utopic

« back to all changes in this revision

Viewing changes to lib/excon/middlewares/response_parser.rb

  • Committer: Package Import Robot
  • Author(s): Praveen Arimbrathodiyil
  • Date: 2014-01-14 18:44:24 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20140114184424-3nx5mqudhzjpcrjs
Tags: 0.31.0-1
* Team upload
* New upstream release
* Refresh patches
* Bump standards version to 3.9.5 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
module Excon
 
2
  module Middleware
 
3
    class ResponseParser < Excon::Middleware::Base
 
4
      def response_call(datum)
 
5
        unless datum.has_key?(:response)
 
6
          datum = Excon::Response.parse(datum[:connection].send(:socket), datum)
 
7
 
 
8
          # only requests without a :response_block add 'deflate, gzip' to the TE header.
 
9
          unless datum[:response_block]
 
10
            if key = datum[:response][:headers].keys.detect {|k| k.casecmp('Transfer-Encoding') == 0 }
 
11
              encodings = Utils.split_header_value(datum[:response][:headers][key])
 
12
              if encoding = encodings.last
 
13
                if encoding.casecmp('deflate') == 0
 
14
                  # assume inflate omits header
 
15
                  datum[:response][:body] = Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(datum[:response][:body])
 
16
                  encodings.pop
 
17
                elsif encoding.casecmp('gzip') == 0 || encoding.casecmp('x-gzip') == 0
 
18
                  datum[:response][:body] = Zlib::GzipReader.new(StringIO.new(datum[:response][:body])).read
 
19
                  encodings.pop
 
20
                end
 
21
                datum[:response][:headers][key] = encodings.join(', ')
 
22
              end
 
23
            end
 
24
          end
 
25
        end
 
26
        @stack.response_call(datum)
 
27
      end
 
28
    end
 
29
  end
 
30
end