[hackers] [quark] cgi: parse Status: header || Hiltjo Posthuma

From: <git_AT_suckless.org>
Date: Wed, 20 Aug 2014 12:56:24 +0200

commit fa113f8b47c909003aea0c0f35126f6598026506
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Wed Aug 20 12:41:28 2014 +0000

    cgi: parse Status: header
    
    CGI applications can specify a HTTP status to output with the Status:
    header. For simplicity the CGI application must use this header on the
    first line. With this change cloning git repositories over HTTP with
    cgit works.
    
    in config.mk specify _GNU_SOURCE and _POSIX_C_SOURCE instead of
    _GNU_SOURCE, this is for getline().

diff --git a/config.mk b/config.mk
index 74099b6..a9314d0 100644
--- a/config.mk
+++ b/config.mk
_AT_@ -12,7 +12,7 @@ INCS = -I. -I/usr/include
 LIBS = -L/usr/lib -lc
 
 # flags
-CPPFLAGS = -DVERSION=\"${VERSION}\" -D_GNU_SOURCE
+CPPFLAGS = -DVERSION=\"${VERSION}\" -D_POSIX_C_SOURCE=200809 -D_BSD_SOURCE
 CFLAGS = -g -std=c99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS}
 LDFLAGS = ${LIBS}
 #LDFLAGS = -s ${LIBS}
diff --git a/quark.c b/quark.c
index bf2bed6..6ae71b9 100644
--- a/quark.c
+++ b/quark.c
_AT_@ -306,8 +306,9 @@ responsedir(void) {
 void
 responsecgi(void) {
         FILE *cgi;
- size_t r;
- char *q;
+ size_t r, linesiz = 0;
+ char *q, *line = NULL, *statusline = HttpOk;
+ ssize_t linelen;
 
         if (req.type == GET)
                 setenv("REQUEST_METHOD", "GET", 1);
_AT_@ -338,15 +339,29 @@ responsecgi(void) {
                 logerrmsg("error chdir to cgi directory %s failed: %s
",
                           cgi_dir, strerror(errno));
         if ((cgi = popen(cgi_script, "r"))) {
- if (putresentry(HEADER, HttpOk, tstamp(0)))
- return;
                 status = 200;
+ if ((linelen = getline(&line, &linesiz, cgi)) > 0) {
+ if (strncmp(line, "Status:", strlen("Status:")) == 0) {
+ statusline = line + strlen("Status:") + 1;
+ errno = 0;
+ status = strtol(statusline, NULL, 10);
+ if(errno)
+ status = 200;
+ if (putresentry(HEADER, statusline, tstamp(0)))
+ return;
+ writedata(line, linelen);
+ } else {
+ if (putresentry(HEADER, statusline, tstamp(0)))
+ return;
+ }
+ }
                 while ((r = fread(resbuf, 1, MAXBUFLEN, cgi)) > 0) {
                         if (writedata(resbuf, r)) {
                                 pclose(cgi);
                                 return;
                         }
                 }
+ free(line);
                 pclose(cgi);
         } else {
                 logerrmsg("error %s requests %s, but cannot run cgi script %s: %s
",
Received on Wed Aug 20 2014 - 12:56:24 CEST

This archive was generated by hypermail 2.3.0 : Wed Aug 20 2014 - 13:00:14 CEST