| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											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>
 | 
					
						
							| 
									
										
										
										
											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>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-02 20:27:26 +02:00
										 |  |  | NonnullRefPtr<Resource> Resource::create(Badge<ResourceLoader>, Type type, const LoadRequest& request) | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-02 20:27:26 +02:00
										 |  |  |     if (type == Type::Image) | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |         return adopt_ref(*new ImageResource(request)); | 
					
						
							|  |  |  |     return adopt_ref(*new Resource(type, request)); | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-02 20:27:26 +02:00
										 |  |  | Resource::Resource(Type type, const LoadRequest& 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-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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 10:07:17 +02:00
										 |  |  | static Optional<String> encoding_from_content_type(const String& 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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-11 00:07:16 +02:00
										 |  |  | static String mime_type_from_content_type(const String& 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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-03 15:11:36 +02:00
										 |  |  | void Resource::did_load(Badge<ResourceLoader>, ReadonlyBytes data, const HashMap<String, String, CaseInsensitiveStringTraits>& headers, Optional<u32> status_code) | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY(!m_loaded); | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |     m_response_headers = headers; | 
					
						
							| 
									
										
										
										
											2021-04-03 15:11:36 +02:00
										 |  |  |     m_status_code = move(status_code); | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |     m_loaded = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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()); | 
					
						
							| 
									
										
										
										
											2021-12-21 01:27:38 +01:00
										 |  |  |         // FIXME: "The Quite OK Image Format" doesn't have an official mime type yet,
 | 
					
						
							|  |  |  |         //        and servers like nginx will send a generic octet-stream mime type instead.
 | 
					
						
							|  |  |  |         //        Let's use image/x-qoi for now, which is also what our Core::MimeData uses & would guess.
 | 
					
						
							|  |  |  |         if (m_mime_type == "application/octet-stream" && url().path().ends_with(".qoi")) | 
					
						
							|  |  |  |             m_mime_type = "image/x-qoi"; | 
					
						
							| 
									
										
										
										
											2020-06-06 14:02:43 +02:00
										 |  |  |     } else if (url().protocol() == "data" && !url().data_mime_type().is_empty()) { | 
					
						
							| 
									
										
										
										
											2021-02-07 15:33:24 +03:30
										 |  |  |         dbgln_if(RESOURCE_DEBUG, "This is a data URL with mime-type _{}_", url().data_mime_type()); | 
					
						
							| 
									
										
										
										
											2020-06-06 14:02:43 +02:00
										 |  |  |         m_mime_type = url().data_mime_type(); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2021-05-19 13:37:36 +00:00
										 |  |  |         auto content_type_options = headers.get("X-Content-Type-Options"); | 
					
						
							|  |  |  |         if (content_type_options.value_or("").equals_ignoring_case("nosniff")) { | 
					
						
							|  |  |  |             m_mime_type = "text/plain"; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             m_mime_type = Core::guess_mime_type_based_on_filename(url().path()); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											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()); | 
					
						
							|  |  |  |         if (encoding.has_value()) { | 
					
						
							|  |  |  |             dbgln_if(RESOURCE_DEBUG, "Set encoding '{}' from Content-Type", encoding.has_value()); | 
					
						
							|  |  |  |             m_encoding = encoding.value(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |     for_each_client([](auto& client) { | 
					
						
							|  |  |  |         client.resource_did_load(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-03 15:11:36 +02:00
										 |  |  | void Resource::did_fail(Badge<ResourceLoader>, const String& error, Optional<u32> status_code) | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     m_error = error; | 
					
						
							| 
									
										
										
										
											2021-04-03 15:11:36 +02:00
										 |  |  |     m_status_code = move(status_code); | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |     m_failed = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Make sure that reused resources also have their load callback fired.
 | 
					
						
							|  |  |  |         if (resource->is_loaded()) | 
					
						
							|  |  |  |             resource_did_load(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Make sure that reused resources also have their fail callback fired.
 | 
					
						
							|  |  |  |         if (resource->is_failed()) | 
					
						
							|  |  |  |             resource_did_fail(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ResourceClient::~ResourceClient() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_resource) | 
					
						
							|  |  |  |         m_resource->unregister_client({}, *this); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |