2023-08-13 22:35:35 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2023-09-16 21:51:00 +01:00
|
|
|
#include <AK/NumberFormat.h>
|
2023-08-13 22:35:35 +02:00
|
|
|
#include <AK/QuickSort.h>
|
|
|
|
#include <AK/SourceGenerator.h>
|
|
|
|
#include <LibCore/DateTime.h>
|
|
|
|
#include <LibCore/Directory.h>
|
2023-12-26 10:24:05 +01:00
|
|
|
#include <LibCore/Resource.h>
|
2023-08-13 22:35:35 +02:00
|
|
|
#include <LibCore/System.h>
|
2023-09-21 17:32:31 +02:00
|
|
|
#include <LibWeb/Loader/GeneratedPagesLoader.h>
|
2024-05-26 11:12:49 -04:00
|
|
|
#include <LibWeb/Loader/UserAgent.h>
|
2023-08-13 22:35:35 +02:00
|
|
|
|
|
|
|
namespace Web {
|
|
|
|
|
2024-01-16 18:55:40 +01:00
|
|
|
void set_chrome_process_command_line(StringView command_line)
|
|
|
|
{
|
|
|
|
s_chrome_process_command_line = MUST(String::from_utf8(command_line));
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_chrome_process_executable_path(StringView executable_path)
|
|
|
|
{
|
|
|
|
s_chrome_process_executable_path = MUST(String::from_utf8(executable_path));
|
|
|
|
}
|
|
|
|
|
2024-06-18 15:26:12 +02:00
|
|
|
ErrorOr<String> load_error_page(URL::URL const& url, StringView error_message)
|
2023-09-21 17:55:14 +02:00
|
|
|
{
|
|
|
|
// Generate HTML error page from error template file
|
|
|
|
// FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time)
|
2024-01-12 19:38:55 +01:00
|
|
|
auto template_file = TRY(Core::Resource::load_from_uri("resource://ladybird/templates/error.html"sv));
|
2023-09-21 17:55:14 +02:00
|
|
|
StringBuilder builder;
|
2024-07-07 20:08:09 +01:00
|
|
|
SourceGenerator generator { builder, '%', '%' };
|
2023-12-16 17:49:34 +03:30
|
|
|
generator.set("failed_url", url.to_byte_string());
|
2024-06-18 15:26:12 +02:00
|
|
|
generator.set("error_message", escape_html_entities(error_message));
|
2024-01-12 19:38:55 +01:00
|
|
|
generator.append(template_file->data());
|
2023-09-21 17:55:14 +02:00
|
|
|
return TRY(String::from_utf8(generator.as_string_view()));
|
|
|
|
}
|
2023-09-21 17:32:31 +02:00
|
|
|
|
2024-03-18 16:22:27 +13:00
|
|
|
ErrorOr<String> load_file_directory_page(URL::URL const& url)
|
2023-08-13 22:35:35 +02:00
|
|
|
{
|
|
|
|
// Generate HTML contents entries table
|
2023-12-26 10:12:35 +01:00
|
|
|
auto lexical_path = LexicalPath(url.serialize_path());
|
2023-08-13 22:35:35 +02:00
|
|
|
Core::DirIterator dt(lexical_path.string(), Core::DirIterator::Flags::SkipParentAndBaseDir);
|
2023-12-16 17:49:34 +03:30
|
|
|
Vector<ByteString> names;
|
2023-08-13 22:35:35 +02:00
|
|
|
while (dt.has_next())
|
|
|
|
names.append(dt.next_path());
|
|
|
|
quick_sort(names);
|
|
|
|
|
|
|
|
StringBuilder contents;
|
|
|
|
contents.append("<table>"sv);
|
|
|
|
for (auto& name : names) {
|
|
|
|
auto path = lexical_path.append(name);
|
|
|
|
auto maybe_st = Core::System::stat(path.string());
|
|
|
|
if (!maybe_st.is_error()) {
|
|
|
|
auto st = maybe_st.release_value();
|
2023-09-16 21:51:00 +01:00
|
|
|
auto is_directory = S_ISDIR(st.st_mode);
|
|
|
|
|
2023-08-13 22:35:35 +02:00
|
|
|
contents.append("<tr>"sv);
|
2023-09-16 21:51:00 +01:00
|
|
|
contents.appendff("<td><span class=\"{}\"></span></td>", is_directory ? "folder" : "file");
|
2023-08-13 22:35:35 +02:00
|
|
|
contents.appendff("<td><a href=\"file://{}\">{}</a></td><td> </td>"sv, path, name);
|
2024-01-24 12:16:32 +00:00
|
|
|
contents.appendff("<td>{:10}</td><td> </td>", is_directory ? "-"_string : human_readable_size(st.st_size));
|
2023-12-16 17:49:34 +03:30
|
|
|
contents.appendff("<td>{}</td>"sv, Core::DateTime::from_timestamp(st.st_mtime).to_byte_string());
|
2023-08-13 22:35:35 +02:00
|
|
|
contents.append("</tr>\n"sv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contents.append("</table>"sv);
|
|
|
|
|
|
|
|
// Generate HTML directory page from directory template file
|
|
|
|
// FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time)
|
2024-01-12 19:38:55 +01:00
|
|
|
auto template_file = TRY(Core::Resource::load_from_uri("resource://ladybird/templates/directory.html"sv));
|
2023-08-13 22:35:35 +02:00
|
|
|
StringBuilder builder;
|
2024-07-07 20:08:09 +01:00
|
|
|
SourceGenerator generator { builder, '%', '%' };
|
2023-08-13 22:35:35 +02:00
|
|
|
generator.set("path", escape_html_entities(lexical_path.string()));
|
2023-12-26 10:12:35 +01:00
|
|
|
generator.set("parent_url", TRY(String::formatted("file://{}", escape_html_entities(lexical_path.parent().string()))));
|
2023-12-16 17:49:34 +03:30
|
|
|
generator.set("contents", contents.to_byte_string());
|
2024-01-12 19:38:55 +01:00
|
|
|
generator.append(template_file->data());
|
2023-09-21 17:32:31 +02:00
|
|
|
return TRY(String::from_utf8(generator.as_string_view()));
|
2023-08-13 22:35:35 +02:00
|
|
|
}
|
|
|
|
|
2024-01-12 19:41:26 +01:00
|
|
|
ErrorOr<String> load_about_version_page()
|
|
|
|
{
|
|
|
|
// Generate HTML about version page from template file
|
|
|
|
// FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time)
|
|
|
|
auto template_file = TRY(Core::Resource::load_from_uri("resource://ladybird/templates/version.html"sv));
|
|
|
|
StringBuilder builder;
|
2024-07-07 20:08:09 +01:00
|
|
|
SourceGenerator generator { builder, '%', '%' };
|
2024-01-12 19:41:26 +01:00
|
|
|
generator.set("browser_name", BROWSER_NAME);
|
|
|
|
generator.set("browser_version", BROWSER_VERSION);
|
|
|
|
generator.set("arch_name", CPU_STRING);
|
|
|
|
generator.set("os_name", OS_STRING);
|
|
|
|
generator.set("user_agent", default_user_agent);
|
2024-01-16 18:55:40 +01:00
|
|
|
generator.set("command_line", s_chrome_process_command_line);
|
|
|
|
generator.set("executable_path", s_chrome_process_executable_path);
|
2024-01-12 19:41:26 +01:00
|
|
|
generator.append(template_file->data());
|
|
|
|
return TRY(String::from_utf8(generator.as_string_view()));
|
|
|
|
}
|
|
|
|
|
2023-08-13 22:35:35 +02:00
|
|
|
}
|