From: Florian Forster Date: Sat, 19 Jun 2010 08:34:44 +0000 (+0200) Subject: Rename: src/utils_params.[ch] → src/utils_cgi.[ch] X-Git-Tag: v4.0.0~231 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=e9d94ae68f4081b25c4d278cea49b51354062e7d;p=collection4.git Rename: src/utils_params.[ch] → src/utils_cgi.[ch] --- diff --git a/src/Makefile.am b/src/Makefile.am index 9b91a91..ac11e9f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -27,4 +27,4 @@ collection_fcgi_SOURCES = main.c \ graph_instance.c graph_instance.h \ graph_list.c graph_list.h \ utils_array.c utils_array.h \ - utils_params.c utils_params.h + utils_cgi.c utils_cgi.h diff --git a/src/action_graph.c b/src/action_graph.c index 0bb5856..45a9c94 100644 --- a/src/action_graph.c +++ b/src/action_graph.c @@ -13,7 +13,7 @@ #include "common.h" #include "action_graph.h" #include "graph_list.h" -#include "utils_params.h" +#include "utils_cgi.h" #include "utils_array.h" #include diff --git a/src/action_list_graphs.c b/src/action_list_graphs.c index b6fc039..92d82d7 100644 --- a/src/action_list_graphs.c +++ b/src/action_list_graphs.c @@ -7,7 +7,7 @@ #include "graph.h" #include "graph_ident.h" #include "graph_list.h" -#include "utils_params.h" +#include "utils_cgi.h" #include #include diff --git a/src/graph.c b/src/graph.c index 2809ebb..da4b5ea 100644 --- a/src/graph.c +++ b/src/graph.c @@ -14,7 +14,7 @@ #include "graph_config.h" #include "common.h" #include "filesystem.h" -#include "utils_params.h" +#include "utils_cgi.h" #include #include diff --git a/src/graph_instance.c b/src/graph_instance.c index 90d1753..45dcefb 100644 --- a/src/graph_instance.c +++ b/src/graph_instance.c @@ -8,7 +8,7 @@ #include "graph_ident.h" #include "graph_list.h" #include "common.h" -#include "utils_params.h" +#include "utils_cgi.h" #include #include diff --git a/src/graph_list.c b/src/graph_list.c index 3c8d375..f3e9738 100644 --- a/src/graph_list.c +++ b/src/graph_list.c @@ -12,7 +12,7 @@ #include "graph_config.h" #include "common.h" #include "filesystem.h" -#include "utils_params.h" +#include "utils_cgi.h" #include #include diff --git a/src/main.c b/src/main.c index 097c55c..5c2b146 100644 --- a/src/main.c +++ b/src/main.c @@ -11,7 +11,7 @@ #include "common.h" #include "graph_list.h" -#include "utils_params.h" +#include "utils_cgi.h" #include "action_graph.h" #include "action_list_graphs.h" diff --git a/src/utils_cgi.c b/src/utils_cgi.c new file mode 100644 index 0000000..1845788 --- /dev/null +++ b/src/utils_cgi.c @@ -0,0 +1,291 @@ +#include +#include +#include +#include +#include +#include + +#include "utils_cgi.h" + +struct parameter_s +{ + char *key; + char *value; +}; +typedef struct parameter_s parameter_t; + +static parameter_t *parameters = NULL; +static size_t parameters_num = 0; +static _Bool parameters_init = 0; + +static int parameter_add (const char *key, const char *value) /* {{{ */ +{ + parameter_t *ptr; + + if (value == NULL) + return (EINVAL); + + ptr = realloc (parameters, sizeof (*parameters) * (parameters_num + 1)); + if (ptr == NULL) + return (ENOMEM); + parameters = ptr; + + ptr = parameters + parameters_num; + if (key == NULL) + { + ptr->key = NULL; + } + else + { + ptr->key = strdup (key); + if (ptr->key == NULL) + return (ENOMEM); + } + + ptr->value = strdup (value); + if (ptr->value == NULL) + { + free (ptr->key); + return (ENOMEM); + } + + parameters_num++; + return (0); +} /* }}} int parameter_add */ + +static char *parameter_lookup (const char *key) /* {{{ */ +{ + size_t i; + + for (i = 0; i < parameters_num; i++) + { + if ((key == NULL) && (parameters[i].key == NULL)) + return (parameters[i].value); + else if ((key != NULL) && (parameters[i].key != NULL) + && (strcmp (key, parameters[i].key) == 0)) + return (parameters[i].value); + } + + return (NULL); +} /* }}} char *parameter_lookup */ + +static char *uri_unescape (char *string) /* {{{ */ +{ + char *in; + char *out; + + if (string == NULL) + return (NULL); + + in = string; + out = string; + + while (*in != 0) + { + if (*in == '+') + { + *out = ' '; + } + else if ((in[0] == '%') + && isxdigit ((int) in[1]) && isxdigit ((int) in[2])) + { + char tmpstr[3]; + char *endptr; + long value; + + tmpstr[0] = in[1]; + tmpstr[1] = in[2]; + tmpstr[2] = 0; + + errno = 0; + endptr = NULL; + value = strtol (tmpstr, &endptr, /* base = */ 16); + if ((endptr == tmpstr) || (errno != 0)) + { + *out = '?'; + } + else + { + *out = (char) value; + } + + in += 2; + } + else + { + *out = *in; + } + + in++; + out++; + } /* while (*in != 0) */ + + *out = 0; + return (string); +} /* }}} char *uri_unescape */ + +static int parse_keyval (char *keyval) /* {{{ */ +{ + char *key; + char *val; + + val = strchr (keyval, '='); + if (val == NULL) + { + key = NULL; + val = keyval; + } + else + { + key = keyval; + *val = 0; + val++; + } + + parameter_add (uri_unescape (key), uri_unescape (val)); + + return (0); +} /* }}} int parse_keyval */ + +static int parse_query_string (char *query_string) /* {{{ */ +{ + char *dummy; + char *keyval; + + if (query_string == NULL) + return (EINVAL); + + dummy = query_string; + while ((keyval = strtok (dummy, ";&")) != NULL) + { + dummy = NULL; + parse_keyval (keyval); + } + + return (0); +} /* }}} int parse_query_string */ + +int param_init (void) /* {{{ */ +{ + const char *query_string; + char *copy; + int status; + + if (parameters_init) + return (0); + + query_string = getenv ("QUERY_STRING"); + if (query_string == NULL) + return (ENOENT); + + copy = strdup (query_string); + if (copy == NULL) + return (ENOMEM); + + status = parse_query_string (copy); + free (copy); + + parameters_init = 1; + + return (status); +} /* }}} int param_init */ + +void param_finish (void) /* {{{ */ +{ + size_t i; + + if (!parameters_init) + return; + + for (i = 0; i < parameters_num; i++) + { + free (parameters[i].key); + free (parameters[i].value); + } + free (parameters); + + parameters = NULL; + parameters_num = 0; + parameters_init = 0; +} /* }}} void param_finish */ + +const char *param (const char *key) /* {{{ */ +{ + param_init (); + + return (parameter_lookup (key)); +} /* }}} const char *param */ + +int uri_escape (char *dst, const char *src, size_t size) /* {{{ */ +{ + size_t in; + size_t out; + + in = 0; + out = 0; + while (42) + { + if (src[in] == 0) + { + dst[out] = 0; + return (0); + } + else if ((src[in] < 32) + || (src[in] == '&') + || (src[in] == ';') + || (((unsigned char) src[in]) >= 128)) + { + char esc[4]; + + if ((size - out) < 4) + break; + + snprintf (esc, sizeof (esc), "%%%02x", (unsigned int) src[in]); + dst[out] = esc[0]; + dst[out+1] = esc[1]; + dst[out+2] = esc[2]; + + out += 3; + in++; + } + else + { + dst[out] = src[in]; + out++; + in++; + } + } /* while (42) */ + + return (0); +} /* }}} int uri_escape */ + +const char *script_name (void) /* {{{ */ +{ + char *ret; + + ret = getenv ("SCRIPT_NAME"); + if (ret == NULL) + ret = "collection4.fcgi"; + + return (ret); +} /* }}} char *script_name */ + +int time_to_rfc1123 (time_t t, char *buffer, size_t buffer_size) /* {{{ */ +{ + struct tm tm_tmp; + size_t status; + + /* RFC 1123 *requires* the time to be GMT and the "GMT" timezone string. + * Apache will ignore the timezone if "localtime_r" and "%z" is used, + * resulting in weird behavior. */ + if (gmtime_r (&t, &tm_tmp) == NULL) + return (errno); + + status = strftime (buffer, buffer_size, "%a, %d %b %Y %T GMT", &tm_tmp); + if (status == 0) + return (errno); + + return (0); +} /* }}} int time_to_rfc1123 */ + +/* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/utils_cgi.h b/src/utils_cgi.h new file mode 100644 index 0000000..3a0105a --- /dev/null +++ b/src/utils_cgi.h @@ -0,0 +1,17 @@ +#ifndef UTILS_CGI_H +#define UTILS_CGI_H 1 + +#include + +int param_init (void); +void param_finish (void); + +const char *param (const char *key); + +int uri_escape (char *dst, const char *src, size_t size); + +const char *script_name (void); + +int time_to_rfc1123 (time_t t, char *buffer, size_t buffer_size); + +#endif /* UTILS_CGI_H */ diff --git a/src/utils_params.c b/src/utils_params.c deleted file mode 100644 index 5b63334..0000000 --- a/src/utils_params.c +++ /dev/null @@ -1,291 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "utils_params.h" - -struct parameter_s -{ - char *key; - char *value; -}; -typedef struct parameter_s parameter_t; - -static parameter_t *parameters = NULL; -static size_t parameters_num = 0; -static _Bool parameters_init = 0; - -static int parameter_add (const char *key, const char *value) /* {{{ */ -{ - parameter_t *ptr; - - if (value == NULL) - return (EINVAL); - - ptr = realloc (parameters, sizeof (*parameters) * (parameters_num + 1)); - if (ptr == NULL) - return (ENOMEM); - parameters = ptr; - - ptr = parameters + parameters_num; - if (key == NULL) - { - ptr->key = NULL; - } - else - { - ptr->key = strdup (key); - if (ptr->key == NULL) - return (ENOMEM); - } - - ptr->value = strdup (value); - if (ptr->value == NULL) - { - free (ptr->key); - return (ENOMEM); - } - - parameters_num++; - return (0); -} /* }}} int parameter_add */ - -static char *parameter_lookup (const char *key) /* {{{ */ -{ - size_t i; - - for (i = 0; i < parameters_num; i++) - { - if ((key == NULL) && (parameters[i].key == NULL)) - return (parameters[i].value); - else if ((key != NULL) && (parameters[i].key != NULL) - && (strcmp (key, parameters[i].key) == 0)) - return (parameters[i].value); - } - - return (NULL); -} /* }}} char *parameter_lookup */ - -static char *uri_unescape (char *string) /* {{{ */ -{ - char *in; - char *out; - - if (string == NULL) - return (NULL); - - in = string; - out = string; - - while (*in != 0) - { - if (*in == '+') - { - *out = ' '; - } - else if ((in[0] == '%') - && isxdigit ((int) in[1]) && isxdigit ((int) in[2])) - { - char tmpstr[3]; - char *endptr; - long value; - - tmpstr[0] = in[1]; - tmpstr[1] = in[2]; - tmpstr[2] = 0; - - errno = 0; - endptr = NULL; - value = strtol (tmpstr, &endptr, /* base = */ 16); - if ((endptr == tmpstr) || (errno != 0)) - { - *out = '?'; - } - else - { - *out = (char) value; - } - - in += 2; - } - else - { - *out = *in; - } - - in++; - out++; - } /* while (*in != 0) */ - - *out = 0; - return (string); -} /* }}} char *uri_unescape */ - -static int parse_keyval (char *keyval) /* {{{ */ -{ - char *key; - char *val; - - val = strchr (keyval, '='); - if (val == NULL) - { - key = NULL; - val = keyval; - } - else - { - key = keyval; - *val = 0; - val++; - } - - parameter_add (uri_unescape (key), uri_unescape (val)); - - return (0); -} /* }}} int parse_keyval */ - -static int parse_query_string (char *query_string) /* {{{ */ -{ - char *dummy; - char *keyval; - - if (query_string == NULL) - return (EINVAL); - - dummy = query_string; - while ((keyval = strtok (dummy, ";&")) != NULL) - { - dummy = NULL; - parse_keyval (keyval); - } - - return (0); -} /* }}} int parse_query_string */ - -int param_init (void) /* {{{ */ -{ - const char *query_string; - char *copy; - int status; - - if (parameters_init) - return (0); - - query_string = getenv ("QUERY_STRING"); - if (query_string == NULL) - return (ENOENT); - - copy = strdup (query_string); - if (copy == NULL) - return (ENOMEM); - - status = parse_query_string (copy); - free (copy); - - parameters_init = 1; - - return (status); -} /* }}} int param_init */ - -void param_finish (void) /* {{{ */ -{ - size_t i; - - if (!parameters_init) - return; - - for (i = 0; i < parameters_num; i++) - { - free (parameters[i].key); - free (parameters[i].value); - } - free (parameters); - - parameters = NULL; - parameters_num = 0; - parameters_init = 0; -} /* }}} void param_finish */ - -const char *param (const char *key) /* {{{ */ -{ - param_init (); - - return (parameter_lookup (key)); -} /* }}} const char *param */ - -int uri_escape (char *dst, const char *src, size_t size) /* {{{ */ -{ - size_t in; - size_t out; - - in = 0; - out = 0; - while (42) - { - if (src[in] == 0) - { - dst[out] = 0; - return (0); - } - else if ((src[in] < 32) - || (src[in] == '&') - || (src[in] == ';') - || (((unsigned char) src[in]) >= 128)) - { - char esc[4]; - - if ((size - out) < 4) - break; - - snprintf (esc, sizeof (esc), "%%%02x", (unsigned int) src[in]); - dst[out] = esc[0]; - dst[out+1] = esc[1]; - dst[out+2] = esc[2]; - - out += 3; - in++; - } - else - { - dst[out] = src[in]; - out++; - in++; - } - } /* while (42) */ - - return (0); -} /* }}} int uri_escape */ - -const char *script_name (void) /* {{{ */ -{ - char *ret; - - ret = getenv ("SCRIPT_NAME"); - if (ret == NULL) - ret = "collection4.fcgi"; - - return (ret); -} /* }}} char *script_name */ - -int time_to_rfc1123 (time_t t, char *buffer, size_t buffer_size) /* {{{ */ -{ - struct tm tm_tmp; - size_t status; - - /* RFC 1123 *requires* the time to be GMT and the "GMT" timezone string. - * Apache will ignore the timezone if "localtime_r" and "%z" is used, - * resulting in weird behavior. */ - if (gmtime_r (&t, &tm_tmp) == NULL) - return (errno); - - status = strftime (buffer, buffer_size, "%a, %d %b %Y %T GMT", &tm_tmp); - if (status == 0) - return (errno); - - return (0); -} /* }}} int time_to_rfc1123 */ - -/* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/utils_params.h b/src/utils_params.h deleted file mode 100644 index 0d3f0e5..0000000 --- a/src/utils_params.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef UTILS_PARAMS_H -#define UTILS_PARAMS_H 1 - -#include - -int param_init (void); -void param_finish (void); - -const char *param (const char *key); - -int uri_escape (char *dst, const char *src, size_t size); - -const char *script_name (void); - -int time_to_rfc1123 (time_t t, char *buffer, size_t buffer_size); - -#endif /* UTILS_PARAMS_H */