Class: WWW::Mechanize::Chain::BodyDecodingHandler

Inherits:
Object
  • Object
show all
Includes:
Handler
Defined in:
lib/www/mechanize/chain/body_decoding_handler.rb

Instance Attribute Summary

Attributes included from Handler

#chain

Instance Method Summary collapse

Instance Method Details

#handle(ctx, options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/www/mechanize/chain/body_decoding_handler.rb', line 7

def handle(ctx, options)
  body = options[:response_body]
  response = options[:response]

  options[:response_body] = 
    if encoding = response['Content-Encoding']
      case encoding.downcase
      when 'gzip'
        Mechanize.log.debug('gunzip body') if Mechanize.log
        if response['Content-Length'].to_i > 0 || body.length > 0
          begin
            Zlib::GzipReader.new(body).read
          rescue Zlib::BufError, Zlib::GzipFile::Error
            if Mechanize.log
              Mechanize.log.error('Caught a Zlib::BufError')
            end
            body.rewind
            body.read(10)
            Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(body.read)
          end
        else
          ''
        end
      when 'x-gzip'
        body.read
      else
        raise 'Unsupported content encoding'
      end
    else
      body.read
    end
  super
end