From e6e7c3a058e38db93330c68ebea9564c753075b6 Mon Sep 17 00:00:00 2001 From: zedshaw Date: Tue, 23 May 2006 06:40:24 +0000 Subject: Implemented a DeflateFilter for dynamic gzip of responses. git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@208 19e92222-5c0b-0410-8929-a290d50e31e9 --- Rakefile | 4 ++-- examples/simpletest.rb | 22 +++++++++------------- lib/mongrel.rb | 1 + lib/mongrel/handlers.rb | 10 +++++----- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/Rakefile b/Rakefile index acb2d0a..cc371e9 100644 --- a/Rakefile +++ b/Rakefile @@ -40,8 +40,8 @@ task :site_rdoc do sh %{ scp -r doc/rdoc/* #{ENV['SSH_USER']}@rubyforge.org:/var/www/gforge-projects/mongrel/rdoc/ } end -task :site_coverage => [:test_units_with_coverage] do - sh %{ scp -r test/coverage/unit/* #{ENV['SSH_USER']}@rubyforge.org:/var/www/gforge-projects/mongrel/coverage/ } +task :site_coverage => [:rcov] do + sh %{ scp -r test/coverage/* #{ENV['SSH_USER']}@rubyforge.org:/var/www/gforge-projects/mongrel/coverage/ } end task :site_projects_rdoc do diff --git a/examples/simpletest.rb b/examples/simpletest.rb index fbad8f4..8ac2e98 100644 --- a/examples/simpletest.rb +++ b/examples/simpletest.rb @@ -1,22 +1,16 @@ +$LOAD_PATH << File.join(File.dirname(__FILE__), "..", "lib") require 'mongrel' require 'yaml' require 'zlib' class SimpleHandler < Mongrel::HttpHandler - def process(request, response) - response.start do |head,out| - head["Content-Type"] = "text/html" - results = "Your request:
#{request.params.to_yaml}
View the files." - if request.params["HTTP_ACCEPT_ENCODING"] == "gzip,deflate" - head["Content-Encoding"] = "deflate" - # send it back deflated - out << Zlib::Deflate.deflate(results) - else - # no gzip supported, send it back normal - out << results - end - end + def process(request, response) + response.start do |head,out| + head["Content-Type"] = "text/html" + results = "Your request:
#{request.params.to_yaml}
View the files." + out << results end + end end class DumbHandler < Mongrel::HttpHandler @@ -37,7 +31,9 @@ end config = Mongrel::Configurator.new :host => ARGV[0], :port => ARGV[1] do listener do uri "/", :handler => SimpleHandler.new + uri "/", :handler => Mongrel::DeflateFilter.new uri "/dumb", :handler => DumbHandler.new + uri "/dumb", :handler => Mongrel::DeflateFilter.new uri "/files", :handler => Mongrel::DirHandler.new(ARGV[2]) end diff --git a/lib/mongrel.rb b/lib/mongrel.rb index 599bf2b..9165f0a 100644 --- a/lib/mongrel.rb +++ b/lib/mongrel.rb @@ -319,6 +319,7 @@ module Mongrel class HttpResponse attr_reader :socket attr_reader :body + attr_writer :body attr_reader :header attr_reader :status attr_writer :status diff --git a/lib/mongrel/handlers.rb b/lib/mongrel/handlers.rb index 1134bdf..0e34a15 100644 --- a/lib/mongrel/handlers.rb +++ b/lib/mongrel/handlers.rb @@ -269,21 +269,21 @@ module Mongrel # When added to a config script (-S in mongrel_rails) it will # look at the client's allowed response types and then gzip # compress anything that is going out. - class DeflateCompressFilter < HttpHandler + class DeflateFilter < HttpHandler HTTP_ACCEPT_ENCODING = "HTTP_ACCEPT_ENCODING" - def initialize(ops) + def initialize(ops={}) @options = ops end def process(request, response) accepts = request.params[HTTP_ACCEPT_ENCODING] # only process if they support compression - if accepts.include? "gzip" or accepts.include? "deflate" and not response.body_sent - head["Content-Encoding"] = "deflate" + if 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 << Zlib::Deflate.deflate(response.body.read) + gzout = StringIO.new(Zlib::Deflate.deflate(response.body.read)) gzout.rewind response.body.close response.body = gzout -- cgit v1.2.3-24-ge0c7