d36520526f5eab773858d1390a33745c9d4e03de
[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 int write_buffer (char *buffer, size_t buffer_size) /* {{{ */
44 {
45   size_t status;
46
47   while (buffer_size > 0)
48   {
49     status = fwrite (buffer,  /* size = */ 1,
50         /* nmemb = */ buffer_size, stdout);
51     if (status == 0)
52       return (errno);
53
54     buffer += status;
55     buffer_size -= status;
56   }
57
58   return (0);
59 } /* }}} int write_buffer */
60
61 int action_show_graph_json (void) /* {{{ */
62 {
63   graph_config_t *cfg;
64
65   yajl_gen_config handler_config;
66   yajl_gen handler;
67
68   const unsigned char *buffer;
69   unsigned int buffer_length;
70
71   time_t now;
72   char time_buffer[128];
73   int status;
74
75   cfg = gl_graph_get_selected ();
76   if (cfg == NULL)
77     return (ENOMEM);
78
79   memset (&handler_config, 0, sizeof (handler_config));
80   handler_config.beautify = 1;
81   handler_config.indentString = "  ";
82
83   handler = yajl_gen_alloc (&handler_config,
84       /* alloc functions = */ NULL);
85   if (handler == NULL)
86   {
87     graph_destroy (cfg);
88     return (-1);
89   }
90
91   printf ("Content-Type: application/json\n");
92
93   now = time (NULL);
94   status = time_to_rfc1123 (now + 300, time_buffer, sizeof (time_buffer));
95   if (status == 0)
96     printf ("Expires: %s\n"
97         "Cache-Control: public\n",
98         time_buffer);
99   printf ("\n");
100
101   status = graph_to_json (cfg, handler);
102   if (status != 0)
103   {
104     graph_destroy (cfg);
105     yajl_gen_free (handler);
106     return (status);
107   }
108
109   buffer = NULL;
110   buffer_length = 0;
111   status = (int) yajl_gen_get_buf (handler, &buffer, &buffer_length);
112
113   write_buffer ((char *) buffer, (size_t) buffer_length);
114
115   graph_destroy (cfg);
116   yajl_gen_free (handler);
117   return (0);
118 } /* }}} int action_show_graph_json */
119
120 /* vim: set sw=2 sts=2 et fdm=marker : */