Resource.cpp 4.6 KB

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