Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[http-cache] Don't bypass the cache for if-unmodified-since or if-match #33080

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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