7: def handle(ctx, options)
8: body = options[:response_body]
9: response = options[:response]
10:
11: options[:response_body] =
12: if encoding = response['Content-Encoding']
13: case encoding.downcase
14: when 'gzip'
15: Mechanize.log.debug('gunzip body') if Mechanize.log
16: if response['Content-Length'].to_i > 0 || body.length > 0
17: begin
18: Zlib::GzipReader.new(body).read
19: rescue Zlib::BufError, Zlib::GzipFile::Error
20: if Mechanize.log
21: Mechanize.log.error('Caught a Zlib::BufError')
22: end
23: body.rewind
24: body.read(10)
25: Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(body.read)
26: rescue Zlib::DataError
27: if Mechanize.log
28: Mechanize.log.error("Caught a Zlib::DataError, unable to decode page: #{$!.to_s}")
29: end
30: ''
31: end
32: else
33: ''
34: end
35: when 'x-gzip'
36: body.read
37: else
38: raise 'Unsupported content encoding'
39: end
40: else
41: body.read
42: end
43: super
44: end