1 # -*- encoding: binary -*-
4 # allows storing pre-deflated data on disk and serving it
5 # as-is for clients that accept that deflate encoding
6 module Metropolis::Deflate
7 include Metropolis::Constants
8 Compression = "deflate"
11 status, headers, body = r = super
12 if 200 == status && /\bdeflate\b/ !~ env[HTTP_ACCEPT_ENCODING]
13 inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
14 body[0] = inflater.inflate(body[0]) << inflater.finish
16 headers[Content_Length] = body[0].size.to_s
17 headers.delete(Content_Encoding)
24 Wrapper.new(env) if Compression != env[HTTP_CONTENT_ENCODING]
28 def self.extended(obj)
30 @headers[Content_Encoding] = Compression
31 @headers[Vary] = Accept_Encoding
35 class Wrapper < Metropolis::InputWrapper
38 deflater = Zlib::Deflate.new(
39 Zlib::DEFAULT_COMPRESSION,
40 # drop the zlib header which causes both Safari and IE to choke
43 Zlib::DEFAULT_STRATEGY
45 deflater.deflate(@input.read) << deflater.finish