1 module ActionController
\r
5 module ClassMethods #:nodoc:
\r
6 def caches_gzip_action(*actions)
\r
7 around_filter(GzipActionCacheFilter.new(perform_caching, *actions))
\r
11 class GzipActionCacheFilter #:nodoc:
\r
15 GZIP_SUPPORTED = true
\r
17 GZIP_SUPPORTED = false
\r
20 def initialize(perform_caching, *actions, &block)
\r
22 @perform_caching = perform_caching
\r
25 def before(controller)
\r
26 return unless @actions.include?(controller.action_name.intern)
\r
29 if cache = controller.read_fragment(controller.url_for.split("://").last)
\r
30 controller.rendered_action_cache = true
\r
32 if accepts_gzip?(controller)
\r
33 cache = compress_output(cache)
\r
34 controller.response.headers['Content-encoding'] = @compression_encoding
\r
37 controller.send(:render_text, cache)
\r
40 elsif accepts_gzip?(controller)
\r
41 controller.response.body = compress_output(controller.response.body)
\r
45 def after(controller)
\r
46 return if !@perform_caching || !@actions.include?(controller.action_name.intern) || controller.rendered_action_cache
\r
47 controller.write_fragment(controller.url_for.split("://").last, controller.response.body)
\r
52 def accepts_gzip?(controller)
\r
53 return false unless GZIP_SUPPORTED
\r
54 accepts = controller.request.env['HTTP_ACCEPT_ENCODING']
\r
55 return false unless accepts && accepts =~ /(x-gzip|gzip)/
\r
56 @compression_encoding = $1
\r
60 def compress_output(body)
\r
61 output = StringIO.new
\r
63 # Zlib does a close. Bad Zlib...
\r
66 gz = Zlib::GzipWriter.new(output)
\r
69 if output.length < body.length
\r
70 @old_response_body = body
\r