Browse code

get_body_part() function is introduced, that returns a specific part of the message body, for example application/sdp. The result can be the whole body, or a part of it in case of multipart/somehing Content-Type.

Miklos Tirpak authored on 22/05/2008 16:12:16
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,291 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2008 iptelorg GmbH
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License 
23
+ * along with this program; if not, write to the Free Software 
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ *
26
+ * History:
27
+ * --------
28
+ *  2008-05-22  Initial version, get_body_part() is introduced (Miklos)
29
+ */
30
+
31
+#include "../trim.h"
32
+#include "parser_f.h"
33
+#include "parse_content.h"
34
+#include "parse_param.h"
35
+#include "keys.h"
36
+#include "parse_body.h"
37
+
38
+/* returns the value of boundary parameter from the Contect-Type HF */
39
+static inline int get_boundary_param(struct sip_msg *msg, str *boundary)
40
+{
41
+	str	s;
42
+	char	*c;
43
+	param_t	*p, *list;
44
+
45
+#define is_boundary(c) \
46
+	(((c)[0] == 'b' || (c)[0] == 'B') && \
47
+	((c)[1] == 'o' || (c)[1] == 'O') && \
48
+	((c)[2] == 'u' || (c)[2] == 'U') && \
49
+	((c)[3] == 'n' || (c)[3] == 'N') && \
50
+	((c)[4] == 'd' || (c)[4] == 'D') && \
51
+	((c)[5] == 'a' || (c)[5] == 'A') && \
52
+	((c)[6] == 'r' || (c)[6] == 'R') && \
53
+	((c)[7] == 'y' || (c)[7] == 'Y'))
54
+
55
+#define boundary_param_len (sizeof("boundary")-1)
56
+
57
+	/* get the pointer to the beginning of the parameter list */
58
+	s.s = msg->content_type->body.s;
59
+	s.len = msg->content_type->body.len;
60
+	c = find_not_quoted(&s, ';');
61
+	if (!c)
62
+		return -1;
63
+	c++;
64
+	s.len = s.len - (c - s.s);
65
+	s.s = c;
66
+	trim_leading(&s);
67
+
68
+	if (s.len <= 0)
69
+		return -1;
70
+
71
+	/* parse the parameter list, and search for boundary */
72
+	if (parse_params(&s, CLASS_ANY, NULL, &list)<0)
73
+		return -1;
74
+
75
+	boundary->s = NULL;
76
+	for (p = list; p; p = p->next)
77
+		if ((p->name.len == boundary_param_len) &&
78
+			is_boundary(p->name.s)
79
+		) {
80
+			boundary->s = p->body.s;
81
+			boundary->len = p->body.len;
82
+			break;
83
+		}
84
+	free_params(list);
85
+	if (!boundary->s || !boundary->len)
86
+		return -1;
87
+
88
+	DBG("boundary is \"%.*s\"\n",
89
+		boundary->len, boundary->s);
90
+	return 0;
91
+}
92
+
93
+/* search the next boundary in the buffer */
94
+static inline char *search_boundary(char *buf, char *buf_end, str *boundary)
95
+{
96
+	char *c;
97
+
98
+	c = buf;
99
+	while (c + 2 /* -- */ + boundary->len < buf_end) {
100
+		if ((*c == '-') && (*(c+1) == '-') &&
101
+			(memcmp(c+2, boundary->s, boundary->len) == 0)
102
+		)
103
+			return c; /* boundary found */
104
+
105
+		/* go to the next line */
106
+		while ((c < buf_end) && (*c != '\n')) c++;
107
+		c++;
108
+	}
109
+	return NULL;
110
+}
111
+
112
+/* extract the body of a part from a multipart SIP msg body */
113
+inline static char *get_multipart_body(char *buf,
114
+					char *buf_end,
115
+					str *boundary,
116
+					int *len)
117
+{
118
+	char *beg, *end;
119
+
120
+	if (buf >= buf_end)
121
+		goto error;
122
+
123
+	beg = buf;
124
+	while ((*beg != '\r') && (*beg != '\n')) {
125
+		while ((beg < buf_end) && (*beg != '\n'))
126
+			beg++;
127
+		beg++;
128
+		if (beg >= buf_end)
129
+			goto error;
130
+	}
131
+	/* CRLF delimeter found, the body begins right after it */
132
+	while ((beg < buf_end) && (*beg != '\n'))
133
+		beg++;
134
+	beg++;
135
+	if (beg >= buf_end)
136
+		goto error;
137
+
138
+	if (!(end = search_boundary(beg, buf_end, boundary)))
139
+		goto error;
140
+
141
+	/* CRLF preceding the boundary belongs to the boundary
142
+	and not to the body */
143
+	if (*(end-1) == '\n') end--;
144
+	if (*(end-1) == '\r') end--;
145
+
146
+	if (end < beg)
147
+		goto error;
148
+
149
+	*len = end-beg;
150
+	return beg;
151
+error:
152
+	ERR("failed to extract the body from the multipart mime type\n");
153
+	return NULL;
154
+}
155
+
156
+
157
+/* macros from parse_hname2.c */
158
+#define READ(val) \
159
+(*(val + 0) + (*(val + 1) << 8) + (*(val + 2) << 16) + (*(val + 3) << 24))
160
+
161
+#define LOWER_DWORD(d) ((d) | 0x20202020)
162
+
163
+/* Returns the pointer within the msg body to the given type/subtype,
164
+ * and sets the length of the body part.
165
+ * The result can be the whole msg body, or a part of a multipart body.
166
+ */
167
+char *get_body_part(	struct sip_msg *msg,
168
+			unsigned short type, unsigned short subtype,
169
+			int *len)
170
+{
171
+	int	mime;
172
+	unsigned int	umime;
173
+	char	*c, *c2, *buf_end;
174
+	str	boundary;
175
+
176
+#define content_type_len \
177
+	(sizeof("Content-Type") - 1)
178
+
179
+	if ((mime = parse_content_type_hdr(msg)) <= 0)
180
+		return NULL;
181
+
182
+	if (mime == ((type<<16)|subtype)) {
183
+		/* content-type is type/subtype */
184
+		c = get_body(msg);
185
+		if (c)
186
+			*len = msg->buf+msg->len - c;
187
+		return c;
188
+
189
+	} else if ((mime>>16) == TYPE_MULTIPART) {
190
+		/* type is multipart/something, search for type/subtype part */
191
+
192
+		if (get_boundary_param(msg, &boundary)) {
193
+			ERR("failed to get boundary parameter\n");
194
+			return NULL;
195
+		}
196
+		if (!(c = get_body(msg)))
197
+			return NULL;
198
+		buf_end = msg->buf+msg->len;
199
+
200
+		/* check all the body parts delimated by the boundary value,
201
+		and search for the Content-Type HF with the given 
202
+		type/subtype */
203
+next_part:
204
+		while ((c = search_boundary(c, buf_end, &boundary))) {
205
+			/* skip boundary */
206
+			c += 2 + boundary.len;
207
+
208
+			if ((c+2 > buf_end) ||
209
+				((*c == '-') && (*(c+1) == '-'))
210
+			)
211
+				/* end boundary, no more body part
212
+				will follow */
213
+				return NULL;
214
+
215
+			/* go to the next line */
216
+			while ((c < buf_end) && (*c != '\n')) c++;
217
+			c++;
218
+			if (c >= buf_end)
219
+				return NULL;
220
+
221
+			/* try to find the content-type header */
222
+			while ((*c != '\r') && (*c != '\n')) {
223
+				if (c + content_type_len >= buf_end)
224
+					return NULL;
225
+
226
+				if ((LOWER_DWORD(READ(c)) == _cont_) &&
227
+					(LOWER_DWORD(READ(c+4)) == _ent__) &&
228
+					(LOWER_DWORD(READ(c+8)) == _type_)
229
+				) {
230
+					/* Content-Type HF found */
231
+					c += content_type_len;
232
+					while ((c < buf_end) &&
233
+						((*c == ' ') || (*c == '\t'))
234
+					)
235
+						c++;
236
+
237
+					if (c + 1 /* : */ >= buf_end)
238
+						return NULL;
239
+
240
+					if (*c != ':')
241
+						/* not realy a Content-Type HF */
242
+						goto next_hf;
243
+					c++;
244
+
245
+					/* search the end of the header body,
246
+					decode_mime_type() needs it */
247
+					c2 = c;
248
+					while (((c2 < buf_end) && (*c2 != '\n')) ||
249
+						((c2+1 < buf_end) && (*c2 == '\n') &&
250
+							((*(c2+1) == ' ') || (*(c2+1) == '\t')))
251
+					)
252
+						c2++;
253
+
254
+					if (c2 >= buf_end)
255
+						return NULL;
256
+					if (*(c2-1) == '\r') c2--;
257
+
258
+					if (!decode_mime_type(c, c2 , &umime)) {
259
+						ERR("failed to decode the mime type\n");
260
+						return NULL;
261
+					}
262
+
263
+					/* c2 points to the CRLF at the end of the line,
264
+					move the pointer to the beginning of the next line */
265
+					c = c2;
266
+					if ((c < buf_end) && (*c == '\r')) c++;
267
+					if ((c < buf_end) && (*c == '\n')) c++;
268
+
269
+					if (umime != ((type<<16)|subtype)) {
270
+						/* this is not the part we are looking for */
271
+						goto next_part;
272
+					}
273
+
274
+					/* the requested type/subtype is found! */
275
+					return get_multipart_body(c,
276
+							buf_end,
277
+							&boundary,
278
+							len);
279
+				}
280
+next_hf:
281
+				/* go to the next line */
282
+				while ((c < buf_end) && (*c != '\n')) c++;
283
+				c++;
284
+			}
285
+			/* CRLF delimeter reached,
286
+			no Content-Type HF was found */
287
+		}
288
+	}
289
+	return NULL;
290
+}
0 291
new file mode 100644
... ...
@@ -0,0 +1,40 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2008 iptelorg GmbH
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License 
23
+ * along with this program; if not, write to the Free Software 
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ *
26
+ */
27
+
28
+#ifndef PARSE_BODY_H
29
+#define PARSE_BODY_H
30
+
31
+/* Returns the pointer within the msg body to the given type/subtype,
32
+ * and sets the length.
33
+ * The result can be the whole msg body, or a part of a multipart body.
34
+ */
35
+char *get_body_part(	struct sip_msg *msg,
36
+			unsigned short type, unsigned short subtype,
37
+			int *len);
38
+
39
+#endif /* PARSE_BODY_H */
... ...
@@ -120,4 +120,9 @@ int parse_accept_hdr( struct sip_msg *msg );
120 120
  */
121 121
 char* parse_content_length( char* buffer, char* end, int* len);
122 122
 
123
+/*
124
+ * Sets the mime type from the body of a Content-Type header
125
+ */
126
+char* decode_mime_type(char *start, char *end, unsigned int *mime_type);
127
+
123 128
 #endif