diff --git a/server/request_handling.c b/server/request_handling.c index 5c0f404..0b4a003 100644 --- a/server/request_handling.c +++ b/server/request_handling.c @@ -2,6 +2,7 @@ #include #include #include +#include /** * Identical to strcpy, but strips a trailing '\r\n' @@ -93,20 +94,20 @@ int get_file_from_url(const char *uri, struct requested_file *req_file) { if (*uri == '/') { uri++; } + + struct stat file_stat; + int stat_result = stat(uri, &file_stat); + if (stat_result == -1) { + return -1; + } + FILE *open_file = fopen(uri, "r"); if (open_file == NULL) { return -1; } - int seek_result = fseek(open_file, 0, SEEK_END); - if (seek_result != 0) { - return -1; - } - long size = ftell(open_file); - if (size == -1) { - return -1; - } + rewind(open_file); - req_file->size = size; + req_file->size = file_stat.st_size; req_file->file = open_file; return 0;