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)
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 $@
--- /dev/null
+#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);
+}
+
--- /dev/null
+#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 */
+++ /dev/null
-#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 */
#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);
}
#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 {
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;