FrameLoader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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/JsonArray.h>
  8. #include <AK/LexicalPath.h>
  9. #include <AK/SourceGenerator.h>
  10. #include <LibGemini/Document.h>
  11. #include <LibGfx/ImageDecoder.h>
  12. #include <LibMarkdown/Document.h>
  13. #include <LibWeb/Cookie/ParsedCookie.h>
  14. #include <LibWeb/DOM/Document.h>
  15. #include <LibWeb/DOM/ElementFactory.h>
  16. #include <LibWeb/DOM/Text.h>
  17. #include <LibWeb/HTML/BrowsingContext.h>
  18. #include <LibWeb/HTML/HTMLIFrameElement.h>
  19. #include <LibWeb/HTML/Parser/HTMLParser.h>
  20. #include <LibWeb/ImageDecoding.h>
  21. #include <LibWeb/Loader/FrameLoader.h>
  22. #include <LibWeb/Loader/ResourceLoader.h>
  23. #include <LibWeb/Page/Page.h>
  24. namespace Web {
  25. static RefPtr<Gfx::Bitmap> s_default_favicon_bitmap;
  26. FrameLoader::FrameLoader(HTML::BrowsingContext& browsing_context)
  27. : m_browsing_context(browsing_context)
  28. {
  29. if (!s_default_favicon_bitmap) {
  30. s_default_favicon_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-browser.png").release_value_but_fixme_should_propagate_errors();
  31. VERIFY(s_default_favicon_bitmap);
  32. }
  33. }
  34. FrameLoader::~FrameLoader()
  35. {
  36. }
  37. static bool build_markdown_document(DOM::Document& document, const ByteBuffer& data)
  38. {
  39. auto markdown_document = Markdown::Document::parse(data);
  40. if (!markdown_document)
  41. return false;
  42. auto parser = HTML::HTMLParser::create(document, markdown_document->render_to_html(), "utf-8");
  43. parser->run(document.url());
  44. return true;
  45. }
  46. static bool build_text_document(DOM::Document& document, const ByteBuffer& data)
  47. {
  48. auto html_element = document.create_element("html").release_value();
  49. document.append_child(html_element);
  50. auto head_element = document.create_element("head").release_value();
  51. html_element->append_child(head_element);
  52. auto title_element = document.create_element("title").release_value();
  53. head_element->append_child(title_element);
  54. auto title_text = document.create_text_node(document.url().basename());
  55. title_element->append_child(title_text);
  56. auto body_element = document.create_element("body").release_value();
  57. html_element->append_child(body_element);
  58. auto pre_element = document.create_element("pre").release_value();
  59. body_element->append_child(pre_element);
  60. pre_element->append_child(document.create_text_node(String::copy(data)));
  61. return true;
  62. }
  63. static bool build_image_document(DOM::Document& document, ByteBuffer const& data)
  64. {
  65. NonnullRefPtr decoder = image_decoder_client();
  66. auto image = decoder->decode_image(data);
  67. if (!image.has_value() || image->frames.is_empty())
  68. return false;
  69. auto const& frame = image->frames[0];
  70. auto const& bitmap = frame.bitmap;
  71. if (!bitmap)
  72. return false;
  73. auto html_element = document.create_element("html").release_value();
  74. document.append_child(html_element);
  75. auto head_element = document.create_element("head").release_value();
  76. html_element->append_child(head_element);
  77. auto title_element = document.create_element("title").release_value();
  78. head_element->append_child(title_element);
  79. auto basename = LexicalPath::basename(document.url().path());
  80. auto title_text = adopt_ref(*new DOM::Text(document, String::formatted("{} [{}x{}]", basename, bitmap->width(), bitmap->height())));
  81. title_element->append_child(title_text);
  82. auto body_element = document.create_element("body").release_value();
  83. html_element->append_child(body_element);
  84. auto image_element = document.create_element("img").release_value();
  85. image_element->set_attribute(HTML::AttributeNames::src, document.url().to_string());
  86. body_element->append_child(image_element);
  87. return true;
  88. }
  89. static bool build_gemini_document(DOM::Document& document, const ByteBuffer& data)
  90. {
  91. StringView gemini_data { data };
  92. auto gemini_document = Gemini::Document::parse(gemini_data, document.url());
  93. String html_data = gemini_document->render_to_html();
  94. dbgln_if(GEMINI_DEBUG, "Gemini data:\n\"\"\"{}\"\"\"", gemini_data);
  95. dbgln_if(GEMINI_DEBUG, "Converted to HTML:\n\"\"\"{}\"\"\"", html_data);
  96. auto parser = HTML::HTMLParser::create(document, html_data, "utf-8");
  97. parser->run(document.url());
  98. return true;
  99. }
  100. bool FrameLoader::parse_document(DOM::Document& document, const ByteBuffer& data)
  101. {
  102. auto& mime_type = document.content_type();
  103. if (mime_type == "text/html" || mime_type == "image/svg+xml") {
  104. auto parser = HTML::HTMLParser::create_with_uncertain_encoding(document, data);
  105. parser->run(document.url());
  106. return true;
  107. }
  108. if (mime_type.starts_with("image/"))
  109. return build_image_document(document, data);
  110. if (mime_type == "text/plain" || mime_type == "application/json")
  111. return build_text_document(document, data);
  112. if (mime_type == "text/markdown")
  113. return build_markdown_document(document, data);
  114. if (mime_type == "text/gemini")
  115. return build_gemini_document(document, data);
  116. return false;
  117. }
  118. bool FrameLoader::load(LoadRequest& request, Type type)
  119. {
  120. if (!request.is_valid()) {
  121. load_error_page(request.url(), "Invalid request");
  122. return false;
  123. }
  124. if (!m_browsing_context.is_frame_nesting_allowed(request.url())) {
  125. dbgln("No further recursion is allowed for the frame, abort load!");
  126. return false;
  127. }
  128. auto& url = request.url();
  129. if (type == Type::Navigation || type == Type::Reload) {
  130. if (auto* page = browsing_context().page())
  131. page->client().page_did_start_loading(url);
  132. }
  133. // https://fetch.spec.whatwg.org/#concept-fetch
  134. // Step 12: If request’s header list does not contain `Accept`, then:
  135. // 1. Let value be `*/*`. (NOTE: Not necessary as we're about to override it)
  136. // 2. A user agent should set value to the first matching statement, if any, switching on request’s destination:
  137. // -> "document"
  138. // -> "frame"
  139. // -> "iframe"
  140. // `text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8`
  141. // FIXME: This should be case-insensitive.
  142. if (!request.headers().contains("Accept"))
  143. request.set_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
  144. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
  145. if (type == Type::IFrame)
  146. return true;
  147. if (url.protocol() == "http" || url.protocol() == "https") {
  148. AK::URL favicon_url;
  149. favicon_url.set_protocol(url.protocol());
  150. favicon_url.set_host(url.host());
  151. favicon_url.set_port(url.port_or_default());
  152. favicon_url.set_paths({ "favicon.ico" });
  153. ResourceLoader::the().load(
  154. favicon_url,
  155. [this, favicon_url](auto data, auto&, auto) {
  156. dbgln_if(SPAM_DEBUG, "Favicon downloaded, {} bytes from {}", data.size(), favicon_url);
  157. if (data.is_empty())
  158. return;
  159. RefPtr<Gfx::Bitmap> favicon_bitmap;
  160. auto decoded_image = image_decoder_client().decode_image(data);
  161. if (!decoded_image.has_value() || decoded_image->frames.is_empty()) {
  162. dbgln("Could not decode favicon {}", favicon_url);
  163. } else {
  164. favicon_bitmap = decoded_image->frames[0].bitmap;
  165. dbgln_if(IMAGE_DECODER_DEBUG, "Decoded favicon, {}", favicon_bitmap->size());
  166. }
  167. load_favicon(favicon_bitmap);
  168. },
  169. [this](auto&, auto) {
  170. load_favicon();
  171. });
  172. } else {
  173. load_favicon();
  174. }
  175. return true;
  176. }
  177. bool FrameLoader::load(const AK::URL& url, Type type)
  178. {
  179. dbgln_if(SPAM_DEBUG, "FrameLoader::load: {}", url);
  180. if (!url.is_valid()) {
  181. load_error_page(url, "Invalid URL");
  182. return false;
  183. }
  184. auto request = LoadRequest::create_for_url_on_page(url, browsing_context().page());
  185. return load(request, type);
  186. }
  187. void FrameLoader::load_html(StringView html, const AK::URL& url)
  188. {
  189. auto document = DOM::Document::create(url);
  190. auto parser = HTML::HTMLParser::create(document, html, "utf-8");
  191. parser->run(url);
  192. browsing_context().set_active_document(&parser->document());
  193. }
  194. // FIXME: Use an actual templating engine (our own one when it's built, preferably
  195. // with a way to check these usages at compile time)
  196. void FrameLoader::load_error_page(const AK::URL& failed_url, const String& error)
  197. {
  198. auto error_page_url = "file:///res/html/error.html";
  199. ResourceLoader::the().load(
  200. error_page_url,
  201. [this, failed_url, error](auto data, auto&, auto) {
  202. VERIFY(!data.is_null());
  203. StringBuilder builder;
  204. SourceGenerator generator { builder };
  205. generator.set("failed_url", escape_html_entities(failed_url.to_string()));
  206. generator.set("error", escape_html_entities(error));
  207. generator.append(data);
  208. auto document = HTML::parse_html_document(generator.as_string_view(), failed_url, "utf-8");
  209. VERIFY(document);
  210. browsing_context().set_active_document(document);
  211. },
  212. [](auto& error, auto) {
  213. dbgln("Failed to load error page: {}", error);
  214. VERIFY_NOT_REACHED();
  215. });
  216. }
  217. void FrameLoader::load_favicon(RefPtr<Gfx::Bitmap> bitmap)
  218. {
  219. if (auto* page = browsing_context().page()) {
  220. if (bitmap)
  221. page->client().page_did_change_favicon(*bitmap);
  222. else
  223. page->client().page_did_change_favicon(*s_default_favicon_bitmap);
  224. }
  225. }
  226. void FrameLoader::store_response_cookies(AK::URL const& url, String const& cookies)
  227. {
  228. auto* page = browsing_context().page();
  229. if (!page)
  230. return;
  231. auto set_cookie_json_value = MUST(JsonValue::from_string(cookies));
  232. VERIFY(set_cookie_json_value.type() == JsonValue::Type::Array);
  233. for (const auto& set_cookie_entry : set_cookie_json_value.as_array().values()) {
  234. VERIFY(set_cookie_entry.type() == JsonValue::Type::String);
  235. auto cookie = Cookie::parse_cookie(set_cookie_entry.as_string());
  236. if (!cookie.has_value())
  237. continue;
  238. page->client().page_did_set_cookie(url, cookie.value(), Cookie::Source::Http); // FIXME: Determine cookie source correctly
  239. }
  240. }
  241. void FrameLoader::resource_did_load()
  242. {
  243. auto url = resource()->url();
  244. if (auto set_cookie = resource()->response_headers().get("Set-Cookie"); set_cookie.has_value())
  245. store_response_cookies(url, *set_cookie);
  246. // For 3xx (Redirection) responses, the Location value refers to the preferred target resource for automatically redirecting the request.
  247. auto status_code = resource()->status_code();
  248. if (status_code.has_value() && *status_code >= 300 && *status_code <= 399) {
  249. auto location = resource()->response_headers().get("Location");
  250. if (location.has_value()) {
  251. if (m_redirects_count > maximum_redirects_allowed) {
  252. m_redirects_count = 0;
  253. load_error_page(url, "Too many redirects");
  254. return;
  255. }
  256. m_redirects_count++;
  257. load(url.complete_url(location.value()), FrameLoader::Type::Navigation);
  258. return;
  259. }
  260. }
  261. m_redirects_count = 0;
  262. if (!resource()->has_encoded_data()) {
  263. load_error_page(url, "No data");
  264. return;
  265. }
  266. if (resource()->has_encoding()) {
  267. dbgln_if(RESOURCE_DEBUG, "This content has MIME type '{}', encoding '{}'", resource()->mime_type(), resource()->encoding().value());
  268. } else {
  269. dbgln_if(RESOURCE_DEBUG, "This content has MIME type '{}', encoding unknown", resource()->mime_type());
  270. }
  271. auto document = DOM::Document::create();
  272. document->set_url(url);
  273. document->set_encoding(resource()->encoding());
  274. document->set_content_type(resource()->mime_type());
  275. browsing_context().set_active_document(document);
  276. if (!parse_document(*document, resource()->encoded_data())) {
  277. load_error_page(url, "Failed to parse content.");
  278. return;
  279. }
  280. if (!url.fragment().is_empty())
  281. browsing_context().scroll_to_anchor(url.fragment());
  282. else
  283. browsing_context().set_viewport_scroll_offset({ 0, 0 });
  284. if (auto* page = browsing_context().page())
  285. page->client().page_did_finish_loading(url);
  286. }
  287. void FrameLoader::resource_did_fail()
  288. {
  289. load_error_page(resource()->url(), resource()->error());
  290. }
  291. }