diff options
Diffstat (limited to 'server.c')
| -rw-r--r-- | server.c | 22 |
1 files changed, 13 insertions, 9 deletions
@@ -11,6 +11,18 @@ #include <sys/sendfile.h> #include <unistd.h> +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); |
