diff --git a/src/file.c b/src/file.c index 1dcb12a..626bf62 100644 --- a/src/file.c +++ b/src/file.c @@ -1,6 +1,28 @@ #include #include +#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) { + } diff --git a/src/file.h b/src/file.h new file mode 100644 index 0000000..e115710 --- /dev/null +++ b/src/file.h @@ -0,0 +1,3 @@ +#define MAX_PATH_SIZE 4096 + +void get_file_path(char *uri, char *file_path); diff --git a/src/server.c b/src/server.c index a4d17e2..49c7362 100644 --- a/src/server.c +++ b/src/server.c @@ -9,6 +9,7 @@ #include #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)); }