diff --git a/lib/rack/deflater.rb b/lib/rack/deflater.rb index 1dc8b45d7..20401a455 100644 --- a/lib/rack/deflater.rb +++ b/lib/rack/deflater.rb @@ -1,5 +1,6 @@ require "zlib" require "stringio" +require "time" module Rack @@ -17,7 +18,8 @@ def call(env) case encoding when "gzip" - [status, headers.merge("Content-Encoding" => "gzip"), self.class.gzip(body)] + mtime = headers["Last-Modified"] || Time.now + [status, headers.merge("Content-Encoding" => "gzip"), self.class.gzip(body, mtime)] when "deflate" [status, headers.merge("Content-Encoding" => "deflate"), self.class.deflate(body)] when "identity" @@ -28,9 +30,10 @@ def call(env) end end - def self.gzip(body) + def self.gzip(body, mtime=Time.now) io = StringIO.new gzip = Zlib::GzipWriter.new(io) + gzip.mtime = mtime # TODO: Add streaming # TODO: Consider all part types diff --git a/test/spec_rack_deflater.rb b/test/spec_rack_deflater.rb index 3a4424190..33d984aec 100644 --- a/test/spec_rack_deflater.rb +++ b/test/spec_rack_deflater.rb @@ -41,7 +41,11 @@ class << body; def each; yield("foo"); yield("bar"); end; end response[0].should.equal(200) response[1].should.equal({ "Content-Encoding" => "gzip" }) - # response[2].to_s.should.equal("\037\213\b\000J\340mH\000\003K\313\317OJ,\002\000\225\037\366\236\006\000\000\000") + + io = StringIO.new(response[2].to_s) + gz = Zlib::GzipReader.new(io) + gz.read.should.equal("foobar") + gz.close end specify "should be able to fallback to no deflation" do