GeneratedPagesLoader.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/NumberFormat.h>
  7. #include <AK/QuickSort.h>
  8. #include <AK/SourceGenerator.h>
  9. #include <LibCore/DateTime.h>
  10. #include <LibCore/Directory.h>
  11. #include <LibCore/Resource.h>
  12. #include <LibCore/System.h>
  13. #include <LibWeb/Loader/GeneratedPagesLoader.h>
  14. #include <LibWeb/Loader/UserAgent.h>
  15. namespace Web {
  16. void set_chrome_process_command_line(StringView command_line)
  17. {
  18. s_chrome_process_command_line = MUST(String::from_utf8(command_line));
  19. }
  20. void set_chrome_process_executable_path(StringView executable_path)
  21. {
  22. s_chrome_process_executable_path = MUST(String::from_utf8(executable_path));
  23. }
  24. ErrorOr<String> load_error_page(URL::URL const& url, StringView error_message)
  25. {
  26. // Generate HTML error page from error template file
  27. // FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time)
  28. auto template_file = TRY(Core::Resource::load_from_uri("resource://ladybird/templates/error.html"sv));
  29. StringBuilder builder;
  30. SourceGenerator generator { builder, '%', '%' };
  31. generator.set("failed_url", url.to_byte_string());
  32. generator.set("error_message", escape_html_entities(error_message));
  33. generator.append(template_file->data());
  34. return TRY(String::from_utf8(generator.as_string_view()));
  35. }
  36. ErrorOr<String> load_file_directory_page(URL::URL const& url)
  37. {
  38. // Generate HTML contents entries table
  39. auto lexical_path = LexicalPath(URL::percent_decode(url.serialize_path()));
  40. Core::DirIterator dt(lexical_path.string(), Core::DirIterator::Flags::SkipParentAndBaseDir);
  41. Vector<ByteString> names;
  42. while (dt.has_next())
  43. names.append(dt.next_path());
  44. quick_sort(names);
  45. StringBuilder contents;
  46. contents.append("<table>"sv);
  47. for (auto& name : names) {
  48. auto path = lexical_path.append(name);
  49. auto maybe_st = Core::System::stat(path.string());
  50. if (!maybe_st.is_error()) {
  51. auto st = maybe_st.release_value();
  52. auto is_directory = S_ISDIR(st.st_mode);
  53. contents.append("<tr>"sv);
  54. contents.appendff("<td><span class=\"{}\"></span></td>", is_directory ? "folder" : "file");
  55. contents.appendff("<td><a href=\"file://{}\">{}</a></td><td>&nbsp;</td>"sv, path, name);
  56. contents.appendff("<td>{:10}</td><td>&nbsp;</td>", is_directory ? "-"_string : human_readable_size(st.st_size));
  57. contents.appendff("<td>{}</td>"sv, Core::DateTime::from_timestamp(st.st_mtime).to_byte_string());
  58. contents.append("</tr>\n"sv);
  59. }
  60. }
  61. contents.append("</table>"sv);
  62. // Generate HTML directory page from directory template file
  63. // FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time)
  64. auto template_file = TRY(Core::Resource::load_from_uri("resource://ladybird/templates/directory.html"sv));
  65. StringBuilder builder;
  66. SourceGenerator generator { builder, '%', '%' };
  67. generator.set("path", escape_html_entities(lexical_path.string()));
  68. generator.set("parent_url", TRY(String::formatted("file://{}", escape_html_entities(lexical_path.parent().string()))));
  69. generator.set("contents", contents.to_byte_string());
  70. generator.append(template_file->data());
  71. return TRY(String::from_utf8(generator.as_string_view()));
  72. }
  73. ErrorOr<String> load_about_version_page()
  74. {
  75. // Generate HTML about version page from template file
  76. // FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time)
  77. auto template_file = TRY(Core::Resource::load_from_uri("resource://ladybird/templates/version.html"sv));
  78. StringBuilder builder;
  79. SourceGenerator generator { builder, '%', '%' };
  80. generator.set("browser_name", BROWSER_NAME);
  81. generator.set("browser_version", BROWSER_VERSION);
  82. generator.set("arch_name", CPU_STRING);
  83. generator.set("os_name", OS_STRING);
  84. generator.set("user_agent", default_user_agent);
  85. generator.set("command_line", s_chrome_process_command_line);
  86. generator.set("executable_path", s_chrome_process_executable_path);
  87. generator.append(template_file->data());
  88. return TRY(String::from_utf8(generator.as_string_view()));
  89. }
  90. }