diff src/c/http.c @ 856:86ec89baee01

cgi protocol
author Adam Chlipala <adamc@hcoop.net>
date Tue, 23 Jun 2009 17:59:23 -0400
parents 28e42b22424d
children 60240acd15b9
line wrap: on
line diff
--- a/src/c/http.c	Tue Jun 23 15:56:04 2009 -0400
+++ b/src/c/http.c	Tue Jun 23 17:59:23 2009 -0400
@@ -8,11 +8,10 @@
 #include <netinet/in.h>
 #include <unistd.h>
 #include <signal.h>
+#include <stdarg.h>
 
 #include <pthread.h>
 
-#include <mhash.h>
-
 #include "urweb.h"
 #include "request.h"
 
@@ -73,9 +72,31 @@
   return NULL;
 }
 
+static void on_success(uw_context ctx) {
+  uw_write_header(ctx, "HTTP/1.1 200 OK\r\n");
+}
+
+static void on_failure(uw_context ctx) {
+  uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
+}
+
+static void log_error(void *data, const char *fmt, ...) {
+  va_list ap;
+  va_start(ap, fmt);
+
+  vfprintf(stderr, fmt, ap);
+}
+
+static void log_debug(void *data, const char *fmt, ...) {
+  va_list ap;
+  va_start(ap, fmt);
+
+  vprintf(fmt, ap);
+}
+
 static void *worker(void *data) {
   int me = *(int *)data;
-  uw_context ctx = uw_request_new_context();
+  uw_context ctx = uw_request_new_context(NULL, log_error, log_debug);
   size_t buf_size = 2;
   char *buf = malloc(buf_size);
   uw_request_context rc = uw_new_request_context();
@@ -205,7 +226,10 @@
 
         uw_set_headers(ctx, get_header, headers);
 
-        rr = uw_request(rc, ctx, method, path, query_string, body, back - body, sock);
+        rr = uw_request(rc, ctx, method, path, query_string, body, back - body,
+                        on_success, on_failure,
+                        NULL, log_error, log_debug,
+                        sock);
         uw_send(ctx, sock);
 
         if (rr == SERVED || rr == FAILED)
@@ -231,6 +255,8 @@
   exit(0);
 }
 
+static loggers ls = {NULL, log_error, log_debug};
+
 int main(int argc, char *argv[]) {
   // The skeleton for this function comes from Beej's sockets tutorial.
   int sockfd;  // listen on sock_fd
@@ -277,7 +303,7 @@
     }
   }
 
-  uw_request_init();
+  uw_request_init(NULL, log_error, log_debug);
 
   names = calloc(nthreads, sizeof(int));
 
@@ -316,7 +342,7 @@
     pthread_t thread;
     int name;
 
-    if (pthread_create(&thread, NULL, client_pruner, &name)) {
+    if (pthread_create(&thread, NULL, client_pruner, &ls)) {
       fprintf(stderr, "Error creating pruner thread\n");
       return 1;
     }