Browse code

xhttp: warning log if tcp_accept_no_cl is not set

Daniel-Constantin Mierla authored on 23/06/2021 06:48:10
Showing 1 changed files
... ...
@@ -41,6 +41,7 @@
41 41
 #include "../../core/sip_msg_clone.h"
42 42
 #include "../../core/mod_fix.h"
43 43
 #include "../../core/pvar.h"
44
+#include "../../core/tcp_options.h"
44 45
 #include "../../core/kemi.h"
45 46
 
46 47
 #include "api.h"
... ...
@@ -142,6 +143,11 @@ static int mod_init(void)
142 143
 		xhttp_route_no = route_no;
143 144
 	}
144 145
 
146
+	if(cfg_get(tcp, tcp_cfg, accept_no_cl)==0) {
147
+		LM_WARN("tcp_accept_no_cl not set - usually required"
148
+				" to handle HTTP requests with no Content-Lenght\n");
149
+	}
150
+
145 151
 	/* bind the SL API */
146 152
 	if(sl_load_api(&slb) != 0) {
147 153
 		LM_ERR("cannot bind to SL API\n");
Browse code

xhttp: kemi funtion to return $hu - http uri

Daniel-Constantin Mierla authored on 25/12/2020 19:00:49
Showing 1 changed files
... ...
@@ -513,6 +513,28 @@ int bind_xhttp(xhttp_api_t *api)
513 513
 	return 0;
514 514
 }
515 515
 
516
+/**
517
+ *
518
+ */
519
+static sr_kemi_xval_t _sr_kemi_xhttp_xval = {0};
520
+
521
+/**
522
+ *
523
+ */
524
+static sr_kemi_xval_t* ki_xhttp_get_hu(sip_msg_t *msg)
525
+{
526
+	memset(&_sr_kemi_xhttp_xval, 0, sizeof(sr_kemi_xval_t));
527
+
528
+	if(msg==NULL) {
529
+		sr_kemi_xval_null(&_sr_kemi_xhttp_xval, SR_KEMI_XVAL_NULL_EMPTY);
530
+		return &_sr_kemi_xhttp_xval;
531
+	}
532
+
533
+	_sr_kemi_xhttp_xval.vtype = SR_KEMIP_STR;
534
+	_sr_kemi_xhttp_xval.v.s = msg->first_line.u.request.uri;
535
+	return &_sr_kemi_xhttp_xval;
536
+}
537
+
516 538
 /**
517 539
  *
518 540
  */
... ...
@@ -523,6 +545,11 @@ static sr_kemi_t sr_kemi_xhttp_exports[] = {
523 545
 		{ SR_KEMIP_INT, SR_KEMIP_STR, SR_KEMIP_STR,
524 546
 			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
525 547
 	},
548
+	{ str_init("xhttp"), str_init("get_hu"),
549
+		SR_KEMIP_XVAL, ki_xhttp_get_hu,
550
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
551
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
552
+	},
526 553
 
527 554
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
528 555
 };
Browse code

xhttp: clang format code

Daniel-Constantin Mierla authored on 16/12/2020 07:29:41
Showing 1 changed files
... ...
@@ -48,20 +48,19 @@
48 48
 
49 49
 MODULE_VERSION
50 50
 
51
-static int xhttp_handler(sip_msg_t* msg);
52
-static int w_xhttp_send_reply(sip_msg_t* msg, char* pcode, char* preason,
53
-		char *pctype, char* pbody);
51
+static int xhttp_handler(sip_msg_t *msg);
52
+static int w_xhttp_send_reply(
53
+		sip_msg_t *msg, char *pcode, char *preason, char *pctype, char *pbody);
54 54
 static int mod_init(void);
55 55
 
56
-static int fixup_xhttp_reply(void** param, int param_no);
56
+static int fixup_xhttp_reply(void **param, int param_no);
57 57
 
58
-static int pv_get_huri(struct sip_msg *msg, pv_param_t *param,
59
-		pv_value_t *res);
58
+static int pv_get_huri(struct sip_msg *msg, pv_param_t *param, pv_value_t *res);
60 59
 
61
-static int xhttp_route_no=DEFAULT_RT;
62
-static char* xhttp_url_match = NULL;
60
+static int xhttp_route_no = DEFAULT_RT;
61
+static char *xhttp_url_match = NULL;
63 62
 static regex_t xhttp_url_match_regexp;
64
-static char* xhttp_url_skip = NULL;
63
+static char *xhttp_url_skip = NULL;
65 64
 static regex_t xhttp_url_skip_regexp;
66 65
 
67 66
 /** SL API structure */
... ...
@@ -69,14 +68,7 @@ sl_api_t slb;
69 68
 
70 69
 static str xhttp_event_callback = STR_NULL;
71 70
 
72
-static cmd_export_t cmds[] = {
73
-	{"xhttp_reply",    (cmd_function)w_xhttp_send_reply,
74
-		4, fixup_xhttp_reply,  0, REQUEST_ROUTE},
75
-	{"bind_xhttp",     (cmd_function)bind_xhttp,
76
-		0, 0, 0, ANY_ROUTE},
77
-	{0, 0, 0, 0, 0}
78
-};
79
-
71
+/* clang-format off */
80 72
 static pv_export_t mod_pvs[] = {
81 73
 	{{"hu", (sizeof("hu")-1)}, /* */
82 74
 		PVT_OTHER, pv_get_huri, 0,
... ...
@@ -85,6 +77,13 @@ static pv_export_t mod_pvs[] = {
85 77
 	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
86 78
 };
87 79
 
80
+static tr_export_t mod_trans[] = {
81
+	{ {"url", sizeof("url")-1},
82
+		xhttp_tr_parse_url },
83
+
84
+	{ { 0, 0 }, 0 }
85
+};
86
+
88 87
 static param_export_t params[] = {
89 88
 	{"url_match",       PARAM_STRING, &xhttp_url_match},
90 89
 	{"url_skip",        PARAM_STRING, &xhttp_url_skip},
... ...
@@ -92,6 +91,14 @@ static param_export_t params[] = {
92 91
 	{0, 0, 0}
93 92
 };
94 93
 
94
+static cmd_export_t cmds[] = {
95
+	{"xhttp_reply",    (cmd_function)w_xhttp_send_reply,
96
+		4, fixup_xhttp_reply,  0, REQUEST_ROUTE},
97
+	{"bind_xhttp",     (cmd_function)bind_xhttp,
98
+		0, 0, 0, ANY_ROUTE},
99
+	{0, 0, 0, 0, 0}
100
+};
101
+
95 102
 /** module exports */
96 103
 struct module_exports exports= {
97 104
 	"xhttp",         /* module name */
... ...
@@ -105,13 +112,7 @@ struct module_exports exports= {
105 112
 	0,               /* per-child init function */
106 113
 	0                /* module destroy function */
107 114
 };
108
-
109
-static tr_export_t mod_trans[] = {
110
-	{ {"url", sizeof("url")-1},
111
-		xhttp_tr_parse_url },
112
-
113
-	{ { 0, 0 }, 0 }
114
-};
115
+/* clang-format on */
115 116
 
116 117
 /**
117 118
  *
... ...
@@ -122,29 +123,27 @@ static int mod_init(void)
122 123
 	int route_no;
123 124
 	sr_kemi_eng_t *keng = NULL;
124 125
 
125
-	if(xhttp_event_callback.s!=NULL && xhttp_event_callback.len>0) {
126
+	if(xhttp_event_callback.s != NULL && xhttp_event_callback.len > 0) {
126 127
 		keng = sr_kemi_eng_get();
127
-		if(keng==NULL) {
128
+		if(keng == NULL) {
128 129
 			LM_ERR("failed to find kemi engine\n");
129 130
 			return -1;
130 131
 		}
131
-		xhttp_route_no=-1;
132
+		xhttp_route_no = -1;
132 133
 	} else {
133
-		route_no=route_lookup(&event_rt, "xhttp:request");
134
-		if (route_no==-1)
135
-		{
134
+		route_no = route_lookup(&event_rt, "xhttp:request");
135
+		if(route_no == -1) {
136 136
 			LM_ERR("failed to find event_route[xhttp:request]\n");
137 137
 			return -1;
138 138
 		}
139
-		if (event_rt.rlist[route_no]==0)
140
-		{
139
+		if(event_rt.rlist[route_no] == 0) {
141 140
 			LM_WARN("event_route[xhttp:request] is empty\n");
142 141
 		}
143
-		xhttp_route_no=route_no;
142
+		xhttp_route_no = route_no;
144 143
 	}
145
-	
144
+
146 145
 	/* bind the SL API */
147
-	if (sl_load_api(&slb)!=0) {
146
+	if(sl_load_api(&slb) != 0) {
148 147
 		LM_ERR("cannot bind to SL API\n");
149 148
 		return -1;
150 149
 	}
... ...
@@ -154,24 +153,22 @@ static int mod_init(void)
154 153
 	nsh.name = "xhttp";
155 154
 	nsh.destroy = 0;
156 155
 	nsh.on_nonsip_req = xhttp_handler;
157
-	if (register_nonsip_msg_hook(&nsh)<0)
158
-	{
156
+	if(register_nonsip_msg_hook(&nsh) < 0) {
159 157
 		LM_ERR("Failed to register non sip msg hooks\n");
160 158
 		return -1;
161 159
 	}
162 160
 
163
-	if(xhttp_url_match!=NULL)
164
-	{
161
+	if(xhttp_url_match != NULL) {
165 162
 		memset(&xhttp_url_match_regexp, 0, sizeof(regex_t));
166
-		if (regcomp(&xhttp_url_match_regexp, xhttp_url_match, REG_EXTENDED)!=0) {
163
+		if(regcomp(&xhttp_url_match_regexp, xhttp_url_match, REG_EXTENDED)
164
+				!= 0) {
167 165
 			LM_ERR("bad match re %s\n", xhttp_url_match);
168 166
 			return E_BAD_RE;
169 167
 		}
170 168
 	}
171
-	if(xhttp_url_skip!=NULL)
172
-	{
169
+	if(xhttp_url_skip != NULL) {
173 170
 		memset(&xhttp_url_skip_regexp, 0, sizeof(regex_t));
174
-		if (regcomp(&xhttp_url_skip_regexp, xhttp_url_skip, REG_EXTENDED)!=0) {
171
+		if(regcomp(&xhttp_url_skip_regexp, xhttp_url_skip, REG_EXTENDED) != 0) {
175 172
 			LM_ERR("bad skip re %s\n", xhttp_url_skip);
176 173
 			return E_BAD_RE;
177 174
 		}
... ...
@@ -180,30 +177,29 @@ static int mod_init(void)
180 177
 }
181 178
 
182 179
 
183
-/** 
184
- * 
180
+/**
181
+ *
185 182
  */
186
-static int pv_get_huri(struct sip_msg *msg, pv_param_t *param,
187
-		pv_value_t *res)
183
+static int pv_get_huri(struct sip_msg *msg, pv_param_t *param, pv_value_t *res)
188 184
 {
189
-	if(msg==NULL || res==NULL)
185
+	if(msg == NULL || res == NULL)
190 186
 		return -1;
191 187
 
192 188
 	return pv_get_strval(msg, param, res, &msg->first_line.u.request.uri);
193 189
 }
194 190
 
195 191
 
196
-/** 
197
- * 
192
+/**
193
+ *
198 194
  */
199
-static char* xhttp_to_sip(sip_msg_t* msg, int* new_msg_len)
195
+static char *xhttp_to_sip(sip_msg_t *msg, int *new_msg_len)
200 196
 {
201 197
 	unsigned int len, via_len;
202
-	char* via, *new_msg, *p;
198
+	char *via, *new_msg, *p;
203 199
 	str ip, port;
204 200
 	struct hostport hp;
205 201
 	struct dest_info dst;
206
-	
202
+
207 203
 	ip.s = ip_addr2strz(&msg->rcv.src_ip);
208 204
 	ip.len = strlen(ip.s);
209 205
 	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)
211 207
 	hp.port = &port;
212 208
 	init_dst_from_rcv(&dst, &msg->rcv);
213 209
 	via = via_builder(&via_len, NULL, &dst, 0, 0, &hp);
214
-	if (via == 0)
215
-	{
210
+	if(via == 0) {
216 211
 		LM_DBG("failed to build via\n");
217 212
 		return 0;
218 213
 	}
219 214
 	len = via_len + msg->len;
220 215
 	p = new_msg = pkg_malloc(len + 1);
221
-	if (new_msg == 0)
222
-	{
216
+	if(new_msg == 0) {
223 217
 		PKG_MEM_ERROR_FMT(" (%d bytes)\n", len);
224 218
 		pkg_free(via);
225 219
 		return 0;
226 220
 	}
227 221
 
228 222
 	/* new message:
229
-	 * <orig first line> 
223
+	 * <orig first line>
230 224
 	 * Via: <faked via>
231 225
 	 * <orig http message w/o the first line>
232 226
 	 */
233
-	memcpy(p, msg->first_line.u.request.method.s, 
234
-		   msg->first_line.len);
227
+	memcpy(p, msg->first_line.u.request.method.s, msg->first_line.len);
235 228
 	p += msg->first_line.len;
236 229
 	memcpy(p, via, via_len);
237 230
 	p += via_len;
238
-	memcpy(p,  SIP_MSG_START(msg) + msg->first_line.len, 
239
-		   msg->len - msg->first_line.len);
231
+	memcpy(p, SIP_MSG_START(msg) + msg->first_line.len,
232
+			msg->len - msg->first_line.len);
240 233
 	new_msg[len] = 0;
241 234
 	pkg_free(via);
242 235
 	*new_msg_len = len;
... ...
@@ -244,11 +237,11 @@ static char* xhttp_to_sip(sip_msg_t* msg, int* new_msg_len)
244 237
 }
245 238
 
246 239
 
247
-/** 
248
- * 
240
+/**
241
+ *
249 242
  */
250
-static int xhttp_process_request(sip_msg_t* orig_msg, 
251
-							  char* new_buf, unsigned int new_len)
243
+static int xhttp_process_request(
244
+		sip_msg_t *orig_msg, char *new_buf, unsigned int new_len)
252 245
 {
253 246
 	int ret;
254 247
 	int backup_rt;
... ...
@@ -257,10 +250,9 @@ static int xhttp_process_request(sip_msg_t* orig_msg,
257 250
 	sr_kemi_eng_t *keng = NULL;
258 251
 	str evrtname = str_init("xhttp:request");
259 252
 
260
-	ret=0;
253
+	ret = 0;
261 254
 	backup_rt = get_route_type();
262
-	if (new_buf && new_len)
263
-	{
255
+	if(new_buf && new_len) {
264 256
 		memset(&tmp_msg, 0, sizeof(sip_msg_t));
265 257
 		tmp_msg.buf = new_buf;
266 258
 		tmp_msg.len = new_len;
... ...
@@ -268,8 +260,7 @@ static int xhttp_process_request(sip_msg_t* orig_msg,
268 260
 		tmp_msg.id = orig_msg->id;
269 261
 		tmp_msg.set_global_address = orig_msg->set_global_address;
270 262
 		tmp_msg.set_global_port = orig_msg->set_global_port;
271
-		if (parse_msg(new_buf, new_len, &tmp_msg) != 0)
272
-		{
263
+		if(parse_msg(new_buf, new_len, &tmp_msg) != 0) {
273 264
 			LM_ERR("parse_msg failed\n");
274 265
 			goto error;
275 266
 		}
... ...
@@ -277,33 +268,31 @@ static int xhttp_process_request(sip_msg_t* orig_msg,
277 268
 	} else {
278 269
 		msg = orig_msg;
279 270
 	}
280
-	
281
-	if ((msg->first_line.type != SIP_REQUEST) || (msg->via1 == 0) ||
282
-		(msg->via1->error != PARSE_OK))
283
-	{
271
+
272
+	if((msg->first_line.type != SIP_REQUEST) || (msg->via1 == 0)
273
+			|| (msg->via1->error != PARSE_OK)) {
284 274
 		LM_CRIT("strange message: %.*s\n", msg->len, msg->buf);
285 275
 		goto error;
286 276
 	}
287 277
 
288 278
 	set_route_type(EVENT_ROUTE);
289
-	if (exec_pre_script_cb(msg, REQUEST_CB_TYPE) == 0)
290
-	{
279
+	if(exec_pre_script_cb(msg, REQUEST_CB_TYPE) == 0) {
291 280
 		goto done;
292 281
 	}
293 282
 
294 283
 	init_run_actions_ctx(&ra_ctx);
295
-	if(xhttp_route_no>=0) {
296
-		if (run_actions(&ra_ctx, event_rt.rlist[xhttp_route_no], msg) < 0)
297
-		{
298
-			ret=-1;
284
+	if(xhttp_route_no >= 0) {
285
+		if(run_actions(&ra_ctx, event_rt.rlist[xhttp_route_no], msg) < 0) {
286
+			ret = -1;
299 287
 			LM_DBG("error while trying script\n");
300 288
 			goto done;
301 289
 		}
302 290
 	} else {
303 291
 		keng = sr_kemi_eng_get();
304
-		if(keng!=NULL) {
305
-			if(sr_kemi_route(keng, msg, EVENT_ROUTE,
306
-						&xhttp_event_callback, &evrtname)<0) {
292
+		if(keng != NULL) {
293
+			if(sr_kemi_route(
294
+					   keng, msg, EVENT_ROUTE, &xhttp_event_callback, &evrtname)
295
+					< 0) {
307 296
 				LM_ERR("error running event route kemi callback\n");
308 297
 			}
309 298
 		} else {
... ...
@@ -313,8 +302,7 @@ static int xhttp_process_request(sip_msg_t* orig_msg,
313 302
 
314 303
 done:
315 304
 	exec_post_script_cb(msg, REQUEST_CB_TYPE);
316
-	if (msg != orig_msg)
317
-	{
305
+	if(msg != orig_msg) {
318 306
 		free_sip_msg(msg);
319 307
 	}
320 308
 	set_route_type(backup_rt);
... ...
@@ -325,72 +313,68 @@ error:
325 313
 }
326 314
 
327 315
 
328
-/** 
329
- * 
316
+/**
317
+ *
330 318
  */
331
-static int xhttp_handler(sip_msg_t* msg)
319
+static int xhttp_handler(sip_msg_t *msg)
332 320
 {
333 321
 	int ret;
334
-	char* fake_msg;
322
+	char *fake_msg;
335 323
 	int fake_msg_len;
336 324
 	regmatch_t pmatch;
337 325
 	char c;
338 326
 
339
-	ret=NONSIP_MSG_DROP;
327
+	ret = NONSIP_MSG_DROP;
340 328
 
341
-	if(!IS_HTTP(msg))
342
-	{
329
+	if(!IS_HTTP(msg)) {
343 330
 		/* oly http msg type */
344 331
 		return NONSIP_MSG_PASS;
345 332
 	}
346 333
 
347
-	if(xhttp_url_skip!=NULL || xhttp_url_match!=NULL)
348
-	{
334
+	if(xhttp_url_skip != NULL || xhttp_url_match != NULL) {
349 335
 		c = msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len];
350
-		msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len]
351
-			= '\0';
352
-		if (xhttp_url_skip!=NULL &&
353
-			regexec(&xhttp_url_skip_regexp, msg->first_line.u.request.uri.s,
354
-					1, &pmatch, 0)==0)
355
-		{
336
+		msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len] =
337
+				'\0';
338
+		if(xhttp_url_skip != NULL
339
+				&& regexec(&xhttp_url_skip_regexp,
340
+						   msg->first_line.u.request.uri.s, 1, &pmatch, 0)
341
+						   == 0) {
356 342
 			LM_DBG("URL matched skip re\n");
357
-			msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len]
358
-				= c;
343
+			msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len] =
344
+					c;
359 345
 			return NONSIP_MSG_PASS;
360 346
 		}
361
-		if (xhttp_url_match!=NULL &&
362
-			regexec(&xhttp_url_match_regexp, msg->first_line.u.request.uri.s,
363
-					1, &pmatch, 0)!=0)
364
-		{
347
+		if(xhttp_url_match != NULL
348
+				&& regexec(&xhttp_url_match_regexp,
349
+						   msg->first_line.u.request.uri.s, 1, &pmatch, 0)
350
+						   != 0) {
365 351
 			LM_DBG("URL not matched\n");
366
-			msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len]
367
-				= c;
352
+			msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len] =
353
+					c;
368 354
 			return NONSIP_MSG_PASS;
369 355
 		}
370 356
 		msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len] = c;
371 357
 	}
372 358
 
373
-	if (msg->via1 == 0)
374
-	{
359
+	if(msg->via1 == 0) {
375 360
 		fake_msg = xhttp_to_sip(msg, &fake_msg_len);
376
-		if (fake_msg == 0)
377
-		{
361
+		if(fake_msg == 0) {
378 362
 			LM_ERR("out of memory\n");
379
-			ret=NONSIP_MSG_ERROR;
363
+			ret = NONSIP_MSG_ERROR;
380 364
 		} else {
381
-			DBG("new fake msg created (%d bytes):\n<%.*s>\n",
382
-					fake_msg_len, fake_msg_len, fake_msg);
383
-			if (xhttp_process_request(msg, fake_msg, fake_msg_len)<0) {
384
-				ret=NONSIP_MSG_ERROR;
365
+			DBG("new fake msg created (%d bytes):\n<%.*s>\n", fake_msg_len,
366
+					fake_msg_len, fake_msg);
367
+			if(xhttp_process_request(msg, fake_msg, fake_msg_len) < 0) {
368
+				ret = NONSIP_MSG_ERROR;
385 369
 			}
386 370
 			pkg_free(fake_msg);
387 371
 		}
388 372
 		return ret;
389 373
 	} else {
390
-		LM_DBG("http msg unchanged (%d bytes):\n<%.*s>\n",
391
-				msg->len, msg->len, msg->buf);
392
-		if (xhttp_process_request(msg, 0, 0)<0)
393
-			ret=NONSIP_MSG_ERROR;
374
+		LM_DBG("http msg unchanged (%d bytes):\n<%.*s>\n", msg->len, msg->len,
375
+				msg->buf);
376
+		if(xhttp_process_request(msg, 0, 0) < 0)
377
+			ret = NONSIP_MSG_ERROR;
394 378
 		return ret;
395 379
 	}
396 380
 }
... ...
@@ -399,28 +383,25 @@ static int xhttp_handler(sip_msg_t* msg)
399 383
 /**
400 384
  *
401 385
  */
402
-static int xhttp_send_reply(sip_msg_t *msg, int code, str *reason,
403
-		str *ctype, str *body)
386
+static int xhttp_send_reply(
387
+		sip_msg_t *msg, int code, str *reason, str *ctype, str *body)
404 388
 {
405 389
 	str tbuf;
406 390
 
407
-	if(ctype!=NULL && ctype->len>0)
408
-	{
391
+	if(ctype != NULL && ctype->len > 0) {
409 392
 		/* add content-type */
410
-		tbuf.len=sizeof("Content-Type: ") - 1 + ctype->len + CRLF_LEN;
411
-		tbuf.s=pkg_malloc(sizeof(char)*(tbuf.len));
393
+		tbuf.len = sizeof("Content-Type: ") - 1 + ctype->len + CRLF_LEN;
394
+		tbuf.s = pkg_malloc(sizeof(char) * (tbuf.len));
412 395
 
413
-		if (tbuf.s==0)
414
-		{
396
+		if(tbuf.s == 0) {
415 397
 			PKG_MEM_ERROR;
416 398
 			return -1;
417 399
 		}
418 400
 		memcpy(tbuf.s, "Content-Type: ", sizeof("Content-Type: ") - 1);
419
-		memcpy(tbuf.s+sizeof("Content-Type: ") - 1, ctype->s, ctype->len);
420
-		memcpy(tbuf.s+sizeof("Content-Type: ") - 1 + ctype->len,
421
-				CRLF, CRLF_LEN);
422
-		if (add_lump_rpl(msg, tbuf.s, tbuf.len, LUMP_RPL_HDR) == 0)
423
-		{
401
+		memcpy(tbuf.s + sizeof("Content-Type: ") - 1, ctype->s, ctype->len);
402
+		memcpy(tbuf.s + sizeof("Content-Type: ") - 1 + ctype->len, CRLF,
403
+				CRLF_LEN);
404
+		if(add_lump_rpl(msg, tbuf.s, tbuf.len, LUMP_RPL_HDR) == 0) {
424 405
 			LM_ERR("failed to insert content-type lump\n");
425 406
 			pkg_free(tbuf.s);
426 407
 			return -1;
... ...
@@ -429,18 +410,15 @@ static int xhttp_send_reply(sip_msg_t *msg, int code, str *reason,
429 410
 		LM_DBG("response with content-type: %.*s\n", ctype->len, ctype->s);
430 411
 	}
431 412
 
432
-	if(body!=NULL && body->len>0)
433
-	{
434
-		if (add_lump_rpl(msg, body->s, body->len, LUMP_RPL_BODY) == 0)
435
-		{
413
+	if(body != NULL && body->len > 0) {
414
+		if(add_lump_rpl(msg, body->s, body->len, LUMP_RPL_BODY) == 0) {
436 415
 			LM_ERR("Error while adding reply lump\n");
437 416
 			return -1;
438 417
 		}
439 418
 		LM_DBG("response with body: %.*s\n", body->len, body->s);
440 419
 	}
441 420
 	LM_DBG("sending out response: %d %.*s\n", code, reason->len, reason->s);
442
-	if (slb.sreply(msg, code, reason) < 0)
443
-	{
421
+	if(slb.sreply(msg, code, reason) < 0) {
444 422
 		LM_ERR("Error while sending reply\n");
445 423
 		return -1;
446 424
 	}
... ...
@@ -450,83 +428,74 @@ static int xhttp_send_reply(sip_msg_t *msg, int code, str *reason,
450 428
 /**
451 429
  *
452 430
  */
453
-static int w_xhttp_send_reply(sip_msg_t* msg, char* pcode, char* preason,
454
-		char *pctype, char* pbody)
431
+static int w_xhttp_send_reply(
432
+		sip_msg_t *msg, char *pcode, char *preason, char *pctype, char *pbody)
455 433
 {
456 434
 	str body = {0, 0};
457 435
 	str reason = {"OK", 2};
458 436
 	str ctype = {"text/plain", 10};
459 437
 	int code = 200;
460 438
 
461
-	if(pcode==0 || preason==0 || pctype==0 || pbody==0)
462
-	{
439
+	if(pcode == 0 || preason == 0 || pctype == 0 || pbody == 0) {
463 440
 		LM_ERR("invalid parameters\n");
464 441
 		return -1;
465 442
 	}
466 443
 
467
-	if(fixup_get_ivalue(msg, (gparam_p)pcode, &code)!=0)
468
-	{
444
+	if(fixup_get_ivalue(msg, (gparam_p)pcode, &code) != 0) {
469 445
 		LM_ERR("no reply code value\n");
470 446
 		return -1;
471 447
 	}
472
-	if(code<100 || code>700)
473
-	{
448
+	if(code < 100 || code > 700) {
474 449
 		LM_ERR("invalid code parameter\n");
475 450
 		return -1;
476 451
 	}
477 452
 
478
-	if(fixup_get_svalue(msg, (gparam_p)preason, &reason)!=0)
479
-	{
453
+	if(fixup_get_svalue(msg, (gparam_p)preason, &reason) != 0) {
480 454
 		LM_ERR("unable to get reason\n");
481 455
 		return -1;
482 456
 	}
483
-	if(reason.s==NULL || reason.len == 0)
484
-	{
457
+	if(reason.s == NULL || reason.len == 0) {
485 458
 		LM_ERR("invalid reason parameter\n");
486 459
 		return -1;
487 460
 	}
488 461
 
489
-	if(fixup_get_svalue(msg, (gparam_p)pctype, &ctype)!=0)
490
-	{
462
+	if(fixup_get_svalue(msg, (gparam_p)pctype, &ctype) != 0) {
491 463
 		LM_ERR("unable to get content type\n");
492 464
 		return -1;
493 465
 	}
494
-	if(ctype.s==NULL)
495
-	{
466
+	if(ctype.s == NULL) {
496 467
 		LM_ERR("invalid content-type parameter\n");
497 468
 		return -1;
498 469
 	}
499 470
 
500
-	if(fixup_get_svalue(msg, (gparam_p)pbody, &body)!=0)
501
-	{
471
+	if(fixup_get_svalue(msg, (gparam_p)pbody, &body) != 0) {
502 472
 		LM_ERR("unable to get body\n");
503 473
 		return -1;
504 474
 	}
505
-	if(body.s==NULL)
506
-	{
475
+	if(body.s == NULL) {
507 476
 		LM_ERR("invalid body parameter\n");
508 477
 		return -1;
509 478
 	}
510 479
 
511
-	if(xhttp_send_reply(msg, code, &reason, &ctype, &body)<0)
480
+	if(xhttp_send_reply(msg, code, &reason, &ctype, &body) < 0)
512 481
 		return -1;
513 482
 	return 1;
514 483
 }
515 484
 
516 485
 
517
-/** 
518
- * 
486
+/**
487
+ *
519 488
  */
520
-static int fixup_xhttp_reply(void** param, int param_no)
489
+static int fixup_xhttp_reply(void **param, int param_no)
521 490
 {
522
-	if (param_no == 1) {
523
-	    return fixup_igp_null(param, 1);
524
-	} else if (param_no == 2) {
525
-	    return fixup_spve_null(param, 1);
526
-	} else if (param_no == 3) {
527
-	    return fixup_spve_null(param, 1);
528
-	} else if (param_no == 4) {
529
-	    return fixup_spve_null(param, 1);
491
+	if(param_no == 1) {
492
+		return fixup_igp_null(param, 1);
493
+	} else if(param_no == 2) {
494
+		return fixup_spve_null(param, 1);
495
+	} else if(param_no == 3) {
496
+		return fixup_spve_null(param, 1);
497
+	} else if(param_no == 4) {
498
+		return fixup_spve_null(param, 1);
530 499
 	}
531 500
 	return 0;
532 501
 }
... ...
@@ -534,9 +503,9 @@ static int fixup_xhttp_reply(void** param, int param_no)
534 503
 /**
535 504
  *
536 505
  */
537
-int bind_xhttp(xhttp_api_t* api)
506
+int bind_xhttp(xhttp_api_t *api)
538 507
 {
539
-	if (!api) {
508
+	if(!api) {
540 509
 		ERR("Invalid parameter value\n");
541 510
 		return -1;
542 511
 	}
Browse code

xhttp: convert to memory logging helper

Henning Westerholt authored on 25/01/2020 18:20:34
Showing 1 changed files
... ...
@@ -220,7 +220,7 @@ static char* xhttp_to_sip(sip_msg_t* msg, int* new_msg_len)
220 220
 	p = new_msg = pkg_malloc(len + 1);
221 221
 	if (new_msg == 0)
222 222
 	{
223
-		LM_DBG("memory allocation failure (%d bytes)\n", len);
223
+		PKG_MEM_ERROR_FMT(" (%d bytes)\n", len);
224 224
 		pkg_free(via);
225 225
 		return 0;
226 226
 	}
... ...
@@ -412,7 +412,7 @@ static int xhttp_send_reply(sip_msg_t *msg, int code, str *reason,
412 412
 
413 413
 		if (tbuf.s==0)
414 414
 		{
415
-			LM_ERR("out of pkg memory\n");
415
+			PKG_MEM_ERROR;
416 416
 			return -1;
417 417
 		}
418 418
 		memcpy(tbuf.s, "Content-Type: ", sizeof("Content-Type: ") - 1);
Browse code

xhttp: use kemi wrapper to execute routing functions

Daniel-Constantin Mierla authored on 27/02/2019 08:23:13
Showing 1 changed files
... ...
@@ -302,7 +302,7 @@ static int xhttp_process_request(sip_msg_t* orig_msg,
302 302
 	} else {
303 303
 		keng = sr_kemi_eng_get();
304 304
 		if(keng!=NULL) {
305
-			if(keng->froute(msg, EVENT_ROUTE,
305
+			if(sr_kemi_route(keng, msg, EVENT_ROUTE,
306 306
 						&xhttp_event_callback, &evrtname)<0) {
307 307
 				LM_ERR("error running event route kemi callback\n");
308 308
 			}
Browse code

xhttp: updates for new signature of via_builder()

Daniel-Constantin Mierla authored on 04/10/2018 14:47:25
Showing 1 changed files
... ...
@@ -210,7 +210,7 @@ static char* xhttp_to_sip(sip_msg_t* msg, int* new_msg_len)
210 210
 	hp.host = &ip;
211 211
 	hp.port = &port;
212 212
 	init_dst_from_rcv(&dst, &msg->rcv);
213
-	via = via_builder(&via_len, &dst, 0, 0, &hp);
213
+	via = via_builder(&via_len, NULL, &dst, 0, 0, &hp);
214 214
 	if (via == 0)
215 215
 	{
216 216
 		LM_DBG("failed to build via\n");
Browse code

xhttp: updated to the new mod interface

Daniel-Constantin Mierla authored on 27/09/2018 15:50:33 • Victor Seva committed on 28/09/2018 11:03:26
Showing 1 changed files
... ...
@@ -94,18 +94,16 @@ static param_export_t params[] = {
94 94
 
95 95
 /** module exports */
96 96
 struct module_exports exports= {
97
-	"xhttp",
97
+	"xhttp",         /* module name */
98 98
 	DEFAULT_DLFLAGS, /* dlopen flags */
99
-	cmds,
100
-	params,
101
-	0,          /* exported statistics */
102
-	0  ,        /* exported MI functions */
103
-	mod_pvs,    /* exported pseudo-variables */
104
-	0,          /* extra processes */
105
-	mod_init,   /* module initialization function */
106
-	0,
107
-	0,
108
-	0           /* per-child init function */
99
+	cmds,            /* cmd (cfg function) exports */
100
+	params,          /* param exports */
101
+	0,               /* exported rpc functions */
102
+	mod_pvs,         /* exported pseudo-variables */
103
+	0,               /* response handling function */
104
+	mod_init,        /* module init function */
105
+	0,               /* per-child init function */
106
+	0                /* module destroy function */
109 107
 };
110 108
 
111 109
 static tr_export_t mod_trans[] = {
... ...
@@ -115,8 +113,8 @@ static tr_export_t mod_trans[] = {
115 113
 	{ { 0, 0 }, 0 }
116 114
 };
117 115
 
118
-/** 
119
- * 
116
+/**
117
+ *
120 118
  */
121 119
 static int mod_init(void)
122 120
 {
Browse code

xhttp: updating name of function and parameter type

- Updating the name to reflect the original name. Mismatch between export and function declaration with regards to the type of parameters.

Samuel authored on 26/11/2017 20:33:19 • Daniel-Constantin Mierla committed on 27/11/2017 07:29:08
Showing 1 changed files
... ...
@@ -551,9 +551,9 @@ int bind_xhttp(xhttp_api_t* api)
551 551
  */
552 552
 /* clang-format off */
553 553
 static sr_kemi_t sr_kemi_xhttp_exports[] = {
554
-	{ str_init("xhttp"), str_init("reply"),
554
+	{ str_init("xhttp"), str_init("xhttp_reply"),
555 555
 		SR_KEMIP_INT, xhttp_send_reply,
556
-		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
556
+		{ SR_KEMIP_INT, SR_KEMIP_STR, SR_KEMIP_STR,
557 557
 			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
558 558
 	},
559 559
 
Browse code

xhttp: proper check for return of add lump rpl

Daniel-Constantin Mierla authored on 21/07/2017 11:51:36
Showing 1 changed files
... ...
@@ -382,11 +382,12 @@ static int xhttp_handler(sip_msg_t* msg)
382 382
 		} else {
383 383
 			DBG("new fake msg created (%d bytes):\n<%.*s>\n",
384 384
 					fake_msg_len, fake_msg_len, fake_msg);
385
-			if (xhttp_process_request(msg, fake_msg, fake_msg_len)<0)
385
+			if (xhttp_process_request(msg, fake_msg, fake_msg_len)<0) {
386 386
 				ret=NONSIP_MSG_ERROR;
387
-				pkg_free(fake_msg);
388 387
 			}
389
-			return ret;
388
+			pkg_free(fake_msg);
389
+		}
390
+		return ret;
390 391
 	} else {
391 392
 		LM_DBG("http msg unchanged (%d bytes):\n<%.*s>\n",
392 393
 				msg->len, msg->len, msg->buf);
... ...
@@ -432,7 +433,7 @@ static int xhttp_send_reply(sip_msg_t *msg, int code, str *reason,
432 433
 
433 434
 	if(body!=NULL && body->len>0)
434 435
 	{
435
-		if (add_lump_rpl(msg, body->s, body->len, LUMP_RPL_BODY) < 0)
436
+		if (add_lump_rpl(msg, body->s, body->len, LUMP_RPL_BODY) == 0)
436 437
 		{
437 438
 			LM_ERR("Error while adding reply lump\n");
438 439
 			return -1;
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) {