This commit is contained in:
Win 2024-05-24 11:54:00 +07:00
parent 3e08995072
commit 218e41082c
3 changed files with 38 additions and 3 deletions

View File

@ -1,6 +1,28 @@
#include <stdio.h>
#include <string.h>
#include "file.h"
void get_file_path(char *uri, char *file_path) {
char *question = strchr(uri, '?');
char *question = strrchr(uri, '?');
if(question) {
*question = '\0';
}
if(uri[strlen(uri) - 1] == '/') {
strncat(uri, "index.html", 11);
}
strncpy(file_path, "htdocs", 7);
printf("%s\n", file_path);
strncat(file_path, uri, (int)sizeof(uri));
const char *dot = strrchr(file_path, '.');
if(!dot || dot == file_path) {
strncat(file_path, ".html", 6);
}
}
void get_mime_type(char *file, char *mime) {
}

3
src/file.h Normal file
View File

@ -0,0 +1,3 @@
#define MAX_PATH_SIZE 4096
void get_file_path(char *uri, char *file_path);

View File

@ -9,6 +9,7 @@
#include <time.h>
#include "server.h"
#include "file.h"
int send_response(int fd, const char *header, char *content_type, void *body, int content_length) {
const int max_response_size = MAX_RESPONSE_SIZE;
@ -107,8 +108,17 @@ int main() {
if (strncmp("GET", method, 7) != 0) {
send_response(client_sock_fd, BAD_REQUEST_HEAD, "text/html", NULL, 0);
} else {
char body[100];
int body_size = snprintf(body, 100, "%s\r\n", "hello what up bruh");
char file_path[4096];
get_file_path(uri, file_path);
FILE *file = fopen(file_path, "r");
if(!file) {
send_response(client_sock_fd, NOT_FOUND_HEAD, "text/html", NULL, 0);
}
// int body_size = snprintf(body, 100, "%s\r\n", "hello what up bruh");
send_response(client_sock_fd, OK_HEAD, "text/html", body, (body_size + 1));
}