| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2020, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-17 18:17:00 +01:00
										 |  |  | #include <AK/Debug.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-01 22:09:38 +02:00
										 |  |  | #include <AK/Function.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-27 19:49:43 +02:00
										 |  |  | #include <LibCore/MimeData.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-20 21:22:54 +01:00
										 |  |  | #include <LibTextCodec/Decoder.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-26 15:08:16 +02:00
										 |  |  | #include <LibWeb/HTML/HTMLImageElement.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | #include <LibWeb/Loader/Resource.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-23 14:00:01 -04:00
										 |  |  | #include <LibWeb/Loader/ResourceLoader.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-07 20:30:31 +02:00
										 |  |  | #include <LibWeb/Platform/EventLoopPlugin.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | NonnullRefPtr<Resource> Resource::create(Badge<ResourceLoader>, Type type, LoadRequest const& request) | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |     return adopt_ref(*new Resource(type, request)); | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | Resource::Resource(Type type, LoadRequest const& request) | 
					
						
							| 
									
										
										
										
											2020-06-01 21:58:29 +02:00
										 |  |  |     : m_request(request) | 
					
						
							| 
									
										
										
										
											2020-06-02 20:27:26 +02:00
										 |  |  |     , m_type(type) | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 08:20:41 -04:00
										 |  |  | Resource::Resource(Type type, Resource& resource) | 
					
						
							|  |  |  |     : m_request(resource.m_request) | 
					
						
							|  |  |  |     , m_encoded_data(move(resource.m_encoded_data)) | 
					
						
							|  |  |  |     , m_type(type) | 
					
						
							| 
									
										
										
										
											2023-05-30 20:24:52 +03:00
										 |  |  |     , m_state(resource.m_state) | 
					
						
							| 
									
										
										
										
											2022-03-23 08:20:41 -04:00
										 |  |  |     , m_error(move(resource.m_error)) | 
					
						
							|  |  |  |     , m_encoding(move(resource.m_encoding)) | 
					
						
							|  |  |  |     , m_mime_type(move(resource.m_mime_type)) | 
					
						
							|  |  |  |     , m_response_headers(move(resource.m_response_headers)) | 
					
						
							|  |  |  |     , m_status_code(move(resource.m_status_code)) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-23 14:00:01 -04:00
										 |  |  |     ResourceLoader::the().evict_from_cache(m_request); | 
					
						
							| 
									
										
										
										
											2022-03-23 08:20:41 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-14 13:21:51 -06:00
										 |  |  | Resource::~Resource() = default; | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-01 22:09:38 +02:00
										 |  |  | void Resource::for_each_client(Function<void(ResourceClient&)> callback) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Vector<WeakPtr<ResourceClient>, 16> clients_copy; | 
					
						
							|  |  |  |     clients_copy.ensure_capacity(m_clients.size()); | 
					
						
							|  |  |  |     for (auto* client : m_clients) | 
					
						
							|  |  |  |         clients_copy.append(client->make_weak_ptr()); | 
					
						
							|  |  |  |     for (auto client : clients_copy) { | 
					
						
							|  |  |  |         if (client) | 
					
						
							|  |  |  |             callback(*client); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  | static Optional<ByteString> encoding_from_content_type(ByteString const& content_type) | 
					
						
							| 
									
										
										
										
											2020-06-06 14:02:43 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-24 11:50:46 +02:00
										 |  |  |     auto offset = content_type.find("charset="sv); | 
					
						
							| 
									
										
										
										
											2020-06-25 22:23:47 +02:00
										 |  |  |     if (offset.has_value()) { | 
					
						
							|  |  |  |         auto encoding = content_type.substring(offset.value() + 8, content_type.length() - offset.value() - 8).to_lowercase(); | 
					
						
							|  |  |  |         if (encoding.length() >= 2 && encoding.starts_with('"') && encoding.ends_with('"')) | 
					
						
							|  |  |  |             return encoding.substring(1, encoding.length() - 2); | 
					
						
							|  |  |  |         if (encoding.length() >= 2 && encoding.starts_with('\'') && encoding.ends_with('\'')) | 
					
						
							|  |  |  |             return encoding.substring(1, encoding.length() - 2); | 
					
						
							|  |  |  |         return encoding; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-06 14:02:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 10:07:17 +02:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-06-06 14:02:43 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  | static ByteString mime_type_from_content_type(ByteString const& content_type) | 
					
						
							| 
									
										
										
										
											2020-06-06 14:02:43 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-24 11:50:46 +02:00
										 |  |  |     auto offset = content_type.find(';'); | 
					
						
							| 
									
										
										
										
											2020-06-06 14:02:43 +02:00
										 |  |  |     if (offset.has_value()) | 
					
						
							|  |  |  |         return content_type.substring(0, offset.value()).to_lowercase(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return content_type; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-13 17:23:31 +00:00
										 |  |  | static bool is_valid_encoding(StringView encoding) | 
					
						
							| 
									
										
										
										
											2022-03-20 21:22:54 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-17 17:45:08 +00:00
										 |  |  |     return TextCodec::decoder_for(encoding).has_value(); | 
					
						
							| 
									
										
										
										
											2022-03-20 21:22:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-09 11:28:37 +02:00
										 |  |  | void Resource::did_load(Badge<ResourceLoader>, ReadonlyBytes data, HTTP::HeaderMap const& headers, Optional<u32> status_code) | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-05-30 20:24:52 +03:00
										 |  |  |     VERIFY(m_state == State::Pending); | 
					
						
							| 
									
										
										
										
											2021-09-06 03:29:52 +04:30
										 |  |  |     // FIXME: Handle OOM failure.
 | 
					
						
							| 
									
										
										
										
											2022-01-20 17:47:39 +00:00
										 |  |  |     m_encoded_data = ByteBuffer::copy(data).release_value_but_fixme_should_propagate_errors(); | 
					
						
							| 
									
										
										
										
											2024-06-09 11:28:37 +02:00
										 |  |  |     m_response_headers = headers; | 
					
						
							| 
									
										
										
										
											2021-04-03 15:11:36 +02:00
										 |  |  |     m_status_code = move(status_code); | 
					
						
							| 
									
										
										
										
											2023-05-30 20:24:52 +03:00
										 |  |  |     m_state = State::Loaded; | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-06 14:02:43 +02:00
										 |  |  |     auto content_type = headers.get("Content-Type"); | 
					
						
							| 
									
										
										
										
											2021-05-12 10:07:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-06 14:02:43 +02:00
										 |  |  |     if (content_type.has_value()) { | 
					
						
							| 
									
										
										
										
											2021-05-01 21:10:08 +02:00
										 |  |  |         dbgln_if(RESOURCE_DEBUG, "Content-Type header: '{}'", content_type.value()); | 
					
						
							| 
									
										
										
										
											2020-06-06 14:02:43 +02:00
										 |  |  |         m_mime_type = mime_type_from_content_type(content_type.value()); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2021-05-19 13:37:36 +00:00
										 |  |  |         auto content_type_options = headers.get("X-Content-Type-Options"); | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |         if (content_type_options.value_or("").equals_ignoring_ascii_case("nosniff"sv)) { | 
					
						
							| 
									
										
										
										
											2021-05-19 13:37:36 +00:00
										 |  |  |             m_mime_type = "text/plain"; | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2025-02-14 15:31:43 +05:00
										 |  |  |             m_mime_type = Core::guess_mime_type_based_on_filename(url().file_path()); | 
					
						
							| 
									
										
										
										
											2021-05-19 13:37:36 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-06 14:02:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 10:21:00 +02:00
										 |  |  |     m_encoding = {}; | 
					
						
							| 
									
										
										
										
											2021-05-12 10:07:17 +02:00
										 |  |  |     if (content_type.has_value()) { | 
					
						
							|  |  |  |         auto encoding = encoding_from_content_type(content_type.value()); | 
					
						
							| 
									
										
										
										
											2022-03-20 21:22:54 +01:00
										 |  |  |         if (encoding.has_value() && is_valid_encoding(encoding.value())) { | 
					
						
							|  |  |  |             dbgln_if(RESOURCE_DEBUG, "Set encoding '{}' from Content-Type", encoding.value()); | 
					
						
							| 
									
										
										
										
											2021-05-12 10:07:17 +02:00
										 |  |  |             m_encoding = encoding.value(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |     for_each_client([](auto& client) { | 
					
						
							|  |  |  |         client.resource_did_load(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-21 18:35:31 +02:00
										 |  |  | void Resource::did_fail(Badge<ResourceLoader>, ByteString const& error, ReadonlyBytes data, HTTP::HeaderMap const& headers, Optional<u32> status_code) | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     m_error = error; | 
					
						
							| 
									
										
										
										
											2024-09-21 18:35:31 +02:00
										 |  |  |     m_encoded_data = ByteBuffer::copy(data).release_value_but_fixme_should_propagate_errors(); | 
					
						
							|  |  |  |     m_response_headers = headers; | 
					
						
							| 
									
										
										
										
											2021-04-03 15:11:36 +02:00
										 |  |  |     m_status_code = move(status_code); | 
					
						
							| 
									
										
										
										
											2023-05-30 20:24:52 +03:00
										 |  |  |     m_state = State::Failed; | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for_each_client([](auto& client) { | 
					
						
							|  |  |  |         client.resource_did_fail(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Resource::register_client(Badge<ResourceClient>, ResourceClient& client) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY(!m_clients.contains(&client)); | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |     m_clients.set(&client); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Resource::unregister_client(Badge<ResourceClient>, ResourceClient& client) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY(m_clients.contains(&client)); | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |     m_clients.remove(&client); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ResourceClient::set_resource(Resource* resource) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_resource) | 
					
						
							|  |  |  |         m_resource->unregister_client({}, *this); | 
					
						
							|  |  |  |     m_resource = resource; | 
					
						
							|  |  |  |     if (m_resource) { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY(resource->type() == client_type()); | 
					
						
							| 
									
										
										
										
											2020-06-05 23:32:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |         m_resource->register_client({}, *this); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-20 18:19:55 +01:00
										 |  |  |         // For resources that are already loaded, we fire their load/fail callbacks via the event loop.
 | 
					
						
							|  |  |  |         // This ensures that these callbacks always happen in a consistent way, instead of being invoked
 | 
					
						
							|  |  |  |         // synchronously in some cases, and asynchronously in others.
 | 
					
						
							|  |  |  |         if (resource->is_loaded() || resource->is_failed()) { | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |             Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(ResourceLoader::the().heap(), [weak_this = make_weak_ptr(), strong_resource = NonnullRefPtr { *m_resource }] { | 
					
						
							| 
									
										
										
										
											2022-09-18 02:15:01 +02:00
										 |  |  |                 if (!weak_this) | 
					
						
							|  |  |  |                     return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (weak_this->m_resource != strong_resource.ptr()) | 
					
						
							| 
									
										
										
										
											2022-03-20 18:19:55 +01:00
										 |  |  |                     return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // Make sure that reused resources also have their load callback fired.
 | 
					
						
							| 
									
										
										
										
											2022-09-21 10:36:29 +02:00
										 |  |  |                 if (weak_this->m_resource->is_loaded()) { | 
					
						
							| 
									
										
										
										
											2022-09-18 02:15:01 +02:00
										 |  |  |                     weak_this->resource_did_load(); | 
					
						
							| 
									
										
										
										
											2022-09-21 10:36:29 +02:00
										 |  |  |                     return; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-03-20 18:19:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 // Make sure that reused resources also have their fail callback fired.
 | 
					
						
							| 
									
										
										
										
											2022-09-21 10:36:29 +02:00
										 |  |  |                 if (weak_this->m_resource->is_failed()) { | 
					
						
							| 
									
										
										
										
											2022-09-18 02:15:01 +02:00
										 |  |  |                     weak_this->resource_did_fail(); | 
					
						
							| 
									
										
										
										
											2022-09-21 10:36:29 +02:00
										 |  |  |                     return; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2024-10-31 02:39:29 +13:00
										 |  |  |             })); | 
					
						
							| 
									
										
										
										
											2022-03-20 18:19:55 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ResourceClient::~ResourceClient() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_resource) | 
					
						
							|  |  |  |         m_resource->unregister_client({}, *this); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |