Created
August 17, 2008 19:40
-
-
Save michaelklishin/5817 to your computer and use it in GitHub Desktop.
This file contains 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
module Middleware | |
class ETag < ::Merb::Rack::Middleware | |
def call(env) | |
status, headers, body = @app.call(env) | |
if tag?(status, headers, env) | |
sha1 = Digest::SHA1.hexdigest(body) | |
Merb.logger.info! "Set ETag to #{sha1}" | |
headers['ETag'] = sha1 | |
end | |
[status, headers, body] | |
end | |
def tag?(status, headers, env) | |
status == 200 | |
end | |
end | |
class Deflate < ::Merb::Rack::Middleware | |
def call(env) | |
status, headers, body = @app.call(env) | |
if tag?(status, headers, env) | |
require "zlib" | |
Merb.logger.info! "Compressing response body using deflate" | |
body = Zlib::Deflate.new.deflate(body, Zlib::BEST_COMPRESSION) | |
headers['Content-Encoding'] = 'deflate' | |
end | |
[status, headers, body] | |
end | |
def tag?(status, headers, env) | |
Merb.logger.info! "Verifying accept encoding: #{env['HTTP_ACCEPT_ENCODING']}" | |
env['HTTP_ACCEPT_ENCODING'] =~ /deflate/ | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment