Logo Search packages:      
Sourcecode: sofia-sip version File versions  Download package

MIME Multipart Body

Detailed Description

Representing MIME multipart bodies and their manipulation.

The msg_multipart_t is an object for storing MIME multipart message bodies. It includes message components used for framing and identifying message parts. Its syntax is defined in as follows:

   multipart-body := [preamble CRLF]
                     dash-boundary transport-padding CRLF
                     body-part *encapsulation
                     close-delimiter transport-padding
                     [CRLF epilogue]

   preamble := discard-text

   discard-text := *(*text CRLF)
                   ; May be ignored or discarded.

   dash-boundary := "--" boundary
                    ; boundary taken from the value of boundary parameter
                    ; of the Content-Type field.

   boundary := 0*69<bchars> bcharsnospace

   bchars := bcharsnospace / " "

   bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
                    "+" / "_" / "," / "-" / "." /
                    "/" / ":" / "=" / "?"

   transport-padding := *LWSP-char
                        ; Composers MUST NOT generate non-zero length
                        ; transport padding, but receivers MUST be able to
                        ; handle padding added by message transports.

   body-part := <"message" as defined in @RFC822, with all header fields
                 optional, not starting with the specified dash-boundary,
                 and with the delimiter not occurring anywhere in the body
                 part. Note that the semantics of a part differ from the
                 semantics of a message, as described in the text.>

   encapsulation := delimiter transport-padding CRLF 

   close-delimiter := delimiter "--"

   delimiter := CRLF dash-boundary

   epilogue := discard-text

Parsing a Multipart Message
When a message body contains a multipart entity (in other words, it has a MIME media type of "multipart"), the application can split the multipart entity into body parts

The parsing is relatively simple, the application just gives a memory home object, a Content-Type header object and message body object as an argument to msg_multipart_parse() function:

    if (sip->sip_content_type && 
        strncasecmp(sip->sip_content_type, "multipart/", 10) == 0) {
      msg_multipart_t *mp;

      if (sip->sip_multipart)
        mp = sip->sip_multipart;
        mp = msg_multipart_parse(msg_home(msg), 
                                 (sip_payload_t *)sip->sip_payload);
      if (mp)
        ... processing multipart ...
        ... error handling ...

The resulting list of msg_multipart_t structures contain the parts of the multipart entity, each part represented by a separate msg_multipart_t structure. Please note that in order to make error recovery possible, the parsing is not recursive - if multipart contains another multipart, the application is responsible for scanning for it and parsing it.

Constructing a Multipart Message
Constructing a multipart body is a bit more hairy. The application needs a message object (msg_t), which is used to buffer the encoding of multipart components.

As an example, let us create a "multipart/mixed" multipart entity with a HTML and GIF contents, and convert it into a sip_payload_t structure:

   msg_t *msg = msg_create(sip_default_mclass, 0);
   su_home_t *home = msg_home(msg);
   sip_t *sip = sip_object(msg);
   sip_content_type_t *c;
   msg_multipart_t *mp = NULL;
   msg_header_t *h = NULL;
   char *b;
   size_t len, offset;
   mp = msg_multipart_create(home, "text/html;level=3", html, strlen(html));
   mp->mp_next = msg_multipart_create(home, "image/gif", gif, giflen);

   c = sip_content_type_make(home, "multipart/mixed");
   // Add delimiters to multipart, and boundary parameter to content-type
   if (msg_multipart_complete(home, c, mp) < 0)
     return -1;         // Error

   // Combine multipart components into the chain
   h = NULL;
   if (msg_multipart_serialize(&h, mp) < 0)
     return -1;         // Error

   // Encode all multipart components 
   len = msg_multipart_prepare(msg, mp, 0);
   if (len < 0)
     return -1;         // Error

   pl = sip_payload_create(home, NULL, len);

   // Copy each element from multipart to pl_data 
   b = pl->pl_data;
   for (offset = 0, h = mp; offset < len; h = h->sh_succ) {
     memcpy(b + offset, h->sh_data, h->sh_len);
     offset += h->sh_len;


struct  msg_multipart_s
 Structure for a part in MIME multipart message. More...


#define msg_multipart_init(x)   MSG_HEADER_INIT(x, msg_multipart_class, sizeof(msg_multipart_t))
#define MSG_MULTIPART_INIT()   MSG_HDR_INIT(multipart)
#define msg_multipart_p(h)   msg_is_multipart((h))


enum  { msg_multipart_hash = 58884 }


int msg_is_multipart (msg_header_t const *header)
msg_multipart_tmsg_multipart_copy (su_home_t *home, msg_multipart_t const *header)
msg_multipart_tmsg_multipart_dup (su_home_t *home, msg_multipart_t const *header)
msg_multipart_tmsg_multipart_format (su_home_t *home, char const *fmt,...) __attribute__((__format__(printf
msg_multipart_tmsg_multipart_make (su_home_t *home, char const *s)


MSG_DLL msg_parse_f msg_multipart_d
MSG_DLL msg_dup_f msg_multipart_dup_one
MSG_DLL msg_xtra_f msg_multipart_dup_xtra
MSG_DLL msg_print_f msg_multipart_e

Generated by  Doxygen 1.6.0   Back to index