FrameLoader.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. auto& url = request.url();
  114. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
  115. if (type == Type::Navigation) {
  116. if (auto* page = frame().page())
  117. page->client().page_did_start_loading(url);
  118. }
  119. if (type == Type::IFrame)
  120. return true;
  121. if (url.protocol() == "http" || url.protocol() == "https") {
  122. URL favicon_url;
  123. favicon_url.set_protocol(url.protocol());
  124. favicon_url.set_host(url.host());
  125. favicon_url.set_port(url.port());
  126. favicon_url.set_path("/favicon.ico");
  127. ResourceLoader::the().load(
  128. favicon_url,
  129. [this, favicon_url](auto data, auto&, auto) {
  130. dbgln("Favicon downloaded, {} bytes from {}", data.size(), favicon_url);
  131. auto decoder = Gfx::ImageDecoder::create(data.data(), data.size());
  132. auto bitmap = decoder->bitmap();
  133. if (!bitmap) {
  134. dbgln("Could not decode favicon {}", favicon_url);
  135. return;
  136. }
  137. dbgln("Decoded favicon, {}", bitmap->size());
  138. if (auto* page = frame().page())
  139. page->client().page_did_change_favicon(*bitmap);
  140. });
  141. }
  142. return true;
  143. }
  144. bool FrameLoader::load(const URL& url, Type type)
  145. {
  146. dbgln("FrameLoader::load: {}", url);
  147. if (!url.is_valid()) {
  148. load_error_page(url, "Invalid URL");
  149. return false;
  150. }
  151. auto request = LoadRequest::create_for_url_on_page(url, frame().page());
  152. return load(request, type);
  153. }
  154. void FrameLoader::load_html(const StringView& html, const URL& url)
  155. {
  156. auto document = DOM::Document::create(url);
  157. HTML::HTMLDocumentParser parser(document, html, "utf-8");
  158. parser.run(url);
  159. frame().set_document(&parser.document());
  160. }
  161. // FIXME: Use an actual templating engine (our own one when it's built, preferably
  162. // with a way to check these usages at compile time)
  163. void FrameLoader::load_error_page(const URL& failed_url, const String& error)
  164. {
  165. auto error_page_url = "file:///res/html/error.html";
  166. ResourceLoader::the().load(
  167. error_page_url,
  168. [this, failed_url, error](auto data, auto&, auto) {
  169. VERIFY(!data.is_null());
  170. StringBuilder builder;
  171. SourceGenerator generator { builder };
  172. generator.set("failed_url", escape_html_entities(failed_url.to_string()));
  173. generator.set("error", escape_html_entities(error));
  174. generator.append(data);
  175. auto document = HTML::parse_html_document(generator.as_string_view(), failed_url, "utf-8");
  176. VERIFY(document);
  177. frame().set_document(document);
  178. },
  179. [](auto& error, auto) {
  180. dbgln("Failed to load error page: {}", error);
  181. VERIFY_NOT_REACHED();
  182. });
  183. }
  184. void FrameLoader::resource_did_load()
  185. {
  186. auto url = resource()->url();
  187. if (!resource()->has_encoded_data()) {
  188. load_error_page(url, "No data");
  189. return;
  190. }
  191. // FIXME: Also check HTTP status code before redirecting
  192. auto location = resource()->response_headers().get("Location");
  193. if (location.has_value()) {
  194. load(url.complete_url(location.value()), FrameLoader::Type::Navigation);
  195. return;
  196. }
  197. dbgln("I believe this content has MIME type '{}', , encoding '{}'", resource()->mime_type(), resource()->encoding());
  198. auto document = DOM::Document::create();
  199. document->set_url(url);
  200. document->set_encoding(resource()->encoding());
  201. document->set_content_type(resource()->mime_type());
  202. frame().set_document(document);
  203. if (!parse_document(*document, resource()->encoded_data())) {
  204. load_error_page(url, "Failed to parse content.");
  205. return;
  206. }
  207. // FIXME: Support multiple instances of the Set-Cookie response header.
  208. auto set_cookie = resource()->response_headers().get("Set-Cookie");
  209. if (set_cookie.has_value())
  210. document->set_cookie(set_cookie.value(), Cookie::Source::Http);
  211. if (!url.fragment().is_empty())
  212. frame().scroll_to_anchor(url.fragment());
  213. if (auto* host_element = frame().host_element()) {
  214. // FIXME: Perhaps in the future we'll have a better common base class for <frame> and <iframe>
  215. VERIFY(is<HTML::HTMLIFrameElement>(*host_element));
  216. downcast<HTML::HTMLIFrameElement>(*host_element).content_frame_did_load({});
  217. }
  218. if (auto* page = frame().page())
  219. page->client().page_did_finish_loading(url);
  220. }
  221. void FrameLoader::resource_did_fail()
  222. {
  223. load_error_page(resource()->url(), resource()->error());
  224. }
  225. }