changeset 864:6304f5e8fbb4

FastCGI working with lighttpd
author Adam Chlipala <adamc@hcoop.net>
date Sat, 27 Jun 2009 19:07:28 -0400
parents 305bc0a431de
children ebefb0609ac3
files include/urweb.h src/c/fastcgi.c src/c/request.c src/c/urweb.c
diffstat 4 files changed, 45 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb.h	Sat Jun 27 17:50:31 2009 -0400
+++ b/include/urweb.h	Sat Jun 27 19:07:28 2009 -0400
@@ -13,7 +13,8 @@
 
 void uw_client_connect(unsigned id, int pass, int sock,
                        int (*send)(int sockfd, const void *buf, size_t len),
-                       int (*close)(int fd));
+                       int (*close)(int fd),
+                       void *logger_data, uw_logger log_error);
 void uw_prune_clients(uw_context);
 failure_kind uw_initialize(uw_context);
 
--- a/src/c/fastcgi.c	Sat Jun 27 17:50:31 2009 -0400
+++ b/src/c/fastcgi.c	Sat Jun 27 19:07:28 2009 -0400
@@ -202,13 +202,15 @@
   *s = 0;
 
   if (!strcasecmp(saved_h, "Content-length")
-      || !strcasecmp(saved_h, "Content-type"))
-    return search_nvps(hs->nvps, hs->uppercased + 5);
-  else
-    return search_nvps(hs->nvps, hs->uppercased);
+      || !strcasecmp(saved_h, "Content-type")) {
+    if (s = search_nvps(hs->nvps, hs->uppercased + 5))
+      return s;
+  }
+  
+  return search_nvps(hs->nvps, hs->uppercased);
 }
 
-static int read_funny_len(char **buf, int *len) {
+static int read_funny_len(unsigned char **buf, int *len) {
   if (*len <= 0)
     return -1;
 
@@ -228,12 +230,12 @@
   }
 }
 
-static int read_nvp(char *buf, int len, nvp *nv) {
+static int read_nvp(unsigned char **buf, int len, nvp *nv) {
   int nameLength, valueLength;
 
-  if ((nameLength = read_funny_len(&buf, &len)) < 0)
+  if ((nameLength = read_funny_len(buf, &len)) < 0)
     return -1;
-  if ((valueLength = read_funny_len(&buf, &len)) < 0)
+  if ((valueLength = read_funny_len(buf, &len)) < 0)
     return -1;
   if (len < nameLength + valueLength)
     return -1;
@@ -247,12 +249,14 @@
     nv->value = realloc(nv->value, nv->value_len);
   }
 
-  memcpy(nv->name, buf, nameLength);
+  memcpy(nv->name, *buf, nameLength);
   nv->name[nameLength] = 0;
 
-  memcpy(nv->value, buf + nameLength, valueLength);
+  memcpy(nv->value, *buf + nameLength, valueLength);
   nv->value[valueLength] = 0;
 
+  *buf += nameLength + valueLength;
+
   return 0;
 }
 
@@ -337,7 +341,8 @@
     }
 
     while (1) {
-      char *buf;
+      unsigned char *buf;
+      int len;
 
       if (!(r = fastcgi_recv(in))) {
         write_stderr(out, "Error receiving environment variables\n");
@@ -352,21 +357,27 @@
       if (r->contentLengthB1 == 0 && r->contentLengthB0 == 0)
         break;
 
-      if (used_nvps == hs.n_nvps-1) {
-        ++hs.n_nvps;
-        hs.nvps = realloc(hs.nvps, hs.n_nvps * sizeof(nvp));
-        hs.nvps[hs.n_nvps-1].name = malloc(1);
-        hs.nvps[hs.n_nvps-1].value = malloc(0);
-        hs.nvps[hs.n_nvps-1].name_len = 1;
-        hs.nvps[hs.n_nvps-1].value_len = 0;
+      len = (r->contentLengthB1 << 8) | r->contentLengthB0;
+
+      for (buf = r->contentData; buf < r->contentData + len; ) {
+        if (used_nvps == hs.n_nvps-1) {
+          ++hs.n_nvps;
+          hs.nvps = realloc(hs.nvps, hs.n_nvps * sizeof(nvp));
+          hs.nvps[hs.n_nvps-1].name = malloc(1);
+          hs.nvps[hs.n_nvps-1].value = malloc(0);
+          hs.nvps[hs.n_nvps-1].name_len = 1;
+          hs.nvps[hs.n_nvps-1].value_len = 0;
+        }
+        
+        if (read_nvp(&buf, len - (buf - r->contentData), &hs.nvps[used_nvps]) < 0) {
+          write_stderr(out, "Error reading FCGI_PARAMS name-value pair\n");
+          goto done;
+        }
+
+        write_stderr(out, "PARAM: %s -> %s\n", hs.nvps[used_nvps].name, hs.nvps[used_nvps].value);
+
+        ++used_nvps;
       }
-
-      if (read_nvp(r->contentData, (r->contentLengthB1 << 8) | r->contentLengthB0, &hs.nvps[used_nvps]) < 0) {
-        write_stderr(out, "Error reading FCGI_PARAMS name-value pair\n");
-        goto done;
-      }
-
-      ++used_nvps;
     }
 
     hs.nvps[used_nvps].name[0] = 0;
--- a/src/c/request.c	Sat Jun 27 17:50:31 2009 -0400
+++ b/src/c/request.c	Sat Jun 27 19:07:28 2009 -0400
@@ -217,7 +217,7 @@
 
     if (id && pass) {
       unsigned idn = atoi(id);
-      uw_client_connect(idn, atoi(pass), sock, send, close);
+      uw_client_connect(idn, atoi(pass), sock, send, close, logger_data, log_error);
       log_error(logger_data, "Processed request for messages by client %u\n\n", idn);
       return KEEP_OPEN;
     }
--- a/src/c/urweb.c	Sat Jun 27 17:50:31 2009 -0400
+++ b/src/c/urweb.c	Sat Jun 27 19:07:28 2009 -0400
@@ -206,12 +206,13 @@
 
 void uw_client_connect(unsigned id, int pass, int sock,
                        int (*send)(int sockfd, const void *buf, ssize_t len),
-                       int (*close)(int fd)) {
+                       int (*close)(int fd),
+                       void *logger_data, uw_logger log_error) {
   client *c = find_client(id);
 
   if (c == NULL) {
     close(sock);
-    fprintf(stderr, "Out-of-bounds client request (%u)\n", id);
+    log_error(logger_data, "Out-of-bounds client request (%u)\n", id);
     return;
   }
 
@@ -220,14 +221,14 @@
   if (c->mode != USED) {
     pthread_mutex_unlock(&c->lock);
     close(sock);
-    fprintf(stderr, "Client request for unused slot (%u)\n", id);
+    log_error(logger_data, "Client request for unused slot (%u)\n", id);
     return;
   }
 
   if (pass != c->pass) {
     pthread_mutex_unlock(&c->lock);
     close(sock);
-    fprintf(stderr, "Wrong client password (%u, %d)\n", id, pass);
+    log_error(logger_data, "Wrong client password (%u, %d)\n", id, pass);
     return;
   }
 
@@ -563,7 +564,7 @@
       client *c = new_client();
       use_client(c);
       ctx->client = c;
-    }  
+    }
   }
 }