about summary refs log tree commit diff
path: root/cgit.c
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-04-27 11:01:08 +0200
committerRory& <root@rory.gay>2026-02-06 14:14:41 +0100
commitddabab8f413764771924aabd56711aea2081b94c (patch)
treebe73e8ee63d0d082519a486c3d5b49e88e7756a0 /cgit.c
parentFix test script, serve resources (diff)
downloadcgit-magenta-ddabab8f413764771924aabd56711aea2081b94c.tar.xz
Sendfile, minor fixes in static file support, spawn more processes in test, css fixes
Diffstat (limited to 'cgit.c')
-rw-r--r--cgit.c22
1 files changed, 19 insertions, 3 deletions
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; } }