2 * collection4 - utils_cgi.c
3 * Copyright (C) 2010 Florian octo Forster
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.
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.
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
21 * Florian octo Forster <ff at octo.it>
34 #include "utils_cgi.h"
38 #include <fcgi_stdio.h>
45 typedef struct parameter_s parameter_t;
49 parameter_t *parameters;
50 size_t parameters_num;
53 static param_list_t *pl_global = NULL;
55 static char *uri_unescape_copy (char *dest, const char *src, size_t n) /* {{{ */
60 if ((dest == NULL) || (src == NULL) || (n < 1))
77 else if ((src_ptr[0] == '%')
78 && isxdigit ((int) src_ptr[1]) && isxdigit ((int) src_ptr[2]))
84 tmpstr[0] = src_ptr[1];
85 tmpstr[1] = src_ptr[2];
90 value = strtol (tmpstr, &endptr, /* base = */ 16);
91 if ((endptr == tmpstr) || (errno != 0))
97 *dest_ptr = (char) value;
104 *dest_ptr = *src_ptr;
110 } /* while (*src_ptr != 0) */
112 assert (*dest_ptr == 0);
114 } /* }}} char *uri_unescape */
116 static char *uri_unescape (const char *string) /* {{{ */
123 uri_unescape_copy (buffer, string, sizeof (buffer));
125 return (strdup (buffer));
126 } /* }}} char *uri_unescape */
128 static int param_parse_keyval (param_list_t *pl, char *keyval) /* {{{ */
136 value_raw = strchr (key_raw, '=');
137 if (value_raw == NULL)
142 key = uri_unescape (key_raw);
146 value = uri_unescape (value_raw);
153 param_set (pl, key, value);
159 } /* }}} int param_parse_keyval */
161 static int parse_query_string (param_list_t *pl, /* {{{ */
167 if ((pl == NULL) || (query_string == NULL))
170 dummy = query_string;
171 while ((keyval = strtok (dummy, ";&")) != NULL)
174 param_parse_keyval (pl, keyval);
178 } /* }}} int parse_query_string */
180 int param_init (void) /* {{{ */
182 if (pl_global != NULL)
185 pl_global = param_create (/* query string = */ NULL);
186 if (pl_global == NULL)
190 } /* }}} int param_init */
192 void param_finish (void) /* {{{ */
194 param_destroy (pl_global);
196 } /* }}} void param_finish */
198 const char *param (const char *key) /* {{{ */
202 return (param_get (pl_global, key));
203 } /* }}} const char *param */
205 param_list_t *param_create (const char *query_string) /* {{{ */
210 if (query_string == NULL)
211 query_string = getenv ("QUERY_STRING");
213 if (query_string == NULL)
216 tmp = strdup (query_string);
220 pl = malloc (sizeof (*pl));
226 memset (pl, 0, sizeof (*pl));
228 parse_query_string (pl, tmp);
232 } /* }}} param_list_t *param_create */
234 param_list_t *param_clone (__attribute__((unused)) param_list_t *pl) /* {{{ */
236 /* FIXME: To be implemented. */
239 } /* }}} param_list_t *param_clone */
241 void param_destroy (param_list_t *pl) /* {{{ */
248 for (i = 0; i < pl->parameters_num; i++)
250 free (pl->parameters[i].key);
251 free (pl->parameters[i].value);
253 free (pl->parameters);
255 } /* }}} void param_destroy */
257 const char *param_get (param_list_t *pl, const char *name) /* {{{ */
261 if ((pl == NULL) || (name == NULL))
264 for (i = 0; i < pl->parameters_num; i++)
266 if ((name == NULL) && (pl->parameters[i].key == NULL))
267 return (pl->parameters[i].value);
268 else if ((name != NULL) && (pl->parameters[i].key != NULL)
269 && (strcmp (name, pl->parameters[i].key) == 0))
270 return (pl->parameters[i].value);
274 } /* }}} char *param_get */
276 static int param_add (param_list_t *pl, /* {{{ */
277 const char *key, const char *value)
281 tmp = realloc (pl->parameters,
282 sizeof (*pl->parameters) * (pl->parameters_num + 1));
285 pl->parameters = tmp;
286 tmp = pl->parameters + pl->parameters_num;
288 memset (tmp, 0, sizeof (*tmp));
289 tmp->key = strdup (key);
290 if (tmp->key == NULL)
293 tmp->value = strdup (value);
294 if (tmp->value == NULL)
300 pl->parameters_num++;
303 } /* }}} int param_add */
305 static int param_delete (param_list_t *pl, /* {{{ */
310 if ((pl == NULL) || (name == NULL))
313 for (i = 0; i < pl->parameters_num; i++)
314 if (strcasecmp (pl->parameters[i].key, name) == 0)
317 if (i >= pl->parameters_num)
320 if (i < (pl->parameters_num - 1))
324 p = pl->parameters[i];
325 pl->parameters[i] = pl->parameters[pl->parameters_num - 1];
326 pl->parameters[pl->parameters_num - 1] = p;
329 pl->parameters_num--;
330 free (pl->parameters[pl->parameters_num].key);
331 free (pl->parameters[pl->parameters_num].value);
334 } /* }}} int param_delete */
336 int param_set (param_list_t *pl, const char *name, /* {{{ */
343 if ((pl == NULL) || (name == NULL))
347 return (param_delete (pl, name));
350 for (i = 0; i < pl->parameters_num; i++)
352 if (strcasecmp (pl->parameters[i].key, name) == 0)
354 p = pl->parameters + i;
360 return (param_add (pl, name, value));
362 value_copy = strdup (value);
363 if (value_copy == NULL)
367 p->value = value_copy;
370 } /* }}} int param_set */
372 char *param_as_string (param_list_t *pl) /* {{{ */
383 for (i = 0; i < pl->parameters_num; i++)
385 uri_escape_copy (key, pl->parameters[i].key, sizeof (key));
386 uri_escape_copy (value, pl->parameters[i].value, sizeof (value));
389 strlcat (buffer, ";", sizeof (buffer));
390 strlcat (buffer, key, sizeof (buffer));
391 strlcat (buffer, "=", sizeof (buffer));
392 strlcat (buffer, value, sizeof (buffer));
395 return (strdup (buffer));
396 } /* }}} char *param_as_string */
398 int param_print_hidden (param_list_t *pl) /* {{{ */
407 for (i = 0; i < pl->parameters_num; i++)
409 html_escape_copy (key, pl->parameters[i].key, sizeof (key));
410 html_escape_copy (value, pl->parameters[i].value, sizeof (value));
412 printf (" <input type=\"hidden\" name=\"%s\" value=\"%s\" />\n",
417 } /* }}} int param_print_hidden */
419 char *uri_escape_copy (char *dest, const char *src, size_t n) /* {{{ */
433 else if ((((unsigned char) src[in]) < 32)
438 || (((unsigned char) src[in]) >= 128))
445 snprintf (esc, sizeof (esc), "%%%02x", (unsigned int) src[in]);
447 dest[out+1] = esc[1];
448 dest[out+2] = esc[2];
462 } /* }}} char *uri_escape_copy */
464 char *uri_escape (const char *string) /* {{{ */
471 uri_escape_copy (buffer, string, sizeof (buffer));
473 return (strdup (buffer));
474 } /* }}} char *uri_escape */
476 const char *script_name (void) /* {{{ */
480 ret = getenv ("SCRIPT_NAME");
482 ret = "collection4.fcgi";
485 } /* }}} char *script_name */
487 int time_to_rfc1123 (time_t t, char *buffer, size_t buffer_size) /* {{{ */
492 /* RFC 1123 *requires* the time to be GMT and the "GMT" timezone string.
493 * Apache will ignore the timezone if "localtime_r" and "%z" is used,
494 * resulting in weird behavior. */
495 if (gmtime_r (&t, &tm_tmp) == NULL)
498 status = strftime (buffer, buffer_size, "%a, %d %b %Y %T GMT", &tm_tmp);
503 } /* }}} int time_to_rfc1123 */
505 #define COPY_ENTITY(e) do { \
506 size_t len = strlen (e); \
507 if (dest_size < (len + 1)) \
509 strcpy (dest_ptr, (e)); \
514 char *html_escape_copy (char *dest, const char *src, size_t n) /* {{{ */
523 for (pos = 0; src[pos] != 0; pos++)
526 COPY_ENTITY (""");
527 else if (src[pos] == '<')
528 COPY_ENTITY ("<");
529 else if (src[pos] == '>')
530 COPY_ENTITY (">");
531 else if (src[pos] == '&')
532 COPY_ENTITY ("&");
535 *dest_ptr = src[pos];
546 } /* }}} char *html_escape_copy */
550 char *html_escape_buffer (char *buffer, size_t buffer_size) /* {{{ */
552 char tmp[buffer_size];
554 html_escape_copy (tmp, buffer, sizeof (tmp));
555 memcpy (buffer, tmp, buffer_size);
558 } /* }}} char *html_escape_buffer */
560 char *html_escape (const char *string) /* {{{ */
567 html_escape_copy (buffer, string, sizeof (buffer));
569 return (strdup (buffer));
570 } /* }}} char *html_escape */
572 int html_print_page (const char *title, /* {{{ */
573 const page_callbacks_t *cb, void *user_data)
577 printf ("Content-Type: text/html\n"
578 "X-Generator: "PACKAGE_STRING"\n"
582 title = "c4: collection4 graph interface";
584 title_html = html_escape (title);
588 " <title>%s</title>\n"
589 " <link rel=\"stylesheet\" type=\"text/css\" href=\"../share/style.css\" />\n"
590 " <script type=\"text/javascript\" src=\"../share/jquery-1.4.2.min.js\">\n"
592 " <script type=\"text/javascript\" src=\"../share/collection.js\">\n"
598 " <table id=\"layout-table\">\n"
599 " <tr id=\"layout-top\">\n"
600 " <td id=\"layout-top-left\">");
601 if (cb->top_left != NULL)
602 (*cb->top_left) (user_data);
604 html_print_logo (NULL);
606 " <td id=\"layout-top-center\">");
607 if (cb->top_center != NULL)
608 (*cb->top_center) (user_data);
610 printf ("<h1>%s</h1>", title_html);
612 " <td id=\"layout-top-right\">");
613 if (cb->top_right != NULL)
614 (*cb->top_right) (user_data);
617 " <tr id=\"layout-middle\">\n"
618 " <td id=\"layout-middle-left\">");
619 if (cb->middle_left != NULL)
620 (*cb->middle_left) (user_data);
622 " <td id=\"layout-middle-center\">");
623 if (cb->middle_center != NULL)
624 (*cb->middle_center) (user_data);
626 " <td id=\"layout-middle-right\">");
627 if (cb->middle_right != NULL)
628 (*cb->middle_right) (user_data);
631 " <tr id=\"layout-bottom\">\n"
632 " <td id=\"layout-bottom-left\">");
633 if (cb->bottom_left != NULL)
634 (*cb->bottom_left) (user_data);
636 " <td id=\"layout-bottom-center\">");
637 if (cb->bottom_center != NULL)
638 (*cb->bottom_center) (user_data);
640 " <td id=\"layout-bottom-right\">");
641 if (cb->bottom_right != NULL)
642 (*cb->bottom_right) (user_data);
646 " <div class=\"footer\">"PACKAGE_STRING"</div>\n"
652 } /* }}} int html_print_page */
654 int html_print_logo (__attribute__((unused)) void *user_data) /* {{{ */
656 printf ("<a href=\"%s?action=list_graphs\" id=\"logo-canvas\">\n"
657 " <h1>c<sup>4</sup></h1>\n"
658 " <div id=\"logo-subscript\">collection 4</div>\n"
662 } /* }}} int html_print_search_box */
664 int html_print_search_box (__attribute__((unused)) void *user_data) /* {{{ */
668 term_html = html_escape (param ("q"));
670 printf ("<form action=\"%s\" method=\"get\" id=\"search-form\">\n"
671 " <input type=\"hidden\" name=\"action\" value=\"list_graphs\" />\n"
672 " <input type=\"text\" name=\"q\" value=\"%s\" id=\"search-input\" />\n"
673 " <input type=\"submit\" name=\"button\" value=\"Search\" />\n"
676 (term_html != NULL) ? term_html : "");
681 } /* }}} int html_print_search_box */
683 /* vim: set sw=2 sts=2 et fdm=marker : */