about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorzedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9>2006-10-24 23:22:16 +0000
committerzedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9>2006-10-24 23:22:16 +0000
commit6b55d11c08daafc8e4ec84fb04efc29c8adc7bd8 (patch)
treee15c7e88edd137bc0533fcf6e02e73030d20a1ca /lib
parenta75e81b14f14dfc935e18dcddcaca59179e2e625 (diff)
downloadunicorn-6b55d11c08daafc8e4ec84fb04efc29c8adc7bd8.tar.gz
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@366 19e92222-5c0b-0410-8929-a290d50e31e9
Diffstat (limited to 'lib')
-rw-r--r--lib/mongrel/handlers.rb24
1 files changed, 18 insertions, 6 deletions
diff --git a/lib/mongrel/handlers.rb b/lib/mongrel/handlers.rb
index fb32d2e..d19f3bb 100644
--- a/lib/mongrel/handlers.rb
+++ b/lib/mongrel/handlers.rb
@@ -282,6 +282,7 @@ module Mongrel
   # Valid option is :always_deflate => false which tells the handler to
   # deflate everything even if the client can't handle it.
   class DeflateFilter < HttpHandler
+    include Zlib
     HTTP_ACCEPT_ENCODING = "HTTP_ACCEPT_ENCODING"
 
     def initialize(ops={})
@@ -294,14 +295,25 @@ module Mongrel
       # only process if they support compression
       if @always_deflate or (accepts and (accepts.include? "deflate" and not response.body_sent))
         response.header["Content-Encoding"] = "deflate"
-        # we can't just rewind the body and gzip it since the body could be an attached file
-        response.body.rewind
-        gzout = StringIO.new(Zlib::Deflate.deflate(response.body.read))
-        gzout.rewind
-        response.body.close
-        response.body = gzout
+        response.body = deflate(response.body)
       end
     end
+
+    private
+      def deflate(stream)
+        deflater = Deflate.new(
+          DEFAULT_COMPRESSION,
+          # drop the zlib header which causes both Safari and IE to choke
+          -MAX_WBITS,
+          DEF_MEM_LEVEL,
+          DEFAULT_STRATEGY)
+
+        stream.rewind
+        gzout = StringIO.new(deflater.deflate(stream.read, FINISH))
+        stream.close
+        gzout.rewind
+        gzout
+      end
   end