Move things around

This commit is contained in:
Adam Ruzicka 2023-04-28 16:54:39 +02:00
parent e50f0d1515
commit 8f7f4918ee
3 changed files with 83 additions and 69 deletions

View File

@ -14,7 +14,8 @@
"perl": "6.d",
"provides": {
"Ramini": "lib/Ramini.rakumod",
"Ramini::Logger": "lib/Logger.raku"
"Ramini::Logger": "lib/Logger.raku",
"Ramini::Server": "lib/Server.raku"
},
"resources": [
],

View File

@ -1,76 +1,10 @@
#!/usr/bin/env raku
use IO::Socket::Async::SSL;
use URI;
constant ROOT = ".".IO.absolute;
constant DOMAIN = "touched-mammal.lxd.test";
use Ramini;
use Ramini::Logger;
use Ramini::Server;
sub write-response($conn, $status, $meta, :$body-stream) {
my $header = $status.Str ~ " " ~ $meta ~ "\r\n";
await $conn.print($header);
$body-stream.tap: { await $conn.write($_) } if $body-stream;
}
sub handle-request($conn, $request) {
when $request.host !~~ DOMAIN {
write-response($conn, 53, "This server does not accept proxy requests")
}
default {
write-response($conn, 20, "",
body-stream => ROOT.IO.add($request.path).IO.open(:bin).Supply)
}
}
sub handle-connection($conn) {
my $start = now;
my $buf = Buf.new;
my $log = Ramini::Logger.new();
$log.log("Incoming connection");
react {
whenever $conn.Supply(:bin) {
$buf.append($_);
# URL can be up to 1024 bytes long + CRLF
my $req = try $buf.subbuf(0..1025).decode('utf-8');
when $req.contains("\r\n") {
my $uri = URI.new($req.lines[0].chomp);
handle-request($conn, $uri);
$conn.close;
}
when $buf.bytes > 1024 {
write-response($conn, 59, "Request URL too long");
$conn.close;
}
}
}
CATCH {
default {
$log.exception($_);
$conn.close;
}
}
$log.log("Closed connection after " ~ (now - $start) ~ " seconds");
}
my %ssl-config =
certificate-file => 'cert.pem',
private-key-file => 'key.pem';
my $connections = IO::Socket::Async::SSL.listen('::', 4433, |%ssl-config);
react {
my $listener = do whenever $connections -> $socket {
start handle-connection($socket)
}
whenever signal(SIGINT) {
say "Shutting down...";
$listener.close;
exit;
}
}
Ramini::Server.new(root => ROOT.IO, domain => DOMAIN).run()

79
lib/Server.raku Normal file
View File

@ -0,0 +1,79 @@
use IO::Socket::Async::SSL;
use URI;
use Ramini::Logger;
unit class Ramini::Server;
has IO::Path $.root;
has Str $.domain;
method run() {
my %ssl-config =
certificate-file => 'cert.pem',
private-key-file => 'key.pem';
my $connections = IO::Socket::Async::SSL.listen('::', 4433, |%ssl-config);
react {
my $listener = do whenever $connections -> $socket {
start self!handle-connection($socket)
}
say "Server listening on port 4433";
whenever signal(SIGINT) {
say "Shutting down...";
$listener.close;
exit;
}
}
}
method !write-response($conn, $status, $meta, :$body-stream) {
my $header = $status.Str ~ " " ~ $meta ~ "\r\n";
await $conn.print($header);
$body-stream.tap: { await $conn.write($_) } if $body-stream;
}
method !handle-request($conn, $request) {
when $request.host !~~ $.domain {
self!write-response($conn, 53, "This server does not accept proxy requests")
}
default {
self!write-response($conn, 20, "",
body-stream => $.root.IO.add($request.path).IO.open(:bin).Supply)
}
}
method !handle-connection($conn) {
my $start = now;
my $buf = Buf.new;
my $log = Ramini::Logger.new();
$log.log("Incoming connection");
react {
whenever $conn.Supply(:bin) {
$buf.append($_);
# URL can be up to 1024 bytes long + CRLF
my $req = try $buf.subbuf(0..1025).decode('utf-8');
when $req && $req.contains("\r\n") {
my $uri = URI.new($req.lines[0].chomp);
self!handle-request($conn, $uri);
$conn.close;
}
when $buf.bytes > 1024 {
self!write-response($conn, 59, "Request URL too long");
$conn.close;
}
}
}
CATCH {
default {
$log.exception($_);
$conn.close;
}
}
$log.log("Closed connection after " ~ (now - $start) ~ " seconds");
}