Skip to content

Commit

Permalink
Merge pull request #65 from Shopify/weak_304
Browse files Browse the repository at this point in the history
Relaxed 304 If-None-Match test to support weak key
  • Loading branch information
colinbendell authored Mar 23, 2023
2 parents 1ff3caa + 6821ecf commit 82ca12e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/response_bank/response_cache_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ def serving_from_noncurrent_but_recent_version_acceptable?
end

def serve_from_browser_cache(cache_key_hash)
if @env["HTTP_IF_NONE_MATCH"] == cache_key_hash
# Support for Etag variations including:
# If-None-Match: abc
# If-None-Match: "abc"
# If-None-Match: W/"abc"
# If-None-Match: "abc", "def"
if !@env["HTTP_IF_NONE_MATCH"].nil? && @env["HTTP_IF_NONE_MATCH"].include?(cache_key_hash)
@env['cacheable.miss'] = false
@env['cacheable.store'] = 'client'

Expand Down
12 changes: 12 additions & 0 deletions test/response_cache_handler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ def test_client_cache_hit
assert_env(false, 'client')
end

def test_client_cache_hit_quoted
controller.request.env['HTTP_IF_NONE_MATCH'] = "\"#{handler.versioned_key_hash}\""
handler.run!
assert_env(false, 'client')
end

def test_client_cache_hit_weak
controller.request.env['HTTP_IF_NONE_MATCH'] = "W/\"#{handler.versioned_key_hash}\""
handler.run!
assert_env(false, 'client')
end

def test_server_cache_hit
controller.request.env['gzip'] = false
@cache_store.expects(:read).with(handler.versioned_key_hash, raw: true).returns(page_serialized)
Expand Down

0 comments on commit 82ca12e

Please sign in to comment.