1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:27:45 +00:00

LibWeb/Fetch: Propagate OOM errors from HeaderList::extract_mime_type()

This commit is contained in:
Linus Groh 2023-03-03 09:27:51 +00:00
parent 2d7ce38ee2
commit ad4b4046f4
14 changed files with 36 additions and 36 deletions

View file

@ -164,7 +164,7 @@ JS::NonnullGCPtr<JS::Promise> consume_body(JS::Realm& realm, BodyMixin const& ob
auto steps = [&vm, &realm, &object, type](JS::Value value) -> WebIDL::ExceptionOr<JS::Value> {
VERIFY(value.is_string());
auto bytes = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(TRY(value.as_string().deprecated_string()).bytes()));
return package_data(realm, move(bytes), type, object.mime_type_impl());
return package_data(realm, move(bytes), type, object.mime_type_impl().release_value_but_fixme_should_propagate_errors());
};
// 5. Return the result of upon fulfillment of promise given steps.

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -26,7 +26,7 @@ class BodyMixin {
public:
virtual ~BodyMixin();
virtual Optional<MimeSniff::MimeType> mime_type_impl() const = 0;
virtual ErrorOr<Optional<MimeSniff::MimeType>> mime_type_impl() const = 0;
virtual Optional<Infrastructure::Body&> body_impl() = 0;
virtual Optional<Infrastructure::Body const&> body_impl() const = 0;

View file

@ -447,7 +447,7 @@ WebIDL::ExceptionOr<Optional<JS::NonnullGCPtr<PendingResponse>>> main_fetch(JS::
// FIXME: - should internalResponse to request be blocked by Content Security Policy
|| false
// - should internalResponse to request be blocked due to its MIME type
|| Infrastructure::should_response_to_request_be_blocked_due_to_its_mime_type(internal_response, request) == Infrastructure::RequestOrResponseBlocking::Blocked
|| TRY_OR_IGNORE(Infrastructure::should_response_to_request_be_blocked_due_to_its_mime_type(internal_response, request)) == Infrastructure::RequestOrResponseBlocking::Blocked
// - should internalResponse to request be blocked due to nosniff
|| TRY_OR_IGNORE(Infrastructure::should_response_to_request_be_blocked_due_to_nosniff(internal_response, request)) == Infrastructure::RequestOrResponseBlocking::Blocked)) {
// then set response and internalResponse to a network error.

View file

@ -298,7 +298,7 @@ ErrorOr<Vector<Header>> HeaderList::sort_and_combine() const
}
// https://fetch.spec.whatwg.org/#concept-header-extract-mime-type
Optional<MimeSniff::MimeType> HeaderList::extract_mime_type() const
ErrorOr<Optional<MimeSniff::MimeType>> HeaderList::extract_mime_type() const
{
// 1. Let charset be null.
Optional<String> charset;
@ -312,17 +312,17 @@ Optional<MimeSniff::MimeType> HeaderList::extract_mime_type() const
// 4. Let values be the result of getting, decoding, and splitting `Content-Type` from headers.
auto values_or_error = get_decode_and_split("Content-Type"sv.bytes());
if (values_or_error.is_error())
return {};
return OptionalNone {};
auto values = values_or_error.release_value();
// 5. If values is null, then return failure.
if (!values.has_value())
return {};
return OptionalNone {};
// 6. For each value of values:
for (auto const& value : *values) {
// 1. Let temporaryMimeType be the result of parsing value.
auto temporary_mime_type = MimeSniff::MimeType::parse(value).release_value_but_fixme_should_propagate_errors();
auto temporary_mime_type = TRY(MimeSniff::MimeType::parse(value));
// 2. If temporaryMimeType is failure or its essence is "*/*", then continue.
if (!temporary_mime_type.has_value() || temporary_mime_type->essence() == "*/*"sv)
@ -346,7 +346,7 @@ Optional<MimeSniff::MimeType> HeaderList::extract_mime_type() const
}
// 5. Otherwise, if mimeTypes parameters["charset"] does not exist, and charset is non-null, set mimeTypes parameters["charset"] to charset.
else if (!mime_type->parameters().contains("charset"sv) && charset.has_value()) {
mime_type->set_parameter("charset"_string.release_value_but_fixme_should_propagate_errors(), charset.release_value()).release_value_but_fixme_should_propagate_errors();
TRY(mime_type->set_parameter(TRY("charset"_string), charset.release_value()));
}
}

View file

@ -51,7 +51,7 @@ public:
[[nodiscard]] ErrorOr<void> set(Header);
[[nodiscard]] ErrorOr<void> combine(Header);
[[nodiscard]] ErrorOr<Vector<Header>> sort_and_combine() const;
[[nodiscard]] Optional<MimeSniff::MimeType> extract_mime_type() const;
[[nodiscard]] ErrorOr<Optional<MimeSniff::MimeType>> extract_mime_type() const;
};
struct RangeHeaderValue {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -11,10 +11,10 @@
namespace Web::Fetch::Infrastructure {
// https://fetch.spec.whatwg.org/#ref-for-should-response-to-request-be-blocked-due-to-mime-type?
RequestOrResponseBlocking should_response_to_request_be_blocked_due_to_its_mime_type(Response const& response, Request const& request)
ErrorOr<RequestOrResponseBlocking> should_response_to_request_be_blocked_due_to_its_mime_type(Response const& response, Request const& request)
{
// 1. Let mimeType be the result of extracting a MIME type from responses header list.
auto mime_type = response.header_list()->extract_mime_type();
auto mime_type = TRY(response.header_list()->extract_mime_type());
// 2. If mimeType is failure, then return allowed.
if (!mime_type.has_value())

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -11,6 +11,6 @@
namespace Web::Fetch::Infrastructure {
[[nodiscard]] RequestOrResponseBlocking should_response_to_request_be_blocked_due_to_its_mime_type(Response const&, Request const&);
ErrorOr<RequestOrResponseBlocking> should_response_to_request_be_blocked_due_to_its_mime_type(Response const&, Request const&);
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -38,7 +38,7 @@ ErrorOr<RequestOrResponseBlocking> should_response_to_request_be_blocked_due_to_
return RequestOrResponseBlocking::Allowed;
// 2. Let mimeType be the result of extracting a MIME type from responses header list.
auto mime_type = response.header_list()->extract_mime_type();
auto mime_type = TRY(response.header_list()->extract_mime_type());
// 3. Let destination be requests destination.
auto const& destination = request.destination();

View file

@ -47,7 +47,7 @@ void Request::visit_edges(Cell::Visitor& visitor)
// https://fetch.spec.whatwg.org/#concept-body-mime-type
// https://fetch.spec.whatwg.org/#ref-for-concept-body-mime-type%E2%91%A0
Optional<MimeSniff::MimeType> Request::mime_type_impl() const
ErrorOr<Optional<MimeSniff::MimeType>> Request::mime_type_impl() const
{
// Objects including the Body interface mixin need to define an associated MIME type algorithm which takes no arguments and returns failure or a MIME type.
// A Request objects MIME type is to return the result of extracting a MIME type from its requests header list.

View file

@ -71,7 +71,7 @@ public:
virtual ~Request() override;
// ^BodyMixin
virtual Optional<MimeSniff::MimeType> mime_type_impl() const override;
virtual ErrorOr<Optional<MimeSniff::MimeType>> mime_type_impl() const override;
virtual Optional<Infrastructure::Body&> body_impl() override;
virtual Optional<Infrastructure::Body const&> body_impl() const override;

View file

@ -43,7 +43,7 @@ void Response::visit_edges(Cell::Visitor& visitor)
// https://fetch.spec.whatwg.org/#concept-body-mime-type
// https://fetch.spec.whatwg.org/#ref-for-concept-header-extract-mime-type%E2%91%A7
Optional<MimeSniff::MimeType> Response::mime_type_impl() const
ErrorOr<Optional<MimeSniff::MimeType>> Response::mime_type_impl() const
{
// Objects including the Body interface mixin need to define an associated MIME type algorithm which takes no arguments and returns failure or a MIME type.
// A Response objects MIME type is to return the result of extracting a MIME type from its responses header list.

View file

@ -38,7 +38,7 @@ public:
virtual ~Response() override;
// ^BodyMixin
virtual Optional<MimeSniff::MimeType> mime_type_impl() const override;
virtual ErrorOr<Optional<MimeSniff::MimeType>> mime_type_impl() const override;
virtual Optional<Infrastructure::Body&> body_impl() override;
virtual Optional<Infrastructure::Body const&> body_impl() const override;