Hello! What is the simplest way to make merb respond to the client sending Accept-Encoding: gzip? Just checking if it exists, otherwise I''ll implement it myself. Regards, Magnus
On Mar 8, 2007, at 9:51 AM, Magnus Naeslund wrote:> Hello! > What is the simplest way to make merb respond to the client sending > Accept-Encoding: gzip? > Just checking if it exists, otherwise I''ll implement it myself. > > Regards, > MagnusHey Magnus- The easiest way would be to use the mongrel deflate handler. You would want to add a config option for it and then at the bottom of merb_server.rb where I mount the mongrel handlers you woudl mount the deflate handler. Cheers- -- Ezra Zygmuntowicz -- Lead Rails Evangelist -- ez at engineyard.com -- Engine Yard, Serious Rails Hosting -- (866) 518-YARD (9273)
Ezra Zygmuntowicz wrote:> > Hey Magnus- > > The easiest way would be to use the mongrel deflate handler. You > would want to add a config option for it and then at the bottom of > merb_server.rb where I mount the mongrel handlers you woudl mount the > deflate handler. >Excellent! Magnus
Ezra Zygmuntowicz wrote:> Hey Magnus- > > The easiest way would be to use the mongrel deflate handler. You > would want to add a config option for it and then at the bottom of > merb_server.rb where I mount the mongrel handlers you woudl mount the > deflate handler. >I patched merb so that I can add additional handlers in merb_init.rb, see patch below. In merb_init.rb i do: Merb::Server.add_handler(:uri => ''/'', :handler => GzipFilter.new, :in_front => true) If I don''t have the :in_front thing nothing happens... The problem is that when i test it, it doesn''t compress the output, it only sets the header: # echo -en "GET /xxx HTTP/1.0\r\nAccept-Encoding: gzip,deflate\r\n\r\n" | nc -v localhost 4000 | strings localhost [127.0.0.1] 4000 (?) open HTTP/1.1 200 OK Connection: close Date: Sat, 17 Mar 2007 11:45:10 GMT Content-Encoding: gzip Content-Type: text/html Content-Length: 5 hello That''s a bit strange, right? Is there any special way I should prepare the body more than returning a string from the controllers action? Magnus
Magnus Naeslund wrote: [snip]> I patched merb so that I can add additional handlers in merb_init.rb, see patch below.[snip] Maybe I should attach the patch aswell :) Magnus Index: lib/merb/merb_server.rb ==================================================================--- lib/merb/merb_server.rb (revision 198) +++ lib/merb/merb_server.rb (working copy) @@ -284,6 +284,11 @@ 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("") + if @@additional_handlers + @@additional_handlers.each do |h| + uri(h[:uri], :handler => h[:handler], :in_front => (h[:in_front] || false)) + end + end end trap("INT") { stop } @@ -295,7 +300,12 @@ def config @@merb_opts end - + + def add_handler(h) + @@additional_handlers ||= [] + @@additional_handlers << h + end + end end # Server
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
Magnus Naeslund wrote:> > 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 >Remember to user deflater.close before returning, if someone copies this code :) Regards, Magnus - butterfingers