diff --git a/lib/rack/cache/context.rb b/lib/rack/cache/context.rb index 052bf5b..c32dd81 100644 --- a/lib/rack/cache/context.rb +++ b/lib/rack/cache/context.rb @@ -265,6 +265,10 @@ def fetch def store(response) strip_ignore_headers(response) metastore.store(@request, response, entitystore) + # Remove header so downstream apps won't choke on it, + # but only if explicitly disabled. Can't be stripped + # untile the digest is used to store the value + response.headers.delete('X-Content-Digest') if disable_digest_header? response.headers['Age'] = response.age.to_s rescue Exception => e log_error(e) diff --git a/lib/rack/cache/options.rb b/lib/rack/cache/options.rb index e1ecf1f..6bb829a 100644 --- a/lib/rack/cache/options.rb +++ b/lib/rack/cache/options.rb @@ -81,7 +81,8 @@ def option_name(key) # Set of response headers that are removed before storing them in the # cache. These headers are only removed for cacheable responses. For # example, in most cases, it makes sense to prevent cookies from being - # stored in the cache. + # stored in the cache. It may also be useful to ignore X-Content-Digest + # if Rack::Cache is talking to another app that sets it. # # Default: ['Set-Cookie'] option_accessor :ignore_headers @@ -109,6 +110,9 @@ def option_name(key) # be used. option_accessor :use_native_ttl + # Specifies whether Rack::Cache should return an X-Content-Digest header + option_accessor :disable_digest_header + # The underlying options Hash. During initialization (or outside of a # request), this is a default values Hash. During a request, this is the # Rack environment Hash. The default values Hash is merged in underneath @@ -151,6 +155,7 @@ def initialize_options(options={}) 'rack-cache.allow_reload' => false, 'rack-cache.allow_revalidate' => false, 'rack-cache.use_native_ttl' => false, + 'rack-cache.disable_digest_header' => false, } self.options = options end diff --git a/test/context_test.rb b/test/context_test.rb index 3ba1c48..0b60af5 100644 --- a/test/context_test.rb +++ b/test/context_test.rb @@ -25,6 +25,19 @@ response.headers.should.not.include 'Age' end + it 'does not respond with x-content-digest if disabled' do + respond_with 200, 'Cache-Control' => 'public', 'ETag' => '"FOO"' + get '/', 'rack-cache.disable_digest_header' => true + + response.should.be.ok + response.body.should.equal 'Hello World' + response.headers.should.include 'Date' + response['X-Content-Digest'].should.be.nil + cache.trace.should.include :miss + cache.trace.should.include :store + cache.metastore.to_hash.keys.length.should.equal 1 + end + %w[post put delete].each do |request_method| it "invalidates on #{request_method} requests" do respond_with 200 @@ -452,6 +465,7 @@ cache.metastore.to_hash.keys.length.should.equal 1 end + it 'caches responses with a max-age directive' do respond_with 200, 'Cache-Control' => 'max-age=5' get '/'