src/data_provider.[ch]: Implement "data_provider_get_ident_data".
[collection4.git] / src / action_graph_def_json.c
1 /**
2  * collection4 - action_graph_def_json.c
3  * Copyright (C) 2010  Florian octo Forster
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  * 
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors:
21  *   Florian octo Forster <ff at octo.it>
22  **/
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <stdint.h>
29 #include <inttypes.h>
30 #include <assert.h>
31
32 #include "action_graph_def_json.h"
33 #include "common.h"
34 #include "graph.h"
35 #include "graph_list.h"
36 #include "utils_cgi.h"
37
38 #include <fcgiapp.h>
39 #include <fcgi_stdio.h>
40
41 /* Expire data after one day. */
42 #define EXPIRES_SECS 86400
43
44 static void write_callback (__attribute__((unused)) void *ctx, /* {{{ */
45     const char *str, unsigned int len)
46 {
47   fwrite ((void *) str, /* size = */ len, /* nmemb = */ 1, stdout);
48 } /* }}} void write_callback */
49
50 int action_graph_def_json (void) /* {{{ */
51 {
52   graph_config_t *cfg;
53
54   yajl_gen_config handler_config;
55   yajl_gen handler;
56
57   time_t now;
58   char time_buffer[128];
59   int status;
60
61   cfg = gl_graph_get_selected ();
62   if (cfg == NULL)
63     return (ENOMEM);
64
65   memset (&handler_config, 0, sizeof (handler_config));
66   handler_config.beautify = 1;
67   handler_config.indentString = "  ";
68
69   handler = yajl_gen_alloc2 (write_callback,
70       &handler_config,
71       /* alloc functions = */ NULL,
72       /* context = */ NULL);
73   if (handler == NULL)
74   {
75     graph_destroy (cfg);
76     return (-1);
77   }
78
79   printf ("Content-Type: application/json\n");
80
81   now = time (NULL);
82   status = time_to_rfc1123 (now + EXPIRES_SECS, time_buffer, sizeof (time_buffer));
83   if (status == 0)
84     printf ("Expires: %s\n"
85         "Cache-Control: public\n",
86         time_buffer);
87   printf ("\n");
88
89   status = graph_def_to_json (cfg, handler);
90
91   graph_destroy (cfg);
92   yajl_gen_free (handler);
93
94   return (status);
95 } /* }}} int action_graph_def_json */
96
97 /* vim: set sw=2 sts=2 et fdm=marker : */