ResourceLoader.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/SharedBuffer.h>
  28. #include <LibCore/EventLoop.h>
  29. #include <LibCore/File.h>
  30. #include <LibProtocol/Client.h>
  31. #include <LibProtocol/Download.h>
  32. #include <LibWeb/ResourceLoader.h>
  33. namespace Web {
  34. ResourceLoader& ResourceLoader::the()
  35. {
  36. static ResourceLoader* s_the;
  37. if (!s_the)
  38. s_the = &ResourceLoader::construct().leak_ref();
  39. return *s_the;
  40. }
  41. ResourceLoader::ResourceLoader()
  42. : m_protocol_client(Protocol::Client::construct())
  43. {
  44. }
  45. void ResourceLoader::load_sync(const URL& url, Function<void(const ByteBuffer&, const HashMap<String, String>& response_headers)> success_callback, Function<void(const String&)> error_callback)
  46. {
  47. Core::EventLoop loop;
  48. load(
  49. url,
  50. [&](auto& data, auto& response_headers) {
  51. success_callback(data, response_headers);
  52. loop.quit(0);
  53. },
  54. [&](auto& string) {
  55. if (error_callback)
  56. error_callback(string);
  57. loop.quit(0);
  58. });
  59. loop.exec();
  60. }
  61. void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&, const HashMap<String, String>& response_headers)> success_callback, Function<void(const String&)> error_callback)
  62. {
  63. if (is_port_blocked(url.port())) {
  64. dbg() << "ResourceLoader::load: Error: blocked port " << url.port() << " for URL: " << url;
  65. return;
  66. }
  67. if (url.protocol() == "about") {
  68. dbg() << "Loading about: URL " << url;
  69. deferred_invoke([success_callback = move(success_callback)](auto&) {
  70. success_callback(ByteBuffer::wrap(String::empty().characters(), 1), {});
  71. });
  72. return;
  73. }
  74. if (url.protocol() == "data") {
  75. dbg() << "ResourceLoader loading a data URL with mime-type: '" << url.data_mime_type() << "', base64=" << url.data_payload_is_base64() << ", payload='" << url.data_payload() << "'";
  76. ByteBuffer data;
  77. if (url.data_payload_is_base64())
  78. data = decode_base64(url.data_payload());
  79. else
  80. data = url.data_payload().to_byte_buffer();
  81. deferred_invoke([data = move(data), success_callback = move(success_callback)](auto&) {
  82. success_callback(data, {});
  83. });
  84. return;
  85. }
  86. if (url.protocol() == "file") {
  87. auto f = Core::File::construct();
  88. f->set_filename(url.path());
  89. if (!f->open(Core::IODevice::OpenMode::ReadOnly)) {
  90. dbg() << "ResourceLoader::load: Error: " << f->error_string();
  91. if (error_callback)
  92. error_callback(f->error_string());
  93. return;
  94. }
  95. auto data = f->read_all();
  96. deferred_invoke([data = move(data), success_callback = move(success_callback)](auto&) {
  97. success_callback(data, {});
  98. });
  99. return;
  100. }
  101. if (url.protocol() == "http" || url.protocol() == "https") {
  102. auto download = protocol_client().start_download(url.to_string());
  103. if (!download) {
  104. if (error_callback)
  105. error_callback("Failed to initiate load");
  106. return;
  107. }
  108. download->on_finish = [this, success_callback = move(success_callback), error_callback = move(error_callback)](bool success, const ByteBuffer& payload, auto, auto& response_headers) {
  109. --m_pending_loads;
  110. if (on_load_counter_change)
  111. on_load_counter_change();
  112. if (!success) {
  113. if (error_callback)
  114. error_callback("HTTP load failed");
  115. return;
  116. }
  117. success_callback(ByteBuffer::copy(payload.data(), payload.size()), response_headers);
  118. };
  119. ++m_pending_loads;
  120. if (on_load_counter_change)
  121. on_load_counter_change();
  122. return;
  123. }
  124. if (error_callback)
  125. error_callback(String::format("Protocol not implemented: %s", url.protocol().characters()));
  126. }
  127. bool ResourceLoader::is_port_blocked(int port)
  128. {
  129. int ports[] { 1, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 25, 37, 42,
  130. 43, 53, 77, 79, 87, 95, 101, 102, 103, 104, 109, 110, 111, 113,
  131. 115, 117, 119, 123, 135, 139, 143, 179, 389, 465, 512, 513, 514,
  132. 515, 526, 530, 531, 532, 540, 556, 563, 587, 601, 636, 993, 995,
  133. 2049, 3659, 4045, 6000, 6379, 6665, 6666, 6667, 6668, 6669, 9000 };
  134. for (auto blocked_port : ports)
  135. if (port == blocked_port)
  136. return true;
  137. return false;
  138. }
  139. }