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. --- .../Libraries/LibWeb/HTML/HTMLScriptElement.cpp | 4 +-- 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 ++-- Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp | 4 +-- Userland/Utilities/test-web.cpp | 8 ++--- 8 files changed, 42 insertions(+), 39 deletions(-) (limited to 'Userland') diff --git a/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp index 658a62983f..f47cfb49e9 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp @@ -231,7 +231,7 @@ void HTMLScriptElement::prepare_script() m_script_filename = url.basename(); ResourceLoader::the().load_sync( url, - [this, url](auto data, auto&) { + [this, url](auto data, auto&, auto) { if (data.is_null()) { dbgln("HTMLScriptElement: Failed to load {}", url); return; @@ -239,7 +239,7 @@ void HTMLScriptElement::prepare_script() m_script_source = String::copy(data); script_became_ready(); }, - [this](auto&) { + [this](auto&, auto) { m_failed_to_load = true; }); } else { 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; diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp index b7ad3b4b93..94a6bef629 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp @@ -228,7 +228,7 @@ DOM::ExceptionOr XMLHttpRequest::send() // we need to make ResourceLoader give us more detailed updates than just "done" and "error". ResourceLoader::the().load( request, - [weak_this = make_weak_ptr()](auto data, auto&) { + [weak_this = make_weak_ptr()](auto data, auto&, auto) { if (!weak_this) return; auto& xhr = const_cast(*weak_this); @@ -248,7 +248,7 @@ DOM::ExceptionOr XMLHttpRequest::send() xhr.fire_progress_event(EventNames::load, transmitted, length); xhr.fire_progress_event(EventNames::loadend, transmitted, length); }, - [weak_this = make_weak_ptr()](auto& error) { + [weak_this = make_weak_ptr()](auto& error, auto) { if (!weak_this) return; dbgln("XHR failed to load: {}", error); diff --git a/Userland/Utilities/test-web.cpp b/Userland/Utilities/test-web.cpp index 4387283179..48d6027afb 100644 --- a/Userland/Utilities/test-web.cpp +++ b/Userland/Utilities/test-web.cpp @@ -211,11 +211,11 @@ void TestRunner::run() Web::ResourceLoader::the().load_sync( page_to_load, - [&](auto data, auto&) { + [&](auto data, auto&, auto) { Web::HTML::HTMLDocumentParser parser(*m_page_view->document(), data, "utf-8"); parser.run(page_to_load); }, - [page_to_load](auto error) { + [page_to_load](auto& error, auto) { printf("Failed to load test page: %s (%s)", page_to_load.to_string().characters(), error.characters()); cleanup_and_exit(); }); @@ -317,7 +317,7 @@ JSFileResult TestRunner::run_file_test(const String& test_path) Web::ResourceLoader::the().load_sync( page_to_load, - [&](auto data, auto&) { + [&](auto data, auto&, auto) { // Create a new parser and immediately get its document to replace the old interpreter. auto document = Web::DOM::Document::create(); Web::HTML::HTMLDocumentParser parser(document, data, "utf-8"); @@ -417,7 +417,7 @@ JSFileResult TestRunner::run_file_test(const String& test_path) file_result.time_taken = get_time_in_ms() - start_time; m_total_elapsed_time_in_ms += file_result.time_taken; }, - [page_to_load](auto error) { + [page_to_load](auto& error, auto) { printf("Failed to load test page: %s (%s)", page_to_load.to_string().characters(), error.characters()); cleanup_and_exit(); }); -- cgit v1.2.3