ResourceLoader.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (c) 2018-2021, 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/Base64.h>
  27. #include <AK/Debug.h>
  28. #include <AK/JsonObject.h>
  29. #include <LibCore/EventLoop.h>
  30. #include <LibCore/File.h>
  31. #include <LibProtocol/Client.h>
  32. #include <LibProtocol/Download.h>
  33. #include <LibWeb/Loader/ContentFilter.h>
  34. #include <LibWeb/Loader/LoadRequest.h>
  35. #include <LibWeb/Loader/Resource.h>
  36. #include <LibWeb/Loader/ResourceLoader.h>
  37. namespace Web {
  38. ResourceLoader& ResourceLoader::the()
  39. {
  40. static ResourceLoader* s_the;
  41. if (!s_the)
  42. s_the = &ResourceLoader::construct().leak_ref();
  43. return *s_the;
  44. }
  45. ResourceLoader::ResourceLoader()
  46. : m_protocol_client(Protocol::Client::construct())
  47. , m_user_agent(default_user_agent)
  48. {
  49. }
  50. void ResourceLoader::load_sync(const URL& url, Function<void(ReadonlyBytes, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers, Optional<u32> status_code)> success_callback, Function<void(const String&, Optional<u32> status_code)> error_callback)
  51. {
  52. Core::EventLoop loop;
  53. load(
  54. url,
  55. [&](auto data, auto& response_headers, auto status_code) {
  56. success_callback(data, response_headers, status_code);
  57. loop.quit(0);
  58. },
  59. [&](auto& string, auto status_code) {
  60. if (error_callback)
  61. error_callback(string, status_code);
  62. loop.quit(0);
  63. });
  64. loop.exec();
  65. }
  66. static HashMap<LoadRequest, NonnullRefPtr<Resource>> s_resource_cache;
  67. RefPtr<Resource> ResourceLoader::load_resource(Resource::Type type, const LoadRequest& request)
  68. {
  69. if (!request.is_valid())
  70. return nullptr;
  71. bool use_cache = request.url().protocol() != "file";
  72. if (use_cache) {
  73. auto it = s_resource_cache.find(request);
  74. if (it != s_resource_cache.end()) {
  75. if (it->value->type() != type) {
  76. dbgln("FIXME: Not using cached resource for {} since there's a type mismatch.", request.url());
  77. } else {
  78. dbgln_if(CACHE_DEBUG, "Reusing cached resource for: {}", request.url());
  79. return it->value;
  80. }
  81. }
  82. }
  83. auto resource = Resource::create({}, type, request);
  84. if (use_cache)
  85. s_resource_cache.set(request, resource);
  86. load(
  87. request,
  88. [=](auto data, auto& headers, auto status_code) {
  89. const_cast<Resource&>(*resource).did_load({}, data, headers, status_code);
  90. },
  91. [=](auto& error, auto status_code) {
  92. const_cast<Resource&>(*resource).did_fail({}, error, status_code);
  93. });
  94. return resource;
  95. }
  96. void ResourceLoader::load(const LoadRequest& request, Function<void(ReadonlyBytes, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers, Optional<u32> status_code)> success_callback, Function<void(const String&, Optional<u32> status_code)> error_callback)
  97. {
  98. auto& url = request.url();
  99. if (is_port_blocked(url.port())) {
  100. dbgln("ResourceLoader::load: Error: blocked port {} from URL {}", url.port(), url);
  101. return;
  102. }
  103. if (ContentFilter::the().is_filtered(url)) {
  104. dbgln("\033[32;1mResourceLoader::load: URL was filtered! {}\033[0m", url);
  105. error_callback("URL was filtered", {});
  106. return;
  107. }
  108. if (url.protocol() == "about") {
  109. dbgln("Loading about: URL {}", url);
  110. deferred_invoke([success_callback = move(success_callback)](auto&) {
  111. success_callback(String::empty().to_byte_buffer(), {}, {});
  112. });
  113. return;
  114. }
  115. if (url.protocol() == "data") {
  116. dbgln("ResourceLoader loading a data URL with mime-type: '{}', base64={}, payload='{}'",
  117. url.data_mime_type(),
  118. url.data_payload_is_base64(),
  119. url.data_payload());
  120. ByteBuffer data;
  121. if (url.data_payload_is_base64())
  122. data = decode_base64(url.data_payload());
  123. else
  124. data = url.data_payload().to_byte_buffer();
  125. deferred_invoke([data = move(data), success_callback = move(success_callback)](auto&) {
  126. success_callback(data, {}, {});
  127. });
  128. return;
  129. }
  130. if (url.protocol() == "file") {
  131. auto f = Core::File::construct();
  132. f->set_filename(url.path());
  133. if (!f->open(Core::IODevice::OpenMode::ReadOnly)) {
  134. dbgln("ResourceLoader::load: Error: {}", f->error_string());
  135. if (error_callback)
  136. error_callback(f->error_string(), {});
  137. return;
  138. }
  139. auto data = f->read_all();
  140. deferred_invoke([data = move(data), success_callback = move(success_callback)](auto&) {
  141. success_callback(data, {}, {});
  142. });
  143. return;
  144. }
  145. if (url.protocol() == "http" || url.protocol() == "https" || url.protocol() == "gemini") {
  146. HashMap<String, String> headers;
  147. headers.set("User-Agent", m_user_agent);
  148. headers.set("Accept-Encoding", "gzip, deflate");
  149. for (auto& it : request.headers()) {
  150. headers.set(it.key, it.value);
  151. }
  152. auto download = protocol_client().start_download(request.method(), url.to_string(), headers, request.body());
  153. if (!download) {
  154. if (error_callback)
  155. error_callback("Failed to initiate load", {});
  156. return;
  157. }
  158. download->on_buffered_download_finish = [this, success_callback = move(success_callback), error_callback = move(error_callback), download](bool success, auto, auto& response_headers, auto status_code, ReadonlyBytes payload) {
  159. if (status_code.has_value() && status_code.value() >= 400 && status_code.value() <= 499) {
  160. if (error_callback)
  161. error_callback(String::formatted("HTTP error ({})", status_code.value()), status_code);
  162. return;
  163. }
  164. --m_pending_loads;
  165. if (on_load_counter_change)
  166. on_load_counter_change();
  167. if (!success) {
  168. if (error_callback)
  169. error_callback("HTTP load failed", {});
  170. return;
  171. }
  172. deferred_invoke([download](auto&) {
  173. // Clear circular reference of `download` captured by copy
  174. const_cast<Protocol::Download&>(*download).on_buffered_download_finish = nullptr;
  175. });
  176. success_callback(payload, response_headers, status_code);
  177. };
  178. download->set_should_buffer_all_input(true);
  179. download->on_certificate_requested = []() -> Protocol::Download::CertificateAndKey {
  180. return {};
  181. };
  182. ++m_pending_loads;
  183. if (on_load_counter_change)
  184. on_load_counter_change();
  185. return;
  186. }
  187. if (error_callback)
  188. error_callback(String::formatted("Protocol not implemented: {}", url.protocol()), {});
  189. }
  190. void ResourceLoader::load(const URL& url, Function<void(ReadonlyBytes, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers, Optional<u32> status_code)> success_callback, Function<void(const String&, Optional<u32> status_code)> error_callback)
  191. {
  192. LoadRequest request;
  193. request.set_url(url);
  194. load(request, move(success_callback), move(error_callback));
  195. }
  196. bool ResourceLoader::is_port_blocked(int port)
  197. {
  198. int ports[] { 1, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 25, 37, 42,
  199. 43, 53, 77, 79, 87, 95, 101, 102, 103, 104, 109, 110, 111, 113,
  200. 115, 117, 119, 123, 135, 139, 143, 179, 389, 465, 512, 513, 514,
  201. 515, 526, 530, 531, 532, 540, 556, 563, 587, 601, 636, 993, 995,
  202. 2049, 3659, 4045, 6000, 6379, 6665, 6666, 6667, 6668, 6669, 9000 };
  203. for (auto blocked_port : ports)
  204. if (port == blocked_port)
  205. return true;
  206. return false;
  207. }
  208. void ResourceLoader::clear_cache()
  209. {
  210. dbgln("Clearing {} items from ResourceLoader cache", s_resource_cache.size());
  211. s_resource_cache.clear();
  212. }
  213. }