diff --git a/cgit.c b/cgit.c
index fc9b18b..f3d885f 100644
--- a/cgit.c
+++ b/cgit.c
@@ -9,6 +9,9 @@
#define USE_THE_REPOSITORY_VARIABLE
#include "cgit.h"
+
+#include <sys/sendfile.h>
+
#include "cache.h"
#include "cmd.h"
#include "configfile.h"
@@ -1104,7 +1107,7 @@ int cmd_main(int argc, const char **argv)
) {
// files are in the same directory as the cgit binary
char filename[1024];
- snprintf(filename, sizeof(filename), "%s/%s", dirname(argv[0]), script_name + 1);
+ snprintf(filename, sizeof(filename), "%s/%s", dirname((char*) argv[0]), script_name + 1);
struct stat st;
if (!stat(filename, &st)) {
FILE *f = fopen(filename, "r");
@@ -1119,12 +1122,25 @@ int cmd_main(int argc, const char **argv)
else if (ext && !strcmp(ext, ".js"))
ctx.page.mimetype = "application/javascript";
+
+ cgit_print_http_headers();
+#ifndef HAVE_LINUX_SENDFILE
char *buf = xmalloc(st.st_size);
fread(buf, 1, st.st_size, f);
- fclose(f);
- cgit_print_http_headers();
+
fwrite(buf, 1, st.st_size, stdout);
free(buf);
+#else
+ fflush(0); // manually flush headers
+ ssize_t sfErr = sendfile(STDOUT_FILENO, fileno(f), NULL, st.st_size);
+ if (sfErr < 0) {
+ perror("sendfile");
+ fclose(f);
+ return -1;
+ }
+#endif
+
+ fclose(f);
return 0;
}
}
|