-CFLAGS = -g
+libmicrohttpd_a = libmicrohttpd-0.9.3/src/daemon/.libs/libmicrohttpd.a
+
+CFLAGS = -g -Ilibmicrohttpd-0.9.3/src/include
LIBS =
-targets = blerg.a blergtool tag_test
+targets = blerg.a blergtool http_blerg
blerg_a_objects = database.o tags.o
blergtool_objects = blergtool.o blerg.a
+http_blerg_objects = http_blerg.o blerg.a
all: $(targets)
clean:
- rm -f $(targets) $(blerg_a_objects) $(blergtool_objects)
+ rm -f $(targets) $(blerg_a_objects) $(blergtool_objects) $(http_blerg_objects)
+ -cd libmicrohttpd-0.9.3 && make distclean
blerg.a: $(blerg_a_objects)
ar rcu $@ $(blerg_a_objects)
+$(libmicrohttpd_a):
+ cd libmicrohttpd-0.9.3 && ./configure --disable-https --disable-shared && make
+
blergtool: $(blergtool_objects)
gcc $^ -o $@
+http_blerg: $(http_blerg_objects) $(libmicrohttpd_a)
+ gcc $(http_blerg_objects) -lpthread $(libmicrohttpd_a) -o $@
+
%.o: %.c
gcc $(CFLAGS) $(LIBS) -c $< -o $@
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <microhttpd.h>
+#include "database.h"
+#include "tags.h"
+
+#define DERP "DERP DERP DERP"
+#define NOTFOUND "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that record.</body></html>"
+
+static int
+ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
+ const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
+ struct MHD_Response *response;
+ int ret, len;
+ const char *c;
+ char *data;
+
+ if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
+ char author[33];
+ uint64_t record;
+
+ if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
+ return MHD_NO;
+ c = strchr(url + 5, '/');
+ if (c == NULL) {
+ len = strlen(url) - 5;
+ } else {
+ len = c - (url + 5);
+ }
+ memcpy(author, url + 5, len);
+ author[len] = 0;
+ printf("author: %s\n", author);
+
+ c = url + (5 + len);
+ if (c[0] == '/' && c[1] != 0) {
+ record = strtoull(c + 1, NULL, 10);
+ printf("record: %s %l\n", c + 1, record);
+ if (*ptr == NULL) {
+ *ptr = (void *) 1;
+ return MHD_YES;
+ } else {
+ *ptr == NULL;
+
+ struct blerg *b = blerg_open(author);
+ ret = blerg_fetch(b, record, &data, &len);
+ blerg_close(b);
+
+ if (ret == 0) {
+ response = MHD_create_response_from_data(strlen(NOTFOUND), NOTFOUND, MHD_NO, MHD_NO);
+ ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
+ } else {
+ response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
+ ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
+ }
+ MHD_destroy_response(response);
+ return ret;
+ }
+ } else {
+ if (*ptr == NULL) {
+ *ptr = (void*) 1;
+ return MHD_YES;
+ } else {
+ *ptr == NULL;
+ response = MHD_create_response_from_data(strlen(DERP), DERP, MHD_NO, MHD_NO);
+ ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
+ MHD_destroy_response(response);
+ return ret;
+ }
+ }
+ } else {
+ return MHD_NO;
+ }
+}
+
+
+int main(int argc, char *argv[]) {
+ struct MHD_Daemon *daemon;
+ fd_set rs, ws, es;
+ int max;
+
+ daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
+ if (daemon == NULL) {
+ fprintf(stderr, "Could not start web server\n");
+ return 1;
+ }
+
+ while (1) {
+ FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es);
+ if (MHD_get_fdset(daemon, &rs, &ws, &es, &max) != MHD_YES) {
+ fprintf(stderr, "Fatal error getting fd sets\n");
+ break;
+ }
+ select(max + 1, &rs, &ws, &es, NULL);
+ MHD_run(daemon);
+ }
+ MHD_stop_daemon(daemon);
+ return 0;
+}