WebServer: Show file size and modification time in directory listings
This exposed an issue with the unveil() implementation which currently short-circuits path resolution for any path containing "/..". This will cause the ".." entry to show up with a 1970-01-01 mtime for now. FIXME. Also add some rulers and a nice little footer. :^)
This commit is contained in:
parent
083f3edcb0
commit
deca1d8b77
Notes:
sideshowbarker
2024-07-19 09:21:59 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/deca1d8b77b
1 changed files with 26 additions and 4 deletions
|
@ -32,7 +32,9 @@
|
|||
#include <LibCore/File.h>
|
||||
#include <LibCore/HttpRequest.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace WebServer {
|
||||
|
||||
|
@ -136,20 +138,40 @@ void Client::handle_directory_listing(const String& requested_path, const String
|
|||
builder.append("<h1>Index of ");
|
||||
builder.append(requested_path);
|
||||
builder.append("</h1>\n");
|
||||
builder.append("<ul>\n");
|
||||
builder.append("<hr>\n");
|
||||
builder.append("<pre>\n");
|
||||
|
||||
Core::DirIterator dt(real_path);
|
||||
while (dt.has_next()) {
|
||||
auto name = dt.next_path();
|
||||
builder.append("<li>");
|
||||
builder.append("<a href=\"");
|
||||
builder.append(name);
|
||||
builder.append("\">");
|
||||
builder.append(name);
|
||||
builder.append("</a></li>\n");
|
||||
builder.append("</a>");
|
||||
for (size_t i = 0; i < (40 - name.length()); ++i)
|
||||
builder.append(' ');
|
||||
|
||||
StringBuilder path_builder;
|
||||
path_builder.append(real_path);
|
||||
path_builder.append('/');
|
||||
path_builder.append(name);
|
||||
struct stat st;
|
||||
memset(&st, 0, sizeof(st));
|
||||
int rc = stat(path_builder.to_string().characters(), &st);
|
||||
if (rc < 0) {
|
||||
perror("wut!");
|
||||
}
|
||||
dbg() << "statted _" << path_builder.to_string() << "_, rc = " << rc << " mtime = " << st.st_mtime;
|
||||
builder.appendf(" %10d", st.st_size);
|
||||
builder.appendf(" ");
|
||||
builder.append(Core::DateTime::from_timestamp(st.st_mtime).to_string());
|
||||
builder.append("\n");
|
||||
}
|
||||
|
||||
builder.append("</ul>\n");
|
||||
builder.append("</pre>\n");
|
||||
builder.append("<hr>\n");
|
||||
builder.append("<i>Generated by WebServer (SerenityOS)</i>\n");
|
||||
builder.append("</body>\n");
|
||||
builder.append("</html>\n");
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue