LibWeb: Implement 'Should response be blocked due to its MIME type?' AO

This commit is contained in:
Linus Groh 2022-10-13 19:57:40 +02:00
parent 62228f0870
commit 7e46150a71
Notes: sideshowbarker 2024-07-17 09:41:18 +09:00
3 changed files with 54 additions and 0 deletions

View file

@ -131,6 +131,7 @@ set(SOURCES
Fetch/Infrastructure/HTTP/Requests.cpp
Fetch/Infrastructure/HTTP/Responses.cpp
Fetch/Infrastructure/HTTP/Statuses.cpp
Fetch/Infrastructure/MimeTypeBlocking.cpp
Fetch/Infrastructure/PortBlocking.cpp
Fetch/Infrastructure/URL.cpp
Fetch/Request.cpp

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
#include <LibWeb/Fetch/Infrastructure/MimeTypeBlocking.h>
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)
{
// 1. Let mimeType be the result of extracting a MIME type from responses header list.
auto mime_type = response.header_list()->extract_mime_type();
// 2. If mimeType is failure, then return allowed.
if (!mime_type.has_value())
return RequestOrResponseBlocking::Allowed;
// 3. Let destination be requests destination.
// 4. If destination is script-like and one of the following is true, then return blocked:
if (request.destination_is_script_like() && (
// - mimeTypes essence starts with "audio/", "image/", or "video/".
any_of(Array { "audio/"sv, "image/"sv, "video/"sv }, [&](auto prefix) { return mime_type->essence().starts_with(prefix); })
// - mimeTypes essence is "text/csv".
|| mime_type->essence() == "text/csv"sv)) {
return RequestOrResponseBlocking::Blocked;
}
// 5. Return allowed.
return RequestOrResponseBlocking::Allowed;
}
}

View file

@ -0,0 +1,16 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/Fetch/Infrastructure/RequestOrResponseBlocking.h>
#include <LibWeb/Forward.h>
namespace Web::Fetch::Infrastructure {
[[nodiscard]] RequestOrResponseBlocking should_response_to_request_be_blocked_due_to_its_mime_type(Response const&, Request const&);
}