Refactor a bunch of stuff for cgi fork
authorChip Black <bytex64@bytex64.net>
Tue, 28 Dec 2010 09:20:29 +0000 (03:20 -0600)
committerChip Black <bytex64@bytex64.net>
Tue, 28 Dec 2010 09:20:29 +0000 (03:20 -0600)
Makefile
common/app.c [new file with mode: 0644]
common/app.h [new file with mode: 0644]
common/auth.c [moved from misc/auth.c with 100% similarity]
common/auth.h [moved from misc/auth.h with 100% similarity]
http.h [deleted file]
http/canned_responses.c
http/http_blerg.c

index e804abe..5113e70 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,12 +1,14 @@
 CFLAGS ?= -g
-INCLUDES = -I. -Idatabase -Imisc -Ilibmicrohttpd-0.9.3/src/include -Iyajl/build/yajl-1.0.11/include
+INCLUDES = -I. -Ifcgi -Idatabase -Icommon -Icgi-util-2.2.1 -Ilibmicrohttpd-0.9.3/src/include -Iyajl/build/yajl-1.0.11/include
 LDFLAGS ?=
-LIBDIRS = -Llibmicrohttpd-0.9.3/src/daemon/.libs -Lyajl/build/yajl-1.0.11/lib
+HTTP_LIBDIRS = -Llibmicrohttpd-0.9.3/src/daemon/.libs -Lyajl/build/yajl-1.0.11/lib
+CGI_LIBDIRS = -Lcgi-util-2.2.1 -Lyajl/build/yajl-1.0.11/lib
 
-targets = blerg.a blergtool http_blerg
+targets = blerg.a blergtool http_blerg cgi_blerg
 blerg_a_objects = database/database.o database/tags.o
 blergtool_objects = tools/blergtool.o blerg.a
-http_blerg_objects = http/http_blerg.o http/canned_responses.o misc/auth.o blerg.a
+http_blerg_objects = http/http_blerg.o http/canned_responses.o common/app.o common/auth.o blerg.a
+cgi_blerg_objects = cgi/cgi_blerg.o common/app.o common/auth.o blerg.a
 
 all: $(targets)
 
@@ -20,7 +22,10 @@ blergtool: $(blergtool_objects)
        gcc $(LDFLAGS) $^ -o $@
 
 http_blerg: $(http_blerg_objects)
-       gcc $(LIBDIRS) $(LDFLAGS) $(http_blerg_objects) -lpthread -lmicrohttpd -lyajl_s -o $@
+       gcc $(HTTP_LIBDIRS) $(LDFLAGS) $(http_blerg_objects) -lpthread -lmicrohttpd -lyajl_s -o $@
+
+cgi_blerg: $(cgi_blerg_objects)
+       gcc $(CGI_LIBDIRS) $(LDFLAGS) $(cgi_blerg_objects) -lcgi-util -lyajl_s -o $@
 
 %.o: %.c
        gcc $(INCLUDES) $(CFLAGS) -c $< -o $@
diff --git a/common/app.c b/common/app.c
new file mode 100644 (file)
index 0000000..9bc4e40
--- /dev/null
@@ -0,0 +1,78 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <yajl/yajl_gen.h>
+#include "app.h"
+
+int parse_url_info(const char *url, struct url_info *info) {
+       const char *c;
+       int len;
+       info->contents = 0;
+
+       c = strchr(url, '/');
+       if (c == NULL) {
+               len = strlen(url);
+       } else {
+               len = c - url;
+       }
+       if (len == 0)
+               return 0;
+       memcpy(info->author, url, len);
+       info->author[len] = 0;
+       info->contents |= URL_INFO_AUTHOR;
+
+       if (c == NULL || c[1] == 0)
+               return info->contents;
+
+       info->record = strtoull(c + 1, NULL, 10);
+       info->contents |= URL_INFO_RECORD;
+
+       c = strchr(c, '-');
+       if (c == NULL || c[1] == 0)
+               return info->contents;
+
+       info->record_to = strtoull(c + 1, NULL, 10);
+       info->contents |= URL_INFO_RECORD_TO;
+
+       return info->contents;
+}
+
+uint64_t *make_sequential_list(uint64_t from, uint64_t to) {
+       uint64_t len = to - from + 1;
+       uint64_t *list = malloc(len * sizeof(uint64_t));
+       uint64_t i;
+
+       for (i = 0; i < len; i++) {
+               list[i] = from + i;
+       }
+
+       return list;
+}
+
+void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record) {
+       char *data;
+       char number[21];
+       int len;
+
+       if (!blerg_fetch(b, record, &data, &len)) {
+               fprintf(stderr, "Could not fetch record\n");
+               return;
+       }
+
+       yajl_gen_map_open(g);
+       if (author != NULL) {
+               yajl_gen_string(g, "author", 6);
+               yajl_gen_string(g, author, strlen(author));
+       }
+       yajl_gen_string(g, "record", 6);
+       snprintf(number, 21, "%llu", record);
+       yajl_gen_string(g, number, strlen(number));
+       yajl_gen_string(g, "timestamp", 9);
+       yajl_gen_integer(g, blerg_get_timestamp(b, record));
+       yajl_gen_string(g, "data", 4);
+       yajl_gen_string(g, data, len);
+       yajl_gen_map_close(g);
+
+       free(data);
+}
+
diff --git a/common/app.h b/common/app.h
new file mode 100644 (file)
index 0000000..98b8521
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef _APP_H
+#define _APP_H
+
+#include <yajl/yajl_gen.h>
+#include "database.h"
+
+#define REALM "Blerg"
+
+#define CONTENT_401 "<html><head><title>401 Unauthorized</title></head><body><h1>401 Unauthorized</h1>DENIED</body></html>"
+#define CONTENT_404 "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
+#define CONTENT_405 "<html><head><title>405 Method Not Allowed</title></head><body><h1>405 Method Not Allowed</h1>I'm sorry, Dave. I'm afraid I can't do that.</body></html>"
+#define JSON_SUCCESS "{status: \"success\"}"
+#define JSON_FAILURE "{status: \"failure\"}"
+
+#define URL_INFO_AUTHOR    0x1
+#define URL_INFO_RECORD    0x2
+#define URL_INFO_RECORD_TO 0x4
+#define DERP "DERP DERP DERP"
+
+struct url_info {
+       unsigned int contents;
+       char author[33];
+       uint64_t record;
+       uint64_t record_to;
+};
+
+int parse_url_info(const char *url, struct url_info *info);
+uint64_t *make_sequential_list(uint64_t from, uint64_t to);
+void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record);
+
+#endif /* _APP_H */
similarity index 100%
rename from misc/auth.c
rename to common/auth.c
similarity index 100%
rename from misc/auth.h
rename to common/auth.h
diff --git a/http.h b/http.h
deleted file mode 100644 (file)
index 959fdb6..0000000
--- a/http.h
+++ /dev/null
@@ -1,13 +0,0 @@
-#ifndef _HTTP_H
-#define _HTTP_H
-
-#define REALM "Blerg"
-
-struct url_info {
-       unsigned int contents;
-       char author[33];
-       uint64_t record;
-       uint64_t record_to;
-};
-
-#endif /* _HTTP_H */
index c61e2a7..5461b46 100644 (file)
@@ -1,22 +1,13 @@
 #include <string.h>
 #include <microhttpd.h>
-#include "http.h"
+#include "app.h"
 #include "canned_responses.h"
 
 void init_responses() {
-#define CONTENT_401 "<html><head><title>401 Unauthorized</title></head><body><h1>401 Unauthorized</h1>DENIED</body></html>"
        response_401 = MHD_create_response_from_data(strlen (CONTENT_401), CONTENT_401, MHD_NO, MHD_NO);  
-
-#define CONTENT_404 "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
        response_404 = MHD_create_response_from_data(strlen(CONTENT_404), CONTENT_404, MHD_NO, MHD_NO);
-
-#define CONTENT_405 "<html><head><title>405 Method Not Allowed</title></head><body><h1>405 Method Not Allowed</h1>I'm sorry, Dave. I'm afraid I can't do that.</body></html>"
        response_405 = MHD_create_response_from_data(strlen(CONTENT_405), CONTENT_405, MHD_NO, MHD_NO);
-
-#define JSON_SUCCESS "{status: \"success\"}"
        response_JSON_Success = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
-
-#define JSON_FAILURE "{status: \"failure\"}"
        response_JSON_Failure = MHD_create_response_from_data(strlen(JSON_FAILURE), JSON_FAILURE, MHD_NO, MHD_NO);
 }
 
index 73f88fe..b790183 100644 (file)
@@ -7,14 +7,9 @@
 #include "tags.h"
 #include "auth.h"
 #include "canned_responses.h"
-#include "http.h"
+#include "app.h"
 #include "config.h"
 
-#define URL_INFO_AUTHOR    0x1
-#define URL_INFO_RECORD    0x2
-#define URL_INFO_RECORD_TO 0x4
-#define DERP "DERP DERP DERP"
-
 yajl_gen_config yajl_c = { 0, 0 };
 
 struct create_state {
@@ -46,78 +41,6 @@ struct tag_state {
        int done;
 };
 
-int parse_url_info(const char *url, struct url_info *info) {
-       const char *c;
-       int len;
-       info->contents = 0;
-
-       c = strchr(url, '/');
-       if (c == NULL) {
-               len = strlen(url);
-       } else {
-               len = c - url;
-       }
-       if (len == 0)
-               return 0;
-       memcpy(info->author, url, len);
-       info->author[len] = 0;
-       info->contents |= URL_INFO_AUTHOR;
-
-       if (c == NULL || c[1] == 0)
-               return info->contents;
-
-       info->record = strtoull(c + 1, NULL, 10);
-       info->contents |= URL_INFO_RECORD;
-
-       c = strchr(c, '-');
-       if (c == NULL || c[1] == 0)
-               return info->contents;
-
-       info->record_to = strtoull(c + 1, NULL, 10);
-       info->contents |= URL_INFO_RECORD_TO;
-
-       return info->contents;
-}
-
-uint64_t *make_sequential_list(uint64_t from, uint64_t to) {
-       uint64_t len = to - from + 1;
-       uint64_t *list = malloc(len * sizeof(uint64_t));
-       uint64_t i;
-
-       for (i = 0; i < len; i++) {
-               list[i] = from + i;
-       }
-
-       return list;
-}
-
-void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record) {
-       char *data;
-       char number[21];
-       int len;
-
-       if (!blerg_fetch(b, record, &data, &len)) {
-               fprintf(stderr, "Could not fetch record\n");
-               return;
-       }
-
-       yajl_gen_map_open(g);
-       if (author != NULL) {
-               yajl_gen_string(g, "author", 6);
-               yajl_gen_string(g, author, strlen(author));
-       }
-       yajl_gen_string(g, "record", 6);
-       snprintf(number, 21, "%llu", record);
-       yajl_gen_string(g, number, strlen(number));
-       yajl_gen_string(g, "timestamp", 9);
-       yajl_gen_integer(g, blerg_get_timestamp(b, record));
-       yajl_gen_string(g, "data", 4);
-       yajl_gen_string(g, data, len);
-       yajl_gen_map_close(g);
-
-       free(data);
-}
-
 ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
        struct get_state *gs = cls;
        const unsigned char *ybuf;