From 103fe88800f32821fa63e1c6b584be5a0c0f538a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Mon, 7 Jun 2021 23:09:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=A6=E4=B8=80=E7=A7=8Dstat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/server.c b/server.c index 41350d1..7624c71 100644 --- a/server.c +++ b/server.c @@ -290,18 +290,19 @@ void execute_cgi(int client, const char *path, const char *method, const char *q /* Parameters: path of the file */ /**********************************************************************/ off_t get_file_size(const char *filepath) { - struct stat statbuf; - if (stat(filepath, &statbuf) == 0) { - printf("file size: %lu\n", statbuf.st_size); - if(statbuf.st_size == 0) { - FILE* fp = fopen(filepath, "ab+"); - off_t sz = ftell(fp); - printf("ftell size: %lu\n", sz); - return sz; - } - return statbuf.st_size; + struct stat *statbuf = malloc(sizeof(struct stat)); + off_t sz; + if (stat(filepath, statbuf) == 0) { + sz = statbuf->st_size; + printf("file size: %lu\n", sz); + free(statbuf); + return sz; + } + else { + free(statbuf); + error_die("stat"); + return -1; } - else return -1; } /**********************************************************************/ @@ -375,7 +376,7 @@ void headers(int client, const char *filepath) { uint extpos = strlen(filepath) - 4; ADD_HERDER(HTTP200 SERVER_STRING); - ADD_HERDER_PARAM(CONTENT_TYPE, EXTNM_IS_NOT("html")?(EXTNM_IS_NOT(".css")?"text/plain":"text/css"):"text/html"); + ADD_HERDER_PARAM(CONTENT_TYPE, EXTNM_IS_NOT("html")?(EXTNM_IS_NOT(".css")?(EXTNM_IS_NOT("ico")?"text/plain":"image/x-icon"):"text/css"):"text/html"); ADD_HERDER_PARAM(CONTENT_LEN "\r\n", get_file_size(filepath)); send(client, buf, offset, 0); }