Sha256: 9ccbc10692d76e8bd5ea68f73b722fbabe1749003b6c77edc075cb0b7774ffab

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

class Mechanize
  class Chain
    class BodyDecodingHandler
      include Mechanize::Handler

      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)
                rescue Zlib::DataError
                  if Mechanize.log
                    Mechanize.log.error("Caught a Zlib::DataError, unable to decode page: #{$!.to_s}")
                  end
                  ''
                end
              else
                ''
              end
            when 'x-gzip'
              body.read
            else
              raise 'Unsupported content encoding'
            end
          else
            body.read
          end
        super
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
kitamomonga-mechanize-0.9.3.20090724215219 lib/mechanize/chain/body_decoding_handler.rb
tenderlove-mechanize-0.9.3.20090911221705 lib/mechanize/chain/body_decoding_handler.rb