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 **/
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>
32 #include "action_graph_def_json.h"
33 #include "common.h"
34 #include "graph.h"
35 #include "graph_instance.h"
36 #include "graph_list.h"
37 #include "utils_cgi.h"
39 #include <fcgiapp.h>
40 #include <fcgi_stdio.h>
42 /* Expire data after one day. */
43 #define EXPIRES_SECS 86400
45 static void write_callback (__attribute__((unused)) void *ctx, /* {{{ */
46 const char *str, unsigned int len)
47 {
48 fwrite ((void *) str, /* size = */ len, /* nmemb = */ 1, stdout);
49 } /* }}} void write_callback */
51 int action_graph_def_json (void) /* {{{ */
52 {
53 graph_config_t *cfg;
54 graph_instance_t *inst;
56 yajl_gen_config handler_config;
57 yajl_gen handler;
59 time_t now;
60 char time_buffer[128];
61 int status;
63 cfg = gl_graph_get_selected ();
64 if (cfg == NULL)
65 return (ENOMEM);
67 inst = inst_get_selected (cfg);
68 if (inst == NULL)
69 return (EINVAL);
71 memset (&handler_config, 0, sizeof (handler_config));
72 handler_config.beautify = 1;
73 handler_config.indentString = " ";
75 handler = yajl_gen_alloc2 (write_callback,
76 &handler_config,
77 /* alloc functions = */ NULL,
78 /* context = */ NULL);
79 if (handler == NULL)
80 return (-1);
82 printf ("Content-Type: application/json\n");
84 now = time (NULL);
85 status = time_to_rfc1123 (now + EXPIRES_SECS, time_buffer, sizeof (time_buffer));
86 if (status == 0)
87 printf ("Expires: %s\n"
88 "Cache-Control: public\n",
89 time_buffer);
90 printf ("\n");
92 status = graph_def_to_json (cfg, inst, handler);
94 yajl_gen_free (handler);
96 return (status);
97 } /* }}} int action_graph_def_json */
99 /* vim: set sw=2 sts=2 et fdm=marker : */