Skip to content

Commit d07cce7

Browse files
committed
Allow customisation of transferable headers
Headers specified in the :transfer_headers option are passed (transferred) from responses from the backend through to responses sent to the client (included cached entries).
1 parent 95a2c8b commit d07cce7

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

lib/rack/cache/context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def validate(entry)
237237

238238
entry = entry.dup
239239
entry.headers.delete('date')
240-
%w[Date expires cache-control etag last-modified].each do |name|
240+
%w[Date expires cache-control etag last-modified].concat(transfer_headers).each do |name|
241241
next unless value = response.headers[name]
242242
entry.headers[name] = value
243243
end

lib/rack/cache/options.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ def option_name(key)
8686
# Default: ['set-cookie']
8787
option_accessor :ignore_headers
8888

89+
# Set of response headers that are transferred from the backend response
90+
# onto the cache entry when validating a cached entity (after receiving a
91+
# 304 response from the backend) before sending it to the client.
92+
#
93+
# Default: []
94+
option_accessor :transfer_headers
95+
8996
# Set of request headers that trigger "private" cache-control behavior
9097
# on responses that don't explicitly state whether the response is
9198
# public or private via a cache-control directive. Applications that use
@@ -149,6 +156,7 @@ def initialize_options(options={})
149156
'rack-cache.entitystore' => 'heap:/',
150157
'rack-cache.default_ttl' => 0,
151158
'rack-cache.ignore_headers' => ['set-cookie'],
159+
'rack-cache.transfer_headers' => [],
152160
'rack-cache.private_headers' => ['Authorization', 'Cookie'],
153161
'rack-cache.allow_reload' => false,
154162
'rack-cache.allow_revalidate' => false,

test/context_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,30 @@
776776
cache.trace.wont_include :miss
777777
end
778778

779+
it 'includes specified additional headers from the backend in responses to the client' do
780+
count = 0
781+
respond_with do |req,res|
782+
count += 1
783+
res['ETAG'] = '"12345"'
784+
res['unique-snowflake'] = '"so-special"'
785+
res.status = (count == 1) ? 200 : 304
786+
end
787+
788+
get '/', 'rack-cache.transfer_headers' => ['unique-snowflake']
789+
assert app.called?
790+
assert response.ok?
791+
response.headers.must_include 'unique-snowflake'
792+
response['unique-snowflake'].must_equal '"so-special"'
793+
cache.trace.must_include :miss
794+
795+
get '/', 'rack-cache.transfer_headers' => ['unique-snowflake']
796+
assert app.called?
797+
assert response.ok?
798+
response.headers.must_include 'unique-snowflake'
799+
response['unique-snowflake'].must_equal '"so-special"'
800+
cache.trace.must_include :valid
801+
end
802+
779803
it 'replaces cached responses when validation results in non-304 response' do
780804
timestamp = Time.now.httpdate
781805
count = 0

0 commit comments

Comments
 (0)