From 33d2ecdd79c1b70360f37a086a6745b7d2314eb6 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 27 Jul 2020 22:38:43 +0200 Subject: WebServer: Show icons in directory listings :^) Just adding some basic folder/file icon makes a big difference here. --- Services/WebServer/Client.cpp | 54 +++++++++++++++++++++++++++++++++++-------- Services/WebServer/main.cpp | 6 +++++ 2 files changed, 50 insertions(+), 10 deletions(-) (limited to 'Services/WebServer') diff --git a/Services/WebServer/Client.cpp b/Services/WebServer/Client.cpp index 833757b818..67215afa7c 100644 --- a/Services/WebServer/Client.cpp +++ b/Services/WebServer/Client.cpp @@ -25,7 +25,9 @@ */ #include "Client.h" +#include #include +#include #include #include #include @@ -156,6 +158,26 @@ void Client::send_redirect(StringView redirect_path, const HTTP::HttpRequest& re log_response(301, request); } +static String folder_image_data() +{ + static String cache; + if (cache.is_empty()) { + MappedFile image("/res/icons/16x16/filetype-folder.png"); + cache = encode_base64({ image.data(), image.size() }); + } + return cache; +} + +static String file_image_data() +{ + static String cache; + if (cache.is_empty()) { + MappedFile image("/res/icons/16x16/filetype-unknown.png"); + cache = encode_base64({ image.data(), image.size() }); + } + return cache; +} + void Client::handle_directory_listing(const String& requested_path, const String& real_path, const HTTP::HttpRequest& request) { StringBuilder builder; @@ -164,22 +186,23 @@ void Client::handle_directory_listing(const String& requested_path, const String builder.append("\n"); builder.append("Index of "); builder.append(escape_html_entities(requested_path)); - builder.append("\n"); - builder.append("\n"); + builder.append("\n"); builder.append("

Index of "); builder.append(escape_html_entities(requested_path)); builder.append("

\n"); builder.append("
\n"); - builder.append("\n"); + builder.append("
\n"); Core::DirIterator dt(real_path); while (dt.has_next()) { auto name = dt.next_path(); - builder.append(""); StringBuilder path_builder; path_builder.append(real_path); @@ -191,14 +214,25 @@ void Client::handle_directory_listing(const String& requested_path, const String if (rc < 0) { perror("stat"); } - builder.appendf("", st.st_size); + + bool is_directory = S_ISDIR(st.st_mode) || name.is_one_of(".", ".."); + + builder.append(""); + builder.appendf("", is_directory ? "folder" : "file"); + builder.append(""); + + builder.appendf("", st.st_size); builder.append(""); builder.append("\n"); } - builder.append("
"); - builder.append(escape_html_entities(name)); - builder.append("%10d
"); + builder.append(escape_html_entities(name)); + builder.append(" %10d "); builder.append(Core::DateTime::from_timestamp(st.st_mtime).to_string()); builder.append("
\n"); + builder.append("\n"); builder.append("
\n"); builder.append("Generated by WebServer (SerenityOS)\n"); builder.append("\n"); diff --git a/Services/WebServer/main.cpp b/Services/WebServer/main.cpp index 807f742e50..1aa67fbb4d 100644 --- a/Services/WebServer/main.cpp +++ b/Services/WebServer/main.cpp @@ -25,6 +25,7 @@ */ #include "Client.h" +#include #include #include #include @@ -75,6 +76,11 @@ int main(int argc, char** argv) server->listen({}, port); printf("Listening on 0.0.0.0:%d\n", port); + if (unveil("/res/icons", "r") < 0) { + perror("unveil"); + return 1; + } + if (unveil(real_root_path.characters(), "r") < 0) { perror("unveil"); return 1; -- cgit v1.2.3