[PATCH] multipart MIME output

"P. A. Bagyenda" <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Message-ID <[email protected]>
hi,

For a while I've noticed spurious CRLF at the end of body parts inside  
multipart MIME messages.  I believe I have tracked this down to a  
couple of bugs in the gwlib/mime.c  module, which is used heavily by  
Mbuni MMS Gateway.

  Attached patch fixes these. Broadly, the code was too complicated  
for what should have been a simpler task (thanks partly to your  
truly!). I've tried to simplify the code to be true to RFC 2046 Sec.  
5. Tests confirm that it is doing the right thing now.

  In addition, the test case for multipart mime is actually mal-formed  
itself :(, lacking CRLF after each header, and at certain boundaries.  
A cleaner one is attached.

P.
mime.diff (application/octet-stream, 2 KB)
Index: gwlib/mime.c
===================================================================
RCS file: /home/cvs/gateway/gwlib/mime.c,v
retrieving revision 1.22
diff -u -r1.22 mime.c
--- gwlib/mime.c	9 Jan 2008 20:06:55 -0000	1.22
+++ gwlib/mime.c	27 Jun 2008 12:02:54 -0000
@@ -213,7 +213,7 @@
  * Mapping function from other data types, mainly Octstr and HTTP.
  */
 
-static Octstr *mime_entity_to_octstr_real(MIMEEntity *m, unsigned int level)
+Octstr *mime_entity_to_octstr(MIMEEntity *m)
 {
     Octstr *mime, *boundary = NULL;
     List *headers;
@@ -250,29 +250,24 @@
         octstr_append(mime, octstr_imm("\r\n"));
     }
     http_destroy_headers(headers);
+    octstr_append(mime, octstr_imm("\r\n")); /* Mark end of headers. */
 
     /* loop through all MIME multipart entities of this entity */
     for (i = 0; i < gwlist_len(m->multiparts); i++) {
         MIMEEntity *e = gwlist_get(m->multiparts, i);
         Octstr *body;
 
-        if (i != 0)
-            octstr_append(mime, octstr_imm("\r\n"));
         octstr_append(mime, octstr_imm("\r\n--"));
         octstr_append(mime, boundary);
         octstr_append(mime, octstr_imm("\r\n"));
 
         /* call ourself to produce the MIME entity body */
-        body = mime_entity_to_octstr_real(e, level + 1);
+        body = mime_entity_to_octstr(e);
         octstr_append(mime, body);
 
         octstr_destroy(body);
     }
 
-    /* add the last boundary statement, but hive an EOL 
-     * if we are on the top of the recursion stack. */
-    if (level > 0) 
-        octstr_append(mime, octstr_imm("\r\n"));
     octstr_append(mime, octstr_imm("\r\n--"));
     octstr_append(mime, boundary);
     octstr_append(mime, octstr_imm("--\r\n"));
@@ -284,17 +279,6 @@
     return mime;
 }
 
-
-Octstr *mime_entity_to_octstr(MIMEEntity *m)
-{
-    Octstr *mime;
-
-    /* mapping function required to pass recurssion level */
-    mime = mime_entity_to_octstr_real(m, 0);
-
-    return mime;
-}
-
 static Octstr *get_start_param(Octstr *content_type)
 {
      Octstr *start;
multipart-msg.txt (text/plain, 782 B)
Content-Type: multipart/related; start=<AAA>; boundary=my_boundary
X-Some-Header: Some-Value
MIME-Version: 1.0


--my_boundary
Content-Type: text/plain

this is the text in this entity

--my_boundary
Content-Type: multipart/mixed; boundary=mms_boundary
MIME-Version: 1.0


--mms_boundary
Content-Type: text/plain

this is the mms message text

--mms_boundary
Content-Type: application/xml

<?xml version="1.0"?>
<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.0//EN"
  "http://www.wapforum.org/DTD/pap_2.0.dtd">
<pap>
  <push-message push-id="4879683648">
    <address address-value="[email protected]"/>
    <address address-value="[email protected]"/>
    <address address-value="[email protected]"/>
  </push-message>
</pap>

--mms_boundary--

--my_boundary--
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.