You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by di...@apache.org on 2008/07/22 06:35:46 UTC
svn commit: r678637 [35/46] - in
/webservices/axis2/branches/c/lighttpd_mod_axis2/lighttpd: ./
autom4te.cache/ cygwin/ doc/ openwrt/ src/ tests/ tests/docroot/
tests/docroot/123/ tests/docroot/www/ tests/docroot/www/dummydir/
tests/docroot/www/expire/ ...
Added: webservices/axis2/branches/c/lighttpd_mod_axis2/lighttpd/src/mod_redirect.c
URL: http://svn.apache.org/viewvc/webservices/axis2/branches/c/lighttpd_mod_axis2/lighttpd/src/mod_redirect.c?rev=678637&view=auto
==============================================================================
--- webservices/axis2/branches/c/lighttpd_mod_axis2/lighttpd/src/mod_redirect.c (added)
+++ webservices/axis2/branches/c/lighttpd_mod_axis2/lighttpd/src/mod_redirect.c Mon Jul 21 21:35:35 2008
@@ -0,0 +1,283 @@
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "base.h"
+#include "log.h"
+#include "buffer.h"
+
+#include "plugin.h"
+#include "response.h"
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+typedef struct {
+ pcre_keyvalue_buffer *redirect;
+ data_config *context; /* to which apply me */
+} plugin_config;
+
+typedef struct {
+ PLUGIN_DATA;
+ buffer *match_buf;
+ buffer *location;
+
+ plugin_config **config_storage;
+
+ plugin_config conf;
+} plugin_data;
+
+INIT_FUNC(mod_redirect_init) {
+ plugin_data *p;
+
+ p = calloc(1, sizeof(*p));
+
+ p->match_buf = buffer_init();
+ p->location = buffer_init();
+
+ return p;
+}
+
+FREE_FUNC(mod_redirect_free) {
+ plugin_data *p = p_d;
+
+ if (!p) return HANDLER_GO_ON;
+
+ if (p->config_storage) {
+ size_t i;
+ for (i = 0; i < srv->config_context->used; i++) {
+ plugin_config *s = p->config_storage[i];
+
+ pcre_keyvalue_buffer_free(s->redirect);
+
+ free(s);
+ }
+ free(p->config_storage);
+ }
+
+
+ buffer_free(p->match_buf);
+ buffer_free(p->location);
+
+ free(p);
+
+ return HANDLER_GO_ON;
+}
+
+SETDEFAULTS_FUNC(mod_redirect_set_defaults) {
+ plugin_data *p = p_d;
+ data_unset *du;
+ size_t i = 0;
+
+ config_values_t cv[] = {
+ { "url.redirect", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
+ { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
+ };
+
+ if (!p) return HANDLER_ERROR;
+
+ /* 0 */
+ p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
+
+ for (i = 0; i < srv->config_context->used; i++) {
+ plugin_config *s;
+ size_t j;
+ array *ca;
+ data_array *da = (data_array *)du;
+
+ s = calloc(1, sizeof(plugin_config));
+ s->redirect = pcre_keyvalue_buffer_init();
+
+ cv[0].destination = s->redirect;
+
+ p->config_storage[i] = s;
+ ca = ((data_config *)srv->config_context->data[i])->value;
+
+ if (0 != config_insert_values_global(srv, ca, cv)) {
+ return HANDLER_ERROR;
+ }
+
+ if (NULL == (du = array_get_element(ca, "url.redirect"))) {
+ /* no url.redirect defined */
+ continue;
+ }
+
+ if (du->type != TYPE_ARRAY) {
+ log_error_write(srv, __FILE__, __LINE__, "sss",
+ "unexpected type for key: ", "url.redirect", "array of strings");
+
+ return HANDLER_ERROR;
+ }
+
+ da = (data_array *)du;
+
+ for (j = 0; j < da->value->used; j++) {
+ if (da->value->data[j]->type != TYPE_STRING) {
+ log_error_write(srv, __FILE__, __LINE__, "sssbs",
+ "unexpected type for key: ",
+ "url.redirect",
+ "[", da->value->data[j]->key, "](string)");
+
+ return HANDLER_ERROR;
+ }
+
+ if (0 != pcre_keyvalue_buffer_append(s->redirect,
+ ((data_string *)(da->value->data[j]))->key->ptr,
+ ((data_string *)(da->value->data[j]))->value->ptr)) {
+
+ log_error_write(srv, __FILE__, __LINE__, "sb",
+ "pcre-compile failed for", da->value->data[j]->key);
+ }
+ }
+ }
+
+ return HANDLER_GO_ON;
+}
+#ifdef HAVE_PCRE_H
+static int mod_redirect_patch_connection(server *srv, connection *con, plugin_data *p) {
+ size_t i, j;
+ plugin_config *s = p->config_storage[0];
+
+ p->conf.redirect = s->redirect;
+ p->conf.context = NULL;
+
+ /* skip the first, the global context */
+ for (i = 1; i < srv->config_context->used; i++) {
+ data_config *dc = (data_config *)srv->config_context->data[i];
+ s = p->config_storage[i];
+
+ /* condition didn't match */
+ if (!config_check_cond(srv, con, dc)) continue;
+
+ /* merge config */
+ for (j = 0; j < dc->value->used; j++) {
+ data_unset *du = dc->value->data[j];
+
+ if (0 == strcmp(du->key->ptr, "url.redirect")) {
+ p->conf.redirect = s->redirect;
+ p->conf.context = dc;
+ }
+ }
+ }
+
+ return 0;
+}
+#endif
+static handler_t mod_redirect_uri_handler(server *srv, connection *con, void *p_data) {
+#ifdef HAVE_PCRE_H
+ plugin_data *p = p_data;
+ size_t i;
+
+ /*
+ * REWRITE URL
+ *
+ * e.g. redirect /base/ to /index.php?section=base
+ *
+ */
+
+ mod_redirect_patch_connection(srv, con, p);
+
+ buffer_copy_string_buffer(p->match_buf, con->request.uri);
+
+ for (i = 0; i < p->conf.redirect->used; i++) {
+ pcre *match;
+ pcre_extra *extra;
+ const char *pattern;
+ size_t pattern_len;
+ int n;
+ pcre_keyvalue *kv = p->conf.redirect->kv[i];
+# define N 10
+ int ovec[N * 3];
+
+ match = kv->key;
+ extra = kv->key_extra;
+ pattern = kv->value->ptr;
+ pattern_len = kv->value->used - 1;
+
+ if ((n = pcre_exec(match, extra, p->match_buf->ptr, p->match_buf->used - 1, 0, 0, ovec, 3 * N)) < 0) {
+ if (n != PCRE_ERROR_NOMATCH) {
+ log_error_write(srv, __FILE__, __LINE__, "sd",
+ "execution error while matching: ", n);
+ return HANDLER_ERROR;
+ }
+ } else {
+ const char **list;
+ size_t start, end;
+ size_t k;
+
+ /* it matched */
+ pcre_get_substring_list(p->match_buf->ptr, ovec, n, &list);
+
+ /* search for $[0-9] */
+
+ buffer_reset(p->location);
+
+ start = 0; end = pattern_len;
+ for (k = 0; k < pattern_len; k++) {
+ if ((pattern[k] == '$' || pattern[k] == '%') &&
+ isdigit((unsigned char)pattern[k + 1])) {
+ /* got one */
+
+ size_t num = pattern[k + 1] - '0';
+
+ end = k;
+
+ buffer_append_string_len(p->location, pattern + start, end - start);
+
+ if (pattern[k] == '$') {
+ /* n is always > 0 */
+ if (num < (size_t)n) {
+ buffer_append_string(p->location, list[num]);
+ }
+ } else if (p->conf.context == NULL) {
+ /* we have no context, we are global */
+ log_error_write(srv, __FILE__, __LINE__, "sb",
+ "used a rewrite containing a %[0-9]+ in the global scope, ignored:",
+ kv->value);
+ } else {
+ config_append_cond_match_buffer(con, p->conf.context, p->location, num);
+ }
+
+ k++;
+ start = k + 1;
+ }
+ }
+
+ buffer_append_string_len(p->location, pattern + start, pattern_len - start);
+
+ pcre_free(list);
+
+ response_header_insert(srv, con, CONST_STR_LEN("Location"), CONST_BUF_LEN(p->location));
+
+ con->http_status = 301;
+ con->file_finished = 1;
+
+ return HANDLER_FINISHED;
+ }
+ }
+#undef N
+
+#else
+ UNUSED(srv);
+ UNUSED(con);
+ UNUSED(p_data);
+#endif
+
+ return HANDLER_GO_ON;
+}
+
+
+int mod_redirect_plugin_init(plugin *p) {
+ p->version = LIGHTTPD_VERSION_ID;
+ p->name = buffer_init_string("redirect");
+
+ p->init = mod_redirect_init;
+ p->handle_uri_clean = mod_redirect_uri_handler;
+ p->set_defaults = mod_redirect_set_defaults;
+ p->cleanup = mod_redirect_free;
+
+ p->data = NULL;
+
+ return 0;
+}
Added: webservices/axis2/branches/c/lighttpd_mod_axis2/lighttpd/src/mod_rewrite.c
URL: http://svn.apache.org/viewvc/webservices/axis2/branches/c/lighttpd_mod_axis2/lighttpd/src/mod_rewrite.c?rev=678637&view=auto
==============================================================================
--- webservices/axis2/branches/c/lighttpd_mod_axis2/lighttpd/src/mod_rewrite.c (added)
+++ webservices/axis2/branches/c/lighttpd_mod_axis2/lighttpd/src/mod_rewrite.c Mon Jul 21 21:35:35 2008
@@ -0,0 +1,458 @@
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "base.h"
+#include "log.h"
+#include "buffer.h"
+
+#include "plugin.h"
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+typedef struct {
+#ifdef HAVE_PCRE_H
+ pcre *key;
+#endif
+
+ buffer *value;
+
+ int once;
+} rewrite_rule;
+
+typedef struct {
+ rewrite_rule **ptr;
+
+ size_t used;
+ size_t size;
+} rewrite_rule_buffer;
+
+typedef struct {
+ rewrite_rule_buffer *rewrite;
+ data_config *context; /* to which apply me */
+} plugin_config;
+
+typedef struct {
+ enum { REWRITE_STATE_UNSET, REWRITE_STATE_FINISHED} state;
+ int loops;
+} handler_ctx;
+
+typedef struct {
+ PLUGIN_DATA;
+ buffer *match_buf;
+
+ plugin_config **config_storage;
+
+ plugin_config conf;
+} plugin_data;
+
+static handler_ctx * handler_ctx_init() {
+ handler_ctx * hctx;
+
+ hctx = calloc(1, sizeof(*hctx));
+
+ hctx->state = REWRITE_STATE_UNSET;
+ hctx->loops = 0;
+
+ return hctx;
+}
+
+static void handler_ctx_free(handler_ctx *hctx) {
+ free(hctx);
+}
+
+rewrite_rule_buffer *rewrite_rule_buffer_init(void) {
+ rewrite_rule_buffer *kvb;
+
+ kvb = calloc(1, sizeof(*kvb));
+
+ return kvb;
+}
+
+int rewrite_rule_buffer_append(rewrite_rule_buffer *kvb, buffer *key, buffer *value, int once) {
+#ifdef HAVE_PCRE_H
+ size_t i;
+ const char *errptr;
+ int erroff;
+
+ if (!key) return -1;
+
+ if (kvb->size == 0) {
+ kvb->size = 4;
+ kvb->used = 0;
+
+ kvb->ptr = malloc(kvb->size * sizeof(*kvb->ptr));
+
+ for(i = 0; i < kvb->size; i++) {
+ kvb->ptr[i] = calloc(1, sizeof(**kvb->ptr));
+ }
+ } else if (kvb->used == kvb->size) {
+ kvb->size += 4;
+
+ kvb->ptr = realloc(kvb->ptr, kvb->size * sizeof(*kvb->ptr));
+
+ for(i = kvb->used; i < kvb->size; i++) {
+ kvb->ptr[i] = calloc(1, sizeof(**kvb->ptr));
+ }
+ }
+
+ if (NULL == (kvb->ptr[kvb->used]->key = pcre_compile(key->ptr,
+ 0, &errptr, &erroff, NULL))) {
+
+ return -1;
+ }
+
+ kvb->ptr[kvb->used]->value = buffer_init();
+ buffer_copy_string_buffer(kvb->ptr[kvb->used]->value, value);
+ kvb->ptr[kvb->used]->once = once;
+
+ kvb->used++;
+
+ return 0;
+#else
+ UNUSED(kvb);
+ UNUSED(value);
+ UNUSED(once);
+ UNUSED(key);
+
+ return -1;
+#endif
+}
+
+void rewrite_rule_buffer_free(rewrite_rule_buffer *kvb) {
+#ifdef HAVE_PCRE_H
+ size_t i;
+
+ for (i = 0; i < kvb->size; i++) {
+ if (kvb->ptr[i]->key) pcre_free(kvb->ptr[i]->key);
+ if (kvb->ptr[i]->value) buffer_free(kvb->ptr[i]->value);
+ free(kvb->ptr[i]);
+ }
+
+ if (kvb->ptr) free(kvb->ptr);
+#endif
+
+ free(kvb);
+}
+
+
+INIT_FUNC(mod_rewrite_init) {
+ plugin_data *p;
+
+ p = calloc(1, sizeof(*p));
+
+ p->match_buf = buffer_init();
+
+ return p;
+}
+
+FREE_FUNC(mod_rewrite_free) {
+ plugin_data *p = p_d;
+
+ UNUSED(srv);
+
+ if (!p) return HANDLER_GO_ON;
+
+ buffer_free(p->match_buf);
+ if (p->config_storage) {
+ size_t i;
+ for (i = 0; i < srv->config_context->used; i++) {
+ plugin_config *s = p->config_storage[i];
+ rewrite_rule_buffer_free(s->rewrite);
+
+ free(s);
+ }
+ free(p->config_storage);
+ }
+
+ free(p);
+
+ return HANDLER_GO_ON;
+}
+
+static int parse_config_entry(server *srv, plugin_config *s, array *ca, const char *option, int once) {
+ data_unset *du;
+
+ if (NULL != (du = array_get_element(ca, option))) {
+ data_array *da = (data_array *)du;
+ size_t j;
+
+ if (du->type != TYPE_ARRAY) {
+ log_error_write(srv, __FILE__, __LINE__, "sss",
+ "unexpected type for key: ", option, "array of strings");
+
+ return HANDLER_ERROR;
+ }
+
+ da = (data_array *)du;
+
+ for (j = 0; j < da->value->used; j++) {
+ if (da->value->data[j]->type != TYPE_STRING) {
+ log_error_write(srv, __FILE__, __LINE__, "sssbs",
+ "unexpected type for key: ",
+ option,
+ "[", da->value->data[j]->key, "](string)");
+
+ return HANDLER_ERROR;
+ }
+
+ if (0 != rewrite_rule_buffer_append(s->rewrite,
+ ((data_string *)(da->value->data[j]))->key,
+ ((data_string *)(da->value->data[j]))->value,
+ once)) {
+#ifdef HAVE_PCRE_H
+ log_error_write(srv, __FILE__, __LINE__, "sb",
+ "pcre-compile failed for", da->value->data[j]->key);
+#else
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "pcre support is missing, please install libpcre and the headers");
+#endif
+ }
+ }
+ }
+
+ return 0;
+}
+
+SETDEFAULTS_FUNC(mod_rewrite_set_defaults) {
+ plugin_data *p = p_d;
+ size_t i = 0;
+
+ config_values_t cv[] = {
+ { "url.rewrite-repeat", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
+ { "url.rewrite-once", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
+
+ /* old names, still supported
+ *
+ * url.rewrite remapped to url.rewrite-once
+ * url.rewrite-final is url.rewrite-once
+ *
+ */
+ { "url.rewrite", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
+ { "url.rewrite-final", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
+ { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
+ };
+
+ if (!p) return HANDLER_ERROR;
+
+ /* 0 */
+ p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
+
+ for (i = 0; i < srv->config_context->used; i++) {
+ plugin_config *s;
+ array *ca;
+
+ s = calloc(1, sizeof(plugin_config));
+ s->rewrite = rewrite_rule_buffer_init();
+
+ cv[0].destination = s->rewrite;
+ cv[1].destination = s->rewrite;
+ cv[2].destination = s->rewrite;
+
+ p->config_storage[i] = s;
+ ca = ((data_config *)srv->config_context->data[i])->value;
+
+ if (0 != config_insert_values_global(srv, ca, cv)) {
+ return HANDLER_ERROR;
+ }
+
+ parse_config_entry(srv, s, ca, "url.rewrite-once", 1);
+ parse_config_entry(srv, s, ca, "url.rewrite-final", 1);
+ parse_config_entry(srv, s, ca, "url.rewrite", 1);
+ parse_config_entry(srv, s, ca, "url.rewrite-repeat", 0);
+ }
+
+ return HANDLER_GO_ON;
+}
+#ifdef HAVE_PCRE_H
+static int mod_rewrite_patch_connection(server *srv, connection *con, plugin_data *p) {
+ size_t i, j;
+ plugin_config *s = p->config_storage[0];
+
+ p->conf.rewrite = s->rewrite;
+ p->conf.context = NULL;
+
+ /* skip the first, the global context */
+ for (i = 1; i < srv->config_context->used; i++) {
+ data_config *dc = (data_config *)srv->config_context->data[i];
+ s = p->config_storage[i];
+
+ if (COMP_HTTP_URL == dc->comp) continue;
+
+ /* condition didn't match */
+ if (!config_check_cond(srv, con, dc)) continue;
+
+ /* merge config */
+ for (j = 0; j < dc->value->used; j++) {
+ data_unset *du = dc->value->data[j];
+
+ if (buffer_is_equal_string(du->key, CONST_STR_LEN("url.rewrite"))) {
+ p->conf.rewrite = s->rewrite;
+ p->conf.context = dc;
+ } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("url.rewrite-once"))) {
+ p->conf.rewrite = s->rewrite;
+ p->conf.context = dc;
+ } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("url.rewrite-repeat"))) {
+ p->conf.rewrite = s->rewrite;
+ p->conf.context = dc;
+ } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("url.rewrite-final"))) {
+ p->conf.rewrite = s->rewrite;
+ p->conf.context = dc;
+ }
+ }
+ }
+
+ return 0;
+}
+#endif
+URIHANDLER_FUNC(mod_rewrite_con_reset) {
+ plugin_data *p = p_d;
+
+ UNUSED(srv);
+
+ if (con->plugin_ctx[p->id]) {
+ handler_ctx_free(con->plugin_ctx[p->id]);
+ con->plugin_ctx[p->id] = NULL;
+ }
+
+ return HANDLER_GO_ON;
+}
+
+URIHANDLER_FUNC(mod_rewrite_uri_handler) {
+#ifdef HAVE_PCRE_H
+ plugin_data *p = p_d;
+ size_t i;
+ handler_ctx *hctx;
+
+ /*
+ * REWRITE URL
+ *
+ * e.g. rewrite /base/ to /index.php?section=base
+ *
+ */
+
+ if (con->plugin_ctx[p->id]) {
+ hctx = con->plugin_ctx[p->id];
+
+ if (hctx->loops++ > 100) {
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "ENDLESS LOOP IN rewrite-rule DETECTED ... aborting request, perhaps you want to use url.rewrite-once instead of url.rewrite-repeat");
+
+ return HANDLER_ERROR;
+ }
+
+ if (hctx->state == REWRITE_STATE_FINISHED) return HANDLER_GO_ON;
+ }
+
+ mod_rewrite_patch_connection(srv, con, p);
+
+ if (!p->conf.rewrite) return HANDLER_GO_ON;
+
+ buffer_copy_string_buffer(p->match_buf, con->request.uri);
+
+ for (i = 0; i < p->conf.rewrite->used; i++) {
+ pcre *match;
+ const char *pattern;
+ size_t pattern_len;
+ int n;
+ rewrite_rule *rule = p->conf.rewrite->ptr[i];
+# define N 10
+ int ovec[N * 3];
+
+ match = rule->key;
+ pattern = rule->value->ptr;
+ pattern_len = rule->value->used - 1;
+
+ if ((n = pcre_exec(match, NULL, p->match_buf->ptr, p->match_buf->used - 1, 0, 0, ovec, 3 * N)) < 0) {
+ if (n != PCRE_ERROR_NOMATCH) {
+ log_error_write(srv, __FILE__, __LINE__, "sd",
+ "execution error while matching: ", n);
+ return HANDLER_ERROR;
+ }
+ } else {
+ const char **list;
+ size_t start, end;
+ size_t k;
+
+ /* it matched */
+ pcre_get_substring_list(p->match_buf->ptr, ovec, n, &list);
+
+ /* search for $[0-9] */
+
+ buffer_reset(con->request.uri);
+
+ start = 0; end = pattern_len;
+ for (k = 0; k < pattern_len; k++) {
+ if ((pattern[k] == '$' || pattern[k] == '%') &&
+ isdigit((unsigned char)pattern[k + 1])) {
+ /* got one */
+
+ size_t num = pattern[k + 1] - '0';
+
+ end = k;
+
+ buffer_append_string_len(con->request.uri, pattern + start, end - start);
+
+ if (pattern[k] == '$') {
+ /* n is always > 0 */
+ if (num < (size_t)n) {
+ buffer_append_string(con->request.uri, list[num]);
+ }
+ } else if (p->conf.context == NULL) {
+ /* we have no context, we are global */
+ log_error_write(srv, __FILE__, __LINE__, "sb",
+ "used a redirect containing a %[0-9]+ in the global scope, ignored:",
+ rule->value);
+
+ } else {
+ config_append_cond_match_buffer(con, p->conf.context, con->request.uri, num);
+ }
+
+ k++;
+ start = k + 1;
+ }
+ }
+
+ buffer_append_string_len(con->request.uri, pattern + start, pattern_len - start);
+
+ pcre_free(list);
+
+ hctx = handler_ctx_init();
+
+ con->plugin_ctx[p->id] = hctx;
+
+ if (rule->once) hctx->state = REWRITE_STATE_FINISHED;
+
+ return HANDLER_COMEBACK;
+ }
+ }
+#undef N
+
+#else
+ UNUSED(srv);
+ UNUSED(con);
+ UNUSED(p_d);
+#endif
+
+ return HANDLER_GO_ON;
+}
+
+int mod_rewrite_plugin_init(plugin *p) {
+ p->version = LIGHTTPD_VERSION_ID;
+ p->name = buffer_init_string("rewrite");
+
+ p->init = mod_rewrite_init;
+ /* it has to stay _raw as we are matching on uri + querystring
+ */
+
+ p->handle_uri_raw = mod_rewrite_uri_handler;
+ p->set_defaults = mod_rewrite_set_defaults;
+ p->cleanup = mod_rewrite_free;
+ p->connection_reset = mod_rewrite_con_reset;
+
+ p->data = NULL;
+
+ return 0;
+}
Added: webservices/axis2/branches/c/lighttpd_mod_axis2/lighttpd/src/mod_rrdtool.c
URL: http://svn.apache.org/viewvc/webservices/axis2/branches/c/lighttpd_mod_axis2/lighttpd/src/mod_rrdtool.c?rev=678637&view=auto
==============================================================================
--- webservices/axis2/branches/c/lighttpd_mod_axis2/lighttpd/src/mod_rrdtool.c (added)
+++ webservices/axis2/branches/c/lighttpd_mod_axis2/lighttpd/src/mod_rrdtool.c Mon Jul 21 21:35:35 2008
@@ -0,0 +1,449 @@
+#define _GNU_SOURCE
+#include <sys/types.h>
+
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <time.h>
+
+#include "server.h"
+#include "connections.h"
+#include "response.h"
+#include "connections.h"
+#include "log.h"
+
+#include "plugin.h"
+#ifdef HAVE_FORK
+/* no need for waitpid if we don't have fork */
+#include <sys/wait.h>
+#endif
+typedef struct {
+ buffer *path_rrdtool_bin;
+ buffer *path_rrd;
+
+ double requests, *requests_ptr;
+ double bytes_written, *bytes_written_ptr;
+ double bytes_read, *bytes_read_ptr;
+} plugin_config;
+
+typedef struct {
+ PLUGIN_DATA;
+
+ buffer *cmd;
+ buffer *resp;
+
+ int read_fd, write_fd;
+ pid_t rrdtool_pid;
+
+ int rrdtool_running;
+
+ plugin_config **config_storage;
+ plugin_config conf;
+} plugin_data;
+
+INIT_FUNC(mod_rrd_init) {
+ plugin_data *p;
+
+ p = calloc(1, sizeof(*p));
+
+ p->resp = buffer_init();
+ p->cmd = buffer_init();
+
+ return p;
+}
+
+FREE_FUNC(mod_rrd_free) {
+ plugin_data *p = p_d;
+ size_t i;
+
+ if (!p) return HANDLER_GO_ON;
+
+ if (p->config_storage) {
+ for (i = 0; i < srv->config_context->used; i++) {
+ plugin_config *s = p->config_storage[i];
+
+ buffer_free(s->path_rrdtool_bin);
+ buffer_free(s->path_rrd);
+
+ free(s);
+ }
+ }
+ buffer_free(p->cmd);
+ buffer_free(p->resp);
+
+ free(p->config_storage);
+
+ if (p->rrdtool_pid) {
+ int status;
+ close(p->read_fd);
+ close(p->write_fd);
+#ifdef HAVE_FORK
+ /* collect status */
+ waitpid(p->rrdtool_pid, &status, 0);
+#endif
+ }
+
+ free(p);
+
+ return HANDLER_GO_ON;
+}
+
+int mod_rrd_create_pipe(server *srv, plugin_data *p) {
+ pid_t pid;
+
+ int to_rrdtool_fds[2];
+ int from_rrdtool_fds[2];
+#ifdef HAVE_FORK
+ if (pipe(to_rrdtool_fds)) {
+ log_error_write(srv, __FILE__, __LINE__, "ss",
+ "pipe failed: ", strerror(errno));
+ return -1;
+ }
+
+ if (pipe(from_rrdtool_fds)) {
+ log_error_write(srv, __FILE__, __LINE__, "ss",
+ "pipe failed: ", strerror(errno));
+ return -1;
+ }
+
+ /* fork, execve */
+ switch (pid = fork()) {
+ case 0: {
+ /* child */
+ char **args;
+ int argc;
+ int i = 0;
+ char *dash = "-";
+
+ /* move stdout to from_rrdtool_fd[1] */
+ close(STDOUT_FILENO);
+ dup2(from_rrdtool_fds[1], STDOUT_FILENO);
+ close(from_rrdtool_fds[1]);
+ /* not needed */
+ close(from_rrdtool_fds[0]);
+
+ /* move the stdin to to_rrdtool_fd[0] */
+ close(STDIN_FILENO);
+ dup2(to_rrdtool_fds[0], STDIN_FILENO);
+ close(to_rrdtool_fds[0]);
+ /* not needed */
+ close(to_rrdtool_fds[1]);
+
+ close(STDERR_FILENO);
+
+ if (srv->errorlog_mode == ERRORLOG_FILE) {
+ dup2(srv->errorlog_fd, STDERR_FILENO);
+ close(srv->errorlog_fd);
+ }
+
+ /* set up args */
+ argc = 3;
+ args = malloc(sizeof(*args) * argc);
+ i = 0;
+
+ args[i++] = p->conf.path_rrdtool_bin->ptr;
+ args[i++] = dash;
+ args[i++] = NULL;
+
+ /* we don't need the client socket */
+ for (i = 3; i < 256; i++) {
+ close(i);
+ }
+
+ /* exec the cgi */
+ execv(args[0], args);
+
+ log_error_write(srv, __FILE__, __LINE__, "sss", "spawing rrdtool failed: ", strerror(errno), args[0]);
+
+ /* */
+ SEGFAULT();
+ break;
+ }
+ case -1:
+ /* error */
+ log_error_write(srv, __FILE__, __LINE__, "ss", "fork failed: ", strerror(errno));
+ break;
+ default: {
+ /* father */
+
+ close(from_rrdtool_fds[1]);
+ close(to_rrdtool_fds[0]);
+
+ /* register PID and wait for them asyncronously */
+ p->write_fd = to_rrdtool_fds[1];
+ p->read_fd = from_rrdtool_fds[0];
+ p->rrdtool_pid = pid;
+
+ break;
+ }
+ }
+
+ return 0;
+#else
+ return -1;
+#endif
+}
+
+static int mod_rrdtool_create_rrd(server *srv, plugin_data *p, plugin_config *s) {
+ struct stat st;
+
+ /* check if DB already exists */
+ if (0 == stat(s->path_rrd->ptr, &st)) {
+ /* check if it is plain file */
+ if (!S_ISREG(st.st_mode)) {
+ log_error_write(srv, __FILE__, __LINE__, "sb",
+ "not a regular file:", s->path_rrd);
+ return HANDLER_ERROR;
+ }
+ } else {
+ int r ;
+ /* create a new one */
+
+ BUFFER_COPY_STRING_CONST(p->cmd, "create ");
+ buffer_append_string_buffer(p->cmd, s->path_rrd);
+ buffer_append_string(p->cmd, " --step 60 ");
+ buffer_append_string(p->cmd, "DS:InOctets:ABSOLUTE:600:U:U ");
+ buffer_append_string(p->cmd, "DS:OutOctets:ABSOLUTE:600:U:U ");
+ buffer_append_string(p->cmd, "DS:Requests:ABSOLUTE:600:U:U ");
+ buffer_append_string(p->cmd, "RRA:AVERAGE:0.5:1:600 ");
+ buffer_append_string(p->cmd, "RRA:AVERAGE:0.5:6:700 ");
+ buffer_append_string(p->cmd, "RRA:AVERAGE:0.5:24:775 ");
+ buffer_append_string(p->cmd, "RRA:AVERAGE:0.5:288:797 ");
+ buffer_append_string(p->cmd, "RRA:MAX:0.5:1:600 ");
+ buffer_append_string(p->cmd, "RRA:MAX:0.5:6:700 ");
+ buffer_append_string(p->cmd, "RRA:MAX:0.5:24:775 ");
+ buffer_append_string(p->cmd, "RRA:MAX:0.5:288:797 ");
+ buffer_append_string(p->cmd, "RRA:MIN:0.5:1:600 ");
+ buffer_append_string(p->cmd, "RRA:MIN:0.5:6:700 ");
+ buffer_append_string(p->cmd, "RRA:MIN:0.5:24:775 ");
+ buffer_append_string(p->cmd, "RRA:MIN:0.5:288:797\n");
+
+ if (-1 == (r = write(p->write_fd, p->cmd->ptr, p->cmd->used - 1))) {
+ log_error_write(srv, __FILE__, __LINE__, "ss",
+ "rrdtool-write: failed", strerror(errno));
+
+ return HANDLER_ERROR;
+ }
+
+ buffer_prepare_copy(p->resp, 4096);
+ if (-1 == (r = read(p->read_fd, p->resp->ptr, p->resp->size))) {
+ log_error_write(srv, __FILE__, __LINE__, "ss",
+ "rrdtool-read: failed", strerror(errno));
+
+ return HANDLER_ERROR;
+ }
+
+ p->resp->used = r;
+
+ if (p->resp->ptr[0] != 'O' ||
+ p->resp->ptr[1] != 'K') {
+ log_error_write(srv, __FILE__, __LINE__, "sbb",
+ "rrdtool-response:", p->cmd, p->resp);
+
+ return HANDLER_ERROR;
+ }
+ }
+
+ return HANDLER_GO_ON;
+}
+
+#define PATCH(x) \
+ p->conf.x = s->x;
+static int mod_rrd_patch_connection(server *srv, connection *con, plugin_data *p) {
+ size_t i, j;
+ plugin_config *s = p->config_storage[0];
+
+ PATCH(path_rrdtool_bin);
+ PATCH(path_rrd);
+
+ p->conf.bytes_written_ptr = &(s->bytes_written);
+ p->conf.bytes_read_ptr = &(s->bytes_read);
+ p->conf.requests_ptr = &(s->requests);
+
+ /* skip the first, the global context */
+ for (i = 1; i < srv->config_context->used; i++) {
+ data_config *dc = (data_config *)srv->config_context->data[i];
+ s = p->config_storage[i];
+
+ /* condition didn't match */
+ if (!config_check_cond(srv, con, dc)) continue;
+
+ /* merge config */
+ for (j = 0; j < dc->value->used; j++) {
+ data_unset *du = dc->value->data[j];
+
+ if (buffer_is_equal_string(du->key, CONST_STR_LEN("rrdtool.db-name"))) {
+ PATCH(path_rrd);
+ /* get pointers to double values */
+
+ p->conf.bytes_written_ptr = &(s->bytes_written);
+ p->conf.bytes_read_ptr = &(s->bytes_read);
+ p->conf.requests_ptr = &(s->requests);
+ }
+ }
+ }
+
+ return 0;
+}
+#undef PATCH
+
+SETDEFAULTS_FUNC(mod_rrd_set_defaults) {
+ plugin_data *p = p_d;
+ size_t i;
+
+ config_values_t cv[] = {
+ { "rrdtool.binary", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER },
+ { "rrdtool.db-name", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },
+ { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
+ };
+
+ if (!p) return HANDLER_ERROR;
+
+ p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
+
+ for (i = 0; i < srv->config_context->used; i++) {
+ plugin_config *s;
+
+ s = calloc(1, sizeof(plugin_config));
+ s->path_rrdtool_bin = buffer_init();
+ s->path_rrd = buffer_init();
+ s->requests = 0;
+ s->bytes_written = 0;
+ s->bytes_read = 0;
+
+ cv[0].destination = s->path_rrdtool_bin;
+ cv[1].destination = s->path_rrd;
+
+ p->config_storage[i] = s;
+
+ if (0 != config_insert_values_global(srv, ((data_config *)srv->config_context->data[i])->value, cv)) {
+ return HANDLER_ERROR;
+ }
+
+ if (i > 0 && !buffer_is_empty(s->path_rrdtool_bin)) {
+ /* path_rrdtool_bin is a global option */
+
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "rrdtool.binary can only be set as a global option.");
+
+ return HANDLER_ERROR;
+ }
+
+ }
+
+ p->conf.path_rrdtool_bin = p->config_storage[0]->path_rrdtool_bin;
+ p->rrdtool_running = 0;
+
+ /* check for dir */
+
+ if (buffer_is_empty(p->conf.path_rrdtool_bin)) {
+ log_error_write(srv, __FILE__, __LINE__, "s",
+ "rrdtool.binary has to be set");
+ return HANDLER_ERROR;
+ }
+
+ /* open the pipe to rrdtool */
+ if (mod_rrd_create_pipe(srv, p)) {
+ return HANDLER_ERROR;
+ }
+
+ p->rrdtool_running = 1;
+
+ return HANDLER_GO_ON;
+}
+
+TRIGGER_FUNC(mod_rrd_trigger) {
+ plugin_data *p = p_d;
+ size_t i;
+
+ if (!p->rrdtool_running) return HANDLER_GO_ON;
+ if ((srv->cur_ts % 60) != 0) return HANDLER_GO_ON;
+
+ for (i = 0; i < srv->config_context->used; i++) {
+ plugin_config *s = p->config_storage[i];
+ int r;
+
+ if (buffer_is_empty(s->path_rrd)) continue;
+
+ /* write the data down every minute */
+
+ if (HANDLER_GO_ON != mod_rrdtool_create_rrd(srv, p, s)) return HANDLER_ERROR;
+
+ BUFFER_COPY_STRING_CONST(p->cmd, "update ");
+ buffer_append_string_buffer(p->cmd, s->path_rrd);
+ BUFFER_APPEND_STRING_CONST(p->cmd, " N:");
+ buffer_append_off_t(p->cmd, s->bytes_read);
+ BUFFER_APPEND_STRING_CONST(p->cmd, ":");
+ buffer_append_off_t(p->cmd, s->bytes_written);
+ BUFFER_APPEND_STRING_CONST(p->cmd, ":");
+ buffer_append_long(p->cmd, s->requests);
+ BUFFER_APPEND_STRING_CONST(p->cmd, "\n");
+
+ if (-1 == (r = write(p->write_fd, p->cmd->ptr, p->cmd->used - 1))) {
+ p->rrdtool_running = 0;
+
+ log_error_write(srv, __FILE__, __LINE__, "ss",
+ "rrdtool-write: failed", strerror(errno));
+
+ return HANDLER_ERROR;
+ }
+
+ buffer_prepare_copy(p->resp, 4096);
+ if (-1 == (r = read(p->read_fd, p->resp->ptr, p->resp->size))) {
+ p->rrdtool_running = 0;
+
+ log_error_write(srv, __FILE__, __LINE__, "ss",
+ "rrdtool-read: failed", strerror(errno));
+
+ return HANDLER_ERROR;
+ }
+
+ p->resp->used = r;
+
+ if (p->resp->ptr[0] != 'O' ||
+ p->resp->ptr[1] != 'K') {
+ p->rrdtool_running = 0;
+
+ log_error_write(srv, __FILE__, __LINE__, "sbb",
+ "rrdtool-response:", p->cmd, p->resp);
+
+ return HANDLER_ERROR;
+ }
+ s->requests = 0;
+ s->bytes_written = 0;
+ s->bytes_read = 0;
+ }
+
+ return HANDLER_GO_ON;
+}
+
+REQUESTDONE_FUNC(mod_rrd_account) {
+ plugin_data *p = p_d;
+
+ mod_rrd_patch_connection(srv, con, p);
+
+ *(p->conf.requests_ptr) += 1;
+ *(p->conf.bytes_written_ptr) += con->bytes_written;
+ *(p->conf.bytes_read_ptr) += con->bytes_read;
+
+ return HANDLER_GO_ON;
+}
+
+int mod_rrdtool_plugin_init(plugin *p) {
+ p->version = LIGHTTPD_VERSION_ID;
+ p->name = buffer_init_string("rrd");
+
+ p->init = mod_rrd_init;
+ p->cleanup = mod_rrd_free;
+ p->set_defaults= mod_rrd_set_defaults;
+
+ p->handle_trigger = mod_rrd_trigger;
+ p->handle_request_done = mod_rrd_account;
+
+ p->data = NULL;
+
+ return 0;
+}