From 1467b66a0a18091efe71889c4dfe7f57bb9b3e04 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Sun, 8 Apr 2007 15:29:36 +0000 Subject: [PATCH] HttpUtil: decode gzipped content --- lib/rbot/core/utils/httputil.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/rbot/core/utils/httputil.rb b/lib/rbot/core/utils/httputil.rb index 318c5483..15586dc1 100644 --- a/lib/rbot/core/utils/httputil.rb +++ b/lib/rbot/core/utils/httputil.rb @@ -21,6 +21,10 @@ rescue LoadError => e error "Secured HTTP connections will fail" end +# To handle Gzipped pages +require 'stringio' +require 'zlib' + module ::Net class HTTPResponse attr_accessor :no_cache @@ -66,8 +70,21 @@ module ::Net return str end + def decompress_body(str) + method = self['content-encoding'] + case method + when nil + return str + when 'gzip', 'x-gzip' + debug "gunzipping body" + return Zlib::GzipReader.new(StringIO.new(str)).read + else + raise "Unhandled content encoding #{method}" + end + end + def body - return self.body_to_utf(self.raw_body) + return self.body_to_utf(self.decompress_body(self.raw_body)) end # Read chunks from the body until we have at least _size_ bytes, yielding -- 2.39.2