Skip to content

Commit

Permalink
[http-cache] Don't bypass the cache for if-unmodified-since or if-mat…
Browse files Browse the repository at this point in the history
…ch (envoyproxy#33080)

* [cache] Don't bypass the cache for if-unmodified-since or if-match

---------

Signed-off-by: Raven Black <ravenblack@dropbox.com>
  • Loading branch information
ravenblackx authored and alyssawilk committed Apr 29, 2024
1 parent 70d3417 commit 9318952
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
10 changes: 6 additions & 4 deletions source/extensions/filters/http/cache/cacheability_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,23 @@ const absl::flat_hash_set<absl::string_view>& cacheableStatusCodes() {
const std::vector<const Http::LowerCaseString*>& conditionalHeaders() {
// As defined by: https://httpwg.org/specs/rfc7232.html#preconditions.
CONSTRUCT_ON_FIRST_USE(
std::vector<const Http::LowerCaseString*>, &Http::CustomHeaders::get().IfMatch,
&Http::CustomHeaders::get().IfNoneMatch, &Http::CustomHeaders::get().IfModifiedSince,
&Http::CustomHeaders::get().IfUnmodifiedSince, &Http::CustomHeaders::get().IfRange);
std::vector<const Http::LowerCaseString*>, &Http::CustomHeaders::get().IfNoneMatch,
&Http::CustomHeaders::get().IfModifiedSince, &Http::CustomHeaders::get().IfRange);
}
} // namespace

bool CacheabilityUtils::canServeRequestFromCache(const Http::RequestHeaderMap& headers) {
const absl::string_view method = headers.getMethodValue();
const Http::HeaderValues& header_values = Http::Headers::get();

// Check if the request contains any conditional headers.
// Check if the request contains any conditional headers other than if-unmodified-since
// or if-match.
// For now, requests with conditional headers bypass the CacheFilter.
// This behavior does not cause any incorrect results, but may reduce the cache effectiveness.
// If needed to be handled properly refer to:
// https://httpwg.org/specs/rfc7234.html#validation.received
// if-unmodified-since and if-match are ignored, as the spec explicitly says these
// header fields can be ignored by caches and intermediaries.
for (auto conditional_header : conditionalHeaders()) {
if (!headers.get(*conditional_header).empty()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ TEST_F(CanServeRequestFromCacheTest, AuthorizationHeader) {
}

INSTANTIATE_TEST_SUITE_P(ConditionalHeaders, RequestConditionalHeadersTest,
testing::Values("if-match", "if-none-match", "if-modified-since",
"if-unmodified-since", "if-range"),
testing::Values("if-none-match", "if-modified-since", "if-range"),
[](const auto& info) {
std::string test_name = info.param;
absl::c_replace_if(
Expand Down

0 comments on commit 9318952

Please sign in to comment.