Skip to content

Commit

Permalink
Allow customisation of transferable headers
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Matt-Yorkley committed Aug 19, 2023
1 parent 95a2c8b commit d07cce7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rack/cache/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def validate(entry)

entry = entry.dup
entry.headers.delete('date')
%w[Date expires cache-control etag last-modified].each do |name|
%w[Date expires cache-control etag last-modified].concat(transfer_headers).each do |name|
next unless value = response.headers[name]
entry.headers[name] = value
end
Expand Down
8 changes: 8 additions & 0 deletions lib/rack/cache/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ def option_name(key)
# Default: ['set-cookie']
option_accessor :ignore_headers

# Set of response headers that are transferred from the backend response
# onto the cache entry when validating a cached entity (after receiving a
# 304 response from the backend) before sending it to the client.
#
# Default: []
option_accessor :transfer_headers

# Set of request headers that trigger "private" cache-control behavior
# on responses that don't explicitly state whether the response is
# public or private via a cache-control directive. Applications that use
Expand Down Expand Up @@ -149,6 +156,7 @@ def initialize_options(options={})
'rack-cache.entitystore' => 'heap:/',
'rack-cache.default_ttl' => 0,
'rack-cache.ignore_headers' => ['set-cookie'],
'rack-cache.transfer_headers' => [],
'rack-cache.private_headers' => ['Authorization', 'Cookie'],
'rack-cache.allow_reload' => false,
'rack-cache.allow_revalidate' => false,
Expand Down
24 changes: 24 additions & 0 deletions test/context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,30 @@
cache.trace.wont_include :miss
end

it 'includes specified additional headers from the backend in responses to the client' do
count = 0
respond_with do |req,res|
count += 1
res['ETAG'] = '"12345"'
res['unique-snowflake'] = '"so-special"'
res.status = (count == 1) ? 200 : 304
end

get '/', 'rack-cache.transfer_headers' => ['unique-snowflake']
assert app.called?
assert response.ok?
response.headers.must_include 'unique-snowflake'
response['unique-snowflake'].must_equal '"so-special"'
cache.trace.must_include :miss

get '/', 'rack-cache.transfer_headers' => ['unique-snowflake']
assert app.called?
assert response.ok?
response.headers.must_include 'unique-snowflake'
response['unique-snowflake'].must_equal '"so-special"'
cache.trace.must_include :valid
end

it 'replaces cached responses when validation results in non-304 response' do
timestamp = Time.now.httpdate
count = 0
Expand Down

0 comments on commit d07cce7

Please sign in to comment.