]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
HttpUtil: decode gzipped content
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 8 Apr 2007 15:29:36 +0000 (15:29 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 8 Apr 2007 15:29:36 +0000 (15:29 +0000)
lib/rbot/core/utils/httputil.rb

index 318c5483a863a01cdab9b5eb7ccfe6ec0b99a30f..15586dc169d07a97e1512e2ba04233d039af1545 100644 (file)
@@ -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