1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2 * BSD-style license. Please see the COPYING file for details.
8 #include <yajl/yajl_gen.h>
12 #include "canned_responses.h"
16 yajl_gen_config yajl_c = { 0, 0 };
18 void respond_for_range(struct blerg *b, uint64_t from, uint64_t to) {
19 const unsigned char *ybuf;
23 uint64_t record_count = blerg_get_record_count(b);
25 printf("Content-type: application/json\r\n\r\n");
27 if (from > to || from >= record_count || to >= record_count || to - from > 99) {
28 respond_JSON_Failure();
32 g = yajl_gen_alloc(&yajl_c, NULL);
33 yajl_gen_array_open(g);
35 for (i = to; i != from - 1; i--) {
36 json_generate_one_record(g, NULL, b, i, 0);
37 yajl_gen_get_buf(g, &ybuf, &len);
38 fwrite(ybuf, len, 1, stdout);
42 yajl_gen_array_close(g);
43 yajl_gen_get_buf(g, &ybuf, &len);
44 fwrite(ybuf, len, 1, stdout);
48 void respond_taglist(struct tag *results, int i) {
49 const unsigned char *ybuf;
56 printf("Content-type: application/json\r\n\r\n");
57 g = yajl_gen_alloc(&yajl_c, NULL);
59 yajl_gen_array_open(g);
62 b = blerg_open(results[i].author);
64 json_generate_one_record(g, results[i].author, b, results[i].record, 0);
67 yajl_gen_get_buf(g, &ybuf, &len);
68 fwrite(ybuf, len, 1, stdout);
74 yajl_gen_array_close(g);
75 yajl_gen_get_buf(g, &ybuf, &len);
76 fwrite(ybuf, len, 1, stdout);
80 int main(int argc, char *argv[]) {
87 if (cgi_init() != CGIERR_NONE)
90 path = getenv("PATH_INFO");
95 request_method = getenv("REQUEST_METHOD");
96 if (request_method == NULL) {
97 fprintf(stderr, "Request method is null!?\n");
101 if (strncmp(path, "/get", 4) == 0 && strlen(path) > 4) {
102 if (strncmp(request_method, "GET", 4) != 0) {
107 if (path[4] != '/') {
112 ret = parse_url_info(path + 5, &info);
113 if ((ret & URL_INFO_NAME) == 0) {
118 if (!blerg_exists(info.name)) {
123 struct blerg *b = blerg_open(info.name);
125 if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
126 respond_for_range(b, info.record, info.record_to);
127 } else if (ret & URL_INFO_RECORD) {
128 ret = blerg_fetch(b, info.record, &data, &len);
134 respond_simple_data(data, len);
136 uint64_t record_count, from, to;
137 record_count = blerg_get_record_count(b);
138 if (record_count == 0) {
139 respond_simple_data("[]", 2);
141 to = record_count - 1;
142 from = (record_count > 50 ? to - 49 : 0);
143 respond_for_range(b, from, to);
148 } else if (strncmp(path, "/tag", 4) == 0 && strlen(path) > 4) {
149 if (strcmp(request_method, "GET") != 0) {
154 if (path[4] != '/') {
159 ret = parse_url_info(path + 5, &info);
160 if ((ret & URL_INFO_NAME) == 0) {
165 if (info.name[0] == 'H')
167 if (!tag_exists(info.name)) {
173 struct tag *taglist = tag_list(info.name, 0, &recs, -1);
176 respond_simple_data("[]", 2);
178 respond_taglist(taglist, recs);
180 } else if (strncmp(path, "/put", 4) == 0) {
181 if (strcmp(request_method, "POST") != 0) {
186 if (path[4] == '/') {
191 const char *username = cgi_getentrystr("username");
192 const char *data = cgi_getentrystr("data");
193 if (username == NULL || username[0] == 0 ||
194 data == NULL || data[0] == 0) {
195 respond_JSON_Failure();
199 const char *given_token = cgi_getcookie("auth");
200 if (!auth_check_token(username, given_token)) {
201 respond_JSON_Failure();
205 struct blerg *b = blerg_open(username);
207 respond_JSON_Failure();
210 ret = blerg_store(b, data, strlen(data));
213 respond_JSON_Failure();
217 respond_JSON_Success();
218 } else if (strncmp(path, "/info", 5) == 0) {
219 if (strcmp(request_method, "GET") != 0) {
224 if (path[5] != '/') {
229 ret = parse_url_info(path + 6, &info);
230 if ((ret & URL_INFO_NAME) == 0) {
235 if (!blerg_exists(info.name)) {
240 struct blerg *b = blerg_open(info.name);
241 uint64_t record_count = blerg_get_record_count(b);
245 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
246 yajl_gen_map_open(g);
247 yajl_gen_string(g, "record_count", 12);
248 snprintf(number, 21, "%llu", record_count);
249 yajl_gen_string(g, number, strlen(number));
250 yajl_gen_map_close(g);
252 const unsigned char *ybuf;
253 yajl_gen_get_buf(g, &ybuf, &len);
255 printf("Content-type: application/json\r\n");
256 printf("Content-length: %d\r\n\r\n", len);
257 fwrite(ybuf, len, 1, stdout);
260 } else if (strncmp(path, "/create", 8) == 0) {
261 if (strcmp(request_method, "POST") != 0) {
266 const char *username = cgi_getentrystr("username");
267 const char *password = cgi_getentrystr("password");
268 if (username == NULL || username[0] == 0 ||
269 password == NULL || password[0] == 0) {
270 respond_JSON_Failure();
274 if (blerg_exists(username)) {
275 respond_JSON_Failure();
279 struct blerg *b = blerg_open(username);
281 auth_set_password(username, password);
283 respond_JSON_Success();
284 } else if (strncmp(path, "/login", 7) == 0) {
285 if (strcmp(request_method, "POST") != 0) {
290 const char *username = cgi_getentrystr("username");
291 const char *password = cgi_getentrystr("password");
292 if (username == NULL || username[0] == 0 ||
293 password == NULL || password[0] == 0) {
294 respond_JSON_Failure();
298 if (!auth_login(username, password)) {
299 respond_JSON_Failure();
303 char *token = auth_get_token(username);
304 printf("Set-Cookie: auth=%s\r\n", token);
307 respond_JSON_Success();
308 } else if (strncmp(path, "/logout", 8) == 0) {
309 if (strcmp(request_method, "POST") != 0) {
314 const char *username = cgi_getentrystr("username");
315 if (username == NULL || username[0] == 0) {
316 respond_JSON_Failure();
321 const char *given_token = cgi_getcookie("auth");
322 if (auth_check_token(username, given_token)) {
323 auth_logout(username);
324 respond_JSON_Success();
326 respond_JSON_Failure();