Semi-dynamic routing

This commit is contained in:
cdricms
2024-11-10 21:04:17 +01:00
parent 3918dc1da9
commit 9ccde76edc
4 changed files with 49 additions and 33 deletions

14
main.c
View File

@@ -7,6 +7,8 @@
#include <sys/socket.h>
#include <unistd.h>
#define DEFAULT_HTML "index.html"
int main() {
const int PORT = 8080;
@@ -50,11 +52,17 @@ int main() {
1024 - 1); // subtract 1 for the null
// terminator at the end
HttpRequest *req = handle_request(request);
// print_request(req);
HttpResponse *res = from_file("./index.html");
HttpResponse *res;
if (!strcmp(req->path, "/")) {
res = from_file("./" DEFAULT_HTML);
} else {
char path[] = ".";
strcat(path, req->path);
res = from_file(path);
}
free(req);
http_respond(*res, clientfd);
free_response(res);
printf("Hello message sent\n");
// closing the connected socket
close(clientfd);