We now partition the HTTP disk cache based on the Vary response header. If a cached response contains a Vary header, we look for each of the header names in the outgoing HTTP request. The outgoing request must match every header value in the original request for the cache entry to be used; otherwise, a new request will be issued, and a separate cache entry will be created. Note that we must now defer creating the disk cache file itself until we have received the response headers. The Vary key is computed from these headers, and affects the partitioned disk cache file name. There are further optimizations we can make here. If we have a Vary mismatch, we could find the best candidate cached response and issue a conditional HTTP request. The content server may then respond with an HTTP 304 if the mismatched request headers are actually okay. But for now, if we have a Vary mismatch, we issue an unconditional request as a purely correctness-oriented patch.
87 lines
2.6 KiB
C++
87 lines
2.6 KiB
C++
/*
|
|
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
|
|
* Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/ByteString.h>
|
|
#include <AK/HashTable.h>
|
|
#include <AK/NonnullRefPtr.h>
|
|
#include <AK/Optional.h>
|
|
#include <AK/RefCounted.h>
|
|
#include <AK/String.h>
|
|
#include <AK/Vector.h>
|
|
#include <LibHTTP/Header.h>
|
|
|
|
namespace HTTP {
|
|
|
|
// https://fetch.spec.whatwg.org/#concept-header-list
|
|
class HeaderList final : public RefCounted<HeaderList> {
|
|
public:
|
|
static NonnullRefPtr<HeaderList> create(Vector<Header> = {});
|
|
|
|
Vector<Header> const& headers() const { return m_headers; }
|
|
|
|
[[nodiscard]] bool is_empty() const { return m_headers.is_empty(); }
|
|
|
|
[[nodiscard]] auto begin() { return m_headers.begin(); }
|
|
[[nodiscard]] auto begin() const { return m_headers.begin(); }
|
|
|
|
[[nodiscard]] auto end() { return m_headers.end(); }
|
|
[[nodiscard]] auto end() const { return m_headers.end(); }
|
|
|
|
void clear() { m_headers.clear(); }
|
|
|
|
[[nodiscard]] bool contains(StringView) const;
|
|
Optional<ByteString> get(StringView) const;
|
|
Optional<Vector<String>> get_decode_and_split(StringView) const;
|
|
void append(Header);
|
|
void delete_(StringView name);
|
|
void set(Header);
|
|
void combine(Header);
|
|
[[nodiscard]] Vector<Header> sort_and_combine() const;
|
|
|
|
struct ExtractHeaderParseFailure { };
|
|
[[nodiscard]] Variant<Empty, Vector<ByteString>, ExtractHeaderParseFailure> extract_header_list_values(StringView) const;
|
|
|
|
struct ExtractLengthFailure { };
|
|
[[nodiscard]] Variant<Empty, u64, ExtractLengthFailure> extract_length() const;
|
|
|
|
[[nodiscard]] Vector<ByteString> unique_names() const;
|
|
|
|
template<typename Callback>
|
|
void for_each_header_value(StringView name, Callback&& callback) const
|
|
{
|
|
for (auto const& header : m_headers) {
|
|
if (!header.name.equals_ignoring_ascii_case(name))
|
|
continue;
|
|
if (callback(header.value) == IterationDecision::Break)
|
|
break;
|
|
}
|
|
}
|
|
|
|
template<typename Callback>
|
|
void for_each_vary_header(Callback&& callback) const
|
|
{
|
|
for_each_header_value("Vary"sv, [&](StringView value) -> IterationDecision {
|
|
IterationDecision result;
|
|
|
|
value.for_each_split_view(","sv, SplitBehavior::Nothing, [&](StringView header) -> IterationDecision {
|
|
result = callback(header.trim_whitespace());
|
|
return result;
|
|
});
|
|
|
|
return result;
|
|
});
|
|
}
|
|
|
|
private:
|
|
explicit HeaderList(Vector<Header>);
|
|
|
|
Vector<Header> m_headers;
|
|
};
|
|
|
|
}
|