Skip to content

Commit 6956f97

Browse files
committedMar 22, 2012
Add option to turn off X-Content-Digest header
* Strips the header from the cache response so that downstream apps don't see it.
1 parent 06d089a commit 6956f97

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed
 

‎lib/rack/cache/context.rb

+4
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ def fetch
265265
def store(response)
266266
strip_ignore_headers(response)
267267
metastore.store(@request, response, entitystore)
268+
# Remove header so downstream apps won't choke on it,
269+
# but only if explicitly disabled. Can't be stripped
270+
# untile the digest is used to store the value
271+
response.headers.delete('X-Content-Digest') if disable_digest_header?
268272
response.headers['Age'] = response.age.to_s
269273
rescue Exception => e
270274
log_error(e)

‎lib/rack/cache/options.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def option_name(key)
8181
# Set of response headers that are removed before storing them in the
8282
# cache. These headers are only removed for cacheable responses. For
8383
# example, in most cases, it makes sense to prevent cookies from being
84-
# stored in the cache.
84+
# stored in the cache. It may also be useful to ignore X-Content-Digest
85+
# if Rack::Cache is talking to another app that sets it.
8586
#
8687
# Default: ['Set-Cookie']
8788
option_accessor :ignore_headers
@@ -109,6 +110,9 @@ def option_name(key)
109110
# be used.
110111
option_accessor :use_native_ttl
111112

113+
# Specifies whether Rack::Cache should return an X-Content-Digest header
114+
option_accessor :disable_digest_header
115+
112116
# The underlying options Hash. During initialization (or outside of a
113117
# request), this is a default values Hash. During a request, this is the
114118
# Rack environment Hash. The default values Hash is merged in underneath
@@ -151,6 +155,7 @@ def initialize_options(options={})
151155
'rack-cache.allow_reload' => false,
152156
'rack-cache.allow_revalidate' => false,
153157
'rack-cache.use_native_ttl' => false,
158+
'rack-cache.disable_digest_header' => false,
154159
}
155160
self.options = options
156161
end

‎test/context_test.rb

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@
2525
response.headers.should.not.include 'Age'
2626
end
2727

28+
it 'does not respond with x-content-digest if disabled' do
29+
respond_with 200, 'Cache-Control' => 'public', 'ETag' => '"FOO"'
30+
get '/', 'rack-cache.disable_digest_header' => true
31+
32+
response.should.be.ok
33+
response.body.should.equal 'Hello World'
34+
response.headers.should.include 'Date'
35+
response['X-Content-Digest'].should.be.nil
36+
cache.trace.should.include :miss
37+
cache.trace.should.include :store
38+
cache.metastore.to_hash.keys.length.should.equal 1
39+
end
40+
2841
%w[post put delete].each do |request_method|
2942
it "invalidates on #{request_method} requests" do
3043
respond_with 200
@@ -452,6 +465,7 @@
452465
cache.metastore.to_hash.keys.length.should.equal 1
453466
end
454467

468+
455469
it 'caches responses with a max-age directive' do
456470
respond_with 200, 'Cache-Control' => 'max-age=5'
457471
get '/'

0 commit comments

Comments
 (0)