Serenity Operating System
at master 200 lines 7.0 kB view raw
1/* 2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <AK/Debug.h> 8#include <AK/Function.h> 9#include <LibCore/MimeData.h> 10#include <LibTextCodec/Decoder.h> 11#include <LibWeb/HTML/HTMLImageElement.h> 12#include <LibWeb/Loader/Resource.h> 13#include <LibWeb/Loader/ResourceLoader.h> 14#include <LibWeb/Platform/EventLoopPlugin.h> 15 16namespace Web { 17 18NonnullRefPtr<Resource> Resource::create(Badge<ResourceLoader>, Type type, LoadRequest const& request) 19{ 20 if (type == Type::Image) 21 return adopt_ref(*new ImageResource(request)); 22 return adopt_ref(*new Resource(type, request)); 23} 24 25Resource::Resource(Type type, LoadRequest const& request) 26 : m_request(request) 27 , m_type(type) 28{ 29} 30 31Resource::Resource(Type type, Resource& resource) 32 : m_request(resource.m_request) 33 , m_encoded_data(move(resource.m_encoded_data)) 34 , m_type(type) 35 , m_loaded(resource.m_loaded) 36 , m_failed(resource.m_failed) 37 , m_error(move(resource.m_error)) 38 , m_encoding(move(resource.m_encoding)) 39 , m_mime_type(move(resource.m_mime_type)) 40 , m_response_headers(move(resource.m_response_headers)) 41 , m_status_code(move(resource.m_status_code)) 42{ 43 ResourceLoader::the().evict_from_cache(m_request); 44} 45 46Resource::~Resource() = default; 47 48void Resource::for_each_client(Function<void(ResourceClient&)> callback) 49{ 50 Vector<WeakPtr<ResourceClient>, 16> clients_copy; 51 clients_copy.ensure_capacity(m_clients.size()); 52 for (auto* client : m_clients) 53 clients_copy.append(client->make_weak_ptr()); 54 for (auto client : clients_copy) { 55 if (client) 56 callback(*client); 57 } 58} 59 60static Optional<DeprecatedString> encoding_from_content_type(DeprecatedString const& content_type) 61{ 62 auto offset = content_type.find("charset="sv); 63 if (offset.has_value()) { 64 auto encoding = content_type.substring(offset.value() + 8, content_type.length() - offset.value() - 8).to_lowercase(); 65 if (encoding.length() >= 2 && encoding.starts_with('"') && encoding.ends_with('"')) 66 return encoding.substring(1, encoding.length() - 2); 67 if (encoding.length() >= 2 && encoding.starts_with('\'') && encoding.ends_with('\'')) 68 return encoding.substring(1, encoding.length() - 2); 69 return encoding; 70 } 71 72 return {}; 73} 74 75static DeprecatedString mime_type_from_content_type(DeprecatedString const& content_type) 76{ 77 auto offset = content_type.find(';'); 78 if (offset.has_value()) 79 return content_type.substring(0, offset.value()).to_lowercase(); 80 81 return content_type; 82} 83 84static bool is_valid_encoding(StringView encoding) 85{ 86 return TextCodec::decoder_for(encoding).has_value(); 87} 88 89void Resource::did_load(Badge<ResourceLoader>, ReadonlyBytes data, HashMap<DeprecatedString, DeprecatedString, CaseInsensitiveStringTraits> const& headers, Optional<u32> status_code) 90{ 91 VERIFY(!m_loaded); 92 // FIXME: Handle OOM failure. 93 m_encoded_data = ByteBuffer::copy(data).release_value_but_fixme_should_propagate_errors(); 94 m_response_headers = headers; 95 m_status_code = move(status_code); 96 m_loaded = true; 97 98 auto content_type = headers.get("Content-Type"); 99 100 if (content_type.has_value()) { 101 dbgln_if(RESOURCE_DEBUG, "Content-Type header: '{}'", content_type.value()); 102 m_mime_type = mime_type_from_content_type(content_type.value()); 103 // FIXME: "The Quite OK Image Format" doesn't have an official mime type yet, 104 // and servers like nginx will send a generic octet-stream mime type instead. 105 // Let's use image/x-qoi for now, which is also what our Core::MimeData uses & would guess. 106 if (m_mime_type == "application/octet-stream" && url().path().ends_with(".qoi"sv)) 107 m_mime_type = "image/x-qoi"; 108 } else if (url().scheme() == "data" && !url().data_mime_type().is_empty()) { 109 dbgln_if(RESOURCE_DEBUG, "This is a data URL with mime-type _{}_", url().data_mime_type()); 110 m_mime_type = url().data_mime_type(); 111 } else { 112 auto content_type_options = headers.get("X-Content-Type-Options"); 113 if (content_type_options.value_or("").equals_ignoring_ascii_case("nosniff"sv)) { 114 m_mime_type = "text/plain"; 115 } else { 116 m_mime_type = Core::guess_mime_type_based_on_filename(url().path()); 117 } 118 } 119 120 m_encoding = {}; 121 if (content_type.has_value()) { 122 auto encoding = encoding_from_content_type(content_type.value()); 123 if (encoding.has_value() && is_valid_encoding(encoding.value())) { 124 dbgln_if(RESOURCE_DEBUG, "Set encoding '{}' from Content-Type", encoding.value()); 125 m_encoding = encoding.value(); 126 } 127 } 128 129 for_each_client([](auto& client) { 130 client.resource_did_load(); 131 }); 132} 133 134void Resource::did_fail(Badge<ResourceLoader>, DeprecatedString const& error, Optional<u32> status_code) 135{ 136 m_error = error; 137 m_status_code = move(status_code); 138 m_failed = true; 139 140 for_each_client([](auto& client) { 141 client.resource_did_fail(); 142 }); 143} 144 145void Resource::register_client(Badge<ResourceClient>, ResourceClient& client) 146{ 147 VERIFY(!m_clients.contains(&client)); 148 m_clients.set(&client); 149} 150 151void Resource::unregister_client(Badge<ResourceClient>, ResourceClient& client) 152{ 153 VERIFY(m_clients.contains(&client)); 154 m_clients.remove(&client); 155} 156 157void ResourceClient::set_resource(Resource* resource) 158{ 159 if (m_resource) 160 m_resource->unregister_client({}, *this); 161 m_resource = resource; 162 if (m_resource) { 163 VERIFY(resource->type() == client_type()); 164 165 m_resource->register_client({}, *this); 166 167 // For resources that are already loaded, we fire their load/fail callbacks via the event loop. 168 // This ensures that these callbacks always happen in a consistent way, instead of being invoked 169 // synchronously in some cases, and asynchronously in others. 170 if (resource->is_loaded() || resource->is_failed()) { 171 Platform::EventLoopPlugin::the().deferred_invoke([weak_this = make_weak_ptr(), strong_resource = NonnullRefPtr { *m_resource }] { 172 if (!weak_this) 173 return; 174 175 if (weak_this->m_resource != strong_resource.ptr()) 176 return; 177 178 // Make sure that reused resources also have their load callback fired. 179 if (weak_this->m_resource->is_loaded()) { 180 weak_this->resource_did_load(); 181 return; 182 } 183 184 // Make sure that reused resources also have their fail callback fired. 185 if (weak_this->m_resource->is_failed()) { 186 weak_this->resource_did_fail(); 187 return; 188 } 189 }); 190 } 191 } 192} 193 194ResourceClient::~ResourceClient() 195{ 196 if (m_resource) 197 m_resource->unregister_client({}, *this); 198} 199 200}