Transfer-encoding: gzip

Magnus Naeslund
Sun, 18 Mar 2007 22:31:09 -0000

I gave up the idea to use the mongrel handlers for compression, and am using
this kind of construct instead:

The action returns stuff like this:
return compress_if_possible(result)

And the utility method defined as:

    def compress_if_possible(str)
      enc = request.env[HTTP_ACCEPT_ENCODING]
      return str if enc.nil? or enc.include?(''deflate'') ==
false
      headers[''Content-Encoding''] =
''deflate''

      # Shamelessly stolen from mongrels deflate filter
		  deflater = Zlib::Deflate.new(
		    Zlib::DEFAULT_COMPRESSION,
		    # drop the zlib header which causes both Safari and IE to choke
        -(Zlib::MAX_WBITS),
        Zlib::DEF_MEM_LEVEL,
        Zlib::DEFAULT_STRATEGY)

      return deflater.deflate(str, Zlib::FINISH)
    end

I''m ok with that ugliness :)

Anyways someone else might want to use additional handlers so here''s my
respun patch, feel free to include it if you like, Ezra.

Index: lib/merb/merb_server.rb
==================================================================---
lib/merb/merb_server.rb     (revision 198)
+++ lib/merb/merb_server.rb     (working copy)
@@ -140,6 +140,7 @@
       end

       def run
+        @@additional_handlers = []
         @@merb_raw_opts = ARGV
         merb_config

@@ -284,6 +285,9 @@
             uri( "/", :handler => MerbUploadHandler.new(yconfig),
:in_front => true) if @@merb_opts[:config]
             uri "/", :handler =>
MerbHandler.new(@@merb_opts[:dist_root]+''/public'')
             uri "/favicon.ico", :handler =>
Mongrel::Error404Handler.new("")
+           @@additional_handlers.each do |h|
+             uri(h[:uri], :handler => h[:handler], :in_front =>
(h[:in_front] || false))
+           end
           end

           trap("INT") { stop }
@@ -295,7 +299,11 @@
       def config
         @@merb_opts
       end
-
+
+      def add_handler(h)
+       @@additional_handlers << h
+      end
+
     end

   end # Server