From: Chip Black Date: Tue, 28 Dec 2010 09:20:29 +0000 (-0600) Subject: Refactor a bunch of stuff for cgi fork X-Git-Tag: ohfuckreddit~34 X-Git-Url: http://git.bytex64.net/?a=commitdiff_plain;h=3d27274b36b44816c736ea8a411341b5a36556f9;p=blerg.git Refactor a bunch of stuff for cgi fork --- diff --git a/Makefile b/Makefile index e804abe..5113e70 100644 --- 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 index 0000000..9bc4e40 --- /dev/null +++ b/common/app.c @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#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 index 0000000..98b8521 --- /dev/null +++ b/common/app.h @@ -0,0 +1,31 @@ +#ifndef _APP_H +#define _APP_H + +#include +#include "database.h" + +#define REALM "Blerg" + +#define CONTENT_401 "401 Unauthorized

401 Unauthorized

DENIED" +#define CONTENT_404 "404 Not Found

404 Not Found

I couldn't find that." +#define CONTENT_405 "405 Method Not Allowed

405 Method Not Allowed

I'm sorry, Dave. I'm afraid I can't do that." +#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 */ diff --git a/misc/auth.c b/common/auth.c similarity index 100% rename from misc/auth.c rename to common/auth.c diff --git a/misc/auth.h b/common/auth.h 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 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 */ diff --git a/http/canned_responses.c b/http/canned_responses.c index c61e2a7..5461b46 100644 --- a/http/canned_responses.c +++ b/http/canned_responses.c @@ -1,22 +1,13 @@ #include #include -#include "http.h" +#include "app.h" #include "canned_responses.h" void init_responses() { -#define CONTENT_401 "401 Unauthorized

401 Unauthorized

DENIED" response_401 = MHD_create_response_from_data(strlen (CONTENT_401), CONTENT_401, MHD_NO, MHD_NO); - -#define CONTENT_404 "404 Not Found

404 Not Found

I couldn't find that." response_404 = MHD_create_response_from_data(strlen(CONTENT_404), CONTENT_404, MHD_NO, MHD_NO); - -#define CONTENT_405 "405 Method Not Allowed

405 Method Not Allowed

I'm sorry, Dave. I'm afraid I can't do that." 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); } diff --git a/http/http_blerg.c b/http/http_blerg.c index 73f88fe..b790183 100644 --- a/http/http_blerg.c +++ b/http/http_blerg.c @@ -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;