mirror of
https://github.com/fumiama/simple-http-server.git
synced 2026-06-05 00:30:23 +08:00
另一种stat
This commit is contained in:
25
server.c
25
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 */
|
/* Parameters: path of the file */
|
||||||
/**********************************************************************/
|
/**********************************************************************/
|
||||||
off_t get_file_size(const char *filepath) {
|
off_t get_file_size(const char *filepath) {
|
||||||
struct stat statbuf;
|
struct stat *statbuf = malloc(sizeof(struct stat));
|
||||||
if (stat(filepath, &statbuf) == 0) {
|
off_t sz;
|
||||||
printf("file size: %lu\n", statbuf.st_size);
|
if (stat(filepath, statbuf) == 0) {
|
||||||
if(statbuf.st_size == 0) {
|
sz = statbuf->st_size;
|
||||||
FILE* fp = fopen(filepath, "ab+");
|
printf("file size: %lu\n", sz);
|
||||||
off_t sz = ftell(fp);
|
free(statbuf);
|
||||||
printf("ftell size: %lu\n", sz);
|
return sz;
|
||||||
return sz;
|
}
|
||||||
}
|
else {
|
||||||
return statbuf.st_size;
|
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;
|
uint extpos = strlen(filepath) - 4;
|
||||||
|
|
||||||
ADD_HERDER(HTTP200 SERVER_STRING);
|
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));
|
ADD_HERDER_PARAM(CONTENT_LEN "\r\n", get_file_size(filepath));
|
||||||
send(client, buf, offset, 0);
|
send(client, buf, offset, 0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user