Skip to content

Commit

Permalink
Add option to turn off X-Content-Digest header
Browse files Browse the repository at this point in the history
* Strips the header from the cache response so
  that downstream apps don't see it.
  • Loading branch information
therabidbanana committed Mar 22, 2012
1 parent 06d089a commit 6956f97
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/rack/cache/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion lib/rack/cache/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 '/'
Expand Down

0 comments on commit 6956f97

Please sign in to comment.