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 } else if (strncmp(path, "/info", 5) == 0) {
201 if (strcmp(request_method, "GET") != 0) {
206 if (path[5] != '/') {
211 ret = parse_url_info(path + 6, &info);
212 if ((ret & URL_INFO_AUTHOR) == 0) {
217 if (!blerg_exists(info.author)) {
222 struct blerg *b = blerg_open(info.author);
223 uint64_t record_count = blerg_get_record_count(b);
227 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
228 yajl_gen_map_open(g);
229 yajl_gen_string(g, "record_count", 12);
230 snprintf(number, 21, "%llu", record_count);
231 yajl_gen_string(g, number, strlen(number));
232 yajl_gen_map_close(g);
234 const unsigned char *ybuf;
235 yajl_gen_get_buf(g, &ybuf, &len);
237 printf("Content-type: application/json\r\n");
238 printf("Content-length: %d\r\n\r\n", len);
242 } else if (strncmp(path, "/create", 8) == 0) {
243 if (strcmp(request_method, "POST") != 0) {
248 const char *username = cgi_getentrystr("username");
249 const char *password = cgi_getentrystr("password");
250 if (username == NULL || username[0] == 0 ||
251 password == NULL || password[0] == 0) {
252 respond_JSON_Failure();
256 if (blerg_exists(username)) {
257 respond_JSON_Failure();
261 struct blerg *b = blerg_open(username);
263 auth_set_password(username, password);
265 respond_JSON_Success();