using the status code from cgi output now

Sat, 29 Oct 2016 21:04:10 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 29 Oct 2016 21:04:10 +0200
changeset 124
85985e88f63b
parent 123
9c639e4941cf
child 125
c913d515be1e

using the status code from cgi output now

src/server/safs/cgi.c file | annotate | diff | comparison | revisions
src/server/safs/cgi.h file | annotate | diff | comparison | revisions
--- a/src/server/safs/cgi.c	Sat Oct 29 20:33:46 2016 +0200
+++ b/src/server/safs/cgi.c	Sat Oct 29 21:04:10 2016 +0200
@@ -84,6 +84,8 @@
         return ret;
     }
     
+    util_env_free(env);
+    
     char buf[4096]; // I/O buffer
     ssize_t r;
     
@@ -111,11 +113,15 @@
             int ret = cgi_parse_response(parser, buf, r, &pos);
             if(ret == -1) {
                 protocol_status(sn, rq, 500, NULL);
+                cgi_parser_free(parser);
                 return REQ_ABORTED;
             } else if(ret == 0) {
                 
             } else if(ret == 1) {
                 cgiheader = FALSE;
+                if(parser->status > 0) {
+                    protocol_status(sn, rq, parser->status, parser->msg);
+                }
                 http_start_response(sn, rq);
                 if(pos < r) {
                     net_write(sn->csd, buf+pos, r-pos);
@@ -126,6 +132,7 @@
         }
     }
     
+    cgi_parser_free(parser);
     return REQ_PROCEED;
 }
 
@@ -188,9 +195,18 @@
     parser->sn = sn;
     parser->rq = rq;
     parser->tmp = ucx_buffer_new(NULL, 64, UCX_BUFFER_AUTOEXTEND);
+    parser->status = 0;
+    parser->msg = NULL;
     return parser;
 }
 
+void cgi_parser_free(CGIResponseParser *parser) {
+    if(parser->tmp) {
+        ucx_buffer_free(parser->tmp);
+    }
+    pool_free(parser->sn->pool, parser);
+}
+
 /*
  * parses a cgi response line and adds the response header to rq->srvhdrs
  * returns 0: incomplete line
@@ -229,12 +245,37 @@
             if(name.length == 0 || value.length == 0) {
                 return -1;
             }
-            pblock_nvlinsert(
-                    name.ptr,
-                    name.length,
-                    value.ptr,
-                    value.length,
-                    parser->rq->srvhdrs);
+            
+            if(!sstrcmp(name, S("status"))) {
+                sstr_t codestr = value;
+                int i;
+                for(i=0;i<codestr.length;i++) {
+                    if(!isdigit(codestr.ptr[i])) {
+                        break;
+                    }
+                    if(i > 2) {
+                        break;
+                    }
+                }
+                codestr.ptr[i] = '\0';
+                
+                int64_t s = 0;
+                util_strtoint(codestr.ptr, &s);
+                parser->status = (int)s;
+                
+                sstr_t msg = sstrtrim(sstrsubs(value, i + 1));
+                
+                if(msg.length > 0) {
+                    parser->msg = sstrdup_pool(parser->sn->pool, msg).ptr;
+                }
+            } else {
+                pblock_nvlinsert(
+                        name.ptr,
+                        name.length,
+                        value.ptr,
+                        value.length,
+                        parser->rq->srvhdrs);
+            }
             
             line_begin = i+1;
             value_begin = line_begin;
--- a/src/server/safs/cgi.h	Sat Oct 29 20:33:46 2016 +0200
+++ b/src/server/safs/cgi.h	Sat Oct 29 21:04:10 2016 +0200
@@ -46,6 +46,8 @@
     Session   *sn;
     Request   *rq;
     UcxBuffer *tmp;
+    int       status;
+    char      *msg;
 } CGIResponseParser;
     
 int send_cgi(pblock *pb, Session *sn, Request *rq);
@@ -55,6 +57,7 @@
 int cgi_start(CGIProcess *p, char *path, char *const argv[], char *const envp[]);
 
 CGIResponseParser* cgi_parser_new(Session *sn, Request *rq);
+void cgi_parser_free(CGIResponseParser *parser);
 int cgi_parse_response(CGIResponseParser *parser, char *buf, size_t len, size_t *bpos);
 
 #ifdef __cplusplus

mercurial