Browse code

- applied patch (modified) from Marc Haisenko <haisenko@comdasys.com>: provides new function has_body([mime])

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@759 689a6050-402a-0410-94f2-e92a70836424

Bogdan-Andrei Iancu authored on 17/03/2006 10:56:52
Showing 3 changed files
... ...
@@ -50,6 +50,7 @@ Daniel-Constantin Mierla
50 50
               1.3.20. append_time()
51 51
               1.3.21. is_method(name)
52 52
               1.3.22. remove_hf(hname)
53
+              1.3.23. has_body(), has_body(mime)
53 54
 
54 55
         1.4. Known Limitations
55 56
 
... ...
@@ -79,6 +80,7 @@ Daniel-Constantin Mierla
79 80
    1-20. append_time usage
80 81
    1-21. is_method usage
81 82
    1-22. remove_hf usage
83
+   1-23. has_body usage
82 84
      _________________________________________________________
83 85
 
84 86
 Chapter 1. User's Guide
... ...
@@ -602,6 +604,32 @@ if(remove_hf("User-Agent"))
602 604
 ...
603 605
      _________________________________________________________
604 606
 
607
+1.3.23. has_body(), has_body(mime)
608
+
609
+   The function returns true if the SIP message has a body
610
+   attached. The checked includes also the "Content-Lenght"
611
+   header presence and value.
612
+
613
+   If a paramter is given, the mime described will be also
614
+   checked against the "Content-Type" header.
615
+
616
+   Meaning of the parameters is as follows:
617
+
618
+     * mime - mime to be checked against the "Content-Type"
619
+       header. If not present or 0, this check will be disabled.
620
+
621
+   This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
622
+   FAILURE_ROUTE and BRANCH_ROUTE.
623
+
624
+   Example 1-23. has_body usage
625
+...
626
+if(has_body("application/sdp"))
627
+{
628
+    # do interesting stuff here
629
+}
630
+...
631
+     _________________________________________________________
632
+
605 633
 1.4. Known Limitations
606 634
 
607 635
    Search functions are applied to the original request, i.e.,
... ...
@@ -869,6 +869,46 @@ if(remove_hf("User-Agent"))
869 869
 		</example>
870 870
 	</section>
871 871
 
872
+	<section>
873
+		<title>
874
+		<function moreinfo="none">has_body()</function>,
875
+		<function moreinfo="none">has_body(mime)</function>
876
+		</title>
877
+		<para>
878
+		The function returns <emphasis>true</emphasis> if the SIP message
879
+		has a body attached. The checked includes also the 
880
+		<quote>Content-Lenght</quote> header presence and value.
881
+		</para>
882
+		<para>
883
+		If a paramter is given, the mime described will be also checked against
884
+		the <quote>Content-Type</quote> header.
885
+		</para>
886
+		<para>Meaning of the parameters is as follows:</para>
887
+		<itemizedlist>
888
+		<listitem>
889
+			<para><emphasis>mime</emphasis> - mime to be checked against the 
890
+				<quote>Content-Type</quote> header. If not present or 0, this
891
+				check will be disabled.
892
+			</para>
893
+		</listitem>
894
+		</itemizedlist>
895
+		<para>
896
+		This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE, 
897
+		FAILURE_ROUTE and BRANCH_ROUTE.
898
+		</para>
899
+		<example>
900
+		<title><function>has_body</function> usage</title>
901
+		<programlisting format="linespecific">
902
+...
903
+if(has_body("application/sdp"))
904
+{
905
+    # do interesting stuff here
906
+}
907
+...
908
+</programlisting>
909
+		</example>
910
+	</section>
911
+
872 912
 
873 913
 
874 914
 
... ...
@@ -42,13 +42,9 @@
42 42
  *  2004-07-06  subst_user added (like subst_uri but only for user) (sobomax)
43 43
  *  2004-11-12  subst_user changes (old serdev mails) (andrei)
44 44
  *  2005-07-05  is_method("name") to check method using id (ramona)
45
- *  
46
- * Example ser module, it implements the following commands:
47
- * search_append("key", "txt") - insert a "txt" after "key"
48
- * replace("txt1", "txt2") - replaces txt1 with txt2 (txt1 can be a re)
49
- * search("txt") - searches for txt (txt can be a regular expression)
50
- * append_to_reply("txt") - appends txt to the reply?
51
- * append_hf("P-foo: bar\r\n");
45
+ *  2006-03-17  applied patch from Marc Haisenko <haisenko@comdasys.com> 
46
+ *              for adding has_body() function (bogdan)
47
+ *
52 48
  */
53 49
 
54 50
 
... ...
@@ -64,6 +60,7 @@
64 60
 #include "../../parser/parse_uri.h"
65 61
 #include "../../parser/parse_hname2.h"
66 62
 #include "../../parser/parse_methods.h"
63
+#include "../../parser/parse_content.h"
67 64
 #include <stdio.h>
68 65
 #include <stdlib.h>
69 66
 #include <string.h>
... ...
@@ -107,6 +104,7 @@ static int insert_hf_2(struct sip_msg* msg, char* str1, char* str2);
107 104
 static int append_urihf(struct sip_msg* msg, char* str1, char* str2);
108 105
 static int append_time_f(struct sip_msg* msg, char* , char *);
109 106
 static int is_method_f(struct sip_msg* msg, char* , char *);
107
+static int has_body_f(struct sip_msg *msg, char *type, char *str2 );
110 108
 
111 109
 static int fixup_regex(void**, int);
112 110
 static int fixup_substre(void**, int);
... ...
@@ -115,6 +113,7 @@ static int hname_fixup(void** param, int param_no);
115 113
 static int fixup_method(void** param, int param_no);
116 114
 static int add_header_fixup(void** param, int param_no);
117 115
 static int it_list_fixup(void** param, int param_no);
116
+static int fixup_body_type(void** param, int param_no);
118 117
 
119 118
 static int mod_init(void);
120 119
 
... ...
@@ -164,26 +163,29 @@ static cmd_export_t cmds[]={
164 163
 			REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
165 164
 	{"is_method",        is_method_f,       1, fixup_method,
166 165
 			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
166
+	{"has_body",         has_body_f,        0, 0,
167
+			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
168
+	{"has_body",         has_body_f,        1, fixup_body_type,
169
+			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
167 170
 	{0,0,0,0,0}
168 171
 };
169 172
 
170
-static param_export_t params[]={ {0,0,0} }; /* no params */
171 173
 
172 174
 struct module_exports exports= {
173
-	"textops",
174
-	cmds,
175
-	params,
176
-	0,        /* exported statistics */
177
-	mod_init, /* module initialization function */
178
-	0, /* response function */
179
-	0,  /* destroy function */
180
-	0, /* per-child init function */
175
+	"textops",  /* module name*/
176
+	cmds,       /* exported functions */
177
+	0,          /* module parameters */
178
+	0,          /* exported statistics */
179
+	mod_init,   /* module initialization function */
180
+	0,          /* response function */
181
+	0,          /* destroy function */
182
+	0,          /* per-child init function */
181 183
 };
182 184
 
183 185
 
184 186
 static int mod_init(void)
185 187
 {
186
-	fprintf(stderr, "%s - initializing\n", exports.name);
188
+	LOG(L_INFO, "TextOPS - initializing\n");
187 189
 	return 0;
188 190
 }
189 191
 
... ...
@@ -1185,3 +1187,79 @@ static int add_header_fixup(void** param, int param_no)
1185 1187
 		return E_UNSPEC;
1186 1188
 	}
1187 1189
 }
1190
+
1191
+
1192
+static int fixup_body_type(void** param, int param_no)
1193
+{
1194
+	char *p;
1195
+	char *r;
1196
+	int type;
1197
+
1198
+	if(param_no==1) {
1199
+		p = (char*)*param;
1200
+		if (p==0 || p[0]==0) {
1201
+			type = 0;
1202
+		} else {
1203
+			r = decode_mime_type( p, p+strlen(p) , &type);
1204
+			if (r==0) {
1205
+				LOG(L_ERR,"ERROR:textops:fixup_body_type: unsupported "
1206
+					"mime <%s>\n",p);
1207
+				return E_CFG;
1208
+			}
1209
+			if ( r!=p+strlen(p) ) {
1210
+				LOG(L_ERR,"ERROR:textops:fixup_body_type: multiple mimes not "
1211
+					"supported!\n");
1212
+				return E_CFG;
1213
+			}
1214
+		}
1215
+		pkg_free(*param);
1216
+		*param = (void*)(long)type;
1217
+	}
1218
+	return 0;
1219
+
1220
+}
1221
+
1222
+
1223
+static int has_body_f(struct sip_msg *msg, char *type, char *str2 )
1224
+{
1225
+	int mime;
1226
+
1227
+	/* get body pointer */
1228
+	if ( get_body(msg)==0 )
1229
+		return -1;
1230
+
1231
+	/* all headears are already parsed by "get_body" */
1232
+	if (msg->content_length==0) {
1233
+		LOG (L_ERR, "ERROR:textops:has_body: very bogus message with body "
1234
+			"but no content length hdr\n");
1235
+		return -1;
1236
+	}
1237
+
1238
+	if (get_content_length (msg)==0) {
1239
+		DBG("DEBUG:textops:has_body: content length is zero\n");
1240
+		/* Nothing to see here, please move on. */
1241
+		return -1;
1242
+	}
1243
+
1244
+	/* check type also? */
1245
+	if (type==0)
1246
+		return 1;
1247
+
1248
+	mime = parse_content_type_hdr (msg);
1249
+	if (mime<0) {
1250
+		LOG (L_ERR, "ERROR:textops:has_body: failed to extract "
1251
+			"content type hdr\n");
1252
+		return -1;
1253
+	}
1254
+	if (mime==0) {
1255
+		/* content type hdr not found -> according the RFC3261 we
1256
+		 * assume APPLICATION/SDP  --bogdan */
1257
+		mime = ((TYPE_APPLICATION << 16) + SUBTYPE_SDP);
1258
+	}
1259
+	DBG("DBUG:textops:has_body: Content type is %d\n",mime);
1260
+
1261
+	if ( mime!=(int)(unsigned long)type )
1262
+		return -1;
1263
+
1264
+	return 1;
1265
+}