Resource.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. } else if (url().protocol() == "data" && !url().data_mime_type().is_empty()) {
  70. dbgln_if(RESOURCE_DEBUG, "This is a data URL with mime-type _{}_", url().data_mime_type());
  71. m_mime_type = url().data_mime_type();
  72. } else {
  73. auto content_type_options = headers.get("X-Content-Type-Options");
  74. if (content_type_options.value_or("").equals_ignoring_case("nosniff")) {
  75. m_mime_type = "text/plain";
  76. } else {
  77. m_mime_type = Core::guess_mime_type_based_on_filename(url().path());
  78. }
  79. }
  80. m_encoding = {};
  81. if (content_type.has_value()) {
  82. auto encoding = encoding_from_content_type(content_type.value());
  83. if (encoding.has_value()) {
  84. dbgln_if(RESOURCE_DEBUG, "Set encoding '{}' from Content-Type", encoding.has_value());
  85. m_encoding = encoding.value();
  86. }
  87. }
  88. for_each_client([](auto& client) {
  89. client.resource_did_load();
  90. });
  91. }
  92. void Resource::did_fail(Badge<ResourceLoader>, const String& error, Optional<u32> status_code)
  93. {
  94. m_error = error;
  95. m_status_code = move(status_code);
  96. m_failed = true;
  97. for_each_client([](auto& client) {
  98. client.resource_did_fail();
  99. });
  100. }
  101. void Resource::register_client(Badge<ResourceClient>, ResourceClient& client)
  102. {
  103. VERIFY(!m_clients.contains(&client));
  104. m_clients.set(&client);
  105. }
  106. void Resource::unregister_client(Badge<ResourceClient>, ResourceClient& client)
  107. {
  108. VERIFY(m_clients.contains(&client));
  109. m_clients.remove(&client);
  110. }
  111. void ResourceClient::set_resource(Resource* resource)
  112. {
  113. if (m_resource)
  114. m_resource->unregister_client({}, *this);
  115. m_resource = resource;
  116. if (m_resource) {
  117. VERIFY(resource->type() == client_type());
  118. m_resource->register_client({}, *this);
  119. // Make sure that reused resources also have their load callback fired.
  120. if (resource->is_loaded())
  121. resource_did_load();
  122. // Make sure that reused resources also have their fail callback fired.
  123. if (resource->is_failed())
  124. resource_did_fail();
  125. }
  126. }
  127. ResourceClient::~ResourceClient()
  128. {
  129. if (m_resource)
  130. m_resource->unregister_client({}, *this);
  131. }
  132. }