ResourceLoader.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2018-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/Base64.h>
  27. #include <AK/JsonObject.h>
  28. #include <AK/SharedBuffer.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/LoadRequest.h>
  34. #include <LibWeb/Loader/Resource.h>
  35. #include <LibWeb/Loader/ResourceLoader.h>
  36. namespace Web {
  37. ResourceLoader& ResourceLoader::the()
  38. {
  39. static ResourceLoader* s_the;
  40. if (!s_the)
  41. s_the = &ResourceLoader::construct().leak_ref();
  42. return *s_the;
  43. }
  44. ResourceLoader::ResourceLoader()
  45. : m_protocol_client(Protocol::Client::construct())
  46. , m_user_agent("Mozilla/4.0 (SerenityOS; x86) LibWeb+LibJS (Not KHTML, nor Gecko) LibWeb")
  47. {
  48. }
  49. void ResourceLoader::load_sync(const URL& url, Function<void(const ByteBuffer&, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers)> success_callback, Function<void(const String&)> error_callback)
  50. {
  51. Core::EventLoop loop;
  52. load(
  53. url,
  54. [&](auto& data, auto& response_headers) {
  55. success_callback(data, response_headers);
  56. loop.quit(0);
  57. },
  58. [&](auto& string) {
  59. if (error_callback)
  60. error_callback(string);
  61. loop.quit(0);
  62. });
  63. loop.exec();
  64. }
  65. static HashMap<LoadRequest, NonnullRefPtr<Resource>> s_resource_cache;
  66. RefPtr<Resource> ResourceLoader::load_resource(Resource::Type type, const LoadRequest& request)
  67. {
  68. if (!request.is_valid())
  69. return nullptr;
  70. auto it = s_resource_cache.find(request);
  71. if (it != s_resource_cache.end()) {
  72. if (it->value->type() != type) {
  73. dbg() << "FIXME: Not using cached resource for " << request.url() << " since there's a type mismatch.";
  74. } else {
  75. dbg() << "Reusing cached resource for: " << request.url();
  76. return it->value;
  77. }
  78. }
  79. auto resource = Resource::create({}, type, request);
  80. s_resource_cache.set(request, resource);
  81. load(
  82. request.url(),
  83. [=](auto& data, auto& headers) {
  84. const_cast<Resource&>(*resource).did_load({}, data, headers);
  85. },
  86. [=](auto& error) {
  87. const_cast<Resource&>(*resource).did_fail({}, error);
  88. });
  89. return resource;
  90. }
  91. void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers)> success_callback, Function<void(const String&)> error_callback)
  92. {
  93. if (is_port_blocked(url.port())) {
  94. dbg() << "ResourceLoader::load: Error: blocked port " << url.port() << " for URL: " << url;
  95. return;
  96. }
  97. if (url.protocol() == "about") {
  98. dbg() << "Loading about: URL " << url;
  99. deferred_invoke([success_callback = move(success_callback)](auto&) {
  100. success_callback(ByteBuffer::wrap(String::empty().characters(), 1), {});
  101. });
  102. return;
  103. }
  104. if (url.protocol() == "data") {
  105. dbg() << "ResourceLoader loading a data URL with mime-type: '" << url.data_mime_type() << "', base64=" << url.data_payload_is_base64() << ", payload='" << url.data_payload() << "'";
  106. ByteBuffer data;
  107. if (url.data_payload_is_base64())
  108. data = decode_base64(url.data_payload());
  109. else
  110. data = url.data_payload().to_byte_buffer();
  111. deferred_invoke([data = move(data), success_callback = move(success_callback)](auto&) {
  112. success_callback(data, {});
  113. });
  114. return;
  115. }
  116. if (url.protocol() == "file") {
  117. auto f = Core::File::construct();
  118. f->set_filename(url.path());
  119. if (!f->open(Core::IODevice::OpenMode::ReadOnly)) {
  120. dbg() << "ResourceLoader::load: Error: " << f->error_string();
  121. if (error_callback)
  122. error_callback(f->error_string());
  123. return;
  124. }
  125. auto data = f->read_all();
  126. deferred_invoke([data = move(data), success_callback = move(success_callback)](auto&) {
  127. success_callback(data, {});
  128. });
  129. return;
  130. }
  131. if (url.protocol() == "http" || url.protocol() == "https" || url.protocol() == "gemini") {
  132. HashMap<String, String> headers;
  133. headers.set("User-Agent", m_user_agent);
  134. auto download = protocol_client().start_download(url.to_string(), headers);
  135. if (!download) {
  136. if (error_callback)
  137. error_callback("Failed to initiate load");
  138. return;
  139. }
  140. download->on_finish = [this, success_callback = move(success_callback), error_callback = move(error_callback)](bool success, const ByteBuffer& payload, auto, auto& response_headers) {
  141. --m_pending_loads;
  142. if (on_load_counter_change)
  143. on_load_counter_change();
  144. if (!success) {
  145. if (error_callback)
  146. error_callback("HTTP load failed");
  147. return;
  148. }
  149. success_callback(ByteBuffer::copy(payload.data(), payload.size()), response_headers);
  150. };
  151. ++m_pending_loads;
  152. if (on_load_counter_change)
  153. on_load_counter_change();
  154. return;
  155. }
  156. if (error_callback)
  157. error_callback(String::format("Protocol not implemented: %s", url.protocol().characters()));
  158. }
  159. bool ResourceLoader::is_port_blocked(int port)
  160. {
  161. int ports[] { 1, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 25, 37, 42,
  162. 43, 53, 77, 79, 87, 95, 101, 102, 103, 104, 109, 110, 111, 113,
  163. 115, 117, 119, 123, 135, 139, 143, 179, 389, 465, 512, 513, 514,
  164. 515, 526, 530, 531, 532, 540, 556, 563, 587, 601, 636, 993, 995,
  165. 2049, 3659, 4045, 6000, 6379, 6665, 6666, 6667, 6668, 6669, 9000 };
  166. for (auto blocked_port : ports)
  167. if (port == blocked_port)
  168. return true;
  169. return false;
  170. }
  171. void ResourceLoader::save_to(JsonObject& object)
  172. {
  173. Object::save_to(object);
  174. object.set("pending_loads", m_pending_loads);
  175. object.set("user_agent", m_user_agent);
  176. }
  177. }