FrameLoader.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/LexicalPath.h>
  8. #include <AK/SourceGenerator.h>
  9. #include <LibGemini/Document.h>
  10. #include <LibGfx/ImageDecoder.h>
  11. #include <LibMarkdown/Document.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/DOM/ElementFactory.h>
  14. #include <LibWeb/DOM/Text.h>
  15. #include <LibWeb/HTML/HTMLIFrameElement.h>
  16. #include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
  17. #include <LibWeb/Loader/FrameLoader.h>
  18. #include <LibWeb/Loader/ResourceLoader.h>
  19. #include <LibWeb/Namespace.h>
  20. #include <LibWeb/Page/Frame.h>
  21. #include <LibWeb/Page/Page.h>
  22. namespace Web {
  23. FrameLoader::FrameLoader(Frame& frame)
  24. : m_frame(frame)
  25. {
  26. }
  27. FrameLoader::~FrameLoader()
  28. {
  29. }
  30. static bool build_markdown_document(DOM::Document& document, const ByteBuffer& data)
  31. {
  32. auto markdown_document = Markdown::Document::parse(data);
  33. if (!markdown_document)
  34. return false;
  35. HTML::HTMLDocumentParser parser(document, markdown_document->render_to_html(), "utf-8");
  36. parser.run(document.url());
  37. return true;
  38. }
  39. static bool build_text_document(DOM::Document& document, const ByteBuffer& data)
  40. {
  41. auto html_element = document.create_element("html");
  42. document.append_child(html_element);
  43. auto head_element = document.create_element("head");
  44. html_element->append_child(head_element);
  45. auto title_element = document.create_element("title");
  46. head_element->append_child(title_element);
  47. auto title_text = document.create_text_node(document.url().basename());
  48. title_element->append_child(title_text);
  49. auto body_element = document.create_element("body");
  50. html_element->append_child(body_element);
  51. auto pre_element = document.create_element("pre");
  52. body_element->append_child(pre_element);
  53. pre_element->append_child(document.create_text_node(String::copy(data)));
  54. return true;
  55. }
  56. static bool build_image_document(DOM::Document& document, const ByteBuffer& data)
  57. {
  58. auto image_decoder = Gfx::ImageDecoder::create(data.data(), data.size());
  59. auto bitmap = image_decoder->bitmap();
  60. if (!bitmap)
  61. return false;
  62. auto html_element = document.create_element("html");
  63. document.append_child(html_element);
  64. auto head_element = document.create_element("head");
  65. html_element->append_child(head_element);
  66. auto title_element = document.create_element("title");
  67. head_element->append_child(title_element);
  68. auto basename = LexicalPath(document.url().path()).basename();
  69. auto title_text = adopt_ref(*new DOM::Text(document, String::formatted("{} [{}x{}]", basename, bitmap->width(), bitmap->height())));
  70. title_element->append_child(title_text);
  71. auto body_element = document.create_element("body");
  72. html_element->append_child(body_element);
  73. auto image_element = document.create_element("img");
  74. image_element->set_attribute(HTML::AttributeNames::src, document.url().to_string());
  75. body_element->append_child(image_element);
  76. return true;
  77. }
  78. static bool build_gemini_document(DOM::Document& document, const ByteBuffer& data)
  79. {
  80. StringView gemini_data { data };
  81. auto gemini_document = Gemini::Document::parse(gemini_data, document.url());
  82. String html_data = gemini_document->render_to_html();
  83. dbgln_if(GEMINI_DEBUG, "Gemini data:\n\"\"\"{}\"\"\"", gemini_data);
  84. dbgln_if(GEMINI_DEBUG, "Converted to HTML:\n\"\"\"{}\"\"\"", html_data);
  85. HTML::HTMLDocumentParser parser(document, html_data, "utf-8");
  86. parser.run(document.url());
  87. return true;
  88. }
  89. bool FrameLoader::parse_document(DOM::Document& document, const ByteBuffer& data)
  90. {
  91. auto& mime_type = document.content_type();
  92. if (mime_type == "text/html" || mime_type == "image/svg+xml") {
  93. HTML::HTMLDocumentParser parser(document, data, document.encoding());
  94. parser.run(document.url());
  95. return true;
  96. }
  97. if (mime_type.starts_with("image/"))
  98. return build_image_document(document, data);
  99. if (mime_type == "text/plain" || mime_type == "application/json")
  100. return build_text_document(document, data);
  101. if (mime_type == "text/markdown")
  102. return build_markdown_document(document, data);
  103. if (mime_type == "text/gemini")
  104. return build_gemini_document(document, data);
  105. return false;
  106. }
  107. bool FrameLoader::load(const LoadRequest& request, Type type)
  108. {
  109. if (!request.is_valid()) {
  110. load_error_page(request.url(), "Invalid request");
  111. return false;
  112. }
  113. if (!m_frame.is_frame_nesting_allowed(request.url())) {
  114. dbgln("No further recursion is allowed for the frame, abort load!");
  115. return false;
  116. }
  117. auto& url = request.url();
  118. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
  119. if (type == Type::Navigation) {
  120. if (auto* page = frame().page())
  121. page->client().page_did_start_loading(url);
  122. }
  123. if (type == Type::IFrame)
  124. return true;
  125. if (url.protocol() == "http" || url.protocol() == "https") {
  126. URL favicon_url;
  127. favicon_url.set_protocol(url.protocol());
  128. favicon_url.set_host(url.host());
  129. favicon_url.set_port(url.port());
  130. favicon_url.set_path("/favicon.ico");
  131. ResourceLoader::the().load(
  132. favicon_url,
  133. [this, favicon_url](auto data, auto&, auto) {
  134. dbgln("Favicon downloaded, {} bytes from {}", data.size(), favicon_url);
  135. auto decoder = Gfx::ImageDecoder::create(data.data(), data.size());
  136. auto bitmap = decoder->bitmap();
  137. if (!bitmap) {
  138. dbgln("Could not decode favicon {}", favicon_url);
  139. return;
  140. }
  141. dbgln("Decoded favicon, {}", bitmap->size());
  142. if (auto* page = frame().page())
  143. page->client().page_did_change_favicon(*bitmap);
  144. });
  145. }
  146. return true;
  147. }
  148. bool FrameLoader::load(const URL& url, Type type)
  149. {
  150. dbgln("FrameLoader::load: {}", url);
  151. if (!url.is_valid()) {
  152. load_error_page(url, "Invalid URL");
  153. return false;
  154. }
  155. auto request = LoadRequest::create_for_url_on_page(url, frame().page());
  156. return load(request, type);
  157. }
  158. void FrameLoader::load_html(const StringView& html, const URL& url)
  159. {
  160. auto document = DOM::Document::create(url);
  161. HTML::HTMLDocumentParser parser(document, html, "utf-8");
  162. parser.run(url);
  163. frame().set_document(&parser.document());
  164. }
  165. // FIXME: Use an actual templating engine (our own one when it's built, preferably
  166. // with a way to check these usages at compile time)
  167. void FrameLoader::load_error_page(const URL& failed_url, const String& error)
  168. {
  169. auto error_page_url = "file:///res/html/error.html";
  170. ResourceLoader::the().load(
  171. error_page_url,
  172. [this, failed_url, error](auto data, auto&, auto) {
  173. VERIFY(!data.is_null());
  174. StringBuilder builder;
  175. SourceGenerator generator { builder };
  176. generator.set("failed_url", escape_html_entities(failed_url.to_string()));
  177. generator.set("error", escape_html_entities(error));
  178. generator.append(data);
  179. auto document = HTML::parse_html_document(generator.as_string_view(), failed_url, "utf-8");
  180. VERIFY(document);
  181. frame().set_document(document);
  182. },
  183. [](auto& error, auto) {
  184. dbgln("Failed to load error page: {}", error);
  185. VERIFY_NOT_REACHED();
  186. });
  187. }
  188. void FrameLoader::resource_did_load()
  189. {
  190. auto url = resource()->url();
  191. if (!resource()->has_encoded_data()) {
  192. load_error_page(url, "No data");
  193. return;
  194. }
  195. // FIXME: Also check HTTP status code before redirecting
  196. auto location = resource()->response_headers().get("Location");
  197. if (location.has_value()) {
  198. load(url.complete_url(location.value()), FrameLoader::Type::Navigation);
  199. return;
  200. }
  201. dbgln("I believe this content has MIME type '{}', encoding '{}'", resource()->mime_type(), resource()->encoding());
  202. auto document = DOM::Document::create();
  203. document->set_url(url);
  204. document->set_encoding(resource()->encoding());
  205. document->set_content_type(resource()->mime_type());
  206. frame().set_document(document);
  207. if (!parse_document(*document, resource()->encoded_data())) {
  208. load_error_page(url, "Failed to parse content.");
  209. return;
  210. }
  211. // FIXME: Support multiple instances of the Set-Cookie response header.
  212. auto set_cookie = resource()->response_headers().get("Set-Cookie");
  213. if (set_cookie.has_value())
  214. document->set_cookie(set_cookie.value(), Cookie::Source::Http);
  215. if (!url.fragment().is_empty())
  216. frame().scroll_to_anchor(url.fragment());
  217. if (auto* host_element = frame().host_element()) {
  218. // FIXME: Perhaps in the future we'll have a better common base class for <frame> and <iframe>
  219. VERIFY(is<HTML::HTMLIFrameElement>(*host_element));
  220. downcast<HTML::HTMLIFrameElement>(*host_element).content_frame_did_load({});
  221. }
  222. if (auto* page = frame().page())
  223. page->client().page_did_finish_loading(url);
  224. }
  225. void FrameLoader::resource_did_fail()
  226. {
  227. load_error_page(resource()->url(), resource()->error());
  228. }
  229. }