-
Notifications
You must be signed in to change notification settings - Fork 5.3k
filter: Add HttpCache interface and helpers #9878
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| #include "extensions/filters/http/cache/http_cache.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <ostream> | ||
|
|
||
| #include "envoy/http/codes.h" | ||
|
|
||
| #include "common/http/headers.h" | ||
| #include "common/protobuf/utility.h" | ||
|
|
||
| #include "extensions/filters/http/cache/http_cache_utils.h" | ||
|
|
||
| #include "absl/time/time.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace HttpFilters { | ||
| namespace Cache { | ||
|
|
||
| std::ostream& operator<<(std::ostream& os, CacheEntryStatus status) { | ||
| switch (status) { | ||
| case CacheEntryStatus::Ok: | ||
| return os << "Ok"; | ||
| case CacheEntryStatus::Unusable: | ||
| return os << "Unusable"; | ||
| case CacheEntryStatus::RequiresValidation: | ||
| return os << "RequiresValidation"; | ||
| case CacheEntryStatus::FoundNotModified: | ||
| return os << "FoundNotModified"; | ||
| case CacheEntryStatus::UnsatisfiableRange: | ||
| return os << "UnsatisfiableRange"; | ||
| } | ||
| NOT_REACHED_GCOVR_EXCL_LINE; | ||
| } | ||
|
|
||
| std::ostream& operator<<(std::ostream& os, const AdjustedByteRange& range) { | ||
| return os << "[" << range.begin() << "," << range.end() << ")"; | ||
| } | ||
|
|
||
| LookupRequest::LookupRequest(const Http::HeaderMap& request_headers, SystemTime timestamp) | ||
| : timestamp_(timestamp), | ||
| request_cache_control_(request_headers.CacheControl() == nullptr | ||
| ? "" | ||
| : request_headers.CacheControl()->value().getStringView()) { | ||
| // These ASSERTs check prerequisites. A request without these headers can't be looked up in cache; | ||
| // CacheFilter doesn't create LookupRequests for such requests. | ||
| ASSERT(request_headers.Path(), "Can't form cache lookup key for malformed Http::HeaderMap " | ||
| "with null Path."); | ||
| ASSERT(request_headers.ForwardedProto(), | ||
| "Can't form cache lookup key for malformed Http::HeaderMap with null ForwardedProto."); | ||
| ASSERT(request_headers.Host(), "Can't form cache lookup key for malformed Http::HeaderMap " | ||
| "with null Host."); | ||
| const Http::HeaderString& forwarded_proto = request_headers.ForwardedProto()->value(); | ||
| const auto& scheme_values = Http::Headers::get().SchemeValues; | ||
| ASSERT(forwarded_proto == scheme_values.Http || forwarded_proto == scheme_values.Https); | ||
| // TODO(toddmgreer): Let config determine whether to include forwarded_proto, host, and | ||
| // query params. | ||
| // TODO(toddmgreer): get cluster name. | ||
| // TODO(toddmgreer): Parse Range header into request_range_spec_, and handle the resultant | ||
| // vector<AdjustedByteRange> in CacheFilter::onOkHeaders. | ||
| key_.set_cluster_name("cluster_name_goes_here"); | ||
| key_.set_host(std::string(request_headers.Host()->value().getStringView())); | ||
| key_.set_path(std::string(request_headers.Path()->value().getStringView())); | ||
| key_.set_clear_http(forwarded_proto == scheme_values.Http); | ||
| } | ||
|
|
||
| // Unless this API is still alpha, calls to stableHashKey() must always return | ||
| // the same result, or a way must be provided to deal with a complete cache | ||
| // flush. localHashKey however, can be changed at will. | ||
| size_t stableHashKey(const Key& key) { return MessageUtil::hash(key); } | ||
| size_t localHashKey(const Key& key) { return stableHashKey(key); } | ||
|
|
||
| // Returns true if response_headers is fresh. | ||
| bool LookupRequest::isFresh(const Http::HeaderMap& response_headers) const { | ||
| if (!response_headers.Date()) { | ||
| return false; | ||
| } | ||
| const Http::HeaderEntry* cache_control_header = response_headers.CacheControl(); | ||
| if (cache_control_header) { | ||
| const SystemTime::duration effective_max_age = | ||
| Utils::effectiveMaxAge(cache_control_header->value().getStringView()); | ||
| return timestamp_ - Utils::httpTime(response_headers.Date()) < effective_max_age; | ||
| } | ||
| // We didn't find a cache-control header with enough info to determine | ||
| // freshness, so fall back to the expires header. | ||
| return timestamp_ <= Utils::httpTime(response_headers.get(Http::Headers::get().Expires)); | ||
| } | ||
|
|
||
| LookupResult LookupRequest::makeLookupResult(Http::HeaderMapPtr&& response_headers, | ||
| uint64_t content_length) const { | ||
| // TODO(toddmgreer): Implement all HTTP caching semantics. | ||
| ASSERT(response_headers); | ||
| LookupResult result; | ||
| result.cache_entry_status_ = | ||
| isFresh(*response_headers) ? CacheEntryStatus::Ok : CacheEntryStatus::RequiresValidation; | ||
| result.headers_ = std::move(response_headers); | ||
| result.content_length_ = content_length; | ||
| if (!adjustByteRangeSet(result.response_ranges_, request_range_spec_, content_length)) { | ||
| result.headers_->setStatus(static_cast<uint64_t>(Http::Code::RangeNotSatisfiable)); | ||
| } | ||
| result.has_trailers_ = false; | ||
| return result; | ||
| } | ||
|
|
||
| bool adjustByteRangeSet(std::vector<AdjustedByteRange>& response_ranges, | ||
| const std::vector<RawByteRange>& request_range_spec, | ||
| uint64_t content_length) { | ||
| if (request_range_spec.empty()) { | ||
| // No range header, so the request can proceed. | ||
| return true; | ||
| } | ||
|
|
||
| if (content_length == 0) { | ||
| // There is a range header, but it's unsatisfiable. | ||
| return false; | ||
| } | ||
|
|
||
| for (const RawByteRange& spec : request_range_spec) { | ||
| if (spec.isSuffix()) { | ||
| // spec is a suffix-byte-range-spec | ||
| if (spec.suffixLength() == 0) { | ||
| // This range is unsatisfiable, so skip it. | ||
| continue; | ||
| } | ||
| if (spec.suffixLength() >= content_length) { | ||
| // All bytes are being requested, so we may as well send a '200 | ||
| // OK' response. | ||
| response_ranges.clear(); | ||
| return true; | ||
| } | ||
| response_ranges.emplace_back(content_length - spec.suffixLength(), content_length); | ||
| } else { | ||
| // spec is a byte-range-spec | ||
| if (spec.firstBytePos() >= content_length) { | ||
| // This range is unsatisfiable, so skip it. | ||
| continue; | ||
| } | ||
| if (spec.lastBytePos() >= content_length - 1) { | ||
| if (spec.firstBytePos() == 0) { | ||
| // All bytes are being requested, so we may as well send a '200 | ||
| // OK' response. | ||
| response_ranges.clear(); | ||
| return true; | ||
| } | ||
| response_ranges.emplace_back(spec.firstBytePos(), content_length); | ||
| } else { | ||
| response_ranges.emplace_back(spec.firstBytePos(), spec.lastBytePos() + 1); | ||
| } | ||
| } | ||
| } | ||
| if (response_ranges.empty()) { | ||
| // All ranges were unsatisfiable. | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| } // namespace Cache | ||
| } // namespace HttpFilters | ||
| } // namespace Extensions | ||
| } // namespace Envoy | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.