Resource.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Debug.h>
  27. #include <AK/Function.h>
  28. #include <LibCore/MimeData.h>
  29. #include <LibWeb/HTML/HTMLImageElement.h>
  30. #include <LibWeb/Loader/Resource.h>
  31. namespace Web {
  32. NonnullRefPtr<Resource> Resource::create(Badge<ResourceLoader>, Type type, const LoadRequest& request)
  33. {
  34. if (type == Type::Image)
  35. return adopt(*new ImageResource(request));
  36. return adopt(*new Resource(type, request));
  37. }
  38. Resource::Resource(Type type, const LoadRequest& request)
  39. : m_request(request)
  40. , m_type(type)
  41. {
  42. }
  43. Resource::~Resource()
  44. {
  45. }
  46. void Resource::for_each_client(Function<void(ResourceClient&)> callback)
  47. {
  48. Vector<WeakPtr<ResourceClient>, 16> clients_copy;
  49. clients_copy.ensure_capacity(m_clients.size());
  50. for (auto* client : m_clients)
  51. clients_copy.append(client->make_weak_ptr());
  52. for (auto client : clients_copy) {
  53. if (client)
  54. callback(*client);
  55. }
  56. }
  57. static String encoding_from_content_type(const String& content_type)
  58. {
  59. auto offset = content_type.index_of("charset=");
  60. if (offset.has_value()) {
  61. auto encoding = content_type.substring(offset.value() + 8, content_type.length() - offset.value() - 8).to_lowercase();
  62. if (encoding.length() >= 2 && encoding.starts_with('"') && encoding.ends_with('"'))
  63. return encoding.substring(1, encoding.length() - 2);
  64. if (encoding.length() >= 2 && encoding.starts_with('\'') && encoding.ends_with('\''))
  65. return encoding.substring(1, encoding.length() - 2);
  66. return encoding;
  67. }
  68. return "utf-8";
  69. }
  70. static String mime_type_from_content_type(const String& content_type)
  71. {
  72. auto offset = content_type.index_of(";");
  73. if (offset.has_value())
  74. return content_type.substring(0, offset.value()).to_lowercase();
  75. return content_type;
  76. }
  77. void Resource::did_load(Badge<ResourceLoader>, ReadonlyBytes data, const HashMap<String, String, CaseInsensitiveStringTraits>& headers, Optional<u32> status_code)
  78. {
  79. VERIFY(!m_loaded);
  80. m_encoded_data = ByteBuffer::copy(data);
  81. m_response_headers = headers;
  82. m_status_code = move(status_code);
  83. m_loaded = true;
  84. auto content_type = headers.get("Content-Type");
  85. if (content_type.has_value()) {
  86. #if RESOURCE_DEBUG
  87. dbgln("Content-Type header: '{}'", content_type.value());
  88. #endif
  89. m_encoding = encoding_from_content_type(content_type.value());
  90. m_mime_type = mime_type_from_content_type(content_type.value());
  91. } else if (url().protocol() == "data" && !url().data_mime_type().is_empty()) {
  92. dbgln_if(RESOURCE_DEBUG, "This is a data URL with mime-type _{}_", url().data_mime_type());
  93. m_encoding = "utf-8"; // FIXME: This doesn't seem nice.
  94. m_mime_type = url().data_mime_type();
  95. } else {
  96. #if RESOURCE_DEBUG
  97. dbgln("No Content-Type header to go on! Guessing based on filename...");
  98. #endif
  99. m_encoding = "utf-8"; // FIXME: This doesn't seem nice.
  100. m_mime_type = Core::guess_mime_type_based_on_filename(url().path());
  101. }
  102. for_each_client([](auto& client) {
  103. client.resource_did_load();
  104. });
  105. }
  106. void Resource::did_fail(Badge<ResourceLoader>, const String& error, Optional<u32> status_code)
  107. {
  108. m_error = error;
  109. m_status_code = move(status_code);
  110. m_failed = true;
  111. for_each_client([](auto& client) {
  112. client.resource_did_fail();
  113. });
  114. }
  115. void Resource::register_client(Badge<ResourceClient>, ResourceClient& client)
  116. {
  117. VERIFY(!m_clients.contains(&client));
  118. m_clients.set(&client);
  119. }
  120. void Resource::unregister_client(Badge<ResourceClient>, ResourceClient& client)
  121. {
  122. VERIFY(m_clients.contains(&client));
  123. m_clients.remove(&client);
  124. }
  125. void ResourceClient::set_resource(Resource* resource)
  126. {
  127. if (m_resource)
  128. m_resource->unregister_client({}, *this);
  129. m_resource = resource;
  130. if (m_resource) {
  131. VERIFY(resource->type() == client_type());
  132. m_resource->register_client({}, *this);
  133. // Make sure that reused resources also have their load callback fired.
  134. if (resource->is_loaded())
  135. resource_did_load();
  136. // Make sure that reused resources also have their fail callback fired.
  137. if (resource->is_failed())
  138. resource_did_fail();
  139. }
  140. }
  141. ResourceClient::~ResourceClient()
  142. {
  143. if (m_resource)
  144. m_resource->unregister_client({}, *this);
  145. }
  146. }