diff options
author | Andreas Kling <kling@serenityos.org> | 2022-02-16 12:34:21 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-16 22:21:45 +0100 |
commit | 0e2cd5540a4be6367168fb8d7e0445153486b26c (patch) | |
tree | ac2fb74637bc2791cfaa0b7f2a8cd9d4ab5187f5 /Userland/Libraries/LibWeb/Loader | |
parent | df1670415d69f99077f234dabfe98fe6ecf95445 (diff) | |
download | serenity-0e2cd5540a4be6367168fb8d7e0445153486b26c.zip |
LibWeb: Follow HTTP 3xx redirections when loading images
This basically copies some logic from FrameLoader to ImageLoader.
Ideally we'd share this code, but for now let's just get redirected
images to show up. :^)
Diffstat (limited to 'Userland/Libraries/LibWeb/Loader')
-rw-r--r-- | Userland/Libraries/LibWeb/Loader/ImageLoader.cpp | 25 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Loader/ImageLoader.h | 3 |
2 files changed, 28 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/Loader/ImageLoader.cpp b/Userland/Libraries/LibWeb/Loader/ImageLoader.cpp index aa60056331..d777e2da1c 100644 --- a/Userland/Libraries/LibWeb/Loader/ImageLoader.cpp +++ b/Userland/Libraries/LibWeb/Loader/ImageLoader.cpp @@ -22,6 +22,12 @@ ImageLoader::ImageLoader(DOM::Element& owner_element) void ImageLoader::load(const AK::URL& url) { + m_redirects_count = 0; + load_without_resetting_redirect_counter(url); +} + +void ImageLoader::load_without_resetting_redirect_counter(AK::URL const& url) +{ m_loading_state = LoadingState::Loading; auto request = LoadRequest::create_for_url_on_page(url, m_owner_element.document().page()); @@ -45,6 +51,25 @@ void ImageLoader::resource_did_load() { VERIFY(resource()); + // For 3xx (Redirection) responses, the Location value refers to the preferred target resource for automatically redirecting the request. + auto status_code = resource()->status_code(); + if (status_code.has_value() && *status_code >= 300 && *status_code <= 399) { + auto location = resource()->response_headers().get("Location"); + if (location.has_value()) { + if (m_redirects_count > maximum_redirects_allowed) { + m_redirects_count = 0; + m_loading_state = LoadingState::Failed; + if (on_fail) + on_fail(); + return; + } + m_redirects_count++; + load_without_resetting_redirect_counter(resource()->url().complete_url(location.value())); + return; + } + } + m_redirects_count = 0; + if (!resource()->mime_type().starts_with("image/")) { m_loading_state = LoadingState::Failed; if (on_fail) diff --git a/Userland/Libraries/LibWeb/Loader/ImageLoader.h b/Userland/Libraries/LibWeb/Loader/ImageLoader.h index a5aa248ff8..43fa0da297 100644 --- a/Userland/Libraries/LibWeb/Loader/ImageLoader.h +++ b/Userland/Libraries/LibWeb/Loader/ImageLoader.h @@ -35,6 +35,8 @@ public: Function<void()> on_animate; private: + void load_without_resetting_redirect_counter(AK::URL const&); + // ^ImageResourceClient virtual void resource_did_load() override; virtual void resource_did_fail() override; @@ -57,6 +59,7 @@ private: size_t m_loops_completed { 0 }; LoadingState m_loading_state { LoadingState::Loading }; NonnullRefPtr<Core::Timer> m_timer; + size_t m_redirects_count { 0 }; }; } |