Resource.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/Function.h>
  8. #include <LibCore/MimeData.h>
  9. #include <LibWeb/HTML/HTMLImageElement.h>
  10. #include <LibWeb/Loader/Resource.h>
  11. namespace Web {
  12. NonnullRefPtr<Resource> Resource::create(Badge<ResourceLoader>, Type type, const LoadRequest& request)
  13. {
  14. if (type == Type::Image)
  15. return adopt_ref(*new ImageResource(request));
  16. return adopt_ref(*new Resource(type, request));
  17. }
  18. Resource::Resource(Type type, const LoadRequest& request)
  19. : m_request(request)
  20. , m_type(type)
  21. {
  22. }
  23. Resource::~Resource()
  24. {
  25. }
  26. void Resource::for_each_client(Function<void(ResourceClient&)> callback)
  27. {
  28. Vector<WeakPtr<ResourceClient>, 16> clients_copy;
  29. clients_copy.ensure_capacity(m_clients.size());
  30. for (auto* client : m_clients)
  31. clients_copy.append(client->make_weak_ptr());
  32. for (auto client : clients_copy) {
  33. if (client)
  34. callback(*client);
  35. }
  36. }
  37. static Optional<String> encoding_from_content_type(const String& content_type)
  38. {
  39. auto offset = content_type.find("charset="sv);
  40. if (offset.has_value()) {
  41. auto encoding = content_type.substring(offset.value() + 8, content_type.length() - offset.value() - 8).to_lowercase();
  42. if (encoding.length() >= 2 && encoding.starts_with('"') && encoding.ends_with('"'))
  43. return encoding.substring(1, encoding.length() - 2);
  44. if (encoding.length() >= 2 && encoding.starts_with('\'') && encoding.ends_with('\''))
  45. return encoding.substring(1, encoding.length() - 2);
  46. return encoding;
  47. }
  48. return {};
  49. }
  50. static String mime_type_from_content_type(const String& content_type)
  51. {
  52. auto offset = content_type.find(';');
  53. if (offset.has_value())
  54. return content_type.substring(0, offset.value()).to_lowercase();
  55. return content_type;
  56. }
  57. void Resource::did_load(Badge<ResourceLoader>, ReadonlyBytes data, const HashMap<String, String, CaseInsensitiveStringTraits>& headers, Optional<u32> status_code)
  58. {
  59. VERIFY(!m_loaded);
  60. // FIXME: Handle OOM failure.
  61. m_encoded_data = ByteBuffer::copy(data).release_value();
  62. m_response_headers = headers;
  63. m_status_code = move(status_code);
  64. m_loaded = true;
  65. auto content_type = headers.get("Content-Type");
  66. if (content_type.has_value()) {
  67. dbgln_if(RESOURCE_DEBUG, "Content-Type header: '{}'", content_type.value());
  68. m_mime_type = mime_type_from_content_type(content_type.value());
  69. // FIXME: "The Quite OK Image Format" doesn't have an official mime type yet,
  70. // and servers like nginx will send a generic octet-stream mime type instead.
  71. // Let's use image/x-qoi for now, which is also what our Core::MimeData uses & would guess.
  72. if (m_mime_type == "application/octet-stream" && url().path().ends_with(".qoi"))
  73. m_mime_type = "image/x-qoi";
  74. } else if (url().protocol() == "data" && !url().data_mime_type().is_empty()) {
  75. dbgln_if(RESOURCE_DEBUG, "This is a data URL with mime-type _{}_", url().data_mime_type());
  76. m_mime_type = url().data_mime_type();
  77. } else {
  78. auto content_type_options = headers.get("X-Content-Type-Options");
  79. if (content_type_options.value_or("").equals_ignoring_case("nosniff")) {
  80. m_mime_type = "text/plain";
  81. } else {
  82. m_mime_type = Core::guess_mime_type_based_on_filename(url().path());
  83. }
  84. }
  85. m_encoding = {};
  86. if (content_type.has_value()) {
  87. auto encoding = encoding_from_content_type(content_type.value());
  88. if (encoding.has_value()) {
  89. dbgln_if(RESOURCE_DEBUG, "Set encoding '{}' from Content-Type", encoding.has_value());
  90. m_encoding = encoding.value();
  91. }
  92. }
  93. for_each_client([](auto& client) {
  94. client.resource_did_load();
  95. });
  96. }
  97. void Resource::did_fail(Badge<ResourceLoader>, const String& error, Optional<u32> status_code)
  98. {
  99. m_error = error;
  100. m_status_code = move(status_code);
  101. m_failed = true;
  102. for_each_client([](auto& client) {
  103. client.resource_did_fail();
  104. });
  105. }
  106. void Resource::register_client(Badge<ResourceClient>, ResourceClient& client)
  107. {
  108. VERIFY(!m_clients.contains(&client));
  109. m_clients.set(&client);
  110. }
  111. void Resource::unregister_client(Badge<ResourceClient>, ResourceClient& client)
  112. {
  113. VERIFY(m_clients.contains(&client));
  114. m_clients.remove(&client);
  115. }
  116. void ResourceClient::set_resource(Resource* resource)
  117. {
  118. if (m_resource)
  119. m_resource->unregister_client({}, *this);
  120. m_resource = resource;
  121. if (m_resource) {
  122. VERIFY(resource->type() == client_type());
  123. m_resource->register_client({}, *this);
  124. // Make sure that reused resources also have their load callback fired.
  125. if (resource->is_loaded())
  126. resource_did_load();
  127. // Make sure that reused resources also have their fail callback fired.
  128. if (resource->is_failed())
  129. resource_did_fail();
  130. }
  131. }
  132. ResourceClient::~ResourceClient()
  133. {
  134. if (m_resource)
  135. m_resource->unregister_client({}, *this);
  136. }
  137. }