Browse code

xhttp: allow execution of callback function for event route via kemi

- new parameter event_callback

Daniel-Constantin Mierla authored on 17/04/2017 06:12:34
Showing 1 changed files
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2
- * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
2
+ * Copyright (C) 2010-2017 Daniel-Constantin Mierla (asipto.com)
3 3
  *
4 4
  * This file is part of Kamailio, a free SIP server.
5 5
  *
... ...
@@ -13,8 +13,8 @@
13 13
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14
  * GNU General Public License for more details.
15 15
  *
16
- * You should have received a copy of the GNU General Public License 
17
- * along with this program; if not, write to the Free Software 
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program; if not, write to the Free Software
18 18
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19 19
  *
20 20
  */
... ...
@@ -67,6 +67,7 @@ static regex_t xhttp_url_skip_regexp;
67 67
 /** SL API structure */
68 68
 sl_api_t slb;
69 69
 
70
+static str xhttp_event_callback = STR_NULL;
70 71
 
71 72
 static cmd_export_t cmds[] = {
72 73
 	{"xhttp_reply",    (cmd_function)w_xhttp_send_reply,
... ...
@@ -87,6 +88,7 @@ static pv_export_t mod_pvs[] = {
87 88
 static param_export_t params[] = {
88 89
 	{"url_match",       PARAM_STRING, &xhttp_url_match},
89 90
 	{"url_skip",        PARAM_STRING, &xhttp_url_skip},
91
+	{"event_callback",  PARAM_STR,    &xhttp_event_callback},
90 92
 	{0, 0, 0}
91 93
 };
92 94
 
... ...
@@ -120,18 +122,28 @@ static int mod_init(void)
120 122
 {
121 123
 	struct nonsip_hook nsh;
122 124
 	int route_no;
123
-	
124
-	route_no=route_get(&event_rt, "xhttp:request");
125
-	if (route_no==-1)
126
-	{
127
-		LM_ERR("failed to find event_route[xhttp:request]\n");
128
-		return -1;
129
-	}
130
-	if (event_rt.rlist[route_no]==0)
131
-	{
132
-		LM_WARN("event_route[xhttp:request] is empty\n");
125
+	sr_kemi_eng_t *keng = NULL;
126
+
127
+	if(xhttp_event_callback.s!=NULL && xhttp_event_callback.len>0) {
128
+		keng = sr_kemi_eng_get();
129
+		if(keng==NULL) {
130
+			LM_ERR("failed to find kemi engine\n");
131
+			return -1;
132
+		}
133
+		xhttp_route_no=-1;
134
+	} else {
135
+		route_no=route_lookup(&event_rt, "xhttp:request");
136
+		if (route_no==-1)
137
+		{
138
+			LM_ERR("failed to find event_route[xhttp:request]\n");
139
+			return -1;
140
+		}
141
+		if (event_rt.rlist[route_no]==0)
142
+		{
143
+			LM_WARN("event_route[xhttp:request] is empty\n");
144
+		}
145
+		xhttp_route_no=route_no;
133 146
 	}
134
-	xhttp_route_no=route_no;
135 147
 	
136 148
 	/* bind the SL API */
137 149
 	if (sl_load_api(&slb)!=0) {
... ...
@@ -241,10 +253,14 @@ static int xhttp_process_request(sip_msg_t* orig_msg,
241 253
 							  char* new_buf, unsigned int new_len)
242 254
 {
243 255
 	int ret;
256
+	int backup_rt;
244 257
 	sip_msg_t tmp_msg, *msg;
245 258
 	struct run_act_ctx ra_ctx;
246
-	
259
+	sr_kemi_eng_t *keng = NULL;
260
+	str evrtname = str_init("xhttp:request");
261
+
247 262
 	ret=0;
263
+	backup_rt = get_route_type();
248 264
 	if (new_buf && new_len)
249 265
 	{
250 266
 		memset(&tmp_msg, 0, sizeof(sip_msg_t));
... ...
@@ -270,17 +286,31 @@ static int xhttp_process_request(sip_msg_t* orig_msg,
270 286
 		LM_CRIT("strange message: %.*s\n", msg->len, msg->buf);
271 287
 		goto error;
272 288
 	}
289
+
290
+	set_route_type(EVENT_ROUTE);
273 291
 	if (exec_pre_script_cb(msg, REQUEST_CB_TYPE) == 0)
274 292
 	{
275 293
 		goto done;
276 294
 	}
277 295
 
278 296
 	init_run_actions_ctx(&ra_ctx);
279
-	if (run_actions(&ra_ctx, event_rt.rlist[xhttp_route_no], msg) < 0)
280
-	{
281
-		ret=-1;
282
-		LM_DBG("error while trying script\n");
283
-		goto done;
297
+	if(xhttp_route_no>=0) {
298
+		if (run_actions(&ra_ctx, event_rt.rlist[xhttp_route_no], msg) < 0)
299
+		{
300
+			ret=-1;
301
+			LM_DBG("error while trying script\n");
302
+			goto done;
303
+		}
304
+	} else {
305
+		keng = sr_kemi_eng_get();
306
+		if(keng!=NULL) {
307
+			if(keng->froute(msg, EVENT_ROUTE,
308
+						&xhttp_event_callback, &evrtname)<0) {
309
+				LM_ERR("error running event route kemi callback\n");
310
+			}
311
+		} else {
312
+			LM_ERR("no event route or kemi callback found for execution\n");
313
+		}
284 314
 	}
285 315
 
286 316
 done:
... ...
@@ -289,6 +319,7 @@ done:
289 319
 	{
290 320
 		free_sip_msg(msg);
291 321
 	}
322
+	set_route_type(backup_rt);
292 323
 	return ret;
293 324
 
294 325
 error:
... ...
@@ -538,4 +569,4 @@ int mod_register(char *path, int *dlflags, void *p1, void *p2)
538 569
 	return register_trans_mod(path, mod_trans);
539 570
 }
540 571
 
541
-/** @} */
542 572
\ No newline at end of file
573
+/** @} */