Frontend: Add support for HTML templates

This commit is contained in:
mrkubax10 2023-09-06 20:09:20 +02:00
parent 2ba4b637de
commit 1ffe5fffb4
3 changed files with 138 additions and 17 deletions

View File

@ -25,6 +25,16 @@ use feature qw(switch);
use strict;
use warnings;
sub readFullFile {
my $aFile = $_[0];
my $content = "";
while(!eof($aFile)) {
$content.=readline($aFile);
}
return $content;
}
use constant {
HTTP_METHOD_UNKNOWN => 0,
HTTP_METHOD_GET => 1,
@ -132,6 +142,123 @@ sub sendNotImplemented {
$aClient->send($response);
}
sub sendNotFound {
my $aClient = $_[0];
my $content = "<h1>404 Not Found</h1><h6>irclogger_web</h6>";
my $response = getBaseResponse(404, "Not Found");
$response.="Content-Type: text/html, charset=utf-8\r\n";
$response.="Content-Length: ".length($content)."\r\n\r\n";
$response.=$content;
$aClient->send($response);
}
use constant {
PREPROCESSOR_STATE_TEXT => 0,
PREPROCESSOR_STATE_VAR => 1,
PREPROCESSOR_STATE_INC => 2
};
sub preprocessHTML {
my $aContent = $_[0];
my $aVariables = $_[1];
my $contentLength = length($aContent);
my $state = PREPROCESSOR_STATE_TEXT;
my $currentString = "";
my $index = 0;
my $output = "";
while($index<$contentLength) {
my $char = substr($aContent, $index++, 1);
given($state) {
when(PREPROCESSOR_STATE_TEXT) {
if($char eq "{" && $index<$contentLength) {
my $nextChar = substr($aContent, $index, 1);
if($nextChar eq "{") {
$index++;
$state = PREPROCESSOR_STATE_VAR;
next;
}
}
if($char eq "[" && $index<$contentLength) {
my $nextChar = substr($aContent, $index, 1);
if($nextChar eq "[") {
$index++;
$state = PREPROCESSOR_STATE_INC;
next;
}
}
$output.=$char;
}
when(PREPROCESSOR_STATE_VAR) {
if($char eq "}" && $index<$contentLength) {
my $nextChar = substr($aContent, $index, 1);
if($nextChar eq "}") {
$index++;
$output.=$aVariables->{$currentString};
$currentString = "";
$state = PREPROCESSOR_STATE_TEXT;
next;
}
}
$currentString.=$char;
}
when(PREPROCESSOR_STATE_INC) {
if($char eq "]" && $index<$contentLength) {
my $nextChar = substr($aContent, $index, 1);
if($nextChar eq "]") {
$index++;
if(open(my $file, "<", $currentString)) {
$output.=readFullFile($file);
close($file);
}
else {
$output.="[[Include file not found]";
}
$currentString = "";
$state = PREPROCESSOR_STATE_TEXT;
next;
}
}
$currentString.=$char;
}
}
}
return $output;
}
sub sendTemplate {
my $aFilePath = $_[0];
my $aClient = $_[1];
my $aVariables = $_[2];
my $result = open(my $file, "<", $aFilePath);
if(!$result) {
sendNotFound($aClient);
return 0;
}
my $content = preprocessHTML(readFullFile($file), $aVariables);
close($file);
my $length = length($content);
my $response = getBaseResponse(200, "OK");
$response.="Content-Length: $length\r\n\r\n";
$response.=$content;
$aClient->send($response);
}
sub handlePath {
my $aClient = $_[0];
my $aPath = $_[1];
my $aRequest = $_[2];
given($aPath) {
when("/") {
sendTemplate("templates/index.html", $aClient, {"var"=>"value"});
return 1;
}
}
return 0;
}
sub sendResponse {
my $aClient = $_[0];
my $aRequest = $_[1];
@ -144,25 +271,19 @@ sub sendResponse {
given($aRequest->{"method"}) {
when(HTTP_METHOD_GET) {
my $path = File::Spec->canonpath($aRequest->{"path"});
if($path eq "/") {
$path = "/index.html";
if($path eq "/index.html" || $path eq "/index.htm") {
$path = "/";
}
my $filePath = "templates".$path;
my $result = open(my $file, "<", $filePath);
print($filePath);
if(!$result) {
my $content = "<h1>404 Not Found</h1><h6>irclogger_web</h6>";
my $response = getBaseResponse(404, "Not Found");
$response.="Content-Type: text/html, charset=utf-8\r\n";
$response.="Content-Length: ".length($content)."\r\n\r\n";
$response.=$content;
$aClient->send($response);
if(handlePath($aClient, $path, $aRequest)) {
return;
}
my $content = "";
while(!eof($file)) {
$content.=readline($file);
my $filePath = "static".$path;
my $result = open(my $file, "<", $filePath);
if(!$result) {
sendNotFound($aClient);
return;
}
my $content = readFullFile($file);
close($file);
my $response = getBaseResponse(200, "OK");
# TODO
@ -170,7 +291,6 @@ sub sendResponse {
#$response.="Content-Type: $mime\r\n";
$response.="Content-Length: ".length($content)."\r\n\r\n";
$response.=$content;
print($response);
$aClient->send($response);
}
default {

1
static/index.html Normal file
View File

@ -0,0 +1 @@
<h1>Hi!</h1>

View File

@ -1 +1 @@
<h1>Hi!</h1>
<h1>{{var}}</h1>