|
@@ -65,7 +65,7 @@ extern ph_framework_t *ph_framework_data;
|
|
|
|
|
|
static int mod_init(void);
|
|
|
static void destroy(void);
|
|
|
-static int xhttp_pi_dispatch(sip_msg_t* msg, char* s1, char* s2);
|
|
|
+static int xhttp_pi_dispatch(sip_msg_t *msg, char *s1, char *s2);
|
|
|
|
|
|
|
|
|
/** The context of the xhttp_pi request being processed.
|
|
@@ -78,7 +78,7 @@ static pi_ctx_t ctx;
|
|
|
|
|
|
static xhttp_api_t xhttp_api;
|
|
|
|
|
|
-gen_lock_t* ph_lock;
|
|
|
+gen_lock_t *ph_lock;
|
|
|
|
|
|
|
|
|
str xhttp_pi_root = str_init("pi");
|
|
@@ -88,32 +88,28 @@ int buf_size = 0;
|
|
|
char error_buf[ERROR_REASON_BUF_LEN];
|
|
|
|
|
|
static cmd_export_t cmds[] = {
|
|
|
- {"dispatch_xhttp_pi",(cmd_function)xhttp_pi_dispatch,0,0,0,
|
|
|
- REQUEST_ROUTE|EVENT_ROUTE},
|
|
|
- {0, 0, 0, 0, 0, 0}
|
|
|
-};
|
|
|
+ {"dispatch_xhttp_pi", (cmd_function)xhttp_pi_dispatch, 0, 0, 0,
|
|
|
+ REQUEST_ROUTE | EVENT_ROUTE},
|
|
|
+ {0, 0, 0, 0, 0, 0}};
|
|
|
|
|
|
-static param_export_t params[] = {
|
|
|
- {"xhttp_pi_root", PARAM_STR, &xhttp_pi_root},
|
|
|
- {"xhttp_pi_buf_size", INT_PARAM, &buf_size},
|
|
|
- {"framework", PARAM_STR, &filename},
|
|
|
- {0, 0, 0}
|
|
|
-};
|
|
|
+static param_export_t params[] = {{"xhttp_pi_root", PARAM_STR, &xhttp_pi_root},
|
|
|
+ {"xhttp_pi_buf_size", INT_PARAM, &buf_size},
|
|
|
+ {"framework", PARAM_STR, &filename}, {0, 0, 0}};
|
|
|
|
|
|
static rpc_export_t rpc_methods[];
|
|
|
|
|
|
/** module exports */
|
|
|
-struct module_exports exports= {
|
|
|
- "xhttp_pi", /* module name */
|
|
|
- DEFAULT_DLFLAGS, /* dlopen flags */
|
|
|
- cmds, /* cmd (cfg function) exports */
|
|
|
- params, /* param exports */
|
|
|
- 0, /* RPC method exports */
|
|
|
- 0, /* pv exports */
|
|
|
- 0, /* response handling function */
|
|
|
- mod_init, /* module init function */
|
|
|
- 0, /* per-child init function */
|
|
|
- destroy /* module destroy function */
|
|
|
+struct module_exports exports = {
|
|
|
+ "xhttp_pi", /* module name */
|
|
|
+ DEFAULT_DLFLAGS, /* dlopen flags */
|
|
|
+ cmds, /* cmd (cfg function) exports */
|
|
|
+ params, /* param exports */
|
|
|
+ 0, /* RPC method exports */
|
|
|
+ 0, /* pv exports */
|
|
|
+ 0, /* response handling function */
|
|
|
+ mod_init, /* module init function */
|
|
|
+ 0, /* per-child init function */
|
|
|
+ destroy /* module destroy function */
|
|
|
};
|
|
|
|
|
|
|
|
@@ -129,7 +125,7 @@ struct module_exports exports= {
|
|
|
* @param code Reason code.
|
|
|
* @param fmt Formatting string used to build the reason phrase.
|
|
|
*/
|
|
|
-static void pi_fault(pi_ctx_t* ctx, int code, char* fmt, ...)
|
|
|
+static void pi_fault(pi_ctx_t *ctx, int code, char *fmt, ...)
|
|
|
{
|
|
|
va_list ap;
|
|
|
struct xhttp_pi_reply *reply = &ctx->reply;
|
|
@@ -147,45 +143,44 @@ static void pi_fault(pi_ctx_t* ctx, int code, char* fmt, ...)
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int pi_send(pi_ctx_t* ctx)
|
|
|
+static int pi_send(pi_ctx_t *ctx)
|
|
|
{
|
|
|
- struct xhttp_pi_reply* reply;
|
|
|
+ struct xhttp_pi_reply *reply;
|
|
|
|
|
|
- if (ctx->reply_sent) return 1;
|
|
|
+ if(ctx->reply_sent)
|
|
|
+ return 1;
|
|
|
|
|
|
reply = &ctx->reply;
|
|
|
|
|
|
- if (0!=ph_run_pi_cmd(ctx)){
|
|
|
+ if(0 != ph_run_pi_cmd(ctx)) {
|
|
|
LM_DBG("pi_fault(500,\"Internal Server Error\"\n");
|
|
|
pi_fault(ctx, 500, "Internal Server Error");
|
|
|
}
|
|
|
|
|
|
ctx->reply_sent = 1;
|
|
|
- if (reply->body.len) {
|
|
|
+ if(reply->body.len) {
|
|
|
xhttp_api.reply(ctx->msg, reply->code, &reply->reason,
|
|
|
- &XHTTP_PI_CONTENT_TYPE_TEXT_HTML, &reply->body);
|
|
|
- }
|
|
|
- else {
|
|
|
- LM_DBG("xhttp_api.reply(%p, %d, %.*s, %.*s, %.*s)\n",
|
|
|
- ctx->msg, reply->code,
|
|
|
- (&reply->reason)->len, (&reply->reason)->s,
|
|
|
- (&XHTTP_PI_CONTENT_TYPE_TEXT_HTML)->len,
|
|
|
- (&XHTTP_PI_CONTENT_TYPE_TEXT_HTML)->s,
|
|
|
- (&reply->reason)->len, (&reply->reason)->s);
|
|
|
+ &XHTTP_PI_CONTENT_TYPE_TEXT_HTML, &reply->body);
|
|
|
+ } else {
|
|
|
+ LM_DBG("xhttp_api.reply(%p, %d, %.*s, %.*s, %.*s)\n", ctx->msg,
|
|
|
+ reply->code, (&reply->reason)->len, (&reply->reason)->s,
|
|
|
+ (&XHTTP_PI_CONTENT_TYPE_TEXT_HTML)->len,
|
|
|
+ (&XHTTP_PI_CONTENT_TYPE_TEXT_HTML)->s, (&reply->reason)->len,
|
|
|
+ (&reply->reason)->s);
|
|
|
xhttp_api.reply(ctx->msg, reply->code, &reply->reason,
|
|
|
- &XHTTP_PI_CONTENT_TYPE_TEXT_HTML, &reply->reason);
|
|
|
+ &XHTTP_PI_CONTENT_TYPE_TEXT_HTML, &reply->reason);
|
|
|
}
|
|
|
|
|
|
- if (reply->buf.s) {
|
|
|
+ if(reply->buf.s) {
|
|
|
pkg_free(reply->buf.s);
|
|
|
reply->buf.s = NULL;
|
|
|
reply->buf.len = 0;
|
|
|
}
|
|
|
- if (ctx->arg.s) {
|
|
|
- pkg_free(ctx->arg.s);
|
|
|
- ctx->arg.s = NULL;
|
|
|
- ctx->arg.len = 0;
|
|
|
- }
|
|
|
+ if(ctx->arg.s) {
|
|
|
+ pkg_free(ctx->arg.s);
|
|
|
+ ctx->arg.s = NULL;
|
|
|
+ ctx->arg.len = 0;
|
|
|
+ }
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
@@ -205,7 +200,7 @@ static int init_xhttp_pi_reply(pi_ctx_t *ctx)
|
|
|
reply->code = 200;
|
|
|
reply->reason = XHTTP_PI_REASON_OK;
|
|
|
reply->buf.s = pkg_malloc(buf_size);
|
|
|
- if (!reply->buf.s) {
|
|
|
+ if(!reply->buf.s) {
|
|
|
PKG_MEM_ERROR;
|
|
|
pi_fault(ctx, 500, "Internal Server Error (No memory left)");
|
|
|
return -1;
|
|
@@ -217,15 +212,14 @@ static int init_xhttp_pi_reply(pi_ctx_t *ctx)
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
int ph_init_async_lock(void)
|
|
|
{
|
|
|
ph_lock = lock_alloc();
|
|
|
- if (ph_lock==NULL) {
|
|
|
+ if(ph_lock == NULL) {
|
|
|
LM_ERR("failed to create lock\n");
|
|
|
return -1;
|
|
|
}
|
|
|
- if (lock_init(ph_lock)==NULL) {
|
|
|
+ if(lock_init(ph_lock) == NULL) {
|
|
|
LM_ERR("failed to init lock\n");
|
|
|
return -1;
|
|
|
}
|
|
@@ -235,7 +229,7 @@ int ph_init_async_lock(void)
|
|
|
|
|
|
void ph_destroy_async_lock(void)
|
|
|
{
|
|
|
- if (ph_lock) {
|
|
|
+ if(ph_lock) {
|
|
|
lock_destroy(ph_lock);
|
|
|
lock_dealloc(ph_lock);
|
|
|
}
|
|
@@ -245,56 +239,56 @@ static int mod_init(void)
|
|
|
{
|
|
|
int i;
|
|
|
|
|
|
- if (rpc_register_array(rpc_methods)!=0) {
|
|
|
+ if(rpc_register_array(rpc_methods) != 0) {
|
|
|
LM_ERR("failed to register RPC commands\n");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
/* bind the XHTTP API */
|
|
|
- if (xhttp_load_api(&xhttp_api) < 0) {
|
|
|
+ if(xhttp_load_api(&xhttp_api) < 0) {
|
|
|
LM_ERR("cannot bind to XHTTP API\n");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
/* Check xhttp_pi_buf_size param */
|
|
|
- if (buf_size == 0)
|
|
|
- buf_size = pkg_mem_size/3;
|
|
|
+ if(buf_size == 0)
|
|
|
+ buf_size = pkg_mem_size / 3;
|
|
|
|
|
|
/* Check xhttp_pi_root param */
|
|
|
- for(i=0;i<xhttp_pi_root.len;i++){
|
|
|
- if ( !isalnum(xhttp_pi_root.s[i]) && xhttp_pi_root.s[i]!='_') {
|
|
|
+ for(i = 0; i < xhttp_pi_root.len; i++) {
|
|
|
+ if(!isalnum(xhttp_pi_root.s[i]) && xhttp_pi_root.s[i] != '_') {
|
|
|
LM_ERR("bad xhttp_pi_root param [%.*s], char [%c] "
|
|
|
- "- use only alphanumerical chars\n",
|
|
|
- xhttp_pi_root.len, xhttp_pi_root.s,
|
|
|
- xhttp_pi_root.s[i]);
|
|
|
+ "- use only alphanumerical chars\n",
|
|
|
+ xhttp_pi_root.len, xhttp_pi_root.s, xhttp_pi_root.s[i]);
|
|
|
return -1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* Check framework param */
|
|
|
- if (!filename.s || filename.len<=0) {
|
|
|
+ if(!filename.s || filename.len <= 0) {
|
|
|
LM_ERR("missing framework\n");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- /* building a cache of pi module commands */
|
|
|
- if (0!=ph_init_cmds(&ph_framework_data, filename.s))
|
|
|
+ /* building a cache of pi module commands */
|
|
|
+ if(0 != ph_init_cmds(&ph_framework_data, filename.s))
|
|
|
+ return -1;
|
|
|
+ for(i = 0; i < ph_framework_data->ph_db_urls_size; i++) {
|
|
|
+ LM_DBG("initializing db[%d] [%s]\n", i,
|
|
|
+ ph_framework_data->ph_db_urls[i].db_url.s);
|
|
|
+ if(init_http_db(ph_framework_data, i) != 0) {
|
|
|
+ LM_ERR("failed to initialize the DB support\n");
|
|
|
return -1;
|
|
|
- for(i=0;i<ph_framework_data->ph_db_urls_size;i++){
|
|
|
- LM_DBG("initializing db[%d] [%s]\n",
|
|
|
- i, ph_framework_data->ph_db_urls[i].db_url.s);
|
|
|
- if (init_http_db(ph_framework_data, i)!=0) {
|
|
|
- LM_ERR("failed to initialize the DB support\n");
|
|
|
- return -1;
|
|
|
- }
|
|
|
- if (connect_http_db(ph_framework_data, i)) {
|
|
|
- LM_ERR("failed to connect to database\n");
|
|
|
- return -1;
|
|
|
- }
|
|
|
}
|
|
|
+ if(connect_http_db(ph_framework_data, i)) {
|
|
|
+ LM_ERR("failed to connect to database\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/* Build async lock */
|
|
|
- if (ph_init_async_lock() != 0) return -1;
|
|
|
+ if(ph_init_async_lock() != 0)
|
|
|
+ return -1;
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
@@ -307,7 +301,7 @@ void destroy(void)
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int ki_xhttp_pi_dispatch(sip_msg_t* msg)
|
|
|
+static int ki_xhttp_pi_dispatch(sip_msg_t *msg)
|
|
|
{
|
|
|
str arg = {NULL, 0};
|
|
|
int ret = 0;
|
|
@@ -319,33 +313,39 @@ static int ki_xhttp_pi_dispatch(sip_msg_t* msg)
|
|
|
}
|
|
|
|
|
|
/* Init xhttp_pi context */
|
|
|
- if (ctx.reply.buf.s) LM_ERR("Unexpected buf value [%p][%d]\n",
|
|
|
- ctx.reply.buf.s, ctx.reply.buf.len);
|
|
|
+ if(ctx.reply.buf.s)
|
|
|
+ LM_ERR("Unexpected buf value [%p][%d]\n", ctx.reply.buf.s,
|
|
|
+ ctx.reply.buf.len);
|
|
|
memset(&ctx, 0, sizeof(pi_ctx_t));
|
|
|
ctx.msg = msg;
|
|
|
ctx.mod = ctx.cmd = -1;
|
|
|
- if (init_xhttp_pi_reply(&ctx) < 0) goto send_reply;
|
|
|
+ if(init_xhttp_pi_reply(&ctx) < 0)
|
|
|
+ goto send_reply;
|
|
|
|
|
|
lock_get(ph_lock);
|
|
|
- LM_DBG("ph_framework_data: [%p]->[%p]\n", &ph_framework_data, ph_framework_data);
|
|
|
+ LM_DBG("ph_framework_data: [%p]->[%p]\n", &ph_framework_data,
|
|
|
+ ph_framework_data);
|
|
|
/* Extract arguments from url */
|
|
|
- if (0!=ph_parse_url(&msg->first_line.u.request.uri,
|
|
|
- &ctx.mod, &ctx.cmd, &arg)){
|
|
|
+ if(0
|
|
|
+ != ph_parse_url(
|
|
|
+ &msg->first_line.u.request.uri, &ctx.mod, &ctx.cmd, &arg)) {
|
|
|
pi_fault(&ctx, 500, "Bad URL");
|
|
|
goto send_reply;
|
|
|
}
|
|
|
|
|
|
- if (arg.s) {
|
|
|
- if (arg.len) {
|
|
|
+ if(arg.s) {
|
|
|
+ if(arg.len) {
|
|
|
/* Unescape args */
|
|
|
- ctx.arg.s = pkg_malloc((arg.len)*sizeof(char));
|
|
|
- if (ctx.arg.s==NULL){
|
|
|
+ ctx.arg.s = pkg_malloc((arg.len) * sizeof(char));
|
|
|
+ if(ctx.arg.s == NULL) {
|
|
|
PKG_MEM_ERROR;
|
|
|
pi_fault(&ctx, 500, "Internal Server Error (oom)");
|
|
|
goto send_reply;
|
|
|
}
|
|
|
- for(i=0;i<arg.len;i++) if (arg.s[i]=='+') arg.s[i]=' ';
|
|
|
- if (0>un_escape(&arg, &ctx.arg)) {
|
|
|
+ for(i = 0; i < arg.len; i++)
|
|
|
+ if(arg.s[i] == '+')
|
|
|
+ arg.s[i] = ' ';
|
|
|
+ if(0 > un_escape(&arg, &ctx.arg)) {
|
|
|
LM_ERR("unable to escape [%.*s]\n", arg.len, arg.s);
|
|
|
pi_fault(&ctx, 500, "Bad arg in URL");
|
|
|
goto send_reply;
|
|
@@ -356,31 +356,32 @@ static int ki_xhttp_pi_dispatch(sip_msg_t* msg)
|
|
|
LM_DBG("Got no arg\n");
|
|
|
goto send_reply;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
send_reply:
|
|
|
- LM_DBG("ph_framework_data: [%p]->[%p]\n", &ph_framework_data, ph_framework_data);
|
|
|
+ LM_DBG("ph_framework_data: [%p]->[%p]\n", &ph_framework_data,
|
|
|
+ ph_framework_data);
|
|
|
lock_release(ph_lock);
|
|
|
- if (!ctx.reply_sent) {
|
|
|
+ if(!ctx.reply_sent) {
|
|
|
ret = pi_send(&ctx);
|
|
|
}
|
|
|
- if (ret < 0) return -1;
|
|
|
+ if(ret < 0)
|
|
|
+ return -1;
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-static int xhttp_pi_dispatch(sip_msg_t* msg, char* s1, char* s2)
|
|
|
+static int xhttp_pi_dispatch(sip_msg_t *msg, char *s1, char *s2)
|
|
|
{
|
|
|
return ki_xhttp_pi_dispatch(msg);
|
|
|
}
|
|
|
|
|
|
/* rpc function documentation */
|
|
|
-static const char *rpc_reload_doc[2] = {
|
|
|
- "Reload the xml framework", 0
|
|
|
-};
|
|
|
+static const char *rpc_reload_doc[2] = {"Reload the xml framework", 0};
|
|
|
|
|
|
/* rpc function implementations */
|
|
|
-static void rpc_reload(rpc_t *rpc, void *c) {
|
|
|
+static void rpc_reload(rpc_t *rpc, void *c)
|
|
|
+{
|
|
|
lock_get(ph_lock);
|
|
|
- if (0!=ph_init_cmds(&ph_framework_data, filename.s)) {
|
|
|
+ if(0 != ph_init_cmds(&ph_framework_data, filename.s)) {
|
|
|
rpc->rpl_printf(c, "Reload failed");
|
|
|
} else {
|
|
|
rpc->rpl_printf(c, "Reload OK");
|
|
@@ -390,9 +391,7 @@ static void rpc_reload(rpc_t *rpc, void *c) {
|
|
|
}
|
|
|
|
|
|
static rpc_export_t rpc_methods[] = {
|
|
|
- {"xhttp_pi.reload", rpc_reload, rpc_reload_doc, 0},
|
|
|
- {0, 0, 0, 0}
|
|
|
-};
|
|
|
+ {"xhttp_pi.reload", rpc_reload, rpc_reload_doc, 0}, {0, 0, 0, 0}};
|
|
|
|
|
|
/**
|
|
|
*
|