Automatically serve index files

This commit is contained in:
Adam Ruzicka 2023-04-28 19:08:50 +02:00
parent 74f9914d16
commit 9b9e9c15c9
1 changed files with 11 additions and 5 deletions

View File

@ -39,16 +39,22 @@ method !write-response($conn, $status, $meta, $log, :$body-stream) {
$body-stream.tap: { await $conn.write($_) } if $body-stream;
}
method !serve-file($conn, $path, $log) {
$log.log("Serving " ~ $path);
self!write-response($conn, 20, "", $log,
body-stream => $path.open(:bin).Supply)
}
method !handle-request($conn, $request, $log) {
my $file = $.root.IO.add($request.path);
when $request.host !~~ $.domain {
self!write-response($conn, 53, "This server does not accept proxy requests", $log)
}
default {
my $file = $.root.IO.add($request.path);
$log.log("Serving " ~ $file);
self!write-response($conn, 20, "", $log,
body-stream => $file.IO.open(:bin).Supply)
when $file.IO ~~ :f { self!serve-file($conn, $file.IO, $log) }
when $file.IO ~~ :d && $file.add("index.gmi").IO ~~ :f {
self!serve-file($conn, $file.add("index.gmi").IO, $log);
}
default { self!write-response($conn, 51, "File not found", $log) }
}
method !handle-connection($conn) {