Browse code

posops: added functions to get where the headers and body start or end

Daniel-Constantin Mierla authored on 17/09/2021 10:58:28
Showing 2 changed files
... ...
@@ -131,6 +131,82 @@ pos_insert("100", "kamailio-$si");
131 131
 				<programlisting format="linespecific">
132 132
 ...
133 133
 pos_insert("100", "10");
134
+...
135
+				</programlisting>
136
+			</example>
137
+		</section>
138
+		<section id="posops.f.pos_headers_start">
139
+			<title>
140
+				<function moreinfo="none">pos_headers_start()</function>
141
+			</title>
142
+			<para>
143
+			Return the position in the message buffer where headers start.
144
+			</para>
145
+			<para>
146
+			This function can be used from ANY_ROUTE.
147
+			</para>
148
+			<example>
149
+				<title><function>pos_headers_start()</function> usage</title>
150
+				<programlisting format="linespecific">
151
+...
152
+$var(pos) = pos_headers_start();
153
+...
154
+				</programlisting>
155
+			</example>
156
+		</section>
157
+		<section id="posops.f.pos_headers_end">
158
+			<title>
159
+				<function moreinfo="none">pos_headers_end()</function>
160
+			</title>
161
+			<para>
162
+			Return the position in the message buffer where headers end.
163
+			</para>
164
+			<para>
165
+			This function can be used from ANY_ROUTE.
166
+			</para>
167
+			<example>
168
+				<title><function>pos_headers_end()</function> usage</title>
169
+				<programlisting format="linespecific">
170
+...
171
+$var(pos) = pos_headers_end();
172
+...
173
+				</programlisting>
174
+			</example>
175
+		</section>
176
+		<section id="posops.f.pos_body_start">
177
+			<title>
178
+				<function moreinfo="none">pos_body_start()</function>
179
+			</title>
180
+			<para>
181
+			Return the position in the message buffer where body starts.
182
+			</para>
183
+			<para>
184
+			This function can be used from ANY_ROUTE.
185
+			</para>
186
+			<example>
187
+				<title><function>pos_body_start()</function> usage</title>
188
+				<programlisting format="linespecific">
189
+...
190
+$var(pos) = pos_body_start();
191
+...
192
+				</programlisting>
193
+			</example>
194
+		</section>
195
+		<section id="posops.f.pos_body_end">
196
+			<title>
197
+				<function moreinfo="none">pos_body_end()</function>
198
+			</title>
199
+			<para>
200
+			Return the position in the message buffer where body ends.
201
+			</para>
202
+			<para>
203
+			This function can be used from ANY_ROUTE.
204
+			</para>
205
+			<example>
206
+				<title><function>pos_body_end()</function> usage</title>
207
+				<programlisting format="linespecific">
208
+...
209
+$var(pos) = pos_body_end();
134 210
 ...
135 211
 				</programlisting>
136 212
 			</example>
... ...
@@ -41,12 +41,21 @@ static void mod_destroy(void);
41 41
 static int w_posops_pos_append(sip_msg_t* msg, char* p1idx, char* p2val);
42 42
 static int w_posops_pos_insert(sip_msg_t* msg, char* p1idx, char* p2val);
43 43
 static int w_posops_pos_rm(sip_msg_t* msg, char* p1idx, char* p2len);
44
+static int w_posops_pos_headers_start(sip_msg_t* msg, char* p1, char* p2);
45
+static int w_posops_pos_headers_end(sip_msg_t* msg, char* p1, char* p2);
46
+static int w_posops_pos_body_start(sip_msg_t* msg, char* p1, char* p2);
47
+static int w_posops_pos_body_end(sip_msg_t* msg, char* p1, char* p2);
44 48
 
45 49
 typedef struct posops_data {
46 50
 	int ret;
47 51
 	int idx;
48 52
 } pospos_data_t;
49 53
 
54
+/**
55
+ *
56
+ */
57
+static pospos_data_t _pospos_data = {0};
58
+
50 59
 /* clang-format off */
51 60
 static cmd_export_t cmds[]={
52 61
 	{"pos_append", (cmd_function)w_posops_pos_append, 2, fixup_igp_spve,
... ...
@@ -55,6 +64,14 @@ static cmd_export_t cmds[]={
55 64
 		fixup_free_igp_spve, ANY_ROUTE},
56 65
 	{"pos_rm", (cmd_function)w_posops_pos_rm, 2, fixup_igp_igp,
57 66
 		fixup_free_igp_igp, ANY_ROUTE},
67
+	{"pos_headers_start", (cmd_function)w_posops_pos_headers_start, 0, 0,
68
+		0, ANY_ROUTE},
69
+	{"pos_headers_end", (cmd_function)w_posops_pos_headers_end, 0, 0,
70
+		0, ANY_ROUTE},
71
+	{"pos_body_start", (cmd_function)w_posops_pos_body_start, 0, 0,
72
+		0, ANY_ROUTE},
73
+	{"pos_body_end", (cmd_function)w_posops_pos_body_end, 0, 0,
74
+		0, ANY_ROUTE},
58 75
 
59 76
 	{0, 0, 0, 0, 0, 0}
60 77
 };
... ...
@@ -270,6 +287,126 @@ static int w_posops_pos_rm(sip_msg_t* msg, char* p1idx, char* p2len)
270 287
 	return ki_posops_pos_rm(msg, idx, len);
271 288
 }
272 289
 
290
+/**
291
+ *
292
+ */
293
+static int ki_posops_pos_headers_start(sip_msg_t* msg)
294
+{
295
+	int ret = 0;
296
+
297
+	memset(&_pospos_data, 0, sizeof(pospos_data_t));
298
+	if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
299
+		LM_ERR("failed to parse headers\n");
300
+		return -1;
301
+	}
302
+
303
+	ret = msg->first_line.len;
304
+	_pospos_data.idx = ret;
305
+
306
+	return (ret==0)?-255:ret;
307
+}
308
+
309
+/**
310
+ *
311
+ */
312
+static int w_posops_pos_headers_start(sip_msg_t* msg, char* p1, char* p2)
313
+{
314
+	return ki_posops_pos_headers_start(msg);
315
+}
316
+
317
+/**
318
+ *
319
+ */
320
+static int ki_posops_pos_headers_end(sip_msg_t* msg)
321
+{
322
+	int ret = 0;
323
+
324
+	memset(&_pospos_data, 0, sizeof(pospos_data_t));
325
+	if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
326
+		LM_ERR("failed to parse headers\n");
327
+		return -1;
328
+	}
329
+
330
+	ret = msg->unparsed - msg->buf;
331
+	_pospos_data.idx = ret;
332
+
333
+	return (ret==0)?-255:ret;
334
+}
335
+
336
+/**
337
+ *
338
+ */
339
+static int w_posops_pos_headers_end(sip_msg_t* msg, char* p1, char* p2)
340
+{
341
+	return ki_posops_pos_headers_end(msg);
342
+}
343
+
344
+/**
345
+ *
346
+ */
347
+static int ki_posops_pos_body_start(sip_msg_t* msg)
348
+{
349
+	int ret = 0;
350
+	char *body = 0;
351
+
352
+	memset(&_pospos_data, 0, sizeof(pospos_data_t));
353
+	if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
354
+		LM_ERR("failed to parse headers\n");
355
+		return -1;
356
+	}
357
+
358
+	body = get_body(msg);
359
+	if(body == NULL) {
360
+		LM_DBG("no body\n");
361
+		return -1;
362
+	}
363
+	ret = body - msg->buf;
364
+	_pospos_data.idx = ret;
365
+
366
+	return (ret==0)?-255:ret;
367
+}
368
+
369
+/**
370
+ *
371
+ */
372
+static int w_posops_pos_body_start(sip_msg_t* msg, char* p1, char* p2)
373
+{
374
+	return ki_posops_pos_body_start(msg);
375
+}
376
+
377
+/**
378
+ *
379
+ */
380
+static int ki_posops_pos_body_end(sip_msg_t* msg)
381
+{
382
+	int ret = 0;
383
+
384
+	memset(&_pospos_data, 0, sizeof(pospos_data_t));
385
+	if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
386
+		LM_ERR("failed to parse headers\n");
387
+		return -1;
388
+	}
389
+
390
+	if(get_body(msg) == NULL) {
391
+		LM_DBG("no body\n");
392
+		return -1;
393
+	}
394
+
395
+	ret = msg->len;
396
+	_pospos_data.idx = ret;
397
+
398
+	return (ret==0)?-255:ret;
399
+}
400
+
401
+/**
402
+ *
403
+ */
404
+static int w_posops_pos_body_end(sip_msg_t* msg, char* p1, char* p2)
405
+{
406
+	return  ki_posops_pos_body_end(msg);
407
+}
408
+
409
+
273 410
 /**
274 411
  *
275 412
  */
... ...
@@ -290,6 +427,26 @@ static sr_kemi_t sr_kemi_posops_exports[] = {
290 427
 		{ SR_KEMIP_INT, SR_KEMIP_INT, SR_KEMIP_NONE,
291 428
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
292 429
 	},
430
+	{ str_init("posops"), str_init("pos_headers_start"),
431
+		SR_KEMIP_INT, ki_posops_pos_headers_start,
432
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
433
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
434
+	},
435
+	{ str_init("posops"), str_init("pos_headers_end"),
436
+		SR_KEMIP_INT, ki_posops_pos_headers_end,
437
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
438
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
439
+	},
440
+	{ str_init("posops"), str_init("pos_body_start"),
441
+		SR_KEMIP_INT, ki_posops_pos_body_start,
442
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
443
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
444
+	},
445
+	{ str_init("posops"), str_init("pos_body_end"),
446
+		SR_KEMIP_INT, ki_posops_pos_body_end,
447
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
448
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
449
+	},
293 450
 
294 451
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
295 452
 };