1
/* $Id: sip_multipart.c 3841 2011-10-24 09:28:13Z ming $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
#include <pjsip/sip_multipart.h>
20
#include <pjsip/sip_parser.h>
21
#include <pjlib-util/scanner.h>
22
#include <pj/assert.h>
25
#include <pj/except.h>
29
#include <pj/string.h>
31
#define THIS_FILE "sip_multipart.c"
33
#define IS_SPACE(c) ((c)==' ' || (c)=='\t')
36
# define TRACE_(x) PJ_LOG(4,x)
41
extern pj_bool_t pjsip_use_compact_form;
43
/* Type of "data" in multipart pjsip_msg_body */
47
pjsip_multipart_part part_head;
51
static int multipart_print_body(struct pjsip_msg_body *msg_body,
52
char *buf, pj_size_t size)
54
const struct multipart_data *m_data;
55
pj_str_t clen_hdr = { "Content-Length: ", 16};
56
pjsip_multipart_part *part;
57
char *p = buf, *end = buf+size;
59
#define SIZE_LEFT() (end-p)
61
m_data = (const struct multipart_data*)msg_body->data;
63
PJ_ASSERT_RETURN(m_data && !pj_list_empty(&m_data->part_head), PJ_EINVAL);
65
part = m_data->part_head.next;
66
while (part != &m_data->part_head) {
67
enum { CLEN_SPACE = 5 };
74
if (SIZE_LEFT() <= (m_data->boundary.slen+8) << 1)
76
*p++ = 13; *p++ = 10; *p++ = '-'; *p++ = '-';
77
pj_memcpy(p, m_data->boundary.ptr, m_data->boundary.slen);
78
p += m_data->boundary.slen;
81
/* Print optional headers */
83
while (hdr != &part->hdr) {
84
int printed = pjsip_hdr_print_on((pjsip_hdr*)hdr, p,
94
/* Automaticly adds Content-Type and Content-Length headers, only
95
* if content_type is set in the message body.
97
if (part->body && part->body->content_type.type.slen) {
98
pj_str_t ctype_hdr = { "Content-Type: ", 14};
99
const pjsip_media_type *media = &part->body->content_type;
101
if (pjsip_use_compact_form) {
102
ctype_hdr.ptr = "c: ";
106
/* Add Content-Type header. */
107
if ( (end-p) < 24 + media->type.slen + media->subtype.slen) {
110
pj_memcpy(p, ctype_hdr.ptr, ctype_hdr.slen);
112
p += pjsip_media_type_print(p, end-p, media);
116
/* Add Content-Length header. */
117
if ((end-p) < clen_hdr.slen + 12 + 2) {
120
pj_memcpy(p, clen_hdr.ptr, clen_hdr.slen);
123
/* Print blanks after "Content-Length:", this is where we'll put
124
* the content length value after we know the length of the
127
pj_memset(p, ' ', CLEN_SPACE);
135
*p++ = 13; *p++ = 10;
138
pj_assert(part->body != NULL);
140
int printed = part->body->print_body(part->body, p, SIZE_LEFT());
145
/* Now that we have the length of the body, print this to the
146
* Content-Length header.
152
len = pj_utoa(printed, tmp);
153
if (len > CLEN_SPACE) len = CLEN_SPACE;
154
pj_memcpy(clen_pos+CLEN_SPACE-len, tmp, len);
161
/* Print closing delimiter */
162
if (SIZE_LEFT() < m_data->boundary.slen+8)
164
*p++ = 13; *p++ = 10; *p++ = '-'; *p++ = '-';
165
pj_memcpy(p, m_data->boundary.ptr, m_data->boundary.slen);
166
p += m_data->boundary.slen;
167
*p++ = '-'; *p++ = '-'; *p++ = 13; *p++ = 10;
174
static void* multipart_clone_data(pj_pool_t *pool, const void *data,
177
const struct multipart_data *src;
178
struct multipart_data *dst;
179
const pjsip_multipart_part *src_part;
183
src = (const struct multipart_data*) data;
184
dst = PJ_POOL_ALLOC_T(pool, struct multipart_data);
186
pj_strdup(pool, &dst->boundary, &src->boundary);
188
src_part = src->part_head.next;
189
while (src_part != &src->part_head) {
190
pjsip_multipart_part *dst_part;
191
const pjsip_hdr *src_hdr;
193
dst_part = pjsip_multipart_create_part(pool);
195
src_hdr = src_part->hdr.next;
196
while (src_hdr != &src_part->hdr) {
197
pjsip_hdr *dst_hdr = (pjsip_hdr*)pjsip_hdr_clone(pool, src_hdr);
198
pj_list_push_back(&dst_part->hdr, dst_hdr);
199
src_hdr = src_hdr->next;
202
dst_part->body = pjsip_msg_body_clone(pool, src_part->body);
204
pj_list_push_back(&dst->part_head, dst_part);
206
src_part = src_part->next;
213
* Create an empty multipart body.
215
PJ_DEF(pjsip_msg_body*) pjsip_multipart_create( pj_pool_t *pool,
216
const pjsip_media_type *ctype,
217
const pj_str_t *boundary)
219
pjsip_msg_body *body;
220
pjsip_param *ctype_param;
221
struct multipart_data *mp_data;
222
pj_str_t STR_BOUNDARY = { "boundary", 8 };
224
PJ_ASSERT_RETURN(pool, NULL);
226
body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
229
if (ctype && ctype->type.slen) {
230
pjsip_media_type_cp(pool, &body->content_type, ctype);
232
pj_str_t STR_MULTIPART = {"multipart", 9};
233
pj_str_t STR_MIXED = { "mixed", 5 };
235
pjsip_media_type_init(&body->content_type,
236
&STR_MULTIPART, &STR_MIXED);
240
mp_data = PJ_POOL_ZALLOC_T(pool, struct multipart_data);
241
pj_list_init(&mp_data->part_head);
243
pj_strdup(pool, &mp_data->boundary, boundary);
245
pj_create_unique_string(pool, &mp_data->boundary);
247
body->data = mp_data;
249
/* Add ";boundary" parameter to content_type parameter. */
250
ctype_param = pjsip_param_find(&body->content_type.param, &STR_BOUNDARY);
252
ctype_param = PJ_POOL_ALLOC_T(pool, pjsip_param);
253
ctype_param->name = STR_BOUNDARY;
254
pj_list_push_back(&body->content_type.param, ctype_param);
256
ctype_param->value = mp_data->boundary;
258
/* function pointers */
259
body->print_body = &multipart_print_body;
260
body->clone_data = &multipart_clone_data;
266
* Create an empty multipart part.
268
PJ_DEF(pjsip_multipart_part*) pjsip_multipart_create_part(pj_pool_t *pool)
270
pjsip_multipart_part *mp;
272
mp = PJ_POOL_ZALLOC_T(pool, pjsip_multipart_part);
273
pj_list_init(&mp->hdr);
282
PJ_DEF(pjsip_multipart_part*)
283
pjsip_multipart_clone_part(pj_pool_t *pool,
284
const pjsip_multipart_part *src)
286
pjsip_multipart_part *dst;
287
const pjsip_hdr *hdr;
289
dst = pjsip_multipart_create_part(pool);
292
while (hdr != &src->hdr) {
293
pj_list_push_back(&dst->hdr, pjsip_hdr_clone(pool, hdr));
297
dst->body = pjsip_msg_body_clone(pool, src->body);
304
* Add a part into multipart bodies.
306
PJ_DEF(pj_status_t) pjsip_multipart_add_part( pj_pool_t *pool,
308
pjsip_multipart_part *part)
310
struct multipart_data *m_data;
312
/* All params must be specified */
313
PJ_ASSERT_RETURN(pool && mp && part, PJ_EINVAL);
315
/* mp must really point to an actual multipart msg body */
316
PJ_ASSERT_RETURN(mp->print_body==&multipart_print_body, PJ_EINVAL);
318
/* The multipart part must contain a valid message body */
319
PJ_ASSERT_RETURN(part->body && part->body->print_body, PJ_EINVAL);
321
m_data = (struct multipart_data*)mp->data;
322
pj_list_push_back(&m_data->part_head, part);
330
* Get the first part of multipart bodies.
332
PJ_DEF(pjsip_multipart_part*)
333
pjsip_multipart_get_first_part(const pjsip_msg_body *mp)
335
struct multipart_data *m_data;
337
/* Must specify mandatory params */
338
PJ_ASSERT_RETURN(mp, NULL);
340
/* mp must really point to an actual multipart msg body */
341
PJ_ASSERT_RETURN(mp->print_body==&multipart_print_body, NULL);
343
m_data = (struct multipart_data*)mp->data;
344
if (pj_list_empty(&m_data->part_head))
347
return m_data->part_head.next;
351
* Get the next part after the specified part.
353
PJ_DEF(pjsip_multipart_part*)
354
pjsip_multipart_get_next_part(const pjsip_msg_body *mp,
355
pjsip_multipart_part *part)
357
struct multipart_data *m_data;
359
/* Must specify mandatory params */
360
PJ_ASSERT_RETURN(mp && part, NULL);
362
/* mp must really point to an actual multipart msg body */
363
PJ_ASSERT_RETURN(mp->print_body==&multipart_print_body, NULL);
365
m_data = (struct multipart_data*)mp->data;
367
/* the part parameter must be really member of the list */
368
PJ_ASSERT_RETURN(pj_list_find_node(&m_data->part_head, part) != NULL,
371
if (part->next == &m_data->part_head)
378
* Find a body inside multipart bodies which has the specified content type.
380
PJ_DEF(pjsip_multipart_part*)
381
pjsip_multipart_find_part( const pjsip_msg_body *mp,
382
const pjsip_media_type *content_type,
383
const pjsip_multipart_part *start)
385
struct multipart_data *m_data;
386
pjsip_multipart_part *part;
388
/* Must specify mandatory params */
389
PJ_ASSERT_RETURN(mp && content_type, NULL);
391
/* mp must really point to an actual multipart msg body */
392
PJ_ASSERT_RETURN(mp->print_body==&multipart_print_body, NULL);
394
m_data = (struct multipart_data*)mp->data;
399
part = m_data->part_head.next;
401
while (part != &m_data->part_head) {
402
if (pjsip_media_type_cmp(&part->body->content_type,
413
/* Parse a multipart part. "pct" is parent content-type */
414
static pjsip_multipart_part *parse_multipart_part(pj_pool_t *pool,
417
const pjsip_media_type *pct)
419
pjsip_multipart_part *part = pjsip_multipart_create_part(pool);
420
char *p = start, *end = start+len, *end_hdr = NULL, *start_body = NULL;
421
pjsip_ctype_hdr *ctype_hdr = NULL;
423
TRACE_((THIS_FILE, "Parsing part: begin--\n%.*s\n--end",
426
/* Find the end of header area, by looking at an empty line */
428
while (p!=end && *p!='\n') ++p;
433
if ((p==start) || (p==start+1 && *(p-1)=='\r')) {
434
/* Empty header section */
438
} else if (p==end-1) {
439
/* Empty body section */
442
} else if ((p>=start+1 && *(p-1)=='\n') ||
443
(p>=start+2 && *(p-1)=='\r' && *(p-2)=='\n'))
446
end_hdr = (*(p-1)=='\r') ? (p-1) : p;
454
/* Parse the headers */
455
if (end_hdr-start > 0) {
459
status = pjsip_parse_headers(pool, start, end_hdr-start,
461
if (status != PJ_SUCCESS) {
462
PJ_PERROR(2,(THIS_FILE, status, "Warning: error parsing multipart"
466
/* Find Content-Type header */
467
hdr = part->hdr.next;
468
while (hdr != &part->hdr) {
469
TRACE_((THIS_FILE, "Header parsed: %.*s", (int)hdr->name.slen,
471
if (hdr->type == PJSIP_H_CONTENT_TYPE) {
472
ctype_hdr = (pjsip_ctype_hdr*)hdr;
478
/* Assign the body */
479
part->body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
481
pjsip_media_type_cp(pool, &part->body->content_type, &ctype_hdr->media);
482
} else if (pct && pj_stricmp2(&pct->subtype, "digest")==0) {
483
part->body->content_type.type = pj_str("message");
484
part->body->content_type.subtype = pj_str("rfc822");
486
part->body->content_type.type = pj_str("text");
487
part->body->content_type.subtype = pj_str("plain");
490
if (start_body < end) {
491
part->body->data = start_body;
492
part->body->len = end - start_body;
494
part->body->data = (void*)"";
497
TRACE_((THIS_FILE, "Body parsed: \"%.*s\"", (int)part->body->len,
499
part->body->print_body = &pjsip_print_text_body;
500
part->body->clone_data = &pjsip_clone_text_data;
505
/* Public function to parse multipart message bodies into its parts */
506
PJ_DEF(pjsip_msg_body*) pjsip_multipart_parse(pj_pool_t *pool,
507
char *buf, pj_size_t len,
508
const pjsip_media_type *ctype,
511
pj_str_t boundary, delim;
512
char *curptr, *endptr;
513
const pjsip_param *ctype_param;
514
const pj_str_t STR_BOUNDARY = { "boundary", 8 };
515
pjsip_msg_body *body = NULL;
517
PJ_ASSERT_RETURN(pool && buf && len && ctype && !options, NULL);
519
TRACE_((THIS_FILE, "Started parsing multipart body"));
521
/* Get the boundary value in the ctype */
524
ctype_param = pjsip_param_find(&ctype->param, &STR_BOUNDARY);
526
boundary = ctype_param->value;
527
if (boundary.slen>2 && *boundary.ptr=='"') {
532
TRACE_((THIS_FILE, "Boundary is specified: '%.*s'", (int)boundary.slen,
536
if (!boundary.slen) {
537
/* Boundary not found or not specified. Try to be clever, get
538
* the boundary from the body.
540
char *p=buf, *end=buf+len;
542
PJ_LOG(4,(THIS_FILE, "Warning: boundary parameter not found or "
543
"not specified when parsing multipart body"));
545
/* Find the first "--". This "--" must be right after a CRLF, unless
546
* it really appears at the start of the buffer.
549
while (p!=end && *p!='-') ++p;
550
if (p!=end && *(p+1)=='-' &&
551
((p>buf && *(p-1)=='\n') || (p==buf)))
561
/* Unable to determine boundary. Maybe this is not a multipart
564
PJ_LOG(4,(THIS_FILE, "Error: multipart boundary not specified and"
565
" unable to calculate from the body"));
570
while (p!=end && !pj_isspace(*p)) ++p;
571
boundary.slen = p - boundary.ptr;
573
TRACE_((THIS_FILE, "Boundary is calculated: '%.*s'",
574
(int)boundary.slen, boundary.ptr));
577
/* Build the delimiter:
578
* delimiter = "--" boundary
580
delim.slen = boundary.slen+2;
581
delim.ptr = (char*)pj_pool_alloc(pool, (int)delim.slen);
584
pj_memcpy(delim.ptr+2, boundary.ptr, boundary.slen);
586
/* Start parsing the body, skip until the first delimiter. */
592
body.ptr = buf; body.slen = len;
593
curptr = pj_strstr(&body, &delim);
598
body = pjsip_multipart_create(pool, ctype, &boundary);
601
char *start_body, *end_body;
602
pjsip_multipart_part *part;
604
/* Eat the boundary */
605
curptr += delim.slen;
606
if (*curptr=='-' && curptr<endptr-1 && *(curptr+1)=='-') {
607
/* Found the closing delimiter */
611
/* Optional whitespace after delimiter */
612
while (curptr!=endptr && IS_SPACE(*curptr)) ++curptr;
614
if (*curptr=='\r') ++curptr;
616
/* Expecting a newline here */
621
/* We now in the start of the body */
624
/* Find the next delimiter */
628
subbody.ptr = curptr; subbody.slen = endptr - curptr;
629
curptr = pj_strstr(&subbody, &delim);
631
/* We're really expecting end delimiter to be found. */
638
/* The newline preceeding the delimiter is conceptually part of
639
* the delimiter, so trim it from the body.
641
if (*(end_body-1) == '\n')
643
if (*(end_body-1) == '\r')
646
/* Now that we have determined the part's boundary, parse it
647
* to get the header and body part of the part.
649
part = parse_multipart_part(pool, start_body, end_body - start_body,
652
pjsip_multipart_add_part(pool, body, part);