diff options
author | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2007-04-08 15:29:36 +0000 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2007-04-08 15:29:36 +0000 |
commit | 1467b66a0a18091efe71889c4dfe7f57bb9b3e04 (patch) | |
tree | ec70f54c19d98b5ebbff2a94d594113232c502c9 /lib | |
parent | 15c0b674d60d719c19f4ebeefca33664d03d16ad (diff) |
HttpUtil: decode gzipped content
Diffstat (limited to 'lib')
-rw-r--r-- | lib/rbot/core/utils/httputil.rb | 19 |
1 files changed, 18 insertions, 1 deletions
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 |