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 (info.author[0] == 'H')
163 info.author[0] = '#';
164 if (!tag_exists(info.author)) {
170 struct tag *taglist = tag_list(info.author, 0, &recs, -1);
173 respond_simple_data("[]", 2);
175 respond_taglist(taglist, recs);
177 } else if (strncmp(path, "/put", 4) == 0) {
178 if (strcmp(request_method, "POST") != 0) {
183 if (path[4] == '/') {
188 const char *username = cgi_getentrystr("username");
189 const char *data = cgi_getentrystr("data");
190 if (username == NULL || username[0] == 0 ||
191 data == NULL || data[0] == 0) {
192 respond_JSON_Failure();
196 const char *given_token = cgi_getcookie("auth");
197 if (!auth_check_token(username, given_token)) {
198 respond_JSON_Failure();
202 struct blerg *b = blerg_open(username);
204 respond_JSON_Failure();
207 ret = blerg_store(b, data, strlen(data));
210 respond_JSON_Failure();
214 respond_JSON_Success();
215 } else if (strncmp(path, "/info", 5) == 0) {
216 if (strcmp(request_method, "GET") != 0) {
221 if (path[5] != '/') {
226 ret = parse_url_info(path + 6, &info);
227 if ((ret & URL_INFO_AUTHOR) == 0) {
232 if (!blerg_exists(info.author)) {
237 struct blerg *b = blerg_open(info.author);
238 uint64_t record_count = blerg_get_record_count(b);
242 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
243 yajl_gen_map_open(g);
244 yajl_gen_string(g, "record_count", 12);
245 snprintf(number, 21, "%llu", record_count);
246 yajl_gen_string(g, number, strlen(number));
247 yajl_gen_map_close(g);
249 const unsigned char *ybuf;
250 yajl_gen_get_buf(g, &ybuf, &len);
252 printf("Content-type: application/json\r\n");
253 printf("Content-length: %d\r\n\r\n", len);
254 fwrite(ybuf, len, 1, stdout);
257 } else if (strncmp(path, "/create", 8) == 0) {
258 if (strcmp(request_method, "POST") != 0) {
263 const char *username = cgi_getentrystr("username");
264 const char *password = cgi_getentrystr("password");
265 if (username == NULL || username[0] == 0 ||
266 password == NULL || password[0] == 0) {
267 respond_JSON_Failure();
271 if (blerg_exists(username)) {
272 respond_JSON_Failure();
276 struct blerg *b = blerg_open(username);
278 auth_set_password(username, password);
280 respond_JSON_Success();
281 } else if (strncmp(path, "/login", 7) == 0) {
282 if (strcmp(request_method, "POST") != 0) {
287 const char *username = cgi_getentrystr("username");
288 const char *password = cgi_getentrystr("password");
289 if (username == NULL || username[0] == 0 ||
290 password == NULL || password[0] == 0) {
291 respond_JSON_Failure();
295 if (!auth_login(username, password)) {
296 respond_JSON_Failure();
300 char *token = auth_get_token(username);
301 printf("Set-Cookie: auth=%s\r\n", token);
304 respond_JSON_Success();
305 } else if (strncmp(path, "/logout", 8) == 0) {
306 if (strcmp(request_method, "POST") != 0) {
311 const char *username = cgi_getentrystr("username");
312 if (username == NULL || username[0] == 0) {
313 respond_JSON_Failure();
318 const char *given_token = cgi_getcookie("auth");
319 if (auth_check_token(username, given_token)) {
320 auth_logout(username);
321 respond_JSON_Success();
323 respond_JSON_Failure();