[Freeswitch-trunk] [commit] r14026 - freeswitch/trunk/contrib/ledr/mod_xml_odbc
FreeSWITCH SVN
ledr at freeswitch.org
Mon Jun 29 04:53:30 PDT 2009
Author: ledr
Date: Mon Jun 29 06:53:29 2009
New Revision: 14026
Log:
broken, need backup, am fixing replace_header
Modified:
freeswitch/trunk/contrib/ledr/mod_xml_odbc/mod_xml_odbc.c
Modified: freeswitch/trunk/contrib/ledr/mod_xml_odbc/mod_xml_odbc.c
==============================================================================
--- freeswitch/trunk/contrib/ledr/mod_xml_odbc/mod_xml_odbc.c (original)
+++ freeswitch/trunk/contrib/ledr/mod_xml_odbc/mod_xml_odbc.c Mon Jun 29 06:53:29 2009
@@ -50,8 +50,67 @@
static switch_status_t xml_odbc_render_template(char *template, switch_event_t *params, switch_xml_t xml_out, int *off);
static switch_status_t xml_odbc_render_tag(switch_xml_t xml_in, switch_event_t *params, switch_xml_t xml_out, int *off);
+static char *my_dup(const char *s)
+{
+ size_t len = strlen(s) + 1;
+ void *new = malloc(len);
+ switch_assert(new);
+
+ return (char *) memcpy(new, s, len);
+}
+
+#ifndef ALLOC
+#define ALLOC(size) malloc(size)
+#endif
+#ifndef DUP
+#define DUP(str) my_dup(str)
+#endif
+#ifndef FREE
+#define FREE(ptr) switch_safe_free(ptr)
+#endif
+
+
#define XML_ODBC_SYNTAX "[debug_on|debug_off|render_template]"
+/* replace all headers in event with new_header_name AND/OR new_header_value, where header_name AND/OR header_value match */
+static switch_status_t switch_event_replace_header(switch_event_t *event, const char *header_name, const char *header_value, char *new_header_name, char *new_header_value)
+{
+ switch_event_header_t *hp;
+ switch_ssize_t hlen = -1, new_hlen = -1;
+ unsigned long hash = 0, new_hash = 0;
+ switch_status_t status = SWITCH_STATUS_FALSE;
+
+ switch_assert(event);
+
+switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "INSIDE switch_event_replace_header header_name[%s] header_value[%s] new_header_name[%s] new_header_value[%s]\n", header_name, header_value, new_header_name, new_header_value);
+
+ if (!header_name && !header_value) return status;
+ if (!new_header_name && !new_header_value) return status;
+
+ hash = switch_ci_hashfunc_default(header_name, &hlen);
+
+ for (hp = event->headers; hp; hp = hp->next) {
+ if ( (!hp->hash || hash == hp->hash) && !(header_name && strcasecmp(hp->name, header_name)) && !(header_value && strcasecmp(hp->value, header_value)) ) {
+
+ if (new_header_name) {
+ FREE(hp->name);
+ hp->name = DUP(new_header_name);
+ new_hash = switch_ci_hashfunc_default(new_header_name, &new_hlen);
+ hp->hash = new_hash;
+ }
+
+ if (new_header_value) {
+ FREE(hp->value);
+ hp->value = new_header_value;
+ }
+
+ status = SWITCH_STATUS_SUCCESS;
+ }
+ }
+ return status;
+}
+
+
static struct {
char *odbc_dsn;
char *configuration_template_name;
@@ -146,6 +205,10 @@
char *name = NULL;
char *value = NULL, *new_value = NULL;
+
+// char *when_key = NULL, *when_val = NULL;
+// char *to_key = NULL, *to_val = NULL;
+
char *empty_result_break_to = NULL;
char *no_template_break_to = NULL;
@@ -156,29 +219,57 @@
/* special case xml-odbc-do - this tag is not copied, but action is done */
if (!strcasecmp(xml_in->name, "xml-odbc-do")) {
name = (char *) switch_xml_attr_soft(xml_in, "name");
- value = (char *) switch_xml_attr_soft(xml_in, "value");
- empty_result_break_to = (char *) switch_xml_attr_soft(xml_in, "on-empty-result-break-to");
- no_template_break_to = (char *) switch_xml_attr_soft(xml_in, "on-no-template-break-to");
+
+ no_template_break_to = (char *) switch_xml_attr_soft(xml_in, "on-no-template-break-to"); // THIS DOESN'T WORK YET !!
if (switch_strlen_zero(name)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Ignoring xml-odbc-do because no name attribute is given\n");
goto done;
}
- if (switch_strlen_zero(value)) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Ignoring xml-odbc-do name=[%s] because no value attr is given\n", name);
- goto done;
- }
+ if (!strcasecmp(name, "replace_header")) {
+ char *when_key = (char *) switch_xml_attr(xml_in, "when-key");
+ char *when_val = (char *) switch_xml_attr(xml_in, "when-val");
+ char *to_key = (char *) switch_xml_attr(xml_in, "to-key");
+ char *to_val = (char *) switch_xml_attr(xml_in, "to-val");
- new_value = switch_event_expand_headers(params, value);
+ if (!when_key && !when_val) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Ignoring xml-odbc-do name=[%s] because no when-key AND no when-val are given\n", name);
+ goto done;
+ }
+
+ if (!to_key && !to_val) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Ignoring xml-odbc-do name=[%s] because no to-key AND no to-val are given\n", name);
+ goto done;
+ }
+
+ switch_event_replace_header(params, when_key, when_val, to_key, to_val);
+ //switch_event_replace_header(params, NULL, NULL, NULL, NULL);
+
+ } else if (!strcasecmp(name, "break-to")) {
+
+ value = (char *) switch_xml_attr_soft(xml_in, "value");
+ if (switch_strlen_zero(value)) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Ignoring xml-odbc-do name=[%s] because no value attr is given\n", name);
+ goto done;
+ }
+ new_value = switch_event_expand_headers(params, value);
- if (!strcasecmp(name, "break-to")) {
/* set a next_template header so xml_odbc_render_template breaks the loop and starts over with a new template */
switch_event_del_header(params, "next_template_name");
switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "next_template_name", value);
goto done;
} else if (!strcasecmp(name, "query")) {
+
+ value = (char *) switch_xml_attr_soft(xml_in, "value");
+ if (switch_strlen_zero(value)) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Ignoring xml-odbc-do name=[%s] because no value attr is given\n", name);
+ goto done;
+ }
+ new_value = switch_event_expand_headers(params, value);
+
+ /* create query_helper that is given to callback function on each returned row */
query_helper.xml_in = xml_in;
query_helper.xml_out = xml_out;
query_helper.off = off;
@@ -187,13 +278,15 @@
if (switch_odbc_handle_callback_exec(globals.master_odbc, new_value, xml_odbc_query_callback, &query_helper) != SWITCH_ODBC_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error running this query: [%s]\n", new_value);
- } else {
- if (!switch_strlen_zero(empty_result_break_to) && query_helper.rowcount == 0) {
- /* set a next_template header so xml_odbc_render_template breaks the loop and starts over with a new template */
- switch_event_del_header(params, "next_template_name");
- switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "next_template_name", empty_result_break_to);
- goto done;
- }
+ goto done;
+ }
+
+ empty_result_break_to = (char *) switch_xml_attr_soft(xml_in, "on-empty-result-break-to");
+ if (!switch_strlen_zero(empty_result_break_to) && query_helper.rowcount == 0) {
+ /* set a next_template header so xml_odbc_render_template breaks the loop and starts over with a new template */
+ switch_event_del_header(params, "next_template_name");
+ switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "next_template_name", empty_result_break_to);
+ goto done;
}
} else {
More information about the Freeswitch-trunk
mailing list