From 453451e03c0e8295031656671eb92fa8b8b0779d Mon Sep 17 00:00:00 2001 From: Chip Black Date: Sun, 19 Dec 2010 04:10:57 -0600 Subject: [PATCH] Add partially-working web server --- Makefile | 16 +++++++-- http_blerg.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 http_blerg.c diff --git a/Makefile b/Makefile index a116407..709b6b9 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,30 @@ -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 $@ diff --git a/http_blerg.c b/http_blerg.c new file mode 100644 index 0000000..86a2690 --- /dev/null +++ b/http_blerg.c @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include "database.h" +#include "tags.h" + +#define DERP "DERP DERP DERP" +#define NOTFOUND "404 Not Found

404 Not Found

I couldn't find that record." + +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; +} -- 2.25.1