From 000ef96613466427a4a84103778e6f0a423e2845 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sat, 3 Apr 2021 15:11:36 +0200 Subject: LibWeb: Pass optional status code to ResourceLoader callbacks This is needed for XMLHttpRequest, and will certainly be useful for other things, too. --- Userland/Libraries/LibWeb/Loader/FrameLoader.cpp | 6 ++-- Userland/Libraries/LibWeb/Loader/Resource.cpp | 6 ++-- Userland/Libraries/LibWeb/Loader/Resource.h | 5 +-- .../Libraries/LibWeb/Loader/ResourceLoader.cpp | 42 +++++++++++----------- Userland/Libraries/LibWeb/Loader/ResourceLoader.h | 6 ++-- 5 files changed, 34 insertions(+), 31 deletions(-) (limited to 'Userland/Libraries/LibWeb/Loader') diff --git a/Userland/Libraries/LibWeb/Loader/FrameLoader.cpp b/Userland/Libraries/LibWeb/Loader/FrameLoader.cpp index de60ced9da..ac39ef1b7f 100644 --- a/Userland/Libraries/LibWeb/Loader/FrameLoader.cpp +++ b/Userland/Libraries/LibWeb/Loader/FrameLoader.cpp @@ -178,7 +178,7 @@ bool FrameLoader::load(const LoadRequest& request, Type type) ResourceLoader::the().load( favicon_url, - [this, favicon_url](auto data, auto&) { + [this, favicon_url](auto data, auto&, auto) { dbgln("Favicon downloaded, {} bytes from {}", data.size(), favicon_url); auto decoder = Gfx::ImageDecoder::create(data.data(), data.size()); auto bitmap = decoder->bitmap(); @@ -226,7 +226,7 @@ void FrameLoader::load_error_page(const URL& failed_url, const String& error) auto error_page_url = "file:///res/html/error.html"; ResourceLoader::the().load( error_page_url, - [this, failed_url, error](auto data, auto&) { + [this, failed_url, error](auto data, auto&, auto) { VERIFY(!data.is_null()); #pragma GCC diagnostic ignored "-Wformat-nonliteral" auto html = String::format( @@ -238,7 +238,7 @@ void FrameLoader::load_error_page(const URL& failed_url, const String& error) VERIFY(document); frame().set_document(document); }, - [](auto error) { + [](auto& error, auto) { dbgln("Failed to load error page: {}", error); VERIFY_NOT_REACHED(); }); diff --git a/Userland/Libraries/LibWeb/Loader/Resource.cpp b/Userland/Libraries/LibWeb/Loader/Resource.cpp index 048d500aec..064965f1ee 100644 --- a/Userland/Libraries/LibWeb/Loader/Resource.cpp +++ b/Userland/Libraries/LibWeb/Loader/Resource.cpp @@ -85,11 +85,12 @@ static String mime_type_from_content_type(const String& content_type) return content_type; } -void Resource::did_load(Badge, ReadonlyBytes data, const HashMap& headers) +void Resource::did_load(Badge, ReadonlyBytes data, const HashMap& headers, Optional status_code) { VERIFY(!m_loaded); m_encoded_data = ByteBuffer::copy(data); m_response_headers = headers; + m_status_code = move(status_code); m_loaded = true; auto content_type = headers.get("Content-Type"); @@ -116,9 +117,10 @@ void Resource::did_load(Badge, ReadonlyBytes data, const HashMap }); } -void Resource::did_fail(Badge, const String& error) +void Resource::did_fail(Badge, const String& error, Optional status_code) { m_error = error; + m_status_code = move(status_code); m_failed = true; for_each_client([](auto& client) { diff --git a/Userland/Libraries/LibWeb/Loader/Resource.h b/Userland/Libraries/LibWeb/Loader/Resource.h index 9838071c65..b108aa190e 100644 --- a/Userland/Libraries/LibWeb/Loader/Resource.h +++ b/Userland/Libraries/LibWeb/Loader/Resource.h @@ -77,8 +77,8 @@ public: void for_each_client(Function); - void did_load(Badge, ReadonlyBytes data, const HashMap& headers); - void did_fail(Badge, const String& error); + void did_load(Badge, ReadonlyBytes data, const HashMap& headers, Optional status_code); + void did_fail(Badge, const String& error, Optional status_code); protected: explicit Resource(Type, const LoadRequest&); @@ -93,6 +93,7 @@ private: String m_encoding; String m_mime_type; HashMap m_response_headers; + Optional m_status_code; HashTable m_clients; }; diff --git a/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp index 49b5e48ce6..4d438aadb0 100644 --- a/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp +++ b/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp @@ -52,19 +52,19 @@ ResourceLoader::ResourceLoader() { } -void ResourceLoader::load_sync(const URL& url, Function& response_headers)> success_callback, Function error_callback) +void ResourceLoader::load_sync(const URL& url, Function& response_headers, Optional status_code)> success_callback, Function status_code)> error_callback) { Core::EventLoop loop; load( url, - [&](auto data, auto& response_headers) { - success_callback(data, response_headers); + [&](auto data, auto& response_headers, auto status_code) { + success_callback(data, response_headers, status_code); loop.quit(0); }, - [&](auto& string) { + [&](auto& string, auto status_code) { if (error_callback) - error_callback(string); + error_callback(string, status_code); loop.quit(0); }); @@ -99,17 +99,17 @@ RefPtr ResourceLoader::load_resource(Resource::Type type, const LoadRe load( request, - [=](auto data, auto& headers) { - const_cast(*resource).did_load({}, data, headers); + [=](auto data, auto& headers, auto status_code) { + const_cast(*resource).did_load({}, data, headers, status_code); }, - [=](auto& error) { - const_cast(*resource).did_fail({}, error); + [=](auto& error, auto status_code) { + const_cast(*resource).did_fail({}, error, status_code); }); return resource; } -void ResourceLoader::load(const LoadRequest& request, Function& response_headers)> success_callback, Function error_callback) +void ResourceLoader::load(const LoadRequest& request, Function& response_headers, Optional status_code)> success_callback, Function status_code)> error_callback) { auto& url = request.url(); @@ -120,14 +120,14 @@ void ResourceLoader::load(const LoadRequest& request, Functionopen(Core::IODevice::OpenMode::ReadOnly)) { dbgln("ResourceLoader::load: Error: {}", f->error_string()); if (error_callback) - error_callback(f->error_string()); + error_callback(f->error_string(), {}); return; } auto data = f->read_all(); deferred_invoke([data = move(data), success_callback = move(success_callback)](auto&) { - success_callback(data, {}); + success_callback(data, {}, {}); }); return; } @@ -179,13 +179,13 @@ void ResourceLoader::load(const LoadRequest& request, Functionon_buffered_download_finish = [this, success_callback = move(success_callback), error_callback = move(error_callback), download](bool success, auto, auto& response_headers, auto status_code, ReadonlyBytes payload) { if (status_code.has_value() && status_code.value() >= 400 && status_code.value() <= 499) { if (error_callback) - error_callback(String::formatted("HTTP error ({})", status_code.value())); + error_callback(String::formatted("HTTP error ({})", status_code.value()), status_code); return; } --m_pending_loads; @@ -193,14 +193,14 @@ void ResourceLoader::load(const LoadRequest& request, Function(*download).on_buffered_download_finish = nullptr; }); - success_callback(payload, response_headers); + success_callback(payload, response_headers, status_code); }; download->set_should_buffer_all_input(true); download->on_certificate_requested = []() -> Protocol::Download::CertificateAndKey { @@ -213,10 +213,10 @@ void ResourceLoader::load(const LoadRequest& request, Function& response_headers)> success_callback, Function error_callback) +void ResourceLoader::load(const URL& url, Function& response_headers, Optional status_code)> success_callback, Function status_code)> error_callback) { LoadRequest request; request.set_url(url); diff --git a/Userland/Libraries/LibWeb/Loader/ResourceLoader.h b/Userland/Libraries/LibWeb/Loader/ResourceLoader.h index ad23dd8da1..02fda51250 100644 --- a/Userland/Libraries/LibWeb/Loader/ResourceLoader.h +++ b/Userland/Libraries/LibWeb/Loader/ResourceLoader.h @@ -44,9 +44,9 @@ public: RefPtr load_resource(Resource::Type, const LoadRequest&); - void load(const LoadRequest&, Function& response_headers)> success_callback, Function error_callback = nullptr); - void load(const URL&, Function& response_headers)> success_callback, Function error_callback = nullptr); - void load_sync(const URL&, Function& response_headers)> success_callback, Function error_callback = nullptr); + void load(const LoadRequest&, Function& response_headers, Optional status_code)> success_callback, Function status_code)> error_callback = nullptr); + void load(const URL&, Function& response_headers, Optional status_code)> success_callback, Function status_code)> error_callback = nullptr); + void load_sync(const URL&, Function& response_headers, Optional status_code)> success_callback, Function status_code)> error_callback = nullptr); Function on_load_counter_change; -- cgit v1.2.3