ResourceLoader.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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&)> success_callback, Function<void(const String&)> error_callback)
  46. {
  47. Core::EventLoop loop;
  48. load(
  49. url,
  50. [&](auto& data) {
  51. success_callback(data);
  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&)> 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() == "data") {
  68. dbg() << "ResourceLoader loading a data URL with mime-type: '" << url.data_mime_type() << "', base64=" << url.data_payload_is_base64() << ", payload='" << url.data_payload() << "'";
  69. ByteBuffer data;
  70. if (url.data_payload_is_base64())
  71. data = decode_base64(url.data_payload());
  72. else
  73. data = url.data_payload().to_byte_buffer();
  74. deferred_invoke([data = move(data), success_callback = move(success_callback)](auto&) {
  75. success_callback(data);
  76. });
  77. return;
  78. }
  79. if (url.protocol() == "file") {
  80. auto f = Core::File::construct();
  81. f->set_filename(url.path());
  82. if (!f->open(Core::IODevice::OpenMode::ReadOnly)) {
  83. dbg() << "ResourceLoader::load: Error: " << f->error_string();
  84. if (error_callback)
  85. error_callback(f->error_string());
  86. return;
  87. }
  88. auto data = f->read_all();
  89. deferred_invoke([data = move(data), success_callback = move(success_callback)](auto&) {
  90. success_callback(data);
  91. });
  92. return;
  93. }
  94. if (url.protocol() == "http") {
  95. auto download = protocol_client().start_download(url.to_string());
  96. if (!download) {
  97. if (error_callback)
  98. error_callback("Failed to initiate load");
  99. return;
  100. }
  101. download->on_finish = [this, success_callback = move(success_callback), error_callback = move(error_callback)](bool success, const ByteBuffer& payload, auto) {
  102. --m_pending_loads;
  103. if (on_load_counter_change)
  104. on_load_counter_change();
  105. if (!success) {
  106. if (error_callback)
  107. error_callback("HTTP load failed");
  108. return;
  109. }
  110. success_callback(ByteBuffer::copy(payload.data(), payload.size()));
  111. };
  112. ++m_pending_loads;
  113. if (on_load_counter_change)
  114. on_load_counter_change();
  115. return;
  116. }
  117. if (error_callback)
  118. error_callback(String::format("Protocol not implemented: %s", url.protocol().characters()));
  119. }
  120. bool ResourceLoader::is_port_blocked(int port)
  121. {
  122. int ports[] { 1, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 25, 37, 42,
  123. 43, 53, 77, 79, 87, 95, 101, 102, 103, 104, 109, 110, 111, 113,
  124. 115, 117, 119, 123, 135, 139, 143, 179, 389, 465, 512, 513, 514,
  125. 515, 526, 530, 531, 532, 540, 556, 563, 587, 601, 636, 993, 995,
  126. 2049, 3659, 4045, 6000, 6379, 6665, 6666, 6667, 6668, 6669, 9000 };
  127. for (auto blocked_port : ports)
  128. if (port == blocked_port)
  129. return true;
  130. return false;
  131. }
  132. }