From 0330ff80ff2fb0e794e88189e6a867cbca8d286e Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sun, 9 Sep 2018 14:52:27 -0400 Subject: [PATCH] Refactor get_file_from_url to use stat --- server/request_handling.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) 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;