2012-10-13 16:37:25 +00:00
|
|
|
/*
|
2006-05-10 03:30:22 +00:00
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
2012-04-18 16:51:48 +00:00
|
|
|
* Copyright (C) 2005-2012, Anthony Minessale II <anthm@freeswitch.org>
|
2006-05-10 03:30:22 +00:00
|
|
|
*
|
|
|
|
* 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
|
2009-02-04 21:20:54 +00:00
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
2006-05-10 03:30:22 +00:00
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2012-10-13 16:37:25 +00:00
|
|
|
*
|
2009-02-04 21:20:54 +00:00
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
2009-06-23 17:57:17 +00:00
|
|
|
* John Wehle <john@feith.com>
|
2012-10-13 16:37:25 +00:00
|
|
|
* Garmt Boekholt <garmt@cimico.com>
|
2006-05-10 03:30:22 +00:00
|
|
|
*
|
|
|
|
* mod_xml_rpc.c -- XML RPC
|
|
|
|
*
|
2012-10-13 16:37:25 +00:00
|
|
|
* embedded webserver for FS
|
|
|
|
* exposes fs api to web (and ajax, javascript, ...)
|
|
|
|
* supports GET/POST requests (as defined below)
|
|
|
|
* and similar XMLRPC format /RPC2 freeswitch.api and management.api
|
|
|
|
*
|
|
|
|
* usage:
|
|
|
|
* (1) http:/host:port/[txt|web|xml]api/fsapicommand[?arg[ arg]*][ &key=value[+&key=value]*]
|
|
|
|
* e.g. http:/host:port/api/show?calls &refresh=5+&weather=nice
|
|
|
|
* (2) http:/host:port/filepath - serves files from conf/htdocs
|
|
|
|
*
|
|
|
|
* NB:
|
|
|
|
* ad (1) - key/value pairs are propagated as event headers
|
|
|
|
* - if &key=value is "&refresh=xx" - special feature: automatic refresh after xx sec (triggered by browser)
|
|
|
|
* note that refresh works only
|
|
|
|
* IF response content-type: text/html (i.e. "webapi" or "api")
|
|
|
|
* AND fs api command created an event header HTTP-REFRESH before the first write_stream
|
|
|
|
* NOTE if "api", fs api command has to overwrite content-type to be text/html i.s.o. text/plain (default)
|
|
|
|
*
|
|
|
|
* - if api format is "api" mod_xml_rpc will automatically assume plain/text (so webunaware fs commands are rendered apropriately)
|
|
|
|
* - xmlapi doesn't seem to be used, however if a fs api command renders xml, you can set the format type to xml
|
|
|
|
* txtapi-text/plain or webapi-text/html surround xml with <pre> xml <pre/>
|
|
|
|
* - typically fs api command arguments are encoded with UrlPathEncode (spaces -> %20), and k/v pairs are urlencoded (space -> +)
|
|
|
|
*
|
|
|
|
* ad (2) ms ie may show extra empty lines when serving large txt files as ie has problems rendering content-type "plain/text"
|
|
|
|
*
|
2006-05-10 03:30:22 +00:00
|
|
|
*/
|
|
|
|
#include <switch.h>
|
2007-11-28 19:56:25 +00:00
|
|
|
#include <switch_version.h>
|
2007-02-13 21:38:12 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(disable:4142)
|
|
|
|
#endif
|
|
|
|
|
2006-05-10 03:30:22 +00:00
|
|
|
#include <xmlrpc-c/base.h>
|
|
|
|
#include <xmlrpc-c/abyss.h>
|
|
|
|
#include <xmlrpc-c/server.h>
|
|
|
|
#include <xmlrpc-c/server_abyss.h>
|
2012-10-13 16:37:25 +00:00
|
|
|
#include <xmlrpc-c/base64_int.h>
|
|
|
|
#include <../lib/abyss/src/token.h>
|
|
|
|
#include <../lib/abyss/src/http.h>
|
|
|
|
#include <../lib/abyss/src/session.h>
|
2006-05-10 03:30:22 +00:00
|
|
|
|
2007-06-13 17:06:10 +00:00
|
|
|
SWITCH_MODULE_LOAD_FUNCTION(mod_xml_rpc_load);
|
|
|
|
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_rpc_shutdown);
|
|
|
|
SWITCH_MODULE_RUNTIME_FUNCTION(mod_xml_rpc_runtime);
|
|
|
|
SWITCH_MODULE_DEFINITION(mod_xml_rpc, mod_xml_rpc_load, mod_xml_rpc_shutdown, mod_xml_rpc_runtime);
|
2006-05-10 03:30:22 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
static abyss_bool HTTPWrite(TSession * s, const char *buffer, const uint32_t len);
|
2008-05-23 20:57:56 +00:00
|
|
|
|
2006-12-10 02:02:30 +00:00
|
|
|
static struct {
|
|
|
|
uint16_t port;
|
|
|
|
uint8_t running;
|
|
|
|
char *realm;
|
|
|
|
char *user;
|
|
|
|
char *pass;
|
2009-07-24 17:39:12 +00:00
|
|
|
char *default_domain;
|
|
|
|
switch_bool_t virtual_host;
|
2007-11-30 22:56:01 +00:00
|
|
|
TServer abyssServer;
|
2012-10-13 16:37:25 +00:00
|
|
|
xmlrpc_registry *registryP;
|
2006-12-10 02:02:30 +00:00
|
|
|
} globals;
|
|
|
|
|
2007-10-17 14:59:17 +00:00
|
|
|
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_realm, globals.realm);
|
|
|
|
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_user, globals.user);
|
|
|
|
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_pass, globals.pass);
|
2009-07-24 17:39:12 +00:00
|
|
|
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_default_domain, globals.default_domain);
|
2006-12-10 02:02:30 +00:00
|
|
|
|
2007-10-17 14:59:17 +00:00
|
|
|
static switch_status_t do_config(void)
|
2006-05-10 03:30:22 +00:00
|
|
|
{
|
|
|
|
char *cf = "xml_rpc.conf";
|
|
|
|
switch_xml_t cfg, xml, settings, param;
|
2009-07-24 17:39:12 +00:00
|
|
|
char *realm, *user, *pass, *default_domain;
|
2006-06-01 17:16:15 +00:00
|
|
|
|
2009-07-24 17:39:12 +00:00
|
|
|
default_domain = realm = user = pass = NULL;
|
2006-05-10 15:47:54 +00:00
|
|
|
if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
|
2008-10-11 06:19:56 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", cf);
|
2006-05-10 03:30:22 +00:00
|
|
|
return SWITCH_STATUS_TERM;
|
|
|
|
}
|
|
|
|
|
2009-07-24 17:39:12 +00:00
|
|
|
globals.virtual_host = SWITCH_TRUE;
|
|
|
|
|
2006-05-10 03:30:22 +00:00
|
|
|
if ((settings = switch_xml_child(cfg, "settings"))) {
|
|
|
|
for (param = switch_xml_child(settings, "param"); param; param = param->next) {
|
2006-05-17 00:58:21 +00:00
|
|
|
char *var = (char *) switch_xml_attr_soft(param, "name");
|
|
|
|
char *val = (char *) switch_xml_attr_soft(param, "value");
|
2006-05-10 03:30:22 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
if (!zstr(var) && !zstr(val)) {
|
|
|
|
if (!strcasecmp(var, "auth-realm")) {
|
|
|
|
realm = val;
|
|
|
|
} else if (!strcasecmp(var, "auth-user")) {
|
|
|
|
user = val;
|
|
|
|
} else if (!strcasecmp(var, "auth-pass")) {
|
|
|
|
pass = val;
|
|
|
|
} else if (!strcasecmp(var, "http-port")) {
|
|
|
|
globals.port = (uint16_t) atoi(val);
|
|
|
|
} else if (!strcasecmp(var, "default-domain")) {
|
|
|
|
default_domain = val;
|
|
|
|
} else if (!strcasecmp(var, "virtual-host")) {
|
|
|
|
globals.virtual_host = switch_true(val);
|
|
|
|
}
|
2006-05-10 03:30:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-05-10 03:30:22 +00:00
|
|
|
if (!globals.port) {
|
|
|
|
globals.port = 8080;
|
|
|
|
}
|
2008-12-23 18:14:57 +00:00
|
|
|
if (realm) {
|
2006-06-01 17:16:15 +00:00
|
|
|
set_global_realm(realm);
|
2008-12-23 18:14:57 +00:00
|
|
|
if (user && pass) {
|
|
|
|
set_global_user(user);
|
|
|
|
set_global_pass(pass);
|
|
|
|
}
|
2006-06-01 17:16:15 +00:00
|
|
|
}
|
2009-07-24 17:39:12 +00:00
|
|
|
if (default_domain) {
|
|
|
|
set_global_default_domain(default_domain);
|
|
|
|
}
|
2006-05-10 03:30:22 +00:00
|
|
|
switch_xml_free(xml);
|
|
|
|
|
2006-12-10 02:02:30 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2006-05-10 03:30:22 +00:00
|
|
|
}
|
|
|
|
|
2007-06-13 17:06:10 +00:00
|
|
|
SWITCH_MODULE_LOAD_FUNCTION(mod_xml_rpc_load)
|
2006-05-10 03:30:22 +00:00
|
|
|
{
|
|
|
|
/* connect my internal structure to the blank pointer passed to me */
|
2007-06-20 08:18:18 +00:00
|
|
|
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
|
2006-05-10 03:30:22 +00:00
|
|
|
|
|
|
|
memset(&globals, 0, sizeof(globals));
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
do_config();
|
2006-05-12 15:33:49 +00:00
|
|
|
|
2006-05-10 03:30:22 +00:00
|
|
|
/* indicate that the module should continue to be loaded */
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
static switch_status_t http_stream_raw_write(switch_stream_handle_t *handle, uint8_t *data, switch_size_t datalen)
|
|
|
|
{
|
2012-10-13 16:37:25 +00:00
|
|
|
TSession *r = (TSession *) handle->data;
|
2007-11-28 19:56:25 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
return HTTPWrite(r, (char *) data, (uint32_t) datalen) ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
|
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
}
|
|
|
|
|
2007-02-14 18:08:11 +00:00
|
|
|
static switch_status_t http_stream_write(switch_stream_handle_t *handle, const char *fmt, ...)
|
2006-05-10 15:47:54 +00:00
|
|
|
{
|
2012-10-13 16:37:25 +00:00
|
|
|
TSession *r = (TSession *) handle->data;
|
|
|
|
int ret = 1;
|
2006-05-10 15:47:54 +00:00
|
|
|
char *data;
|
2012-10-13 16:37:25 +00:00
|
|
|
switch_event_t *evnt = handle->param_event;
|
|
|
|
va_list ap;
|
2006-05-10 15:47:54 +00:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2007-03-09 20:44:13 +00:00
|
|
|
ret = switch_vasprintf(&data, fmt, ap);
|
2006-05-10 15:47:54 +00:00
|
|
|
va_end(ap);
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-05-10 15:47:54 +00:00
|
|
|
if (data) {
|
2012-10-13 16:37:25 +00:00
|
|
|
/* Stream Content-Type (http header) to the xmlrpc (web) client, if fs api command did not do it yet. */
|
|
|
|
/* If (Content-Type in event) then the header was already replied. */
|
|
|
|
/* If fs api command is not "web aware", this will set the Content-Type to "text/plain". */
|
|
|
|
const char *http_refresh = NULL;
|
|
|
|
const char *ct = NULL;
|
|
|
|
const char *refresh = NULL;
|
|
|
|
if (evnt && !(ct = switch_event_get_header(evnt, "Content-Type"))){
|
|
|
|
const char *val = switch_stristr("Content-Type", data);
|
|
|
|
if (!val) {
|
|
|
|
val = "Content-Type: text/plain\r\n\r\n";
|
|
|
|
ret = HTTPWrite(r, val, (uint32_t) strlen(val));
|
|
|
|
}
|
|
|
|
/* flag to prevent running this more than once per http reply */
|
|
|
|
switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "Content-Type", strstr(val,":")+2);
|
|
|
|
ct = switch_event_get_header(evnt, "Content-Type");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
ret = HTTPWrite(r, data, (uint32_t) strlen(data));
|
|
|
|
}
|
|
|
|
switch_safe_free(data);
|
|
|
|
|
|
|
|
/* e.g. "http://www.cluecon.fs/api/show?calls &refresh=5" */
|
|
|
|
/* fs api command can set event header "HTTP-REFRESH" so that the web page will automagically refresh, if */
|
|
|
|
/* "refresh=xxx" was part of the http query kv pairs */
|
|
|
|
if (ret && ct && *ct && (http_refresh = switch_event_get_header(evnt, "HTTP-REFRESH"))
|
|
|
|
&& (refresh = switch_event_get_header(evnt, "refresh"))
|
|
|
|
&& !strstr("text/html", ct)
|
|
|
|
&& (atoi(refresh) > 0 )) {
|
|
|
|
const char *query = switch_event_get_header(evnt, "HTTP-QUERY");
|
|
|
|
const char *uri = switch_event_get_header(evnt, "HTTP-URI");
|
|
|
|
if (uri && query && *uri && *query) {
|
|
|
|
char *buf = switch_mprintf("<META HTTP-EQUIV=REFRESH CONTENT=\"%s; URL=%s?%s\">\n", refresh, uri, query);
|
|
|
|
ret = HTTPWrite(r, buf, (uint32_t) strlen(buf));
|
|
|
|
switch_safe_free(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* only one refresh meta header per reply */
|
|
|
|
if (http_refresh) {
|
|
|
|
switch_event_del_header(evnt, "HTTP-REFRESH");
|
|
|
|
}
|
2006-05-10 15:47:54 +00:00
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-05-10 15:47:54 +00:00
|
|
|
return ret ? SWITCH_STATUS_FALSE : SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
static abyss_bool user_attributes(const char *user, const char *domain_name,
|
|
|
|
const char **ppasswd, const char **pvm_passwd, const char **palias, const char **pallowed_commands)
|
2009-06-23 17:57:17 +00:00
|
|
|
{
|
2009-07-24 17:39:12 +00:00
|
|
|
const char *passwd;
|
|
|
|
const char *vm_passwd;
|
|
|
|
const char *alias;
|
|
|
|
const char *allowed_commands;
|
2009-06-23 17:57:17 +00:00
|
|
|
switch_event_t *params;
|
2010-03-08 18:25:43 +00:00
|
|
|
switch_xml_t x_user, x_params, x_param;
|
2009-06-23 17:57:17 +00:00
|
|
|
|
|
|
|
passwd = NULL;
|
|
|
|
vm_passwd = NULL;
|
|
|
|
alias = NULL;
|
|
|
|
allowed_commands = NULL;
|
|
|
|
|
2010-03-08 18:25:43 +00:00
|
|
|
if (ppasswd) *ppasswd = NULL;
|
|
|
|
if (pvm_passwd) *pvm_passwd = NULL;
|
|
|
|
if (palias) *palias = NULL;
|
|
|
|
if (pallowed_commands) *pallowed_commands = NULL;
|
|
|
|
|
2009-06-23 17:57:17 +00:00
|
|
|
params = NULL;
|
|
|
|
|
|
|
|
switch_event_create(¶ms, SWITCH_EVENT_REQUEST_PARAMS);
|
|
|
|
switch_assert(params);
|
|
|
|
switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "number_alias", "check");
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2010-03-08 18:25:43 +00:00
|
|
|
|
|
|
|
if (switch_xml_locate_user_merged("id", user, domain_name, NULL, &x_user, params) != SWITCH_STATUS_SUCCESS) {
|
2009-06-23 17:57:17 +00:00
|
|
|
switch_event_destroy(¶ms);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-10-13 16:37:25 +00:00
|
|
|
|
2009-06-23 17:57:17 +00:00
|
|
|
switch_event_destroy(¶ms);
|
|
|
|
alias = switch_xml_attr(x_user, "number-alias");
|
|
|
|
|
|
|
|
if ((x_params = switch_xml_child(x_user, "params"))) {
|
|
|
|
for (x_param = switch_xml_child(x_params, "param"); x_param; x_param = x_param->next) {
|
|
|
|
const char *var = switch_xml_attr_soft(x_param, "name");
|
|
|
|
const char *val = switch_xml_attr_soft(x_param, "value");
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2009-06-23 17:57:17 +00:00
|
|
|
if (!strcasecmp(var, "password")) {
|
|
|
|
passwd = val;
|
|
|
|
} else if (!strcasecmp(var, "vm-password")) {
|
|
|
|
vm_passwd = val;
|
|
|
|
} else if (!strcasecmp(var, "http-allowed-api")) {
|
|
|
|
allowed_commands = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-08 18:25:43 +00:00
|
|
|
if (ppasswd && passwd) {
|
2010-01-05 19:47:49 +00:00
|
|
|
*ppasswd = strdup(passwd);
|
2010-03-08 18:25:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pvm_passwd && vm_passwd) {
|
2010-01-05 19:47:49 +00:00
|
|
|
*pvm_passwd = strdup(vm_passwd);
|
2010-03-08 18:25:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (palias && alias) {
|
2010-01-05 19:47:49 +00:00
|
|
|
*palias = strdup(alias);
|
2010-03-08 18:25:43 +00:00
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2010-03-08 18:25:43 +00:00
|
|
|
if (pallowed_commands && allowed_commands) {
|
|
|
|
*pallowed_commands = strdup(allowed_commands);
|
|
|
|
}
|
2010-01-05 19:47:49 +00:00
|
|
|
|
2010-03-08 18:25:43 +00:00
|
|
|
if (x_user) {
|
|
|
|
switch_xml_free(x_user);
|
2010-01-05 19:47:49 +00:00
|
|
|
}
|
2009-06-23 17:57:17 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
static abyss_bool is_authorized(const TSession * r, const char *command)
|
2009-06-23 17:57:17 +00:00
|
|
|
{
|
|
|
|
char *user = NULL, *domain_name = NULL;
|
2010-03-08 18:25:43 +00:00
|
|
|
char *allowed_commands = NULL;
|
2009-06-23 17:57:17 +00:00
|
|
|
char *dp;
|
2010-03-08 18:25:43 +00:00
|
|
|
char *dup = NULL;
|
2009-06-23 17:57:17 +00:00
|
|
|
char *argv[256] = { 0 };
|
2010-03-08 18:25:43 +00:00
|
|
|
int argc = 0, i = 0, ok = 0;
|
2012-10-13 16:37:25 +00:00
|
|
|
unsigned int err = 403;
|
2009-06-23 17:57:17 +00:00
|
|
|
|
2009-08-20 18:52:48 +00:00
|
|
|
if (!r) {
|
2009-06-23 17:57:17 +00:00
|
|
|
return FALSE;
|
2009-08-20 18:52:48 +00:00
|
|
|
}
|
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if (zstr(globals.realm) && zstr(globals.user)) {
|
2009-08-20 18:52:48 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!r->requestInfo.user) {
|
2010-02-06 03:38:24 +00:00
|
|
|
return FALSE;
|
2009-08-20 18:52:48 +00:00
|
|
|
}
|
2009-06-23 17:57:17 +00:00
|
|
|
|
|
|
|
user = strdup(r->requestInfo.user);
|
|
|
|
|
|
|
|
if ((dp = strchr(user, '@'))) {
|
|
|
|
*dp++ = '\0';
|
|
|
|
domain_name = dp;
|
|
|
|
}
|
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if (!zstr(globals.realm) && !zstr(globals.user) && !strcmp(user, globals.user)) {
|
2010-03-09 04:02:01 +00:00
|
|
|
ok = 1;
|
2010-03-08 18:25:43 +00:00
|
|
|
goto end;
|
2009-06-23 17:57:17 +00:00
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if (zstr(user) || zstr(domain_name)) {
|
2010-03-08 18:25:43 +00:00
|
|
|
goto end;
|
2009-08-20 18:52:48 +00:00
|
|
|
}
|
2009-06-23 17:57:17 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
|
2010-03-08 18:25:43 +00:00
|
|
|
err = 686;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
|
|
|
if (!user_attributes(user, domain_name, NULL, NULL, NULL, &allowed_commands)) {
|
2010-03-08 18:25:43 +00:00
|
|
|
goto end;
|
2009-06-23 17:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch_safe_free(user);
|
|
|
|
|
2010-03-08 18:25:43 +00:00
|
|
|
if (!allowed_commands) {
|
|
|
|
goto end;
|
|
|
|
}
|
2009-06-23 17:57:17 +00:00
|
|
|
|
2010-03-08 18:25:43 +00:00
|
|
|
if ((dup = allowed_commands)) {
|
|
|
|
argc = switch_separate_string(dup, ',', argv, (sizeof(argv) / sizeof(argv[0])));
|
2012-10-13 16:37:25 +00:00
|
|
|
|
|
|
|
for (i = 0; i < argc && argv[i]; i++) {
|
2010-03-08 18:25:43 +00:00
|
|
|
if (!strcasecmp(argv[i], command) || !strcasecmp(argv[i], "any")) {
|
|
|
|
ok = 1;
|
|
|
|
break;
|
|
|
|
}
|
2009-06-23 17:57:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-08 18:25:43 +00:00
|
|
|
end:
|
|
|
|
|
|
|
|
switch_safe_free(user);
|
2010-02-06 03:38:24 +00:00
|
|
|
switch_safe_free(dup);
|
2009-06-23 17:57:17 +00:00
|
|
|
|
2010-03-08 18:25:43 +00:00
|
|
|
if (!ok) {
|
|
|
|
ResponseStatus(r, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ok ? TRUE : FALSE;
|
2009-06-23 17:57:17 +00:00
|
|
|
}
|
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
static abyss_bool http_directory_auth(TSession *r, char *domain_name)
|
2006-05-10 03:30:22 +00:00
|
|
|
{
|
2012-10-13 16:37:25 +00:00
|
|
|
char *p = NULL;
|
|
|
|
char *x = NULL;
|
|
|
|
char z[256] = "", t[80] = "";
|
|
|
|
char user[512] = "" ;
|
|
|
|
char *pass = NULL;
|
2007-11-27 19:25:16 +00:00
|
|
|
const char *mypass1 = NULL, *mypass2 = NULL;
|
2009-06-23 17:57:17 +00:00
|
|
|
const char *box = NULL;
|
2007-12-10 19:03:46 +00:00
|
|
|
int at = 0;
|
2012-10-13 16:37:25 +00:00
|
|
|
char *dp = NULL;
|
2010-01-05 19:47:49 +00:00
|
|
|
abyss_bool rval = FALSE;
|
2011-02-02 21:43:26 +00:00
|
|
|
char *dup_domain = NULL;
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
p = RequestHeaderValue(r, "authorization");
|
2007-11-27 19:25:16 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
if (p) {
|
|
|
|
NextToken((const char **const) &p);
|
|
|
|
x = GetToken(&p);
|
|
|
|
if (x) {
|
|
|
|
if (!strcasecmp(x, "basic")) {
|
|
|
|
NextToken((const char **const) &p);
|
2007-11-27 19:25:16 +00:00
|
|
|
switch_b64_decode(p, user, sizeof(user));
|
|
|
|
if ((pass = strchr(user, ':'))) {
|
|
|
|
*pass++ = '\0';
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-08-01 15:06:56 +00:00
|
|
|
if ((dp = strchr(user, '@'))) {
|
|
|
|
*dp++ = '\0';
|
|
|
|
domain_name = dp;
|
2009-01-03 18:57:31 +00:00
|
|
|
at++;
|
2008-08-01 15:06:56 +00:00
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2007-12-10 19:03:46 +00:00
|
|
|
if (!domain_name) {
|
2009-07-24 17:39:12 +00:00
|
|
|
if (globals.virtual_host) {
|
2009-12-08 22:51:39 +00:00
|
|
|
if ((domain_name = (char *) r->requestInfo.host)) {
|
|
|
|
if (!strncasecmp(domain_name, "www.", 3)) {
|
|
|
|
domain_name += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!domain_name) {
|
|
|
|
if (globals.default_domain) {
|
|
|
|
domain_name = globals.default_domain;
|
|
|
|
} else {
|
2011-02-02 21:43:26 +00:00
|
|
|
if ((dup_domain = switch_core_get_variable_dup("domain"))) {
|
|
|
|
domain_name = dup_domain;
|
|
|
|
}
|
2009-07-24 17:39:12 +00:00
|
|
|
}
|
2007-12-10 19:03:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if (zstr(user) || zstr(domain_name)) {
|
2007-12-10 19:03:46 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if (!zstr(globals.realm) && !zstr(globals.user) && !zstr(globals.pass)) {
|
2009-06-23 17:57:17 +00:00
|
|
|
if (at) {
|
|
|
|
switch_snprintf(z, sizeof(z), "%s@%s:%s", globals.user, globals.realm, globals.pass);
|
|
|
|
} else {
|
|
|
|
switch_snprintf(z, sizeof(z), "%s:%s", globals.user, globals.pass);
|
|
|
|
}
|
2012-10-13 16:37:25 +00:00
|
|
|
xmlrpc_base64Encode(z, t);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-12-23 18:14:57 +00:00
|
|
|
if (!strcmp(p, t)) {
|
|
|
|
goto authed;
|
|
|
|
}
|
2007-12-11 02:21:15 +00:00
|
|
|
}
|
2012-10-13 16:37:25 +00:00
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
if (!user_attributes(user, domain_name, &mypass1, &mypass2, &box, NULL)) {
|
2007-11-27 19:25:16 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2008-01-23 20:59:25 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if (!zstr(mypass2) && !strcasecmp(mypass2, "user-choose")) {
|
2010-03-08 18:25:43 +00:00
|
|
|
switch_safe_free(mypass2);
|
2008-10-30 00:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mypass1) {
|
2007-11-27 19:25:16 +00:00
|
|
|
goto authed;
|
2007-11-28 19:56:25 +00:00
|
|
|
} else {
|
2008-10-30 00:52:11 +00:00
|
|
|
if (at) {
|
|
|
|
switch_snprintf(z, sizeof(z), "%s@%s:%s", user, domain_name, mypass1);
|
|
|
|
} else {
|
|
|
|
switch_snprintf(z, sizeof(z), "%s:%s", user, mypass1);
|
|
|
|
}
|
2012-10-13 16:37:25 +00:00
|
|
|
xmlrpc_base64Encode(z, t);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-10-30 00:52:11 +00:00
|
|
|
if (!strcmp(p, t)) {
|
|
|
|
goto authed;
|
2007-11-28 19:56:25 +00:00
|
|
|
}
|
2007-11-27 19:25:16 +00:00
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
if (mypass2) {
|
2007-12-10 19:03:46 +00:00
|
|
|
if (at) {
|
2007-12-12 21:53:32 +00:00
|
|
|
switch_snprintf(z, sizeof(z), "%s@%s:%s", user, domain_name, mypass2);
|
2007-12-10 19:03:46 +00:00
|
|
|
} else {
|
2007-12-12 21:53:32 +00:00
|
|
|
switch_snprintf(z, sizeof(z), "%s:%s", user, mypass2);
|
2007-12-10 19:03:46 +00:00
|
|
|
}
|
2012-10-13 16:37:25 +00:00
|
|
|
xmlrpc_base64Encode(z, t);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
if (!strcmp(p, t)) {
|
|
|
|
goto authed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (box) {
|
2008-10-30 00:52:11 +00:00
|
|
|
if (at) {
|
|
|
|
switch_snprintf(z, sizeof(z), "%s@%s:%s", box, domain_name, mypass1);
|
|
|
|
} else {
|
|
|
|
switch_snprintf(z, sizeof(z), "%s:%s", box, mypass1);
|
|
|
|
}
|
2012-10-13 16:37:25 +00:00
|
|
|
xmlrpc_base64Encode(z, t);
|
2008-10-30 00:52:11 +00:00
|
|
|
|
|
|
|
if (!strcmp(p, t)) {
|
|
|
|
goto authed;
|
2007-11-28 19:56:25 +00:00
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
if (mypass2) {
|
2007-12-10 19:03:46 +00:00
|
|
|
if (at) {
|
2007-12-12 21:53:32 +00:00
|
|
|
switch_snprintf(z, sizeof(z), "%s@%s:%s", box, domain_name, mypass2);
|
2007-12-10 19:03:46 +00:00
|
|
|
} else {
|
2007-12-12 21:53:32 +00:00
|
|
|
switch_snprintf(z, sizeof(z), "%s:%s", box, mypass2);
|
2007-12-10 19:03:46 +00:00
|
|
|
}
|
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
xmlrpc_base64Encode(z, t);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
if (!strcmp(p, t)) {
|
|
|
|
goto authed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-11-27 19:25:16 +00:00
|
|
|
}
|
|
|
|
goto fail;
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
authed:
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2009-06-23 17:57:17 +00:00
|
|
|
switch_snprintf(z, sizeof(z), "%s@%s", (box ? box : user), domain_name);
|
|
|
|
r->requestInfo.user = strdup(z);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2009-06-23 17:57:17 +00:00
|
|
|
ResponseAddField(r, "freeswitch-user", (box ? box : user));
|
|
|
|
ResponseAddField(r, "freeswitch-domain", domain_name);
|
2010-01-05 19:47:49 +00:00
|
|
|
rval = TRUE;
|
|
|
|
goto done;
|
2008-05-27 04:54:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-11-27 19:25:16 +00:00
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
fail:
|
2007-11-27 19:25:16 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_snprintf(z, sizeof(z), "Basic realm=\"%s\"", domain_name ? domain_name : globals.realm);
|
|
|
|
ResponseAddField(r, "WWW-Authenticate", z);
|
|
|
|
ResponseStatus(r, 401);
|
2010-01-05 19:47:49 +00:00
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
done:
|
2010-01-05 19:47:49 +00:00
|
|
|
|
|
|
|
switch_safe_free(mypass1);
|
|
|
|
switch_safe_free(mypass2);
|
|
|
|
switch_safe_free(box);
|
2011-02-02 21:43:26 +00:00
|
|
|
switch_safe_free(dup_domain);
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2010-01-05 19:47:49 +00:00
|
|
|
return rval;
|
2007-11-27 19:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
abyss_bool auth_hook(TSession * r)
|
|
|
|
{
|
|
|
|
char *domain_name, *e;
|
|
|
|
abyss_bool ret = FALSE;
|
|
|
|
|
2008-05-23 20:57:56 +00:00
|
|
|
if (!strncmp(r->requestInfo.uri, "/domains/", 9)) {
|
|
|
|
domain_name = strdup(r->requestInfo.uri + 9);
|
2007-12-12 03:36:39 +00:00
|
|
|
switch_assert(domain_name);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
if ((e = strchr(domain_name, '/'))) {
|
|
|
|
*e++ = '\0';
|
|
|
|
}
|
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
if (!strcmp(domain_name, "this")) {
|
|
|
|
free(domain_name);
|
2008-05-23 20:57:56 +00:00
|
|
|
domain_name = strdup(r->requestInfo.host);
|
2007-11-28 19:56:25 +00:00
|
|
|
}
|
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
ret = !http_directory_auth(r, domain_name);
|
|
|
|
|
|
|
|
free(domain_name);
|
|
|
|
} else {
|
2007-11-28 19:56:25 +00:00
|
|
|
char tmp[512];
|
2008-05-27 04:54:52 +00:00
|
|
|
const char *list[2] = { "index.html", "index.txt" };
|
2007-11-28 19:56:25 +00:00
|
|
|
int x;
|
|
|
|
|
2008-05-23 20:57:56 +00:00
|
|
|
if (!strncmp(r->requestInfo.uri, "/pub", 4)) {
|
2008-05-27 04:54:52 +00:00
|
|
|
char *p = (char *) r->requestInfo.uri;
|
2007-11-28 19:56:25 +00:00
|
|
|
char *new_uri = p + 4;
|
|
|
|
if (!new_uri) {
|
|
|
|
new_uri = "/";
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_snprintf(tmp, sizeof(tmp), "%s%s", SWITCH_GLOBAL_dirs.htdocs_dir, new_uri);
|
2007-11-28 19:56:25 +00:00
|
|
|
|
|
|
|
if (switch_directory_exists(tmp, NULL) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
for (x = 0; x < 2; x++) {
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_snprintf(tmp, sizeof(tmp), "%s%s%s%s",
|
|
|
|
SWITCH_GLOBAL_dirs.htdocs_dir, new_uri, end_of(new_uri) == *SWITCH_PATH_SEPARATOR ? "" : SWITCH_PATH_SEPARATOR, list[x]
|
|
|
|
);
|
|
|
|
|
2007-11-28 19:56:25 +00:00
|
|
|
if (switch_file_exists(tmp, NULL) == SWITCH_STATUS_SUCCESS) {
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_snprintf(tmp, sizeof(tmp), "%s%s%s", new_uri, end_of(new_uri) == '/' ? "" : "/", list[x]
|
|
|
|
);
|
2007-11-28 19:56:25 +00:00
|
|
|
new_uri = tmp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-23 20:57:56 +00:00
|
|
|
r->requestInfo.uri = strdup(new_uri);
|
2007-11-28 19:56:25 +00:00
|
|
|
free(p);
|
|
|
|
|
|
|
|
} else {
|
2008-05-23 20:57:56 +00:00
|
|
|
if (globals.realm && strncmp(r->requestInfo.uri, "/pub", 4)) {
|
2007-12-10 19:03:46 +00:00
|
|
|
ret = !http_directory_auth(r, NULL);
|
2007-11-27 19:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-05-23 20:57:56 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
static abyss_bool HTTPWrite(TSession * s, const char *buffer, const uint32_t len)
|
2008-05-23 20:57:56 +00:00
|
|
|
{
|
2008-05-27 04:54:52 +00:00
|
|
|
if (s->chunkedwrite && s->chunkedwritemode) {
|
|
|
|
char t[16];
|
2008-05-23 20:57:56 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
if (ConnWrite(s->connP, t, sprintf(t, "%x" CRLF, len)))
|
|
|
|
if (ConnWrite(s->connP, buffer, len))
|
|
|
|
return ConnWrite(s->connP, CRLF, 2);
|
2008-05-23 20:57:56 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2008-05-23 20:57:56 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
return ConnWrite(s->connP, buffer, len);
|
2008-05-23 20:57:56 +00:00
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
static abyss_bool HTTPWriteEnd(TSession * s)
|
2008-05-23 20:57:56 +00:00
|
|
|
{
|
2008-05-27 04:54:52 +00:00
|
|
|
if (!s->chunkedwritemode)
|
|
|
|
return TRUE;
|
2008-05-23 20:57:56 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
if (s->chunkedwrite) {
|
|
|
|
/* May be one day trailer dumping will be added */
|
|
|
|
s->chunkedwritemode = FALSE;
|
2012-10-13 16:37:25 +00:00
|
|
|
return ConnWrite(s->connP, "0" CRLF CRLF, 5);
|
2008-05-27 04:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s->requestInfo.keepalive = FALSE;
|
|
|
|
return TRUE;
|
2008-05-23 20:57:56 +00:00
|
|
|
}
|
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
abyss_bool handler_hook(TSession * r)
|
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_stream_handle_t stream = { 0 };
|
2006-05-10 16:10:18 +00:00
|
|
|
char *command;
|
2009-06-23 17:57:17 +00:00
|
|
|
int i;
|
2007-11-28 19:56:25 +00:00
|
|
|
char *fs_user = NULL, *fs_domain = NULL;
|
|
|
|
char *path_info = NULL;
|
|
|
|
abyss_bool ret = TRUE;
|
2012-10-13 16:37:25 +00:00
|
|
|
int html = 0, text = 0, xml = 0, api = 0;
|
2009-02-27 16:35:42 +00:00
|
|
|
const char *api_str;
|
2012-10-13 16:37:25 +00:00
|
|
|
const char *uri = 0;
|
|
|
|
TRequestInfo *info = 0;
|
|
|
|
switch_event_t *evnt = 0; /* shortcut to stream.param_event */
|
|
|
|
|
|
|
|
if (!r || !(info = &r->requestInfo) || !(uri = info->uri)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-05-10 19:07:38 +00:00
|
|
|
|
2006-06-01 17:16:15 +00:00
|
|
|
stream.data = r;
|
|
|
|
stream.write_function = http_stream_write;
|
2007-11-28 19:56:25 +00:00
|
|
|
stream.raw_write_function = http_stream_raw_write;
|
2006-06-01 17:16:15 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
if ((command = strstr(uri, "/api/"))) {
|
2007-11-27 19:25:16 +00:00
|
|
|
command += 5;
|
2012-10-13 16:37:25 +00:00
|
|
|
api++;
|
|
|
|
} else if ((command = strstr(uri, "/webapi/"))) {
|
2007-12-04 16:22:02 +00:00
|
|
|
command += 8;
|
|
|
|
html++;
|
2012-10-13 16:37:25 +00:00
|
|
|
} else if ((command = strstr(uri, "/txtapi/"))) {
|
2008-08-01 15:06:56 +00:00
|
|
|
command += 8;
|
|
|
|
text++;
|
2012-10-13 16:37:25 +00:00
|
|
|
} else if ((command = strstr(uri, "/xmlapi/"))) {
|
2009-01-08 17:36:32 +00:00
|
|
|
command += 8;
|
|
|
|
xml++;
|
2007-11-27 19:25:16 +00:00
|
|
|
} else {
|
2012-10-13 16:37:25 +00:00
|
|
|
return FALSE; /* 404 */
|
2007-11-28 19:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((path_info = strchr(command, '/'))) {
|
|
|
|
*path_info++ = '\0';
|
|
|
|
}
|
2006-06-01 17:16:15 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
for (i = 0; i < r->responseHeaderFields.size; i++) {
|
|
|
|
TTableItem *ti = &r->responseHeaderFields.item[i];
|
2007-11-28 19:56:25 +00:00
|
|
|
if (!strcasecmp(ti->name, "freeswitch-user")) {
|
|
|
|
fs_user = ti->value;
|
|
|
|
} else if (!strcasecmp(ti->name, "freeswitch-domain")) {
|
|
|
|
fs_domain = ti->value;
|
2007-11-27 19:25:16 +00:00
|
|
|
}
|
2006-05-10 03:30:22 +00:00
|
|
|
}
|
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
if (!is_authorized(r, command)) {
|
|
|
|
ret = TRUE;
|
|
|
|
goto end;
|
2007-11-27 19:25:16 +00:00
|
|
|
}
|
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
/* auth: */
|
2007-11-27 19:25:16 +00:00
|
|
|
|
2008-05-24 03:46:19 +00:00
|
|
|
if (switch_event_create(&stream.param_event, SWITCH_EVENT_API) == SWITCH_STATUS_SUCCESS) {
|
2008-05-27 04:54:52 +00:00
|
|
|
const char *const content_length = RequestHeaderValue(r, "content-length");
|
2012-10-13 16:37:25 +00:00
|
|
|
evnt = stream.param_event;
|
|
|
|
|
|
|
|
if (html) {
|
|
|
|
switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "Content-Type", "text/html");
|
|
|
|
} else if (text) {
|
|
|
|
switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "Content-Type", "text/plain");
|
|
|
|
} else if (xml) {
|
|
|
|
switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "Content-Type", "text/xml");
|
|
|
|
}
|
|
|
|
if (api) {
|
|
|
|
switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "HTTP-API", "api");
|
|
|
|
}
|
|
|
|
if (fs_user) switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "FreeSWITCH-User", fs_user);
|
|
|
|
if (fs_domain) switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "FreeSWITCH-Domain", fs_domain);
|
|
|
|
if (path_info) switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "HTTP-Path-Info", path_info);
|
|
|
|
|
|
|
|
if (info->host) switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "HTTP-HOST", info->host);
|
|
|
|
if (info->from) switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "HTTP-FROM", info->from);
|
|
|
|
if (info->useragent) switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "HTTP-USER-AGENT", info->useragent);
|
|
|
|
if (info->referer) switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "HTTP-REFERER", info->referer);
|
|
|
|
if (info->requestline) switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "HTTP-REQUESTLINE", info->requestline);
|
|
|
|
if (info->user) switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "HTTP-USER", info->user);
|
|
|
|
if (info->port) switch_event_add_header(evnt, SWITCH_STACK_BOTTOM, "HTTP-PORT", "%u", info->port);
|
|
|
|
|
|
|
|
{
|
2007-04-27 21:13:02 +00:00
|
|
|
char *q, *qd;
|
|
|
|
char *next;
|
2012-10-13 16:37:25 +00:00
|
|
|
char *query = (char *) info->query;
|
2007-04-27 21:13:02 +00:00
|
|
|
char *name, *val;
|
|
|
|
char qbuf[8192] = "";
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
/* first finish reading from the socket if post method was used*/
|
|
|
|
if (info->method == m_post && content_length) {
|
2007-04-27 21:13:02 +00:00
|
|
|
int len = atoi(content_length);
|
|
|
|
int qlen = 0;
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-04-27 21:13:02 +00:00
|
|
|
if (len > 0) {
|
2012-10-13 16:37:25 +00:00
|
|
|
int succeeded = TRUE;
|
2007-04-27 21:13:02 +00:00
|
|
|
char *qp = qbuf;
|
2012-10-13 16:37:25 +00:00
|
|
|
char *readError;
|
|
|
|
|
2007-04-27 21:13:02 +00:00
|
|
|
do {
|
2012-10-13 16:37:25 +00:00
|
|
|
int blen = r->connP->buffersize - r->connP->bufferpos;
|
2007-04-27 21:13:02 +00:00
|
|
|
|
|
|
|
if ((qlen + blen) > len) {
|
|
|
|
blen = len - qlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
qlen += blen;
|
2008-05-27 04:54:52 +00:00
|
|
|
|
|
|
|
if (qlen > sizeof(qbuf)) {
|
2007-04-27 21:13:02 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
memcpy(qp, r->connP->buffer.b + r->connP->bufferpos, blen);
|
2007-04-27 21:13:02 +00:00
|
|
|
qp += blen;
|
|
|
|
|
|
|
|
if (qlen >= len) {
|
|
|
|
break;
|
|
|
|
}
|
2012-10-13 16:37:25 +00:00
|
|
|
|
|
|
|
ConnRead(r->connP, 2000, NULL, NULL, &readError);
|
|
|
|
if (readError) {
|
|
|
|
succeeded = FALSE;
|
|
|
|
free(readError);
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (succeeded);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-04-27 21:13:02 +00:00
|
|
|
query = qbuf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
/* parse query and add kv-pairs as event headers */
|
|
|
|
/* a kv pair starts with '&', '+' or \0 mark the end */
|
|
|
|
if (query) {
|
|
|
|
switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "HTTP-QUERY", query);
|
2007-04-27 21:13:02 +00:00
|
|
|
qd = strdup(query);
|
2012-10-13 16:37:25 +00:00
|
|
|
} else {
|
|
|
|
qd = strdup(uri);
|
|
|
|
}
|
2007-04-27 21:13:02 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
switch_assert(qd != NULL);
|
|
|
|
|
|
|
|
q = qd;
|
|
|
|
next = q;
|
|
|
|
|
|
|
|
do {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (next = strchr(next, '&')) {
|
|
|
|
if (!query) {
|
|
|
|
/* pass kv pairs from uri to query */
|
|
|
|
/* "?" is absent in url so parse uri */
|
|
|
|
*((char *)uri + (next - q - 1)) = '\0';
|
|
|
|
query = next;
|
|
|
|
switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "HTTP-QUERY", next);
|
|
|
|
/* and strip uri */
|
|
|
|
/* the start of first kv pair marks the end of uri */
|
|
|
|
/* to prevent kv-pairs confusing fs api commands */
|
|
|
|
/* that have arguments separated by space */
|
2007-04-27 21:13:02 +00:00
|
|
|
}
|
2012-10-13 16:37:25 +00:00
|
|
|
*next++ = '\0';
|
|
|
|
}
|
2007-04-27 21:13:02 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
for (p = q; p && *p; p++) {
|
|
|
|
if (*p == '+') {
|
|
|
|
*p = ' ';
|
2007-04-27 21:13:02 +00:00
|
|
|
}
|
2012-10-13 16:37:25 +00:00
|
|
|
}
|
|
|
|
/* hmmm, get method requests are already decoded ... */
|
|
|
|
switch_url_decode(q);
|
2007-04-27 21:13:02 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
name = q;
|
|
|
|
if ((val = strchr(name, '='))) {
|
|
|
|
*val++ = '\0';
|
|
|
|
switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, name, val);
|
|
|
|
}
|
|
|
|
q = next;
|
|
|
|
} while (q != NULL);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
free(qd);
|
2007-04-27 21:13:02 +00:00
|
|
|
}
|
2006-05-10 19:07:38 +00:00
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "HTTP-URI", uri);
|
2007-11-27 19:25:16 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
/* We made it this far, always OK */
|
|
|
|
if (!HTTPWrite(r, "HTTP/1.1 200 OK\r\n", (uint32_t) strlen("HTTP/1.1 200 OK\r\n"))) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-11-27 19:25:16 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
ResponseAddField(r, "Connection", "close");
|
2007-11-30 22:56:01 +00:00
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
/* generation of the date field */
|
2012-10-13 16:37:25 +00:00
|
|
|
if (evnt)
|
2008-05-23 20:57:56 +00:00
|
|
|
{
|
2012-10-13 16:37:25 +00:00
|
|
|
ResponseAddField(r, "Date", switch_event_get_header(evnt, "Event-Date-GMT"));
|
|
|
|
}
|
|
|
|
else {
|
2008-05-27 04:54:52 +00:00
|
|
|
const char *dateValue;
|
2008-05-23 20:57:56 +00:00
|
|
|
|
|
|
|
DateToString(r->date, &dateValue);
|
|
|
|
if (dateValue) {
|
|
|
|
ResponseAddField(r, "Date", dateValue);
|
2012-10-13 16:37:25 +00:00
|
|
|
free((void *)dateValue);
|
2008-05-23 20:57:56 +00:00
|
|
|
}
|
2007-11-27 19:25:16 +00:00
|
|
|
}
|
2008-05-23 20:57:56 +00:00
|
|
|
|
2007-11-27 19:25:16 +00:00
|
|
|
/* Generation of the server field */
|
2008-05-27 04:54:52 +00:00
|
|
|
ResponseAddField(r, "Server", "FreeSWITCH-" SWITCH_VERSION_FULL "-mod_xml_rpc");
|
2007-11-27 19:25:16 +00:00
|
|
|
|
2007-12-04 16:22:02 +00:00
|
|
|
if (html) {
|
|
|
|
ResponseAddField(r, "Content-Type", "text/html");
|
2008-08-01 15:06:56 +00:00
|
|
|
} else if (text) {
|
|
|
|
ResponseAddField(r, "Content-Type", "text/plain");
|
2009-01-12 20:35:37 +00:00
|
|
|
} else if (xml) {
|
|
|
|
ResponseAddField(r, "Content-Type", "text/xml");
|
2007-12-04 16:22:02 +00:00
|
|
|
}
|
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
for (i = 0; i < r->responseHeaderFields.size; i++) {
|
|
|
|
TTableItem *ti = &r->responseHeaderFields.item[i];
|
|
|
|
char *header = switch_mprintf("%s: %s\r\n", ti->name, ti->value);
|
|
|
|
if (!ConnWrite(r->connP, header, (uint32_t) strlen(header))) {
|
|
|
|
switch_safe_free(header);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
switch_safe_free(header);
|
2007-11-27 19:25:16 +00:00
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
/* send end http header */
|
|
|
|
if (html||text||xml) {
|
|
|
|
if (!ConnWrite(r->connP, CRLF, 2)) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* content-type and end of http header will be streamed by fs api or http_stream_write */
|
2007-12-04 16:22:02 +00:00
|
|
|
}
|
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
if (switch_stristr("unload", command) && switch_stristr("mod_xml_rpc", info->query)) {
|
2009-02-27 16:35:42 +00:00
|
|
|
command = "bgapi";
|
|
|
|
api_str = "unload mod_xml_rpc";
|
2012-10-13 16:37:25 +00:00
|
|
|
} else if (switch_stristr("reload", command) && switch_stristr("mod_xml_rpc", info->query)) {
|
2009-02-27 20:21:48 +00:00
|
|
|
command = "bgapi";
|
|
|
|
api_str = "reload mod_xml_rpc";
|
2009-02-27 16:35:42 +00:00
|
|
|
} else {
|
2012-10-13 16:37:25 +00:00
|
|
|
api_str = info->query;
|
2009-02-27 16:35:42 +00:00
|
|
|
}
|
2007-11-30 22:56:01 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
/* TODO (maybe): take "refresh=xxx" out of query as to not confuse fs api commands */
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
/* execute actual fs api command */
|
|
|
|
/* fs api command will write to stream, calling http_stream_write / http_stream_raw_write */
|
|
|
|
/* switch_api_execute will stream INVALID COMMAND before it fails */
|
|
|
|
switch_api_execute(command, api_str, NULL, &stream);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
r->responseStarted = TRUE;
|
|
|
|
ResponseStatus(r, 200); /* we don't want an assertion failure */
|
2008-05-23 20:57:56 +00:00
|
|
|
r->requestInfo.keepalive = 0;
|
2007-11-28 19:56:25 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
end:
|
2007-11-28 19:56:25 +00:00
|
|
|
|
|
|
|
return ret;
|
2006-05-10 03:30:22 +00:00
|
|
|
}
|
|
|
|
|
2009-06-23 17:57:17 +00:00
|
|
|
static xmlrpc_value *freeswitch_api(xmlrpc_env * const envP, xmlrpc_value * const paramArrayP, void *const userData, void *const callInfo)
|
2006-05-10 03:30:22 +00:00
|
|
|
{
|
2007-09-11 22:29:29 +00:00
|
|
|
char *command = NULL, *arg = NULL;
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_stream_handle_t stream = { 0 };
|
2007-03-07 18:34:22 +00:00
|
|
|
xmlrpc_value *val = NULL;
|
2009-02-27 20:21:48 +00:00
|
|
|
switch_bool_t freed = 0;
|
2009-02-27 16:35:42 +00:00
|
|
|
|
2007-03-07 18:34:22 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
/* Parse our argument array. */
|
|
|
|
xmlrpc_decompose_value(envP, paramArrayP, "(ss)", &command, &arg);
|
2008-10-20 15:48:03 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
if (envP->fault_occurred) {
|
2008-10-20 15:48:03 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Request!\n");
|
|
|
|
return NULL;
|
2006-05-10 03:30:22 +00:00
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
if (!is_authorized((const TSession *) callInfo, command)) {
|
2009-06-23 17:57:17 +00:00
|
|
|
val = xmlrpc_build_value(envP, "s", "UNAUTHORIZED!");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2009-02-27 16:35:42 +00:00
|
|
|
if (switch_stristr("unload", command) && switch_stristr("mod_xml_rpc", arg)) {
|
|
|
|
switch_safe_free(command);
|
|
|
|
switch_safe_free(arg);
|
2009-02-27 20:21:48 +00:00
|
|
|
freed = 1;
|
|
|
|
command = "bgapi";
|
|
|
|
arg = "unload mod_xml_rpc";
|
2010-02-06 03:38:24 +00:00
|
|
|
} else if (switch_stristr("reload", command) && switch_stristr("mod_xml_rpc", arg)) {
|
2009-02-27 20:21:48 +00:00
|
|
|
switch_safe_free(command);
|
|
|
|
switch_safe_free(arg);
|
|
|
|
freed = 1;
|
|
|
|
command = "bgapi";
|
|
|
|
arg = "reload mod_xml_rpc";
|
2009-02-27 16:35:42 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 18:34:22 +00:00
|
|
|
SWITCH_STANDARD_STREAM(stream);
|
|
|
|
if (switch_api_execute(command, arg, NULL, &stream) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
/* Return our result. */
|
|
|
|
val = xmlrpc_build_value(envP, "s", stream.data);
|
|
|
|
free(stream.data);
|
|
|
|
} else {
|
|
|
|
val = xmlrpc_build_value(envP, "s", "ERROR!");
|
|
|
|
}
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
end:
|
2009-06-23 17:57:17 +00:00
|
|
|
|
2007-09-11 22:29:29 +00:00
|
|
|
/* xmlrpc-c requires us to free memory it malloced from xmlrpc_decompose_value */
|
2009-02-27 20:21:48 +00:00
|
|
|
if (!freed) {
|
2009-02-27 16:35:42 +00:00
|
|
|
switch_safe_free(command);
|
|
|
|
switch_safe_free(arg);
|
|
|
|
}
|
|
|
|
|
2007-03-07 18:34:22 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
static xmlrpc_value *freeswitch_man(xmlrpc_env * const envP, xmlrpc_value * const paramArrayP, void *const userData)
|
2007-03-07 18:34:22 +00:00
|
|
|
{
|
2007-09-11 22:29:29 +00:00
|
|
|
char *oid = NULL, *relative_oid, *s_action = NULL, *data = NULL;
|
2007-03-07 18:34:22 +00:00
|
|
|
char buf[SWITCH_MAX_MANAGEMENT_BUFFER_LEN] = "";
|
|
|
|
switch_management_action_t action = SMA_NONE;
|
2007-09-11 22:29:29 +00:00
|
|
|
xmlrpc_value *val = NULL;
|
2007-03-07 18:34:22 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
/* Parse our argument array. */
|
|
|
|
xmlrpc_decompose_value(envP, paramArrayP, "(sss)", &oid, &s_action, &data);
|
|
|
|
if (envP->fault_occurred) {
|
2008-10-20 15:48:03 +00:00
|
|
|
return NULL;
|
2007-03-07 18:34:22 +00:00
|
|
|
}
|
|
|
|
|
2007-03-17 20:12:13 +00:00
|
|
|
if (!strncasecmp(oid, FREESWITCH_OID_PREFIX, strlen(FREESWITCH_OID_PREFIX))) {
|
|
|
|
relative_oid = oid + strlen(FREESWITCH_OID_PREFIX);
|
2007-03-07 18:34:22 +00:00
|
|
|
} else {
|
|
|
|
relative_oid = oid;
|
|
|
|
}
|
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if (!zstr(data)) {
|
2007-03-07 18:34:22 +00:00
|
|
|
switch_copy_string(buf, data, sizeof(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(s_action, "get")) {
|
|
|
|
action = SMA_GET;
|
|
|
|
} else if (!strcasecmp(s_action, "set")) {
|
|
|
|
action = SMA_SET;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action) {
|
|
|
|
if (switch_core_management_exec(relative_oid, action, buf, sizeof(buf)) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
if (action == SMA_SET) {
|
2007-08-03 21:29:01 +00:00
|
|
|
if (*buf != '\0') {
|
2007-12-12 21:53:32 +00:00
|
|
|
switch_snprintf(buf, sizeof(buf), "OK\n");
|
2007-03-07 18:34:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2007-08-03 21:29:01 +00:00
|
|
|
if (*buf != '\0') {
|
2007-12-12 21:53:32 +00:00
|
|
|
switch_snprintf(buf, sizeof(buf), "ERROR\n");
|
2007-03-07 18:34:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2007-12-12 21:53:32 +00:00
|
|
|
switch_snprintf(buf, sizeof(buf), "Invalid Action %s\n", s_action);
|
2007-03-07 18:34:22 +00:00
|
|
|
}
|
2006-05-10 03:30:22 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
/* Return our result. */
|
|
|
|
val = xmlrpc_build_value(envP, "s", buf);
|
2007-03-07 18:34:22 +00:00
|
|
|
|
2007-09-11 22:29:29 +00:00
|
|
|
/* xmlrpc-c requires us to free memory it malloced from xmlrpc_decompose_value */
|
|
|
|
switch_safe_free(oid);
|
|
|
|
switch_safe_free(s_action);
|
|
|
|
switch_safe_free(data);
|
2006-05-10 03:30:22 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2007-06-13 17:06:10 +00:00
|
|
|
SWITCH_MODULE_RUNTIME_FUNCTION(mod_xml_rpc_runtime)
|
2006-05-10 03:30:22 +00:00
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
xmlrpc_env env;
|
2006-05-10 03:30:22 +00:00
|
|
|
char logfile[512];
|
2007-11-28 20:44:18 +00:00
|
|
|
switch_hash_index_t *hi;
|
|
|
|
const void *var;
|
|
|
|
void *val;
|
2006-05-10 03:30:22 +00:00
|
|
|
|
|
|
|
globals.running = 1;
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
xmlrpc_env_init(&env);
|
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
globals.registryP = xmlrpc_registry_new(&env);
|
2006-05-10 03:30:22 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
/* TODO why twice and why add_method for freeswitch.api and add_method2 for freeswitch.management ? */
|
|
|
|
xmlrpc_registry_add_method2(&env, globals.registryP, "freeswitch.api", &freeswitch_api, NULL, NULL, NULL);
|
|
|
|
xmlrpc_registry_add_method2(&env, globals.registryP, "freeswitch_api", &freeswitch_api, NULL, NULL, NULL);
|
|
|
|
xmlrpc_registry_add_method(&env, globals.registryP, NULL, "freeswitch.management", &freeswitch_man, NULL);
|
|
|
|
xmlrpc_registry_add_method(&env, globals.registryP, NULL, "freeswitch_management", &freeswitch_man, NULL);
|
2007-03-29 22:31:56 +00:00
|
|
|
|
|
|
|
MIMETypeInit();
|
2012-10-13 16:37:25 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
for (hi = switch_core_mime_index(); hi; hi = switch_hash_next(hi)) {
|
2007-11-28 20:44:18 +00:00
|
|
|
switch_hash_this(hi, &var, NULL, &val);
|
|
|
|
if (var && val) {
|
|
|
|
MIMETypeAdd((char *) val, (char *) var);
|
|
|
|
}
|
|
|
|
}
|
2006-05-10 03:30:22 +00:00
|
|
|
|
2007-12-12 21:53:32 +00:00
|
|
|
switch_snprintf(logfile, sizeof(logfile), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, "freeswitch_http.log");
|
2007-11-30 22:56:01 +00:00
|
|
|
ServerCreate(&globals.abyssServer, "XmlRpcServer", globals.port, SWITCH_GLOBAL_dirs.htdocs_dir, logfile);
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
xmlrpc_server_abyss_set_handler(&env, &globals.abyssServer, "/RPC2", globals.registryP);
|
|
|
|
|
|
|
|
xmlrpc_env_clean(&env);
|
2006-05-10 03:30:22 +00:00
|
|
|
|
2007-11-30 22:56:01 +00:00
|
|
|
if (ServerInit(&globals.abyssServer) != TRUE) {
|
2007-04-25 19:57:14 +00:00
|
|
|
globals.running = 0;
|
2007-04-26 13:52:56 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to start HTTP Port %d\n", globals.port);
|
2012-10-13 16:37:25 +00:00
|
|
|
xmlrpc_registry_free(globals.registryP);
|
|
|
|
MIMETypeTerm();
|
|
|
|
|
2007-04-26 13:52:56 +00:00
|
|
|
return SWITCH_STATUS_TERM;
|
2007-04-25 19:57:14 +00:00
|
|
|
}
|
|
|
|
|
2007-11-30 22:56:01 +00:00
|
|
|
ServerAddHandler(&globals.abyssServer, handler_hook);
|
|
|
|
ServerAddHandler(&globals.abyssServer, auth_hook);
|
2012-10-13 16:37:25 +00:00
|
|
|
ServerSetKeepaliveTimeout(&globals.abyssServer, 5);
|
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Starting HTTP Port %d, DocRoot [%s]\n", globals.port, SWITCH_GLOBAL_dirs.htdocs_dir);
|
2007-12-12 23:40:51 +00:00
|
|
|
ServerRun(&globals.abyssServer);
|
2012-10-13 16:37:25 +00:00
|
|
|
|
2008-10-02 18:50:09 +00:00
|
|
|
switch_yield(1000000);
|
2012-10-13 16:37:25 +00:00
|
|
|
|
2007-12-12 23:40:51 +00:00
|
|
|
globals.running = 0;
|
2006-05-10 03:30:22 +00:00
|
|
|
|
2007-12-12 23:40:51 +00:00
|
|
|
return SWITCH_STATUS_TERM;
|
2006-05-10 03:30:22 +00:00
|
|
|
}
|
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
/* upon module unload */
|
2007-06-13 17:06:10 +00:00
|
|
|
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_rpc_shutdown)
|
2006-05-10 03:30:22 +00:00
|
|
|
{
|
2012-10-13 16:37:25 +00:00
|
|
|
|
|
|
|
/* this makes the worker thread (ServerRun) stop */
|
2008-05-23 20:57:56 +00:00
|
|
|
ServerTerminate(&globals.abyssServer);
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-10-02 18:50:09 +00:00
|
|
|
do {
|
2007-12-12 23:40:51 +00:00
|
|
|
switch_yield(100000);
|
2008-10-02 18:50:09 +00:00
|
|
|
} while (globals.running);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2012-10-13 16:37:25 +00:00
|
|
|
ServerFree(&globals.abyssServer);
|
|
|
|
xmlrpc_registry_free(globals.registryP);
|
|
|
|
MIMETypeTerm();
|
|
|
|
|
2009-02-23 19:17:24 +00:00
|
|
|
switch_safe_free(globals.realm);
|
|
|
|
switch_safe_free(globals.user);
|
|
|
|
switch_safe_free(globals.pass);
|
2012-10-13 16:37:25 +00:00
|
|
|
switch_safe_free(globals.default_domain);
|
2009-02-23 19:17:24 +00:00
|
|
|
|
2006-05-10 03:30:22 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-11-27 22:30:48 +00:00
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
2008-02-03 22:14:57 +00:00
|
|
|
* indent-tabs-mode:t
|
2006-11-27 22:30:48 +00:00
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
2008-07-03 19:12:26 +00:00
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
|
2006-11-27 22:30:48 +00:00
|
|
|
*/
|