1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:38:12 +00:00

LibWeb: Implement ad-hoc steps to allow LibWeb to load resource:// URLs

The resource:// scheme is used for Core::Resource files. Currently, any
users of resource:// URLs in Ladybird must manually create the Resource
and extract its data. This will allow for passing the resource:// URL
along for LibWeb to handle.
This commit is contained in:
Timothy Flynn 2023-12-23 15:42:05 -05:00 committed by Andreas Kling
parent 48f39f0555
commit e511a264fe
3 changed files with 40 additions and 8 deletions

View file

@ -832,6 +832,10 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> scheme_fetch(JS::Realm& r
// Return the result of running HTTP fetch given fetchParams.
return http_fetch(realm, fetch_params);
}
// AD-HOC: "resource"
else if (request->current_url().scheme() == "resource"sv) {
return TRY(nonstandard_resource_loader_file_or_http_network_fetch(realm, fetch_params));
}
// 4. Return a network error.
auto message = request->current_url().scheme() == "about"sv

View file

@ -27,7 +27,10 @@ inline constexpr Array HTTP_SCHEMES = {
// https://fetch.spec.whatwg.org/#fetch-scheme
// A fetch scheme is "about", "blob", "data", "file", or an HTTP(S) scheme.
inline constexpr Array FETCH_SCHEMES = {
"about"sv, "blob"sv, "data"sv, "file"sv, "http"sv, "https"sv
"about"sv, "blob"sv, "data"sv, "file"sv, "http"sv, "https"sv,
// AD-HOC: Internal fetch schemes:
"resource"sv
};
[[nodiscard]] bool is_local_url(AK::URL const&);

View file

@ -10,6 +10,7 @@
#include <LibCore/Directory.h>
#include <LibCore/ElapsedTimer.h>
#include <LibCore/MimeData.h>
#include <LibCore/Resource.h>
#include <LibWeb/Cookie/Cookie.h>
#include <LibWeb/Cookie/ParsedCookie.h>
#include <LibWeb/Loader/ContentFilter.h>
@ -158,6 +159,18 @@ static void store_response_cookies(Page& page, AK::URL const& url, ByteString co
static size_t resource_id = 0;
static HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> response_headers_for_file(StringView path)
{
// For file:// and resource:// URLs, we have to guess the MIME type, since there's no HTTP header to tell us what
// it is. We insert a fake Content-Type header here, so that clients can use it to learn the MIME type.
auto mime_type = Core::guess_mime_type_based_on_filename(path);
HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> response_headers;
response_headers.set("Content-Type"sv, mime_type);
return response_headers;
}
void ResourceLoader::load(LoadRequest& request, SuccessCallback success_callback, ErrorCallback error_callback, Optional<u32> timeout, TimeoutCallback timeout_callback)
{
auto& url = request.url();
@ -230,6 +243,22 @@ void ResourceLoader::load(LoadRequest& request, SuccessCallback success_callback
return;
}
if (url.scheme() == "resource") {
auto resource = Core::Resource::load_from_uri(url.serialize());
if (resource.is_error()) {
log_failure(request, resource.error());
return;
}
auto data = resource.value()->data();
auto response_headers = response_headers_for_file(url.serialize_path());
log_success(request);
success_callback(data, response_headers, {});
return;
}
if (url.scheme() == "file") {
if (request.page())
m_page = request.page();
@ -290,15 +319,11 @@ void ResourceLoader::load(LoadRequest& request, SuccessCallback success_callback
error_callback(ByteString::formatted("{}", maybe_data.error()), 500u, {}, {});
return;
}
auto data = maybe_data.release_value();
auto response_headers = response_headers_for_file(request.url().serialize_path());
log_success(request);
// NOTE: For file:// URLs, we have to guess the MIME type, since there's no HTTP header to tell us what this is.
// We insert a fake Content-Type header here, so that clients can use it to learn the MIME type.
HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> response_headers;
auto mime_type = Core::guess_mime_type_based_on_filename(request.url().serialize_path());
response_headers.set("Content-Type"sv, mime_type);
success_callback(data, response_headers, {});
});