From e07525a0d9fce1eba7a13f451c7d1bcebd5c431c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20=C3=96man?= Date: Fri, 29 Aug 2008 15:59:01 +0000 Subject: [PATCH] Use sendfile() for sending static content --- webui/webui.c | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/webui/webui.c b/webui/webui.c index 8581e46d..6f99995c 100644 --- a/webui/webui.c +++ b/webui/webui.c @@ -16,6 +16,9 @@ * along with this program. If not, see . */ +#define _GNU_SOURCE /* for splice() */ +#include + #include #include #include @@ -24,7 +27,7 @@ #include #include -#include +#include #include "tvhead.h" #include "access.h" @@ -71,16 +74,22 @@ page_root(http_connection_t *hc, const char *remain, void *opaque) static int page_static(http_connection_t *hc, const char *remain, void *opaque) { - int fd, r; + int fd; const char *rootpath = HTS_BUILD_ROOT "/tvheadend/webui/static"; char path[500]; struct stat st; - void *buf; const char *content = NULL, *postfix; if(strstr(remain, "..")) return HTTP_STATUS_BAD_REQUEST; + postfix = strrchr(remain, '.'); + if(postfix != NULL) { + postfix++; + if(!strcmp(postfix, "js")) + content = "text/javascript; charset=UTF-8"; + } + snprintf(path, sizeof(path), "%s/%s", rootpath, remain); if((fd = open(path, O_RDONLY)) < 0) @@ -91,25 +100,9 @@ page_static(http_connection_t *hc, const char *remain, void *opaque) return 404; } - buf = malloc(st.st_size); - r = read(fd, buf, st.st_size); + http_send_header(hc, 200, content, st.st_size, NULL, NULL, 10); + sendfile(hc->hc_fd, fd, NULL, st.st_size); close(fd); - - if(r != st.st_size) { - free(buf); - return 404; - } - - postfix = strrchr(remain, '.'); - if(postfix != NULL) { - postfix++; - - if(!strcmp(postfix, "js")) - content = "text/javascript; charset=UTF-8"; - } - - htsbuf_append_prealloc(&hc->hc_reply, buf, st.st_size); - http_output_content(hc, content); return 0; }