Created
February 13, 2009 13:10
-
-
Save aurelian/63885 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # OutputCompression plugin | |
| # made by http://craz8.com/svn/trunk/plugins/output_compression/ | |
| # | |
| module OutputCompression | |
| protected | |
| def compress_output | |
| return if response.headers['Content-Encoding'] || request.env['HTTP_ACCEPT_ENCODING'].nil? | |
| begin | |
| request.env['HTTP_ACCEPT_ENCODING'].split(/\s*,\s*/).each do |encoding| | |
| # TODO: use "q" values to determine user agent encoding preferences | |
| case encoding | |
| when /\Agzip\b/ | |
| StringIO.open('', 'w') do |strio| | |
| begin | |
| gz = Zlib::GzipWriter.new(strio) | |
| gz.write(response.body) | |
| response.body = strio.string | |
| ensure | |
| gz.close if gz | |
| end | |
| end | |
| when /\Adeflate\b/ | |
| response.body = Zlib::Deflate.deflate(response.body, Zlib::BEST_COMPRESSION) | |
| when /\Aidentity\b/ | |
| # do nothing for identity | |
| else | |
| next # the encoding is not supported, try the next one | |
| end | |
| logger.info "++ Response body was encoded with #{encoding}" | |
| response.headers['Content-Encoding'] = encoding | |
| break # the encoding is supported, stop | |
| end | |
| end | |
| response.headers['Content-Length'] = response.body.length | |
| if response.headers['Vary'] != '*' | |
| response.headers['Vary'] = response.headers['Vary'].to_s.split(',').push('Accept-Encoding').uniq.join(',') | |
| end | |
| end | |
| def self.included(base) | |
| base.send :instance_method, :compress_output | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment