]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add mod_xml_scgi (like xml_curl minus the web server direct to an application backend...
authorAnthony Minessale <anthm@freeswitch.org>
Wed, 9 May 2012 19:06:00 +0000 (14:06 -0500)
committerAnthony Minessale <anthm@freeswitch.org>
Wed, 9 May 2012 19:06:00 +0000 (14:06 -0500)
src/mod/xml_int/mod_xml_scgi/Makefile [new file with mode: 0644]
src/mod/xml_int/mod_xml_scgi/conf/autoload_configs/xml_scgi.conf.xml [new file with mode: 0644]
src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.2008.vcproj [new file with mode: 0644]
src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.2010.vcxproj [new file with mode: 0644]
src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.c [new file with mode: 0644]
src/mod/xml_int/mod_xml_scgi/xml_scgi_server.pl [new file with mode: 0644]

diff --git a/src/mod/xml_int/mod_xml_scgi/Makefile b/src/mod/xml_int/mod_xml_scgi/Makefile
new file mode 100644 (file)
index 0000000..2c7a91d
--- /dev/null
@@ -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 (file)
index 0000000..b9662d1
--- /dev/null
@@ -0,0 +1,12 @@
+<configuration name="xml_scgi.conf" description="SCGI XML Gateway">
+  <bindings>
+    <binding name="example">
+      <!-- one or more |-delim of configuration|directory|dialplan -->
+      <!-- <param name="host" value="127.0.0.1" bindings="dialplan"/> -->
+      <!-- <param name="port" value="8080"/> -->
+      <!-- <param name="timeout" value="10"/> -->
+      <!-- one or more of these imply you want to pick the exact variables that are transmitted -->
+      <!--<param name="enable-post-var" value="Unique-ID"/>-->
+    </binding>
+  </bindings>
+</configuration>
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 (file)
index 0000000..7718059
--- /dev/null
@@ -0,0 +1,287 @@
+<?xml version="1.0" encoding="Windows-1252"?>\r
+<VisualStudioProject\r
+       ProjectType="Visual C++"\r
+       Version="9.00"\r
+       Name="mod_xml_scgi"\r
+       ProjectGUID="{11C9BC3D-45E9-46E3-BE84-B8CEE4685E39}"\r
+       RootNamespace="mod_xml_scgi"\r
+       Keyword="Win32Proj"\r
+       TargetFrameworkVersion="131072"\r
+       >\r
+       <Platforms>\r
+               <Platform\r
+                       Name="Win32"\r
+               />\r
+               <Platform\r
+                       Name="x64"\r
+               />\r
+       </Platforms>\r
+       <ToolFiles>\r
+       </ToolFiles>\r
+       <Configurations>\r
+               <Configuration\r
+                       Name="Debug|Win32"\r
+                       ConfigurationType="2"\r
+                       InheritedPropertySheets="..\..\..\..\w32\module_debug.vsprops"\r
+                       CharacterSet="2"\r
+                       >\r
+                       <Tool\r
+                               Name="VCPreBuildEventTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCCustomBuildTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCXMLDataGeneratorTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCWebServiceProxyGeneratorTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCMIDLTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCCLCompilerTool"\r
+                               UsePrecompiledHeader="0"\r
+                       />\r
+                       <Tool\r
+                               Name="VCManagedResourceCompilerTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCResourceCompilerTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCPreLinkEventTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCLinkerTool"\r
+                               RandomizedBaseAddress="1"\r
+                               DataExecutionPrevention="0"\r
+                       />\r
+                       <Tool\r
+                               Name="VCALinkTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCManifestTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCXDCMakeTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCBscMakeTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCFxCopTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCAppVerifierTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCPostBuildEventTool"\r
+                       />\r
+               </Configuration>\r
+               <Configuration\r
+                       Name="Debug|x64"\r
+                       OutputDirectory="$(PlatformName)\$(ConfigurationName)"\r
+                       IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"\r
+                       ConfigurationType="2"\r
+                       InheritedPropertySheets="..\..\..\..\w32\module_debug.vsprops"\r
+                       CharacterSet="2"\r
+                       >\r
+                       <Tool\r
+                               Name="VCPreBuildEventTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCCustomBuildTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCXMLDataGeneratorTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCWebServiceProxyGeneratorTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCMIDLTool"\r
+                               TargetEnvironment="3"\r
+                       />\r
+                       <Tool\r
+                               Name="VCCLCompilerTool"\r
+                               UsePrecompiledHeader="0"\r
+                       />\r
+                       <Tool\r
+                               Name="VCManagedResourceCompilerTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCResourceCompilerTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCPreLinkEventTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCLinkerTool"\r
+                               OutputFile="$(SolutionDir)$(PlatformName)\$(ConfigurationName)/mod/$(ProjectName).dll"\r
+                               RandomizedBaseAddress="1"\r
+                               DataExecutionPrevention="0"\r
+                               TargetMachine="17"\r
+                       />\r
+                       <Tool\r
+                               Name="VCALinkTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCManifestTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCXDCMakeTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCBscMakeTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCFxCopTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCAppVerifierTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCPostBuildEventTool"\r
+                       />\r
+               </Configuration>\r
+               <Configuration\r
+                       Name="Release|Win32"\r
+                       ConfigurationType="2"\r
+                       InheritedPropertySheets="..\..\..\..\w32\module_release.vsprops"\r
+                       CharacterSet="2"\r
+                       >\r
+                       <Tool\r
+                               Name="VCPreBuildEventTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCCustomBuildTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCXMLDataGeneratorTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCWebServiceProxyGeneratorTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCMIDLTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCCLCompilerTool"\r
+                               UsePrecompiledHeader="0"\r
+                       />\r
+                       <Tool\r
+                               Name="VCManagedResourceCompilerTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCResourceCompilerTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCPreLinkEventTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCLinkerTool"\r
+                               RandomizedBaseAddress="1"\r
+                               DataExecutionPrevention="0"\r
+                       />\r
+                       <Tool\r
+                               Name="VCALinkTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCManifestTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCXDCMakeTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCBscMakeTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCFxCopTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCAppVerifierTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCPostBuildEventTool"\r
+                       />\r
+               </Configuration>\r
+               <Configuration\r
+                       Name="Release|x64"\r
+                       OutputDirectory="$(PlatformName)\$(ConfigurationName)"\r
+                       IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"\r
+                       ConfigurationType="2"\r
+                       InheritedPropertySheets="..\..\..\..\w32\module_release.vsprops"\r
+                       CharacterSet="2"\r
+                       >\r
+                       <Tool\r
+                               Name="VCPreBuildEventTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCCustomBuildTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCXMLDataGeneratorTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCWebServiceProxyGeneratorTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCMIDLTool"\r
+                               TargetEnvironment="3"\r
+                       />\r
+                       <Tool\r
+                               Name="VCCLCompilerTool"\r
+                               UsePrecompiledHeader="0"\r
+                       />\r
+                       <Tool\r
+                               Name="VCManagedResourceCompilerTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCResourceCompilerTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCPreLinkEventTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCLinkerTool"\r
+                               OutputFile="$(SolutionDir)$(PlatformName)\$(ConfigurationName)/mod/$(ProjectName).dll"\r
+                               RandomizedBaseAddress="1"\r
+                               DataExecutionPrevention="0"\r
+                               TargetMachine="17"\r
+                       />\r
+                       <Tool\r
+                               Name="VCALinkTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCManifestTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCXDCMakeTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCBscMakeTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCFxCopTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCAppVerifierTool"\r
+                       />\r
+                       <Tool\r
+                               Name="VCPostBuildEventTool"\r
+                       />\r
+               </Configuration>\r
+       </Configurations>\r
+       <References>\r
+       </References>\r
+       <Files>\r
+               <File\r
+                       RelativePath=".\mod_xml_scgi.c"\r
+                       >\r
+               </File>\r
+       </Files>\r
+       <Globals>\r
+       </Globals>\r
+</VisualStudioProject>\r
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 (file)
index 0000000..cde0e23
--- /dev/null
@@ -0,0 +1,131 @@
+<?xml version="1.0" encoding="utf-8"?>\r
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">\r
+  <ItemGroup Label="ProjectConfigurations">\r
+    <ProjectConfiguration Include="Debug|Win32">\r
+      <Configuration>Debug</Configuration>\r
+      <Platform>Win32</Platform>\r
+    </ProjectConfiguration>\r
+    <ProjectConfiguration Include="Debug|x64">\r
+      <Configuration>Debug</Configuration>\r
+      <Platform>x64</Platform>\r
+    </ProjectConfiguration>\r
+    <ProjectConfiguration Include="Release|Win32">\r
+      <Configuration>Release</Configuration>\r
+      <Platform>Win32</Platform>\r
+    </ProjectConfiguration>\r
+    <ProjectConfiguration Include="Release|x64">\r
+      <Configuration>Release</Configuration>\r
+      <Platform>x64</Platform>\r
+    </ProjectConfiguration>\r
+  </ItemGroup>\r
+  <PropertyGroup Label="Globals">\r
+    <ProjectName>mod_xml_scgi</ProjectName>\r
+    <ProjectGuid>{11C9BC3D-45E9-46E3-BE84-B8CEE4685E39}</ProjectGuid>\r
+    <RootNamespace>mod_xml_scgi</RootNamespace>\r
+    <Keyword>Win32Proj</Keyword>\r
+  </PropertyGroup>\r
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />\r
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">\r
+    <ConfigurationType>DynamicLibrary</ConfigurationType>\r
+    <CharacterSet>MultiByte</CharacterSet>\r
+  </PropertyGroup>\r
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">\r
+    <ConfigurationType>DynamicLibrary</ConfigurationType>\r
+    <CharacterSet>MultiByte</CharacterSet>\r
+  </PropertyGroup>\r
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">\r
+    <ConfigurationType>DynamicLibrary</ConfigurationType>\r
+    <CharacterSet>MultiByte</CharacterSet>\r
+  </PropertyGroup>\r
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">\r
+    <ConfigurationType>DynamicLibrary</ConfigurationType>\r
+    <CharacterSet>MultiByte</CharacterSet>\r
+  </PropertyGroup>\r
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />\r
+  <ImportGroup Label="ExtensionSettings">\r
+  </ImportGroup>\r
+  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets">\r
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />\r
+    <Import Project="..\..\..\..\w32\module_release.props" />\r
+  </ImportGroup>\r
+  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets">\r
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />\r
+    <Import Project="..\..\..\..\w32\module_debug.props" />\r
+  </ImportGroup>\r
+  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">\r
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />\r
+    <Import Project="..\..\..\..\w32\module_release.props" />\r
+  </ImportGroup>\r
+  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">\r
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />\r
+    <Import Project="..\..\..\..\w32\module_debug.props" />\r
+  </ImportGroup>\r
+  <PropertyGroup Label="UserMacros" />\r
+  <PropertyGroup>\r
+    <_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion>\r
+  </PropertyGroup>\r
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">\r
+    <ClCompile>\r
+      <PrecompiledHeader>\r
+      </PrecompiledHeader>\r
+    </ClCompile>\r
+    <Link>\r
+      <RandomizedBaseAddress>false</RandomizedBaseAddress>\r
+      <DataExecutionPrevention>\r
+      </DataExecutionPrevention>\r
+    </Link>\r
+  </ItemDefinitionGroup>\r
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">\r
+    <Midl>\r
+      <TargetEnvironment>X64</TargetEnvironment>\r
+    </Midl>\r
+    <ClCompile>\r
+      <PrecompiledHeader>\r
+      </PrecompiledHeader>\r
+    </ClCompile>\r
+    <Link>\r
+      <RandomizedBaseAddress>false</RandomizedBaseAddress>\r
+      <DataExecutionPrevention>\r
+      </DataExecutionPrevention>\r
+      <TargetMachine>MachineX64</TargetMachine>\r
+    </Link>\r
+  </ItemDefinitionGroup>\r
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">\r
+    <ClCompile>\r
+      <PrecompiledHeader>\r
+      </PrecompiledHeader>\r
+    </ClCompile>\r
+    <Link>\r
+      <RandomizedBaseAddress>false</RandomizedBaseAddress>\r
+      <DataExecutionPrevention>\r
+      </DataExecutionPrevention>\r
+    </Link>\r
+  </ItemDefinitionGroup>\r
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">\r
+    <Midl>\r
+      <TargetEnvironment>X64</TargetEnvironment>\r
+    </Midl>\r
+    <ClCompile>\r
+      <PrecompiledHeader>\r
+      </PrecompiledHeader>\r
+    </ClCompile>\r
+    <Link>\r
+      <RandomizedBaseAddress>false</RandomizedBaseAddress>\r
+      <DataExecutionPrevention>\r
+      </DataExecutionPrevention>\r
+      <TargetMachine>MachineX64</TargetMachine>\r
+    </Link>\r
+  </ItemDefinitionGroup>\r
+  <ItemGroup>\r
+    <ClCompile Include="mod_xml_scgi.c" />\r
+  </ItemGroup>\r
+  <ItemGroup>\r
+    <ProjectReference Include="..\..\..\..\w32\Library\FreeSwitchCore.2010.vcxproj">\r
+      <Project>{202d7a4e-760d-4d0e-afa1-d7459ced30ff}</Project>\r
+      <ReferenceOutputAssembly>false</ReferenceOutputAssembly>\r
+    </ProjectReference>\r
+  </ItemGroup>\r
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />\r
+  <ImportGroup Label="ExtensionTargets">\r
+  </ImportGroup>\r
+</Project>\r
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 (file)
index 0000000..687ec3a
--- /dev/null
@@ -0,0 +1,340 @@
+/* 
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ * Copyright (C) 2005-2012, Anthony Minessale II <anthm@freeswitch.org>
+ *
+ * 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 <anthm@freeswitch.org>
+ * Portions created by the Initial Developer are Copyright (C)
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * 
+ * Anthony Minessale II <anthm@freeswitch.org>
+ *
+ * mod_xml_scgi.c -- SCGI XML Gateway
+ *
+ */
+#include <switch.h>
+#include <scgi.h>
+
+
+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&section=%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 <bindings> 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 (file)
index 0000000..bf56c9c
--- /dev/null
@@ -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#
+<document type="freeswitch/xml">
+  <section name="dialplan">
+   <context name="default">
+     <extension name="foo">
+       <condition>
+         <action application="answer"/>
+         <action application="playback" data="/ram/swimp.raw"/>
+       </condition>
+     </extension>
+   </context>
+
+  </section>
+</document>
+#;
+
+
+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;
+
+}