graph_{data,def}_json actions: Don't destroy the graph_config_t object.
[collection4.git] / src / action_show_graph_json.c
1 /**
2  * collection4 - action_show_graph_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_show_graph_json.h"
33 #include "common.h"
34 #include "graph.h"
35 #include "graph_ident.h"
36 #include "graph_instance.h"
37 #include "graph_list.h"
38 #include "utils_cgi.h"
39
40 #include <fcgiapp.h>
41 #include <fcgi_stdio.h>
42
43 static void write_callback (__attribute__((unused)) void *ctx, /* {{{ */
44     const char *str, unsigned int len)
45 {
46   fwrite ((void *) str, /* size = */ len, /* nmemb = */ 1, stdout);
47 } /* }}} void write_callback */
48
49 int action_show_graph_json (void) /* {{{ */
50 {
51   graph_config_t *cfg;
52
53   yajl_gen_config handler_config;
54   yajl_gen handler;
55
56   time_t now;
57   char time_buffer[128];
58   int status;
59
60   cfg = gl_graph_get_selected ();
61   if (cfg == NULL)
62     return (ENOMEM);
63
64   memset (&handler_config, 0, sizeof (handler_config));
65   handler_config.beautify = 1;
66   handler_config.indentString = "  ";
67
68   handler = yajl_gen_alloc2 (write_callback,
69       &handler_config,
70       /* alloc functions = */ NULL,
71       /* context = */ NULL);
72   if (handler == NULL)
73   {
74     graph_destroy (cfg);
75     return (-1);
76   }
77
78   printf ("Content-Type: application/json\n");
79
80   now = time (NULL);
81   status = time_to_rfc1123 (now + 300, time_buffer, sizeof (time_buffer));
82   if (status == 0)
83     printf ("Expires: %s\n"
84         "Cache-Control: public\n",
85         time_buffer);
86   printf ("\n");
87
88   status = graph_to_json (cfg, handler);
89
90   graph_destroy (cfg);
91   yajl_gen_free (handler);
92
93   return (status);
94 } /* }}} int action_show_graph_json */
95
96 /* vim: set sw=2 sts=2 et fdm=marker : */