1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 23:18:11 +00:00

LibWeb: Make HeaderList ref-counted

This is needed to eventually share a header list between a Request or
Response object's internal infra request/response and the object's
exposed Header object.
This commit is contained in:
Linus Groh 2022-09-25 20:52:51 +01:00
parent 2d57d47132
commit afe2563e2b
8 changed files with 61 additions and 46 deletions

View file

@ -28,6 +28,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> Headers::create_with_global_objec
Headers::Headers(HTML::Window& window)
: PlatformObject(window.realm())
, m_header_list(make_ref_counted<Infrastructure::HeaderList>())
{
set_prototype(&window.cached_web_prototype("Headers"));
}
@ -73,11 +74,11 @@ WebIDL::ExceptionOr<void> Headers::delete_(String const& name_string)
return {};
// 6. If thiss header list does not contain name, then return.
if (!m_header_list.contains(name))
if (!m_header_list->contains(name))
return {};
// 7. Delete name from thiss header list.
m_header_list.delete_(name);
m_header_list->delete_(name);
// 8. If thiss guard is "request-no-cors", then remove privileged no-CORS request headers from this.
if (m_guard == Guard::RequestNoCORS)
@ -97,7 +98,7 @@ WebIDL::ExceptionOr<String> Headers::get(String const& name_string)
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name" };
// 2. Return the result of getting name from thiss header list.
auto byte_buffer = TRY_OR_RETURN_OOM(global_object(), m_header_list.get(name));
auto byte_buffer = TRY_OR_RETURN_OOM(global_object(), m_header_list->get(name));
// FIXME: Teach BindingsGenerator about Optional<String>
return byte_buffer.has_value() ? String { byte_buffer->span() } : String {};
}
@ -113,7 +114,7 @@ WebIDL::ExceptionOr<bool> Headers::has(String const& name_string)
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name" };
// 2. Return true if thiss header list contains name; otherwise false.
return m_header_list.contains(name);
return m_header_list->contains(name);
}
// https://fetch.spec.whatwg.org/#dom-headers-set
@ -154,7 +155,7 @@ WebIDL::ExceptionOr<void> Headers::set(String const& name_string, String const&
return {};
// 7. Set (name, value) in thiss header list.
TRY_OR_RETURN_OOM(global_object(), m_header_list.set(move(header)));
TRY_OR_RETURN_OOM(global_object(), m_header_list->set(move(header)));
// 8. If thiss guard is "request-no-cors", then remove privileged no-CORS request headers from this.
if (m_guard == Guard::RequestNoCORS)
@ -168,7 +169,7 @@ JS::ThrowCompletionOr<void> Headers::for_each(ForEachCallback callback)
{
// The value pairs to iterate over are the return value of running sort and combine with thiss header list.
auto value_pairs_to_iterate_over = [&]() -> JS::ThrowCompletionOr<Vector<Fetch::Infrastructure::Header>> {
auto headers_or_error = m_header_list.sort_and_combine();
auto headers_or_error = m_header_list->sort_and_combine();
if (headers_or_error.is_error())
return vm().throw_completion<JS::InternalError>(JS::ErrorType::NotEnoughMemoryToAllocate);
return headers_or_error.release_value();
@ -226,7 +227,7 @@ WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
// 5. Otherwise, if headerss guard is "request-no-cors":
if (m_guard == Guard::RequestNoCORS) {
// 1. Let temporaryValue be the result of getting name from headerss header list.
auto temporary_value = TRY_OR_RETURN_OOM(global_object(), m_header_list.get(name));
auto temporary_value = TRY_OR_RETURN_OOM(global_object(), m_header_list->get(name));
// 2. If temporaryValue is null, then set temporaryValue to value.
if (!temporary_value.has_value()) {
@ -253,8 +254,8 @@ WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
if (m_guard == Guard::Response && Infrastructure::is_forbidden_response_header_name(name))
return {};
// 7. Append (name, value) to headerss header list.
TRY_OR_RETURN_OOM(global_object(), m_header_list.append(move(header)));
// 7. Append (name, value) to headerss header list
TRY_OR_RETURN_OOM(global_object(), m_header_list->append(move(header)));
// 8. If headerss guard is "request-no-cors", then remove privileged no-CORS request headers from headers.
if (m_guard == Guard::RequestNoCORS)
@ -309,7 +310,7 @@ void Headers::remove_privileged_no_cors_headers()
// 1. For each headerName of privileged no-CORS request-header names:
for (auto const& header_name : privileged_no_cors_request_header_names) {
// 1. Delete headerName from headerss header list.
m_header_list.delete_(header_name.bytes());
m_header_list->delete_(header_name.bytes());
}
}

View file

@ -35,9 +35,9 @@ public:
virtual ~Headers() override;
[[nodiscard]] Infrastructure::HeaderList& header_list() { return m_header_list; }
[[nodiscard]] Infrastructure::HeaderList const& header_list() const { return m_header_list; }
void set_header_list(Infrastructure::HeaderList header_list) { m_header_list = move(header_list); }
[[nodiscard]] NonnullRefPtr<Infrastructure::HeaderList>& header_list() { return m_header_list; }
[[nodiscard]] NonnullRefPtr<Infrastructure::HeaderList> const& header_list() const { return m_header_list; }
void set_header_list(NonnullRefPtr<Infrastructure::HeaderList> header_list) { m_header_list = move(header_list); }
[[nodiscard]] Guard guard() const { return m_guard; }
void set_guard(Guard guard) { m_guard = guard; }
@ -63,7 +63,7 @@ private:
// https://fetch.spec.whatwg.org/#concept-headers-header-list
// A Headers object has an associated header list (a header list), which is initially empty.
Infrastructure::HeaderList m_header_list;
NonnullRefPtr<Infrastructure::HeaderList> m_header_list;
// https://fetch.spec.whatwg.org/#concept-headers-guard
// A Headers object also has an associated guard, which is a headers guard. A headers guard is "immutable", "request", "request-no-cors", "response" or "none".

View file

@ -38,7 +38,7 @@ JS::ThrowCompletionOr<JS::Object*> HeadersIterator::next()
{
// The value pairs to iterate over are the return value of running sort and combine with thiss header list.
auto value_pairs_to_iterate_over = [&]() -> JS::ThrowCompletionOr<Vector<Fetch::Infrastructure::Header>> {
auto headers_or_error = m_headers.m_header_list.sort_and_combine();
auto headers_or_error = m_headers.m_header_list->sort_and_combine();
if (headers_or_error.is_error())
return vm().throw_completion<JS::InternalError>(JS::ErrorType::NotEnoughMemoryToAllocate);
return headers_or_error.release_value();

View file

@ -26,7 +26,9 @@ struct Header {
// https://fetch.spec.whatwg.org/#concept-header-list
// A header list is a list of zero or more headers. It is initially the empty list.
class HeaderList final : Vector<Header> {
class HeaderList final
: public RefCounted<HeaderList>
, Vector<Header> {
public:
using Vector::begin;
using Vector::end;

View file

@ -9,6 +9,11 @@
namespace Web::Fetch::Infrastructure {
Request::Request()
: m_header_list(make_ref_counted<HeaderList>())
{
}
// https://fetch.spec.whatwg.org/#concept-request-url
AK::URL const& Request::url() const
{
@ -202,7 +207,7 @@ ErrorOr<void> Request::add_range_reader(u64 first, Optional<u64> const& last)
.name = TRY(ByteBuffer::copy("Range"sv.bytes())),
.value = move(range_value),
};
TRY(m_header_list.append(move(header)));
TRY(m_header_list->append(move(header)));
return {};
}

View file

@ -154,7 +154,7 @@ public:
using ReservedClientType = Variant<Empty, HTML::Environment*, HTML::EnvironmentSettingsObject*>;
using WindowType = Variant<Window, HTML::EnvironmentSettingsObject*>;
Request() = default;
Request();
[[nodiscard]] ReadonlyBytes method() const { return m_method; }
void set_method(ByteBuffer method) { m_method = move(method); }
@ -162,9 +162,9 @@ public:
[[nodiscard]] bool local_urls_only() const { return m_local_urls_only; }
void set_local_urls_only(bool local_urls_only) { m_local_urls_only = local_urls_only; }
[[nodiscard]] HeaderList const& header_list() const { return m_header_list; }
[[nodiscard]] HeaderList& header_list() { return m_header_list; }
void set_header_list(HeaderList header_list) { m_header_list = move(header_list); }
[[nodiscard]] NonnullRefPtr<HeaderList> const& header_list() const { return m_header_list; }
[[nodiscard]] NonnullRefPtr<HeaderList>& header_list() { return m_header_list; }
void set_header_list(NonnullRefPtr<HeaderList> header_list) { m_header_list = move(header_list); }
[[nodiscard]] bool unsafe_request() const { return m_unsafe_request; }
void set_unsafe_request(bool unsafe_request) { m_unsafe_request = unsafe_request; }
@ -304,7 +304,7 @@ private:
// https://fetch.spec.whatwg.org/#concept-request-header-list
// A request has an associated header list (a header list). Unless stated otherwise it is empty.
HeaderList m_header_list;
NonnullRefPtr<HeaderList> m_header_list;
// https://fetch.spec.whatwg.org/#unsafe-request-flag
// A request has an associated unsafe-request flag. Unless stated otherwise it is unset.

View file

@ -9,6 +9,11 @@
namespace Web::Fetch::Infrastructure {
Response::Response()
: m_header_list(make_ref_counted<HeaderList>())
{
}
// https://fetch.spec.whatwg.org/#ref-for-concept-network-error%E2%91%A3
// A network error is a response whose status is always 0, status message is always
// the empty byte sequence, header list is always empty, and body is always null.
@ -111,16 +116,16 @@ ErrorOr<NonnullOwnPtr<BasicFilteredResponse>> BasicFilteredResponse::create(Resp
{
// A basic filtered response is a filtered response whose type is "basic" and header list excludes
// any headers in internal responses header list whose name is a forbidden response-header name.
HeaderList header_list;
for (auto const& header : internal_response.header_list()) {
auto header_list = make_ref_counted<HeaderList>();
for (auto const& header : *internal_response.header_list()) {
if (!is_forbidden_response_header_name(header.name))
TRY(header_list.append(header));
TRY(header_list->append(header));
}
return adopt_own(*new BasicFilteredResponse(internal_response, move(header_list)));
}
BasicFilteredResponse::BasicFilteredResponse(Response& internal_response, HeaderList header_list)
BasicFilteredResponse::BasicFilteredResponse(Response& internal_response, NonnullRefPtr<HeaderList> header_list)
: FilteredResponse(internal_response)
, m_header_list(move(header_list))
{
@ -135,16 +140,16 @@ ErrorOr<NonnullOwnPtr<CORSFilteredResponse>> CORSFilteredResponse::create(Respon
for (auto const& header_name : internal_response.cors_exposed_header_name_list())
cors_exposed_header_name_list.append(header_name.span());
HeaderList header_list;
for (auto const& header : internal_response.header_list()) {
auto header_list = make_ref_counted<HeaderList>();
for (auto const& header : *internal_response.header_list()) {
if (is_cors_safelisted_response_header_name(header.name, cors_exposed_header_name_list))
TRY(header_list.append(header));
TRY(header_list->append(header));
}
return adopt_own(*new CORSFilteredResponse(internal_response, move(header_list)));
}
CORSFilteredResponse::CORSFilteredResponse(Response& internal_response, HeaderList header_list)
CORSFilteredResponse::CORSFilteredResponse(Response& internal_response, NonnullRefPtr<HeaderList> header_list)
: FilteredResponse(internal_response)
, m_header_list(move(header_list))
{
@ -159,6 +164,7 @@ NonnullOwnPtr<OpaqueFilteredResponse> OpaqueFilteredResponse::create(Response& i
OpaqueFilteredResponse::OpaqueFilteredResponse(Response& internal_response)
: FilteredResponse(internal_response)
, m_header_list(make_ref_counted<HeaderList>())
{
}
@ -169,6 +175,7 @@ NonnullOwnPtr<OpaqueRedirectFilteredResponse> OpaqueRedirectFilteredResponse::cr
OpaqueRedirectFilteredResponse::OpaqueRedirectFilteredResponse(Response& internal_response)
: FilteredResponse(internal_response)
, m_header_list(make_ref_counted<HeaderList>())
{
}

View file

@ -47,7 +47,7 @@ public:
[[nodiscard]] static NonnullOwnPtr<Response> aborted_network_error();
[[nodiscard]] static NonnullOwnPtr<Response> network_error();
Response() = default;
Response();
virtual ~Response() = default;
[[nodiscard]] virtual Type type() const { return m_type; }
@ -66,9 +66,9 @@ public:
[[nodiscard]] virtual ReadonlyBytes status_message() const { return m_status_message; }
void set_status_message(ByteBuffer status_message) { m_status_message = move(status_message); }
[[nodiscard]] virtual HeaderList const& header_list() const { return m_header_list; }
[[nodiscard]] HeaderList& header_list() { return m_header_list; }
void set_header_list(HeaderList header_list) { m_header_list = move(header_list); }
[[nodiscard]] virtual NonnullRefPtr<HeaderList> const& header_list() const { return m_header_list; }
[[nodiscard]] NonnullRefPtr<HeaderList>& header_list() { return m_header_list; }
void set_header_list(NonnullRefPtr<HeaderList> header_list) { m_header_list = move(header_list); }
[[nodiscard]] virtual Optional<Body> const& body() const { return m_body; }
[[nodiscard]] Optional<Body>& body() { return m_body; }
@ -124,7 +124,7 @@ private:
// https://fetch.spec.whatwg.org/#concept-response-header-list
// A response has an associated header list (a header list). Unless stated otherwise it is empty.
HeaderList m_header_list;
NonnullRefPtr<HeaderList> m_header_list;
// https://fetch.spec.whatwg.org/#concept-response-body
// A response has an associated body (null or a body). Unless stated otherwise it is null.
@ -169,7 +169,7 @@ public:
[[nodiscard]] virtual Vector<AK::URL> const& url_list() const override { return m_internal_response.url_list(); }
[[nodiscard]] virtual Status status() const override { return m_internal_response.status(); }
[[nodiscard]] virtual ReadonlyBytes status_message() const override { return m_internal_response.status_message(); }
[[nodiscard]] virtual HeaderList const& header_list() const override { return m_internal_response.header_list(); }
[[nodiscard]] virtual NonnullRefPtr<HeaderList> const& header_list() const override { return m_internal_response.header_list(); }
[[nodiscard]] virtual Optional<Body> const& body() const override { return m_internal_response.body(); }
[[nodiscard]] virtual Optional<CacheState> const& cache_state() const override { return m_internal_response.cache_state(); }
[[nodiscard]] virtual Vector<ByteBuffer> const& cors_exposed_header_name_list() const override { return m_internal_response.cors_exposed_header_name_list(); }
@ -192,12 +192,12 @@ public:
static ErrorOr<NonnullOwnPtr<BasicFilteredResponse>> create(Response&);
[[nodiscard]] virtual Type type() const override { return Type::Basic; }
[[nodiscard]] virtual HeaderList const& header_list() const override { return m_header_list; }
[[nodiscard]] virtual NonnullRefPtr<HeaderList> const& header_list() const override { return m_header_list; }
private:
BasicFilteredResponse(Response&, HeaderList);
BasicFilteredResponse(Response&, NonnullRefPtr<HeaderList>);
HeaderList m_header_list;
NonnullRefPtr<HeaderList> m_header_list;
};
// https://fetch.spec.whatwg.org/#concept-filtered-response-cors
@ -206,12 +206,12 @@ public:
static ErrorOr<NonnullOwnPtr<CORSFilteredResponse>> create(Response&);
[[nodiscard]] virtual Type type() const override { return Type::CORS; }
[[nodiscard]] virtual HeaderList const& header_list() const override { return m_header_list; }
[[nodiscard]] virtual NonnullRefPtr<HeaderList> const& header_list() const override { return m_header_list; }
private:
CORSFilteredResponse(Response&, HeaderList);
CORSFilteredResponse(Response&, NonnullRefPtr<HeaderList>);
HeaderList m_header_list;
NonnullRefPtr<HeaderList> m_header_list;
};
// https://fetch.spec.whatwg.org/#concept-filtered-response-opaque
@ -223,14 +223,14 @@ public:
[[nodiscard]] virtual Vector<AK::URL> const& url_list() const override { return m_url_list; }
[[nodiscard]] virtual Status status() const override { return 0; }
[[nodiscard]] virtual ReadonlyBytes status_message() const override { return {}; }
[[nodiscard]] virtual HeaderList const& header_list() const override { return m_header_list; }
[[nodiscard]] virtual NonnullRefPtr<HeaderList> const& header_list() const override { return m_header_list; }
[[nodiscard]] virtual Optional<Body> const& body() const override { return m_body; }
private:
explicit OpaqueFilteredResponse(Response&);
Vector<AK::URL> m_url_list;
HeaderList m_header_list;
NonnullRefPtr<HeaderList> m_header_list;
Optional<Body> m_body;
};
@ -242,13 +242,13 @@ public:
[[nodiscard]] virtual Type type() const override { return Type::OpaqueRedirect; }
[[nodiscard]] virtual Status status() const override { return 0; }
[[nodiscard]] virtual ReadonlyBytes status_message() const override { return {}; }
[[nodiscard]] virtual HeaderList const& header_list() const override { return m_header_list; }
[[nodiscard]] virtual NonnullRefPtr<HeaderList> const& header_list() const override { return m_header_list; }
[[nodiscard]] virtual Optional<Body> const& body() const override { return m_body; }
private:
explicit OpaqueRedirectFilteredResponse(Response&);
HeaderList m_header_list;
NonnullRefPtr<HeaderList> m_header_list;
Optional<Body> m_body;
};