5 #include <yajl/yajl_gen.h>
12 yajl_gen_config yajl_c = { 0, 0 };
14 void respond_simple_data(unsigned const char *data, int len) {
15 printf("Content-length: %d\r\n\r\n", len);
20 printf("Status: 404 Not Found\r\n");
21 printf("Content-type: text/html\r\n");
22 printf("Content-length: %d\r\n\r\n", strlen(CONTENT_404));
28 printf("Status: 405 Method Not Allowed\r\n");
29 printf("Content-type: text/html\r\n");
30 printf("Content-length: %d\r\n\r\n", strlen(CONTENT_405));
35 void respond_JSON_Failure() {
36 respond_simple_data(JSON_FAILURE, strlen(JSON_FAILURE));
39 void respond_JSON_Success() {
40 respond_simple_data(JSON_SUCCESS, strlen(JSON_SUCCESS));
43 void respond_for_range(struct blerg *b, uint64_t from, uint64_t to) {
44 const unsigned char *ybuf;
48 uint64_t record_count = blerg_get_record_count(b);
50 printf("Content-type: application/json\r\n\r\n");
52 if (from > to || from >= record_count || to >= record_count || to - from > 99) {
53 respond_JSON_Failure();
57 g = yajl_gen_alloc(&yajl_c, NULL);
58 yajl_gen_array_open(g);
60 for (i = to; i != from - 1; i--) {
61 json_generate_one_record(g, NULL, b, i);
62 yajl_gen_get_buf(g, &ybuf, &len);
67 yajl_gen_array_close(g);
68 yajl_gen_get_buf(g, &ybuf, &len);
73 void respond_taglist(struct tag *results, int i) {
74 const unsigned char *ybuf;
81 printf("Content-type: application/json\r\n\r\n");
82 g = yajl_gen_alloc(&yajl_c, NULL);
84 yajl_gen_array_open(g);
87 b = blerg_open(results[i].author);
89 json_generate_one_record(g, results[i].author, b, results[i].record);
92 yajl_gen_get_buf(g, &ybuf, &len);
99 yajl_gen_array_close(g);
100 yajl_gen_get_buf(g, &ybuf, &len);
105 int main(int argc, char *argv[]) {
107 char *request_method;
109 struct url_info info;
112 if (cgi_init() != CGIERR_NONE)
115 path = getenv("PATH_INFO");
120 request_method = getenv("REQUEST_METHOD");
122 if (strncmp(path, "/get", 4) == 0 && strlen(path) > 4) {
123 if (strncmp(request_method, "GET", 4) != 0) {
128 if (path[4] != '/') {
133 ret = parse_url_info(path + 5, &info);
134 if ((ret & URL_INFO_AUTHOR) == 0) {
139 if (!blerg_exists(info.author)) {
144 struct blerg *b = blerg_open(info.author);
146 if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
147 respond_for_range(b, info.record, info.record_to);
148 } else if (ret & URL_INFO_RECORD) {
149 ret = blerg_fetch(b, info.record, &data, &len);
155 respond_simple_data(data, len);
157 uint64_t record_count, from, to;
158 record_count = blerg_get_record_count(b);
159 if (record_count == 0) {
160 respond_simple_data("[]", 2);
162 to = record_count - 1;
163 from = (record_count > 50 ? to - 49 : 0);
164 respond_for_range(b, from, to);
169 } else if (strncmp(path, "/tag", 4) == 0 && strlen(path) > 4) {
170 if (strcmp(request_method, "GET") != 0) {
175 if (path[4] != '/') {
180 ret = parse_url_info(path + 5, &info);
181 if ((ret & URL_INFO_AUTHOR) == 0) {
186 if (!tag_exists(info.author)) {
192 struct tag *taglist = tag_list(info.author, 0, &recs, -1);
195 respond_simple_data("[]", 2);
197 respond_taglist(taglist, recs);
199 } else if (strncmp(path, "/put", 4) == 0) {
200 if (strcmp(request_method, "POST") != 0) {
205 if (path[4] == '/') {
210 const char *username = cgi_getentrystr("username");
211 const char *data = cgi_getentrystr("data");
212 if (username == NULL || username[0] == 0 ||
213 data == NULL || data[0] == 0) {
214 respond_JSON_Failure();
218 const char *given_token = cgi_getcookie("auth");
219 if (!auth_check_token(username, given_token)) {
220 respond_JSON_Failure();
224 struct blerg *b = blerg_open(username);
226 respond_JSON_Failure();
229 ret = blerg_store(b, data, strlen(data));
232 respond_JSON_Failure();
236 respond_JSON_Success();
237 } else if (strncmp(path, "/info", 5) == 0) {
238 if (strcmp(request_method, "GET") != 0) {
243 if (path[5] != '/') {
248 ret = parse_url_info(path + 6, &info);
249 if ((ret & URL_INFO_AUTHOR) == 0) {
254 if (!blerg_exists(info.author)) {
259 struct blerg *b = blerg_open(info.author);
260 uint64_t record_count = blerg_get_record_count(b);
264 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
265 yajl_gen_map_open(g);
266 yajl_gen_string(g, "record_count", 12);
267 snprintf(number, 21, "%llu", record_count);
268 yajl_gen_string(g, number, strlen(number));
269 yajl_gen_map_close(g);
271 const unsigned char *ybuf;
272 yajl_gen_get_buf(g, &ybuf, &len);
274 printf("Content-type: application/json\r\n");
275 printf("Content-length: %d\r\n\r\n", len);
279 } else if (strncmp(path, "/create", 8) == 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 (blerg_exists(username)) {
294 respond_JSON_Failure();
298 struct blerg *b = blerg_open(username);
300 auth_set_password(username, password);
302 respond_JSON_Success();
303 } else if (strncmp(path, "/login", 7) == 0) {
304 if (strcmp(request_method, "POST") != 0) {
309 const char *username = cgi_getentrystr("username");
310 const char *password = cgi_getentrystr("password");
311 if (username == NULL || username[0] == 0 ||
312 password == NULL || password[0] == 0) {
313 respond_JSON_Failure();
317 if (!auth_login(username, password)) {
318 respond_JSON_Failure();
322 char *token = auth_get_token(username);
323 printf("Set-Cookie: auth=%s\r\n", token);
326 respond_JSON_Success();
327 } else if (strncmp(path, "/logout", 8) == 0) {
328 if (strcmp(request_method, "POST") != 0) {
333 const char *username = cgi_getentrystr("username");
334 if (username == NULL || username[0] == 0) {
335 respond_JSON_Failure();
340 const char *given_token = cgi_getcookie("auth");
341 if (auth_check_token(username, given_token)) {
342 auth_logout(username);
343 respond_JSON_Success();
345 respond_JSON_Failure();