From ca0bb22ec7288c3e47f922b982fc68d513e2031a Mon Sep 17 00:00:00 2001 From: verdant Date: Tue, 9 Jun 2026 16:38:12 +0800 Subject: Add shsd_sendfile() function --- server.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'server.c') diff --git a/server.c b/server.c index 296c35f..7bc991a 100644 --- a/server.c +++ b/server.c @@ -11,6 +11,18 @@ #include #include +int shsd_sendfile(int in_fd, FILE *ou_fd, struct stat *s) +{ + int stat_result = fstat(fileno(ou_fd), s); + if (stat_result != 0) { + perror("fstat"); + return -1; + } + sendfile(in_fd, fileno(ou_fd), NULL, s->st_size); + + return 0; +} + int main(int argc, char **argv) { struct addrinfo hints, *res, *servinfo; @@ -59,8 +71,6 @@ int main(int argc, char **argv) return -1; } - int sent_bytes = 0; - if (strncmp(buf, "GET", 3) == 0) { char *http_status = "HTTP/1.1 200 OK\r\n"; char *http_content_type = "text/html\r\n\r\n"; @@ -74,13 +84,7 @@ int main(int argc, char **argv) FILE *index_fd = fopen("./index.html", "r"); struct stat stat_buf; - int stat_result = fstat(fileno(index_fd), &stat_buf); - if (stat_result != 0) { - perror("fstat"); - } - - sendfile(new_fd, fileno(index_fd), NULL, - stat_buf.st_size); + shsd_sendfile(new_fd, index_fd, &stat_buf); } memset(buf, 0, 1024); -- cgit v1.2.3