2 # Rails output compression filters 
 
   4 # Adds two classmethods to ActionController that can be used as after-filters: 
 
   5 # strip_whitespace and compress_output. 
 
   6 # If you use page-caching, you MUST specify the compress_output filter AFTER 
 
   7 # caches_page, otherwise the compressed data will be cached instead of the HTML 
 
   9 # class MyController < ApplicationController 
 
  10 #  after_filter :strip_whitespace 
 
  12 #  after_filter :compress_output 
 
  20   GZIP_SUPPORTED = false 
 
  23 module CompressionSystem 
 
  25     return unless accepts_gzip?
 
  28       # Zlib does a close. Bad Zlib... 
 
  31     gz = Zlib::GzipWriter.new(output) 
 
  32     gz.write(response.body) 
 
  34     if output.length < response.body.length 
 
  35       @old_response_body = response.body 
 
  36       response.body = output.string 
 
  37       response.headers['Content-encoding'] = @compression_encoding 
 
  42     return false unless GZIP_SUPPORTED 
 
  43     accepts = request.env['HTTP_ACCEPT_ENCODING'] 
 
  44     return false unless accepts && accepts =~ /(x-gzip|gzip)/ 
 
  45     @compression_encoding = $1 
 
  50     response.body.gsub!(/()|(.*?<\/script>)|()|()|\s+/m) do |m| 
 
  51       if m =~ /^()(.*?)<\/script>$/m 
 
  52         $1 + $2.strip.gsub(/\s+/, ' ').gsub('',"\n-->") + '' 
 
  55       elsif m =~ /^<(textarea|pre)/ 
 
  60     response.body.gsub! /\s+\s+/, '>' 
 
  64 class ActionController::Base 
 
  65   include CompressionSystem