5 #include <yajl/yajl_gen.h>
9 #include "canned_responses.h"
13 yajl_gen_config yajl_c = { 0, 0 };
15 void respond_for_range(struct blerg *b, uint64_t from, uint64_t to) {
16 const unsigned char *ybuf;
20 uint64_t record_count = blerg_get_record_count(b);
22 printf("Content-type: application/json\r\n\r\n");
24 if (from > to || from >= record_count || to >= record_count || to - from > 99) {
25 respond_JSON_Failure();
29 g = yajl_gen_alloc(&yajl_c, NULL);
30 yajl_gen_array_open(g);
32 for (i = to; i != from - 1; i--) {
33 json_generate_one_record(g, NULL, b, i);
34 yajl_gen_get_buf(g, &ybuf, &len);
35 fwrite(ybuf, len, 1, stdout);
39 yajl_gen_array_close(g);
40 yajl_gen_get_buf(g, &ybuf, &len);
41 fwrite(ybuf, len, 1, stdout);
45 void respond_taglist(struct tag *results, int i) {
46 const unsigned char *ybuf;
53 printf("Content-type: application/json\r\n\r\n");
54 g = yajl_gen_alloc(&yajl_c, NULL);
56 yajl_gen_array_open(g);
59 b = blerg_open(results[i].author);
61 json_generate_one_record(g, results[i].author, b, results[i].record);
64 yajl_gen_get_buf(g, &ybuf, &len);
65 fwrite(ybuf, len, 1, stdout);
71 yajl_gen_array_close(g);
72 yajl_gen_get_buf(g, &ybuf, &len);
73 fwrite(ybuf, len, 1, stdout);
77 int main(int argc, char *argv[]) {
84 if (cgi_init() != CGIERR_NONE)
87 path = getenv("PATH_INFO");
92 request_method = getenv("REQUEST_METHOD");
93 if (request_method == NULL) {
94 fprintf(stderr, "Request method is null!?\n");
98 if (strncmp(path, "/get", 4) == 0 && strlen(path) > 4) {
99 if (strncmp(request_method, "GET", 4) != 0) {
104 if (path[4] != '/') {
109 ret = parse_url_info(path + 5, &info);
110 if ((ret & URL_INFO_AUTHOR) == 0) {
115 if (!blerg_exists(info.author)) {
120 struct blerg *b = blerg_open(info.author);
122 if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
123 respond_for_range(b, info.record, info.record_to);
124 } else if (ret & URL_INFO_RECORD) {
125 ret = blerg_fetch(b, info.record, &data, &len);
131 respond_simple_data(data, len);
133 uint64_t record_count, from, to;
134 record_count = blerg_get_record_count(b);
135 if (record_count == 0) {
136 respond_simple_data("[]", 2);
138 to = record_count - 1;
139 from = (record_count > 50 ? to - 49 : 0);
140 respond_for_range(b, from, to);
145 } else if (strncmp(path, "/tag", 4) == 0 && strlen(path) > 4) {
146 if (strcmp(request_method, "GET") != 0) {
151 if (path[4] != '/') {
156 ret = parse_url_info(path + 5, &info);
157 if ((ret & URL_INFO_AUTHOR) == 0) {
162 if (!tag_exists(info.author)) {
168 struct tag *taglist = tag_list(info.author, 0, &recs, -1);
171 respond_simple_data("[]", 2);
173 respond_taglist(taglist, recs);
175 } else if (strncmp(path, "/put", 4) == 0) {
176 if (strcmp(request_method, "POST") != 0) {
181 if (path[4] == '/') {
186 const char *username = cgi_getentrystr("username");
187 const char *data = cgi_getentrystr("data");
188 if (username == NULL || username[0] == 0 ||
189 data == NULL || data[0] == 0) {
190 respond_JSON_Failure();
194 const char *given_token = cgi_getcookie("auth");
195 if (!auth_check_token(username, given_token)) {
196 respond_JSON_Failure();
200 struct blerg *b = blerg_open(username);
202 respond_JSON_Failure();
205 ret = blerg_store(b, data, strlen(data));
208 respond_JSON_Failure();
212 respond_JSON_Success();
213 } else if (strncmp(path, "/info", 5) == 0) {
214 if (strcmp(request_method, "GET") != 0) {
219 if (path[5] != '/') {
224 ret = parse_url_info(path + 6, &info);
225 if ((ret & URL_INFO_AUTHOR) == 0) {
230 if (!blerg_exists(info.author)) {
235 struct blerg *b = blerg_open(info.author);
236 uint64_t record_count = blerg_get_record_count(b);
240 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
241 yajl_gen_map_open(g);
242 yajl_gen_string(g, "record_count", 12);
243 snprintf(number, 21, "%llu", record_count);
244 yajl_gen_string(g, number, strlen(number));
245 yajl_gen_map_close(g);
247 const unsigned char *ybuf;
248 yajl_gen_get_buf(g, &ybuf, &len);
250 printf("Content-type: application/json\r\n");
251 printf("Content-length: %d\r\n\r\n", len);
252 fwrite(ybuf, len, 1, stdout);
255 } else if (strncmp(path, "/create", 8) == 0) {
256 if (strcmp(request_method, "POST") != 0) {
261 const char *username = cgi_getentrystr("username");
262 const char *password = cgi_getentrystr("password");
263 if (username == NULL || username[0] == 0 ||
264 password == NULL || password[0] == 0) {
265 respond_JSON_Failure();
269 if (blerg_exists(username)) {
270 respond_JSON_Failure();
274 struct blerg *b = blerg_open(username);
276 auth_set_password(username, password);
278 respond_JSON_Success();
279 } else if (strncmp(path, "/login", 7) == 0) {
280 if (strcmp(request_method, "POST") != 0) {
285 const char *username = cgi_getentrystr("username");
286 const char *password = cgi_getentrystr("password");
287 if (username == NULL || username[0] == 0 ||
288 password == NULL || password[0] == 0) {
289 respond_JSON_Failure();
293 if (!auth_login(username, password)) {
294 respond_JSON_Failure();
298 char *token = auth_get_token(username);
299 printf("Set-Cookie: auth=%s\r\n", token);
302 respond_JSON_Success();
303 } else if (strncmp(path, "/logout", 8) == 0) {
304 if (strcmp(request_method, "POST") != 0) {
309 const char *username = cgi_getentrystr("username");
310 if (username == NULL || username[0] == 0) {
311 respond_JSON_Failure();
316 const char *given_token = cgi_getcookie("auth");
317 if (auth_check_token(username, given_token)) {
318 auth_logout(username);
319 respond_JSON_Success();
321 respond_JSON_Failure();