branch: exit status now reflects if branch listing finds an error
[gitweb.git] / http-backend.c
index f8ea9d7faa0494375d3d5413e91869d74252d649..d1e83d0906dbc9d677630cdf74615ec3f9dfc46d 100644 (file)
@@ -108,6 +108,7 @@ static const char *get_parameter(const char *name)
        return i ? i->util : NULL;
 }
 
+__attribute__((format (printf, 2, 3)))
 static void format_write(int fd, const char *fmt, ...)
 {
        static char buffer[1024];
@@ -134,7 +135,7 @@ static void hdr_str(const char *name, const char *value)
        format_write(1, "%s: %s\r\n", name, value);
 }
 
-static void hdr_int(const char *name, size_t value)
+static void hdr_int(const char *name, uintmax_t value)
 {
        format_write(1, "%s: %" PRIuMAX "\r\n", name, value);
 }
@@ -165,6 +166,7 @@ static void end_headers(void)
        safe_write(1, "\r\n", 2);
 }
 
+__attribute__((format (printf, 1, 2)))
 static NORETURN void not_found(const char *err, ...)
 {
        va_list params;
@@ -180,6 +182,7 @@ static NORETURN void not_found(const char *err, ...)
        exit(0);
 }
 
+__attribute__((format (printf, 1, 2)))
 static NORETURN void forbidden(const char *err, ...)
 {
        va_list params;
@@ -216,7 +219,6 @@ static void send_local_file(const char *the_type, const char *name)
        char *buf = xmalloc(buf_alloc);
        int fd;
        struct stat sb;
-       size_t size;
 
        fd = open(p, O_RDONLY);
        if (fd < 0)
@@ -224,14 +226,12 @@ static void send_local_file(const char *the_type, const char *name)
        if (fstat(fd, &sb) < 0)
                die_errno("Cannot stat '%s'", p);
 
-       size = xsize_t(sb.st_size);
-
-       hdr_int(content_length, size);
+       hdr_int(content_length, sb.st_size);
        hdr_str(content_type, the_type);
        hdr_date(last_modified, sb.st_mtime);
        end_headers();
 
-       while (size) {
+       for (;;) {
                ssize_t n = xread(fd, buf, buf_alloc);
                if (n < 0)
                        die_errno("Cannot read '%s'", p);
@@ -538,15 +538,19 @@ static void service_rpc(char *service_name)
 
 static NORETURN void die_webcgi(const char *err, va_list params)
 {
-       char buffer[1000];
+       static int dead;
 
-       http_status(500, "Internal Server Error");
-       hdr_nocache();
-       end_headers();
+       if (!dead) {
+               char buffer[1000];
+               dead = 1;
 
-       vsnprintf(buffer, sizeof(buffer), err, params);
-       fprintf(stderr, "fatal: %s\n", buffer);
-       exit(0);
+               vsnprintf(buffer, sizeof(buffer), err, params);
+               fprintf(stderr, "fatal: %s\n", buffer);
+               http_status(500, "Internal Server Error");
+               hdr_nocache();
+               end_headers();
+       }
+       exit(0); /* we successfully reported a failure ;-) */
 }
 
 static char* getdir(void)
@@ -618,7 +622,7 @@ int main(int argc, char **argv)
                if (regcomp(&re, c->pattern, REG_EXTENDED))
                        die("Bogus regex in service table: %s", c->pattern);
                if (!regexec(&re, dir, 1, out, 0)) {
-                       size_t n = out[0].rm_eo - out[0].rm_so;
+                       size_t n;
 
                        if (strcmp(method, c->method)) {
                                const char *proto = getenv("SERVER_PROTOCOL");
@@ -632,9 +636,10 @@ int main(int argc, char **argv)
                        }
 
                        cmd = c;
+                       n = out[0].rm_eo - out[0].rm_so;
                        cmd_arg = xmalloc(n);
-                       strncpy(cmd_arg, dir + out[0].rm_so + 1, n);
-                       cmd_arg[n] = '\0';
+                       memcpy(cmd_arg, dir + out[0].rm_so + 1, n-1);
+                       cmd_arg[n-1] = '\0';
                        dir[out[0].rm_so] = 0;
                        break;
                }
@@ -647,6 +652,9 @@ int main(int argc, char **argv)
        setup_path();
        if (!enter_repo(dir, 0))
                not_found("Not a git repository: '%s'", dir);
+       if (!getenv("GIT_HTTP_EXPORT_ALL") &&
+           access("git-daemon-export-ok", F_OK) )
+               not_found("Repository not exported: '%s'", dir);
 
        git_config(http_config, NULL);
        cmd->imp(cmd_arg);