From d7cf0bbed0994ae480fdb10c4fe18166ed932792 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 9 May 2012 14:06:00 -0500 Subject: [PATCH] add mod_xml_scgi (like xml_curl minus the web server direct to an application backend that supports the SCGI protocol) --- src/mod/xml_int/mod_xml_scgi/Makefile | 5 + .../conf/autoload_configs/xml_scgi.conf.xml | 12 + .../mod_xml_scgi/mod_xml_scgi.2008.vcproj | 287 +++++++++++++++ .../mod_xml_scgi/mod_xml_scgi.2010.vcxproj | 131 +++++++ src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.c | 340 ++++++++++++++++++ .../xml_int/mod_xml_scgi/xml_scgi_server.pl | 93 +++++ 6 files changed, 868 insertions(+) create mode 100644 src/mod/xml_int/mod_xml_scgi/Makefile create mode 100644 src/mod/xml_int/mod_xml_scgi/conf/autoload_configs/xml_scgi.conf.xml create mode 100644 src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.2008.vcproj create mode 100644 src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.2010.vcxproj create mode 100644 src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.c create mode 100644 src/mod/xml_int/mod_xml_scgi/xml_scgi_server.pl diff --git a/src/mod/xml_int/mod_xml_scgi/Makefile b/src/mod/xml_int/mod_xml_scgi/Makefile new file mode 100644 index 0000000000..2c7a91d26d --- /dev/null +++ b/src/mod/xml_int/mod_xml_scgi/Makefile @@ -0,0 +1,5 @@ +BASE=../../../.. +LOCAL_SOURCES = $(BASE)/libs/libscgi/src/scgi.c +LOCAL_OBJS = $(BASE)/libs/libscgi/src/scgi.o +LOCAL_CFLAGS = -I. -I$(BASE)/libs/libscgi/src/include +include $(BASE)/build/modmake.rules diff --git a/src/mod/xml_int/mod_xml_scgi/conf/autoload_configs/xml_scgi.conf.xml b/src/mod/xml_int/mod_xml_scgi/conf/autoload_configs/xml_scgi.conf.xml new file mode 100644 index 0000000000..b9662d1638 --- /dev/null +++ b/src/mod/xml_int/mod_xml_scgi/conf/autoload_configs/xml_scgi.conf.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.2008.vcproj b/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.2008.vcproj new file mode 100644 index 0000000000..7718059dd5 --- /dev/null +++ b/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.2008.vcproj @@ -0,0 +1,287 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.2010.vcxproj b/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.2010.vcxproj new file mode 100644 index 0000000000..cde0e239e9 --- /dev/null +++ b/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.2010.vcxproj @@ -0,0 +1,131 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + mod_xml_scgi + {11C9BC3D-45E9-46E3-BE84-B8CEE4685E39} + mod_xml_scgi + Win32Proj + + + + DynamicLibrary + MultiByte + + + DynamicLibrary + MultiByte + + + DynamicLibrary + MultiByte + + + DynamicLibrary + MultiByte + + + + + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + + + + + + + + false + + + + + + + X64 + + + + + + + false + + + MachineX64 + + + + + + + + + false + + + + + + + X64 + + + + + + + false + + + MachineX64 + + + + + + + + {202d7a4e-760d-4d0e-afa1-d7459ced30ff} + false + + + + + + diff --git a/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.c b/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.c new file mode 100644 index 0000000000..687ec3aeb9 --- /dev/null +++ b/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.c @@ -0,0 +1,340 @@ +/* + * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2005-2012, Anthony Minessale II + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Anthony Minessale II + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Anthony Minessale II + * + * mod_xml_scgi.c -- SCGI XML Gateway + * + */ +#include +#include + + +SWITCH_MODULE_LOAD_FUNCTION(mod_xml_scgi_load); +SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_scgi_shutdown); +SWITCH_MODULE_DEFINITION(mod_xml_scgi, mod_xml_scgi_load, mod_xml_scgi_shutdown, NULL); + + +struct xml_binding { + char *host; + switch_port_t port; + char *url; + + int timeout; + switch_hash_t *vars_map; + char *bindings; + +}; + +static int GLOBAL_DEBUG = 0; + +typedef struct xml_binding xml_binding_t; + +#define XML_SCGI_MAX_BYTES 1024 * 1024 + +typedef struct hash_node { + switch_hash_t *hash; + struct hash_node *next; +} hash_node_t; + +static struct { + switch_memory_pool_t *pool; + hash_node_t *hash_root; + hash_node_t *hash_tail; +} globals; + +#define XML_SCGI_SYNTAX "[debug_on|debug_off]" +SWITCH_STANDARD_API(xml_scgi_function) +{ + if (session) { + return SWITCH_STATUS_FALSE; + } + + if (zstr(cmd)) { + goto usage; + } + + if (!strcasecmp(cmd, "debug_on")) { + GLOBAL_DEBUG = 1; + } else if (!strcasecmp(cmd, "debug_off")) { + GLOBAL_DEBUG = 0; + } else { + goto usage; + } + + stream->write_function(stream, "OK\n"); + return SWITCH_STATUS_SUCCESS; + + usage: + stream->write_function(stream, "USAGE: %s\n", XML_SCGI_SYNTAX); + return SWITCH_STATUS_SUCCESS; +} + + +static switch_xml_t xml_url_fetch(const char *section, const char *tag_name, const char *key_name, const char *key_value, switch_event_t *params, + void *user_data) +{ + switch_xml_t xml = NULL; + char *data = NULL; + xml_binding_t *binding = (xml_binding_t *) user_data; + char hostname[256] = ""; + char basic_data[512]; + unsigned char buf[16336] = ""; + ssize_t len = -1, bytes = 0; + scgi_handle_t handle = { 0 }; + switch_stream_handle_t stream = { 0 }; + char *txt = NULL; + + strncpy(hostname, switch_core_get_switchname(), sizeof(hostname)); + + if (!binding) { + return NULL; + } + + switch_snprintf(basic_data, sizeof(basic_data), "hostname=%s§ion=%s&tag_name=%s&key_name=%s&key_value=%s", + hostname, section, switch_str_nil(tag_name), switch_str_nil(key_name), switch_str_nil(key_value)); + + data = switch_event_build_param_string(params, basic_data, binding->vars_map); + switch_assert(data); + + scgi_add_param(&handle, "REQUEST_METHOD", "POST"); + scgi_add_param(&handle, "REQUEST_URI", binding->url); + scgi_add_body(&handle, data); + + if (scgi_connect(&handle, binding->host, binding->port, binding->timeout * 1000) == SCGI_SUCCESS) { + scgi_send_request(&handle); + + SWITCH_STANDARD_STREAM(stream); + txt = (char *) stream.data; + + while((len = scgi_recv(&handle, buf, sizeof(buf))) > 0) { + + bytes += len; + + if (bytes > XML_SCGI_MAX_BYTES) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Data too big!\n"); + len = -1; + break; + } + + stream.write_function(&stream, "%s", buf); + } + + scgi_disconnect(&handle); + } + + if (GLOBAL_DEBUG) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DEBUG:\nURL: %s\nPOST_DATA:\n%s\n\nRESPONSE:\n%s\n\n", binding->url, data, txt); + } + + if (!len) { + if (!(xml = switch_xml_parse_str(txt, strlen(txt)))) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Parsing Result! [%s]\ndata: [%s]\n", binding->url, data); + } + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Received error trying to fetch %s\ndata: [%s]\n", binding->url, data); + switch_safe_free(stream.data); + xml = NULL; + } + + switch_safe_free(data); + + + return xml; +} + +#define ENABLE_PARAM_VALUE "enabled" +static switch_status_t do_config(void) +{ + char *cf = "xml_scgi.conf"; + switch_xml_t cfg, xml, bindings_tag, binding_tag, param; + xml_binding_t *binding = NULL; + int x = 0; + int need_vars_map = 0; + switch_hash_t *vars_map = NULL; + + if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf); + return SWITCH_STATUS_TERM; + } + + if (!(bindings_tag = switch_xml_child(cfg, "bindings"))) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing tag!\n"); + goto done; + } + + for (binding_tag = switch_xml_child(bindings_tag, "binding"); binding_tag; binding_tag = binding_tag->next) { + char *bname = (char *) switch_xml_attr_soft(binding_tag, "name"); + char *host = "127.0.0.1"; + char *port = "8080"; + char *bind_mask = NULL; + int timeout = 0; + + hash_node_t *hash_node; + need_vars_map = 0; + vars_map = NULL; + + for (param = switch_xml_child(binding_tag, "param"); param; param = param->next) { + char *var = (char *) switch_xml_attr_soft(param, "name"); + char *val = (char *) switch_xml_attr_soft(param, "value"); + + if (!strcasecmp(var, "host")) { + bind_mask = (char *) switch_xml_attr_soft(param, "bindings"); + if (val) { + host = val; + } + } else if (!strcasecmp(var, "port")) { + port = val; + } else if (!strcasecmp(var, "timeout")) { + int tmp = atoi(val); + if (tmp >= 0) { + timeout = tmp; + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't set a negative timeout!\n"); + } + } else if (!strcasecmp(var, "enable-post-var")) { + if (!vars_map && need_vars_map == 0) { + if (switch_core_hash_init(&vars_map, globals.pool) != SWITCH_STATUS_SUCCESS) { + need_vars_map = -1; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Can't init params hash!\n"); + continue; + } + need_vars_map = 1; + } + + if (vars_map && val) { + if (switch_core_hash_insert(vars_map, val, ENABLE_PARAM_VALUE) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Can't add %s to params hash!\n", val); + } + } + } + } + + if (!host) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Binding has no host!\n"); + if (vars_map) { + switch_core_hash_destroy(&vars_map); + } + continue; + } + + if (!(binding = switch_core_alloc(globals.pool, sizeof(*binding)))) { + if (vars_map) { + switch_core_hash_destroy(&vars_map); + } + goto done; + } + memset(binding, 0, sizeof(*binding)); + + binding->timeout = timeout; + binding->host = switch_core_strdup(globals.pool, host); + binding->port = atoi(port); + binding->vars_map = vars_map; + binding->url = switch_mprintf("scgi://%s:%s", host, port); + + if (bind_mask) { + binding->bindings = switch_core_strdup(globals.pool, bind_mask); + } + + if (vars_map) { + switch_zmalloc(hash_node, sizeof(hash_node_t)); + hash_node->hash = vars_map; + hash_node->next = NULL; + + if (!globals.hash_root) { + globals.hash_root = hash_node; + globals.hash_tail = globals.hash_root; + } + + else { + globals.hash_tail->next = hash_node; + globals.hash_tail = globals.hash_tail->next; + } + + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Binding [%s] XML Fetch Function [%s] [%s]\n", + zstr(bname) ? "N/A" : bname, binding->url, binding->bindings ? binding->bindings : "all"); + switch_xml_bind_search_function(xml_url_fetch, switch_xml_parse_section_string(binding->bindings), binding); + x++; + binding = NULL; + } + + done: + switch_xml_free(xml); + + return x ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE; +} + +SWITCH_MODULE_LOAD_FUNCTION(mod_xml_scgi_load) +{ + switch_api_interface_t *xml_scgi_api_interface; + + /* connect my internal structure to the blank pointer passed to me */ + *module_interface = switch_loadable_module_create_module_interface(pool, modname); + + memset(&globals, 0, sizeof(globals)); + globals.pool = pool; + globals.hash_root = NULL; + globals.hash_tail = NULL; + + if (do_config() != SWITCH_STATUS_SUCCESS) { + return SWITCH_STATUS_FALSE; + } + + SWITCH_ADD_API(xml_scgi_api_interface, "xml_scgi", "XML SCGI", xml_scgi_function, XML_SCGI_SYNTAX); + switch_console_set_complete("add xml_scgi debug_on"); + switch_console_set_complete("add xml_scgi debug_off"); + + /* indicate that the module should continue to be loaded */ + return SWITCH_STATUS_SUCCESS; +} + +SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_scgi_shutdown) +{ + hash_node_t *ptr = NULL; + + while (globals.hash_root) { + ptr = globals.hash_root; + switch_core_hash_destroy(&ptr->hash); + globals.hash_root = ptr->next; + switch_safe_free(ptr); + } + + switch_xml_unbind_search_function_ptr(xml_url_fetch); + + return SWITCH_STATUS_SUCCESS; +} + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4: + */ diff --git a/src/mod/xml_int/mod_xml_scgi/xml_scgi_server.pl b/src/mod/xml_int/mod_xml_scgi/xml_scgi_server.pl new file mode 100644 index 0000000000..bf56c9c0c1 --- /dev/null +++ b/src/mod/xml_int/mod_xml_scgi/xml_scgi_server.pl @@ -0,0 +1,93 @@ +# +# Copyright (c) 2012-2013, Anthony Minessale II +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# * Neither the name of the original author; nor the names of any contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER +# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +### EXAMPLE SERVER SIDE FOR mod_xml_SCGI +### You will need the SCGI module from CPAN + +use SCGI; +use CGI; +use IO::Socket; +use Data::Dumper; + +my $socket = IO::Socket::INET->new(Listen => 5, ReuseAddr => 1, LocalPort => 8080) + or die "cannot bind to port 8080: $!"; + +my $scgi = SCGI->new($socket, blocking => 1); + + +my $xml = qq# + +
+ + + + + + + + + +
+
+#; + + +while (my $request = $scgi->accept) { + + # fork every new req into its own process (optional) + next unless(fork()); + my $handle = $request->connection; + + $request->read_env; + + # get the body that contains the PARAMS + read $handle, $body, $request->env->{CONTENT_LENGTH}; + + # Dump SCGI HEADERS + print Dumper $request->env; + + # Create a CGI parser on the PARAMS + my $cgi = CGI->new($body); + my %params = $cgi->Vars(); + # might be big output + print Dumper \%params; + + + ### DO something CGI-like here with %params ... I'm just going to return static xml + + # header is not necessary but optional + #print $handle "Content-Type: text/xml\n\n"; + + print $handle $xml; + +}