diff --git a/src/modules/xhttp/api.h b/src/modules/xhttp/api.h index df7f59205f7..e6a9311c149 100644 --- a/src/modules/xhttp/api.h +++ b/src/modules/xhttp/api.h @@ -18,21 +18,23 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ - + #ifndef _XHTTP_API_H_ #define _XHTTP_API_H_ #include "../../core/sr_module.h" -typedef int (*xhttp_reply_f)(sip_msg_t *msg, int code, str *reason, - str *ctype, str *body); +typedef int (*xhttp_reply_f)( + sip_msg_t *msg, int code, str *reason, str *ctype, str *body); +/* clang-format off */ typedef struct xhttp_api { xhttp_reply_f reply; } xhttp_api_t; +/* clang-format on */ -typedef int (*bind_xhttp_f)(xhttp_api_t* api); -int bind_xhttp(xhttp_api_t* api); +typedef int (*bind_xhttp_f)(xhttp_api_t *api); +int bind_xhttp(xhttp_api_t *api); /** * @brief Load the XHTTP API @@ -46,8 +48,7 @@ static inline int xhttp_load_api(xhttp_api_t *api) LM_ERR("cannot find bind_xhttp\n"); return -1; } - if(bindxhttp(api)<0) - { + if(bindxhttp(api) < 0) { LM_ERR("cannot bind xhttp api\n"); return -1; } diff --git a/src/modules/xhttp/xhttp_mod.c b/src/modules/xhttp/xhttp_mod.c index 78abf550305..ba57066849e 100644 --- a/src/modules/xhttp/xhttp_mod.c +++ b/src/modules/xhttp/xhttp_mod.c @@ -48,20 +48,19 @@ MODULE_VERSION -static int xhttp_handler(sip_msg_t* msg); -static int w_xhttp_send_reply(sip_msg_t* msg, char* pcode, char* preason, - char *pctype, char* pbody); +static int xhttp_handler(sip_msg_t *msg); +static int w_xhttp_send_reply( + sip_msg_t *msg, char *pcode, char *preason, char *pctype, char *pbody); static int mod_init(void); -static int fixup_xhttp_reply(void** param, int param_no); +static int fixup_xhttp_reply(void **param, int param_no); -static int pv_get_huri(struct sip_msg *msg, pv_param_t *param, - pv_value_t *res); +static int pv_get_huri(struct sip_msg *msg, pv_param_t *param, pv_value_t *res); -static int xhttp_route_no=DEFAULT_RT; -static char* xhttp_url_match = NULL; +static int xhttp_route_no = DEFAULT_RT; +static char *xhttp_url_match = NULL; static regex_t xhttp_url_match_regexp; -static char* xhttp_url_skip = NULL; +static char *xhttp_url_skip = NULL; static regex_t xhttp_url_skip_regexp; /** SL API structure */ @@ -69,14 +68,7 @@ sl_api_t slb; static str xhttp_event_callback = STR_NULL; -static cmd_export_t cmds[] = { - {"xhttp_reply", (cmd_function)w_xhttp_send_reply, - 4, fixup_xhttp_reply, 0, REQUEST_ROUTE}, - {"bind_xhttp", (cmd_function)bind_xhttp, - 0, 0, 0, ANY_ROUTE}, - {0, 0, 0, 0, 0} -}; - +/* clang-format off */ static pv_export_t mod_pvs[] = { {{"hu", (sizeof("hu")-1)}, /* */ PVT_OTHER, pv_get_huri, 0, @@ -85,6 +77,13 @@ static pv_export_t mod_pvs[] = { { {0, 0}, 0, 0, 0, 0, 0, 0, 0 } }; +static tr_export_t mod_trans[] = { + { {"url", sizeof("url")-1}, + xhttp_tr_parse_url }, + + { { 0, 0 }, 0 } +}; + static param_export_t params[] = { {"url_match", PARAM_STRING, &xhttp_url_match}, {"url_skip", PARAM_STRING, &xhttp_url_skip}, @@ -92,6 +91,14 @@ static param_export_t params[] = { {0, 0, 0} }; +static cmd_export_t cmds[] = { + {"xhttp_reply", (cmd_function)w_xhttp_send_reply, + 4, fixup_xhttp_reply, 0, REQUEST_ROUTE}, + {"bind_xhttp", (cmd_function)bind_xhttp, + 0, 0, 0, ANY_ROUTE}, + {0, 0, 0, 0, 0} +}; + /** module exports */ struct module_exports exports= { "xhttp", /* module name */ @@ -105,13 +112,7 @@ struct module_exports exports= { 0, /* per-child init function */ 0 /* module destroy function */ }; - -static tr_export_t mod_trans[] = { - { {"url", sizeof("url")-1}, - xhttp_tr_parse_url }, - - { { 0, 0 }, 0 } -}; +/* clang-format on */ /** * @@ -122,29 +123,27 @@ static int mod_init(void) int route_no; sr_kemi_eng_t *keng = NULL; - if(xhttp_event_callback.s!=NULL && xhttp_event_callback.len>0) { + if(xhttp_event_callback.s != NULL && xhttp_event_callback.len > 0) { keng = sr_kemi_eng_get(); - if(keng==NULL) { + if(keng == NULL) { LM_ERR("failed to find kemi engine\n"); return -1; } - xhttp_route_no=-1; + xhttp_route_no = -1; } else { - route_no=route_lookup(&event_rt, "xhttp:request"); - if (route_no==-1) - { + route_no = route_lookup(&event_rt, "xhttp:request"); + if(route_no == -1) { LM_ERR("failed to find event_route[xhttp:request]\n"); return -1; } - if (event_rt.rlist[route_no]==0) - { + if(event_rt.rlist[route_no] == 0) { LM_WARN("event_route[xhttp:request] is empty\n"); } - xhttp_route_no=route_no; + xhttp_route_no = route_no; } - + /* bind the SL API */ - if (sl_load_api(&slb)!=0) { + if(sl_load_api(&slb) != 0) { LM_ERR("cannot bind to SL API\n"); return -1; } @@ -154,24 +153,22 @@ static int mod_init(void) nsh.name = "xhttp"; nsh.destroy = 0; nsh.on_nonsip_req = xhttp_handler; - if (register_nonsip_msg_hook(&nsh)<0) - { + if(register_nonsip_msg_hook(&nsh) < 0) { LM_ERR("Failed to register non sip msg hooks\n"); return -1; } - if(xhttp_url_match!=NULL) - { + if(xhttp_url_match != NULL) { memset(&xhttp_url_match_regexp, 0, sizeof(regex_t)); - if (regcomp(&xhttp_url_match_regexp, xhttp_url_match, REG_EXTENDED)!=0) { + if(regcomp(&xhttp_url_match_regexp, xhttp_url_match, REG_EXTENDED) + != 0) { LM_ERR("bad match re %s\n", xhttp_url_match); return E_BAD_RE; } } - if(xhttp_url_skip!=NULL) - { + if(xhttp_url_skip != NULL) { memset(&xhttp_url_skip_regexp, 0, sizeof(regex_t)); - if (regcomp(&xhttp_url_skip_regexp, xhttp_url_skip, REG_EXTENDED)!=0) { + if(regcomp(&xhttp_url_skip_regexp, xhttp_url_skip, REG_EXTENDED) != 0) { LM_ERR("bad skip re %s\n", xhttp_url_skip); return E_BAD_RE; } @@ -180,30 +177,29 @@ static int mod_init(void) } -/** - * +/** + * */ -static int pv_get_huri(struct sip_msg *msg, pv_param_t *param, - pv_value_t *res) +static int pv_get_huri(struct sip_msg *msg, pv_param_t *param, pv_value_t *res) { - if(msg==NULL || res==NULL) + if(msg == NULL || res == NULL) return -1; return pv_get_strval(msg, param, res, &msg->first_line.u.request.uri); } -/** - * +/** + * */ -static char* xhttp_to_sip(sip_msg_t* msg, int* new_msg_len) +static char *xhttp_to_sip(sip_msg_t *msg, int *new_msg_len) { unsigned int len, via_len; - char* via, *new_msg, *p; + char *via, *new_msg, *p; str ip, port; struct hostport hp; struct dest_info dst; - + ip.s = ip_addr2strz(&msg->rcv.src_ip); ip.len = strlen(ip.s); port.s = int2str(msg->rcv.src_port, &port.len); @@ -211,32 +207,29 @@ static char* xhttp_to_sip(sip_msg_t* msg, int* new_msg_len) hp.port = &port; init_dst_from_rcv(&dst, &msg->rcv); via = via_builder(&via_len, NULL, &dst, 0, 0, &hp); - if (via == 0) - { + if(via == 0) { LM_DBG("failed to build via\n"); return 0; } len = via_len + msg->len; p = new_msg = pkg_malloc(len + 1); - if (new_msg == 0) - { + if(new_msg == 0) { PKG_MEM_ERROR_FMT(" (%d bytes)\n", len); pkg_free(via); return 0; } /* new message: - * + * * Via: * */ - memcpy(p, msg->first_line.u.request.method.s, - msg->first_line.len); + memcpy(p, msg->first_line.u.request.method.s, msg->first_line.len); p += msg->first_line.len; memcpy(p, via, via_len); p += via_len; - memcpy(p, SIP_MSG_START(msg) + msg->first_line.len, - msg->len - msg->first_line.len); + memcpy(p, SIP_MSG_START(msg) + msg->first_line.len, + msg->len - msg->first_line.len); new_msg[len] = 0; pkg_free(via); *new_msg_len = len; @@ -244,11 +237,11 @@ static char* xhttp_to_sip(sip_msg_t* msg, int* new_msg_len) } -/** - * +/** + * */ -static int xhttp_process_request(sip_msg_t* orig_msg, - char* new_buf, unsigned int new_len) +static int xhttp_process_request( + sip_msg_t *orig_msg, char *new_buf, unsigned int new_len) { int ret; int backup_rt; @@ -257,10 +250,9 @@ static int xhttp_process_request(sip_msg_t* orig_msg, sr_kemi_eng_t *keng = NULL; str evrtname = str_init("xhttp:request"); - ret=0; + ret = 0; backup_rt = get_route_type(); - if (new_buf && new_len) - { + if(new_buf && new_len) { memset(&tmp_msg, 0, sizeof(sip_msg_t)); tmp_msg.buf = new_buf; tmp_msg.len = new_len; @@ -268,8 +260,7 @@ static int xhttp_process_request(sip_msg_t* orig_msg, tmp_msg.id = orig_msg->id; tmp_msg.set_global_address = orig_msg->set_global_address; tmp_msg.set_global_port = orig_msg->set_global_port; - if (parse_msg(new_buf, new_len, &tmp_msg) != 0) - { + if(parse_msg(new_buf, new_len, &tmp_msg) != 0) { LM_ERR("parse_msg failed\n"); goto error; } @@ -277,33 +268,31 @@ static int xhttp_process_request(sip_msg_t* orig_msg, } else { msg = orig_msg; } - - if ((msg->first_line.type != SIP_REQUEST) || (msg->via1 == 0) || - (msg->via1->error != PARSE_OK)) - { + + if((msg->first_line.type != SIP_REQUEST) || (msg->via1 == 0) + || (msg->via1->error != PARSE_OK)) { LM_CRIT("strange message: %.*s\n", msg->len, msg->buf); goto error; } set_route_type(EVENT_ROUTE); - if (exec_pre_script_cb(msg, REQUEST_CB_TYPE) == 0) - { + if(exec_pre_script_cb(msg, REQUEST_CB_TYPE) == 0) { goto done; } init_run_actions_ctx(&ra_ctx); - if(xhttp_route_no>=0) { - if (run_actions(&ra_ctx, event_rt.rlist[xhttp_route_no], msg) < 0) - { - ret=-1; + if(xhttp_route_no >= 0) { + if(run_actions(&ra_ctx, event_rt.rlist[xhttp_route_no], msg) < 0) { + ret = -1; LM_DBG("error while trying script\n"); goto done; } } else { keng = sr_kemi_eng_get(); - if(keng!=NULL) { - if(sr_kemi_route(keng, msg, EVENT_ROUTE, - &xhttp_event_callback, &evrtname)<0) { + if(keng != NULL) { + if(sr_kemi_route( + keng, msg, EVENT_ROUTE, &xhttp_event_callback, &evrtname) + < 0) { LM_ERR("error running event route kemi callback\n"); } } else { @@ -313,8 +302,7 @@ static int xhttp_process_request(sip_msg_t* orig_msg, done: exec_post_script_cb(msg, REQUEST_CB_TYPE); - if (msg != orig_msg) - { + if(msg != orig_msg) { free_sip_msg(msg); } set_route_type(backup_rt); @@ -325,72 +313,68 @@ static int xhttp_process_request(sip_msg_t* orig_msg, } -/** - * +/** + * */ -static int xhttp_handler(sip_msg_t* msg) +static int xhttp_handler(sip_msg_t *msg) { int ret; - char* fake_msg; + char *fake_msg; int fake_msg_len; regmatch_t pmatch; char c; - ret=NONSIP_MSG_DROP; + ret = NONSIP_MSG_DROP; - if(!IS_HTTP(msg)) - { + if(!IS_HTTP(msg)) { /* oly http msg type */ return NONSIP_MSG_PASS; } - if(xhttp_url_skip!=NULL || xhttp_url_match!=NULL) - { + if(xhttp_url_skip != NULL || xhttp_url_match != NULL) { c = msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len]; - msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len] - = '\0'; - if (xhttp_url_skip!=NULL && - regexec(&xhttp_url_skip_regexp, msg->first_line.u.request.uri.s, - 1, &pmatch, 0)==0) - { + msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len] = + '\0'; + if(xhttp_url_skip != NULL + && regexec(&xhttp_url_skip_regexp, + msg->first_line.u.request.uri.s, 1, &pmatch, 0) + == 0) { LM_DBG("URL matched skip re\n"); - msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len] - = c; + msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len] = + c; return NONSIP_MSG_PASS; } - if (xhttp_url_match!=NULL && - regexec(&xhttp_url_match_regexp, msg->first_line.u.request.uri.s, - 1, &pmatch, 0)!=0) - { + if(xhttp_url_match != NULL + && regexec(&xhttp_url_match_regexp, + msg->first_line.u.request.uri.s, 1, &pmatch, 0) + != 0) { LM_DBG("URL not matched\n"); - msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len] - = c; + msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len] = + c; return NONSIP_MSG_PASS; } msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len] = c; } - if (msg->via1 == 0) - { + if(msg->via1 == 0) { fake_msg = xhttp_to_sip(msg, &fake_msg_len); - if (fake_msg == 0) - { + if(fake_msg == 0) { LM_ERR("out of memory\n"); - ret=NONSIP_MSG_ERROR; + ret = NONSIP_MSG_ERROR; } else { - DBG("new fake msg created (%d bytes):\n<%.*s>\n", - fake_msg_len, fake_msg_len, fake_msg); - if (xhttp_process_request(msg, fake_msg, fake_msg_len)<0) { - ret=NONSIP_MSG_ERROR; + DBG("new fake msg created (%d bytes):\n<%.*s>\n", fake_msg_len, + fake_msg_len, fake_msg); + if(xhttp_process_request(msg, fake_msg, fake_msg_len) < 0) { + ret = NONSIP_MSG_ERROR; } pkg_free(fake_msg); } return ret; } else { - LM_DBG("http msg unchanged (%d bytes):\n<%.*s>\n", - msg->len, msg->len, msg->buf); - if (xhttp_process_request(msg, 0, 0)<0) - ret=NONSIP_MSG_ERROR; + LM_DBG("http msg unchanged (%d bytes):\n<%.*s>\n", msg->len, msg->len, + msg->buf); + if(xhttp_process_request(msg, 0, 0) < 0) + ret = NONSIP_MSG_ERROR; return ret; } } @@ -399,28 +383,25 @@ static int xhttp_handler(sip_msg_t* msg) /** * */ -static int xhttp_send_reply(sip_msg_t *msg, int code, str *reason, - str *ctype, str *body) +static int xhttp_send_reply( + sip_msg_t *msg, int code, str *reason, str *ctype, str *body) { str tbuf; - if(ctype!=NULL && ctype->len>0) - { + if(ctype != NULL && ctype->len > 0) { /* add content-type */ - tbuf.len=sizeof("Content-Type: ") - 1 + ctype->len + CRLF_LEN; - tbuf.s=pkg_malloc(sizeof(char)*(tbuf.len)); + tbuf.len = sizeof("Content-Type: ") - 1 + ctype->len + CRLF_LEN; + tbuf.s = pkg_malloc(sizeof(char) * (tbuf.len)); - if (tbuf.s==0) - { + if(tbuf.s == 0) { PKG_MEM_ERROR; return -1; } memcpy(tbuf.s, "Content-Type: ", sizeof("Content-Type: ") - 1); - memcpy(tbuf.s+sizeof("Content-Type: ") - 1, ctype->s, ctype->len); - memcpy(tbuf.s+sizeof("Content-Type: ") - 1 + ctype->len, - CRLF, CRLF_LEN); - if (add_lump_rpl(msg, tbuf.s, tbuf.len, LUMP_RPL_HDR) == 0) - { + memcpy(tbuf.s + sizeof("Content-Type: ") - 1, ctype->s, ctype->len); + memcpy(tbuf.s + sizeof("Content-Type: ") - 1 + ctype->len, CRLF, + CRLF_LEN); + if(add_lump_rpl(msg, tbuf.s, tbuf.len, LUMP_RPL_HDR) == 0) { LM_ERR("failed to insert content-type lump\n"); pkg_free(tbuf.s); return -1; @@ -429,18 +410,15 @@ static int xhttp_send_reply(sip_msg_t *msg, int code, str *reason, LM_DBG("response with content-type: %.*s\n", ctype->len, ctype->s); } - if(body!=NULL && body->len>0) - { - if (add_lump_rpl(msg, body->s, body->len, LUMP_RPL_BODY) == 0) - { + if(body != NULL && body->len > 0) { + if(add_lump_rpl(msg, body->s, body->len, LUMP_RPL_BODY) == 0) { LM_ERR("Error while adding reply lump\n"); return -1; } LM_DBG("response with body: %.*s\n", body->len, body->s); } LM_DBG("sending out response: %d %.*s\n", code, reason->len, reason->s); - if (slb.sreply(msg, code, reason) < 0) - { + if(slb.sreply(msg, code, reason) < 0) { LM_ERR("Error while sending reply\n"); return -1; } @@ -450,83 +428,74 @@ static int xhttp_send_reply(sip_msg_t *msg, int code, str *reason, /** * */ -static int w_xhttp_send_reply(sip_msg_t* msg, char* pcode, char* preason, - char *pctype, char* pbody) +static int w_xhttp_send_reply( + sip_msg_t *msg, char *pcode, char *preason, char *pctype, char *pbody) { str body = {0, 0}; str reason = {"OK", 2}; str ctype = {"text/plain", 10}; int code = 200; - if(pcode==0 || preason==0 || pctype==0 || pbody==0) - { + if(pcode == 0 || preason == 0 || pctype == 0 || pbody == 0) { LM_ERR("invalid parameters\n"); return -1; } - if(fixup_get_ivalue(msg, (gparam_p)pcode, &code)!=0) - { + if(fixup_get_ivalue(msg, (gparam_p)pcode, &code) != 0) { LM_ERR("no reply code value\n"); return -1; } - if(code<100 || code>700) - { + if(code < 100 || code > 700) { LM_ERR("invalid code parameter\n"); return -1; } - if(fixup_get_svalue(msg, (gparam_p)preason, &reason)!=0) - { + if(fixup_get_svalue(msg, (gparam_p)preason, &reason) != 0) { LM_ERR("unable to get reason\n"); return -1; } - if(reason.s==NULL || reason.len == 0) - { + if(reason.s == NULL || reason.len == 0) { LM_ERR("invalid reason parameter\n"); return -1; } - if(fixup_get_svalue(msg, (gparam_p)pctype, &ctype)!=0) - { + if(fixup_get_svalue(msg, (gparam_p)pctype, &ctype) != 0) { LM_ERR("unable to get content type\n"); return -1; } - if(ctype.s==NULL) - { + if(ctype.s == NULL) { LM_ERR("invalid content-type parameter\n"); return -1; } - if(fixup_get_svalue(msg, (gparam_p)pbody, &body)!=0) - { + if(fixup_get_svalue(msg, (gparam_p)pbody, &body) != 0) { LM_ERR("unable to get body\n"); return -1; } - if(body.s==NULL) - { + if(body.s == NULL) { LM_ERR("invalid body parameter\n"); return -1; } - if(xhttp_send_reply(msg, code, &reason, &ctype, &body)<0) + if(xhttp_send_reply(msg, code, &reason, &ctype, &body) < 0) return -1; return 1; } -/** - * +/** + * */ -static int fixup_xhttp_reply(void** param, int param_no) +static int fixup_xhttp_reply(void **param, int param_no) { - if (param_no == 1) { - return fixup_igp_null(param, 1); - } else if (param_no == 2) { - return fixup_spve_null(param, 1); - } else if (param_no == 3) { - return fixup_spve_null(param, 1); - } else if (param_no == 4) { - return fixup_spve_null(param, 1); + if(param_no == 1) { + return fixup_igp_null(param, 1); + } else if(param_no == 2) { + return fixup_spve_null(param, 1); + } else if(param_no == 3) { + return fixup_spve_null(param, 1); + } else if(param_no == 4) { + return fixup_spve_null(param, 1); } return 0; } @@ -534,9 +503,9 @@ static int fixup_xhttp_reply(void** param, int param_no) /** * */ -int bind_xhttp(xhttp_api_t* api) +int bind_xhttp(xhttp_api_t *api) { - if (!api) { + if(!api) { ERR("Invalid parameter value\n"); return -1; } diff --git a/src/modules/xhttp/xhttp_trans.c b/src/modules/xhttp/xhttp_trans.c index 22e56478830..ff9ff82d9f3 100644 --- a/src/modules/xhttp/xhttp_trans.c +++ b/src/modules/xhttp/xhttp_trans.c @@ -1,4 +1,4 @@ -/* +/* * Copyright (C) 2013 Crocodile RCS Ltd * * This file is part of Kamailio, a free SIP server. @@ -13,8 +13,8 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -23,39 +23,48 @@ #include "../../core/trim.h" #include "xhttp_trans.h" -enum _tr_xhttp_type { TR_XHTTP_NONE = 0, TR_XHTTPURL, TR_XHTTPURLQUERYSTRING }; -enum _tr_xhttpurl_subtype { TR_XHTTPURL_NONE = 0, TR_XHTTPURL_PATH, - TR_XHTTPURL_QUERYSTRING}; -enum _tr_xhttpquerystring_subtype { TR_XHTTPUTLQUERYSTRING_NONE = 0, - TR_XHTTPURLQUERYSTRING_VALUE}; +enum _tr_xhttp_type +{ + TR_XHTTP_NONE = 0, + TR_XHTTPURL, + TR_XHTTPURLQUERYSTRING +}; +enum _tr_xhttpurl_subtype +{ + TR_XHTTPURL_NONE = 0, + TR_XHTTPURL_PATH, + TR_XHTTPURL_QUERYSTRING +}; +enum _tr_xhttpquerystring_subtype +{ + TR_XHTTPUTLQUERYSTRING_NONE = 0, + TR_XHTTPURLQUERYSTRING_VALUE +}; static str _httpurl_str = {0, 0}; static int _httpurl_querystring_pos = 0; -int xhttp_tr_eval_xhttpurl(struct sip_msg *msg, tr_param_t *tp, int subtype, - pv_value_t *val) +int xhttp_tr_eval_xhttpurl( + struct sip_msg *msg, tr_param_t *tp, int subtype, pv_value_t *val) { int pos = 0; - if (val == NULL || val->flags & PV_VAL_NULL) + if(val == NULL || val->flags & PV_VAL_NULL) return -1; - if (!(val->flags & PV_VAL_STR)) - { + if(!(val->flags & PV_VAL_STR)) { val->rs.s = int2str(val->ri, &val->rs.len); val->flags = PV_VAL_STR; } - if (_httpurl_str.len == 0 || _httpurl_str.len != val->rs.len - || strncmp(_httpurl_str.s, val->rs.s, val->rs.len) != 0) - { - if (val->rs.len > _httpurl_str.len) - { - if (_httpurl_str.s) pkg_free(_httpurl_str.s); - _httpurl_str.s = (char *) pkg_malloc( - (val->rs.len + 1) * sizeof(char)); - if (_httpurl_str.s == NULL) - { + if(_httpurl_str.len == 0 || _httpurl_str.len != val->rs.len + || strncmp(_httpurl_str.s, val->rs.s, val->rs.len) != 0) { + if(val->rs.len > _httpurl_str.len) { + if(_httpurl_str.s) + pkg_free(_httpurl_str.s); + _httpurl_str.s = + (char *)pkg_malloc((val->rs.len + 1) * sizeof(char)); + if(_httpurl_str.s == NULL) { PKG_MEM_ERROR; memset(&_httpurl_str.s, 0, sizeof(str)); return -1; @@ -64,32 +73,32 @@ int xhttp_tr_eval_xhttpurl(struct sip_msg *msg, tr_param_t *tp, int subtype, _httpurl_str.len = val->rs.len; memcpy(_httpurl_str.s, val->rs.s, val->rs.len); - while (pos < val->rs.len && val->rs.s[pos] != '?') pos++; + while(pos < val->rs.len && val->rs.s[pos] != '?') + pos++; _httpurl_querystring_pos = (pos >= val->rs.len) ? 0 : pos + 1; } - switch (subtype) - { - case TR_XHTTPURL_PATH: - val->rs.len = (_httpurl_querystring_pos == 0) - ? val->rs.len : _httpurl_querystring_pos - 1; - break; - - case TR_XHTTPURL_QUERYSTRING: - if (_httpurl_querystring_pos == 0) - { - val->rs.s[0] = '\0'; - val->rs.len = 0; + switch(subtype) { + case TR_XHTTPURL_PATH: + val->rs.len = (_httpurl_querystring_pos == 0) + ? val->rs.len + : _httpurl_querystring_pos - 1; break; - } - val->rs.s = &val->rs.s[_httpurl_querystring_pos]; - val->rs.len = val->rs.len - _httpurl_querystring_pos; - break; + case TR_XHTTPURL_QUERYSTRING: + if(_httpurl_querystring_pos == 0) { + val->rs.s[0] = '\0'; + val->rs.len = 0; + break; + } + + val->rs.s = &val->rs.s[_httpurl_querystring_pos]; + val->rs.len = val->rs.len - _httpurl_querystring_pos; + break; - default: - LM_ERR("unknown subtype %d\n", subtype); - return -1; + default: + LM_ERR("unknown subtype %d\n", subtype); + return -1; } return 0; @@ -100,7 +109,7 @@ char *xhttp_tr_parse_url(str *in, trans_t *t) char *p; str name; - if (in == NULL || in->s == NULL || t == NULL) + if(in == NULL || in->s == NULL || t == NULL) return NULL; p = in->s; @@ -108,33 +117,28 @@ char *xhttp_tr_parse_url(str *in, trans_t *t) t->type = TR_XHTTPURL; t->trf = xhttp_tr_eval_xhttpurl; - /* find next token */ - while (is_in_str(p, in) && *p != TR_PARAM_MARKER && *p != TR_RBRACKET) - { + /* find next token */ + while(is_in_str(p, in) && *p != TR_PARAM_MARKER && *p != TR_RBRACKET) { p++; } - if (*p == '\0') - { - LM_ERR("invalid transformation: %.*s\n", in->len, in->s); - goto error; - } - name.len = p - name.s; - trim(&name); + if(*p == '\0') { + LM_ERR("invalid transformation: %.*s\n", in->len, in->s); + goto error; + } + name.len = p - name.s; + trim(&name); - if (name.len == 4 && strncasecmp(name.s, "path", 4) == 0) - { + if(name.len == 4 && strncasecmp(name.s, "path", 4) == 0) { t->subtype = TR_XHTTPURL_PATH; goto done; - } - else if (name.len == 11 && strncasecmp(name.s, "querystring", 11) == 0) - { + } else if(name.len == 11 && strncasecmp(name.s, "querystring", 11) == 0) { t->subtype = TR_XHTTPURL_QUERYSTRING; goto done; } - LM_ERR("unknown transformation: %.*s/%.*s/%d!\n", in->len, in->s, - name.len, name.s, name.len); + LM_ERR("unknown transformation: %.*s/%.*s/%d!\n", in->len, in->s, name.len, + name.s, name.len); error: return NULL; diff --git a/src/modules/xhttp/xhttp_trans.h b/src/modules/xhttp/xhttp_trans.h index 0750be73825..d0691da43e3 100644 --- a/src/modules/xhttp/xhttp_trans.h +++ b/src/modules/xhttp/xhttp_trans.h @@ -1,4 +1,4 @@ -/* +/* * Copyright (C) 2013 Crocodile RCS Ltd * * This file is part of Kamailio, a free SIP server. @@ -13,8 +13,8 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */