home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libmime / mimemrel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  23.4 KB  |  792 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /* mimemrel.c --- definition of the MimeMultipartRelated class (see mimei.h)
  20.    Created: Jamie Zawinski <jwz@netscape.com>, 15-May-96.
  21.  */
  22.  
  23. /* Thoughts on how to implement this:
  24.  
  25.    = if the type of this multipart/related is not text/html, then treat
  26.      it the same as multipart/mixed.
  27.  
  28.    = For each part in this multipart/related
  29.      = if this part is not the "top" part
  30.        = then save this part to a tmp file or a memory object,
  31.          kind-of like what we do for multipart/alternative sub-parts.
  32.          If this is an object we're blocked on (see below) send its data along.
  33.      = else
  34.        = emit this part (remember, it's of type text/html)
  35.        = at some point, layout may load a URL for <IMG SRC="cid:xxxx">.
  36.          we intercept that.
  37.          = if one of our cached parts has that cid, return the data for it.
  38.          = else, "block", the same way the image library blocks layout when it
  39.            doesn't yet have the size of the image.
  40.        = at some point, layout may load a URL for <IMG SRC="relative/yyy">.
  41.          we need to intercept that too.
  42.          = expand the URL, and compare it to our cached objects.
  43.            if it matches, return it.
  44.          = else block on it.
  45.  
  46.    = once we get to the end, if we have any sub-part references that we're
  47.      still blocked on, map over them:
  48.      = if they're cid: references, close them ("broken image" results.)
  49.      = if they're URLs, then load them in the normal way.
  50.  
  51.   --------------------------------------------------
  52.  
  53.   Ok, that's fairly complicated.  How about an approach where we go through
  54.   all the parts first, and don't emit until the end?
  55.  
  56.    = if the type of this multipart/related is not text/html, then treat
  57.      it the same as multipart/mixed.
  58.  
  59.    = For each part in this multipart/related
  60.        = save this part to a tmp file or a memory object,
  61.          like what we do for multipart/alternative sub-parts.
  62.  
  63.    = Emit the "top" part (the text/html one)
  64.      = intercept all calls to NET_GetURL, to allow us to rewrite the URL.
  65.        (hook into netlib, or only into imglib's calls to GetURL?)
  66.        (make sure we're behaving in a context-local way.)
  67.  
  68.        = when a URL is loaded, look through our cached parts for a match.
  69.          = if we find one, map that URL to a "cid:" URL
  70.          = else, let it load normally
  71.  
  72.      = at some point, layout may load a URL for <IMG SRC="cid:xxxx">.
  73.        it will do this either because that's what was in the HTML, or because
  74.        that's how we "rewrote" the URLs when we intercepted NET_GetURL.
  75.  
  76.        = if one of our cached parts has the requested cid, return the data
  77.          for it.
  78.        = else, generate a "broken image"
  79.  
  80.    = free all the cached data
  81.  
  82.   --------------------------------------------------
  83.  
  84.   How hard would be an approach where we rewrite the HTML?
  85.   (Looks like it's not much easier, and might be more error-prone.)
  86.  
  87.    = if the type of this multipart/related is not text/html, then treat
  88.      it the same as multipart/mixed.
  89.  
  90.    = For each part in this multipart/related
  91.        = save this part to a tmp file or a memory object,
  92.          like what we do for multipart/alternative sub-parts.
  93.  
  94.    = Parse the "top" part, and emit slightly different HTML:
  95.      = for each <IMG SRC>, <IMG LOWSRC>, <A HREF>?  Any others?
  96.        = look through our cached parts for a matching URL
  97.          = if we find one, map that URL to a "cid:" URL
  98.          = else, let it load normally
  99.  
  100.      = at some point, layout may load a URL for <IMG SRC="cid:xxxx">.
  101.        = if one of our cached parts has the requested cid, return the data
  102.          for it.
  103.        = else, generate a "broken image"
  104.  
  105.    = free all the cached data
  106.  */
  107.  
  108.  
  109.  
  110. #include "mimemrel.h"
  111.  
  112. extern int MK_UNABLE_TO_OPEN_TMP_FILE;
  113.  
  114. #define MIME_SUPERCLASS mimeMultipartClass
  115. MimeDefClass(MimeMultipartRelated, MimeMultipartRelatedClass,
  116.              mimeMultipartRelatedClass, &MIME_SUPERCLASS);
  117.  
  118.  
  119. /* Stupid utility function.  Really ought to be part of the standard string
  120.    package if you ask me...*/
  121.  
  122. static char* mime_strnchr(char* str, char c, int length)
  123. {
  124.     int i;
  125.     for (i=0 ; i<length ; i++) {
  126.         if (*str == c) return str;
  127.         str++;
  128.     }
  129.     return NULL;
  130. }
  131.  
  132.  
  133. static int
  134. MimeMultipartRelated_initialize(MimeObject* obj)
  135. {
  136.     MimeMultipartRelated* relobj = (MimeMultipartRelated*) obj;
  137.     relobj->base_url = MimeHeaders_get(obj->headers, "Content-Base",
  138.                                        FALSE, FALSE);
  139.  
  140.     /* I used to have code here to test if the type was text/html.  Then I
  141.        added multipart/alternative as being OK, too.  Then I found that the
  142.        VCard spec seems to talk about having the first part of a
  143.        multipart/related be an application/directory.  At that point, I decided
  144.        to punt.  We handle anything as the first part, and stomp on the HTML it
  145.        generates to adjust tags to point into the other parts.  This probably
  146.        works out to something reasonable in most cases. */
  147.  
  148.     relobj->hash = XP_HashTableNew(20, XP_StringHash,
  149.                                    (XP_HashCompFunction) strcmp);
  150.     if (!relobj->hash) return MK_OUT_OF_MEMORY;
  151.  
  152.     return ((MimeObjectClass*)&MIME_SUPERCLASS)->initialize(obj);
  153. }
  154.  
  155. XP_Bool mime_multipart_related_nukehash(XP_HashTable table, const void* key,
  156.                                         void* value, void* closure)
  157. {
  158.     XP_FREE((char*) key);
  159.     XP_FREE((char*) value);
  160.     return TRUE;
  161. }
  162.  
  163. static void
  164. MimeMultipartRelated_finalize (MimeObject *obj)
  165. {
  166.     MimeMultipartRelated* relobj = (MimeMultipartRelated*) obj;
  167.     FREEIF(relobj->base_url);
  168.     FREEIF(relobj->curtag);
  169.     if (relobj->hash) {
  170.         XP_MapRemhash(relobj->hash, mime_multipart_related_nukehash, NULL);
  171.         XP_HashTableDestroy(relobj->hash);
  172.         relobj->hash = NULL;
  173.     }
  174.     if (relobj->file_stream) {
  175.         XP_FileClose(relobj->file_stream);
  176.         relobj->file_stream = NULL;
  177.     }
  178.     if (relobj->file_buffer_name) {
  179.         XP_FileRemove(relobj->file_buffer_name, xpTemporary);
  180.         XP_FREE(relobj->file_buffer_name);
  181.         relobj->file_buffer_name = 0;
  182.     }
  183.     ((MimeObjectClass*)&MIME_SUPERCLASS)->finalize(obj);
  184. }
  185.  
  186. char * escape_unescaped_percents(const char *incomingURL);
  187.  
  188. /* This routine is only necessary because the mailbox URL fed to us 
  189.    by the winfe can contain spaces and '>'s in it. It's a hack. */
  190. static char *
  191. escape_for_mrel_subst(char *inURL)
  192. {
  193.     char *output, *inC, *outC, *temp;
  194.  
  195.     /* XP_STRLEN asserts the presence of a string in inURL */
  196.     int size = XP_STRLEN(inURL) + 1;
  197.  
  198.     for(inC = inURL; *inC; inC++)
  199.         if ((*inC == ' ') || (*inC == '>')) 
  200.             size += 2; /* space -> '%20', '>' -> '%3E', etc. */
  201.  
  202.     output = XP_ALLOC(size);
  203.     if (output)
  204.     {
  205.         /* Walk through the source string, copying all chars
  206.             except for spaces, which get escaped. */
  207.         inC = inURL;
  208.         outC = output;
  209.         while(*inC)
  210.         {
  211.             if (*inC == ' ')
  212.             {
  213.                 *outC++ = '%'; *outC++ = '2'; *outC++ = '0';
  214.             }
  215.             else if (*inC == '>')
  216.             {
  217.                 *outC++ = '%'; *outC++ = '3'; *outC++ = 'E';
  218.             }
  219.             else
  220.                 *outC++ = *inC;
  221.  
  222.             inC++;
  223.         }
  224.         *outC = '\0';
  225.     
  226.         temp = escape_unescaped_percents(output);
  227.         if (temp)
  228.         {
  229.             FREEIF(output);
  230.             output = temp;
  231.         }
  232.     }
  233.     return output;
  234. }
  235.  
  236. static XP_Bool
  237. MimeMultipartRelated_output_child_p(MimeObject *obj, MimeObject* child)
  238. {
  239.     MimeMultipartRelated *relobj = (MimeMultipartRelated *) obj;
  240.     if (relobj->head_loaded) {
  241.         /* This is a child part.  Just remember the mapping between the URL
  242.            it represents and the part-URL to get it back. */
  243.         char* location = MimeHeaders_get(child->headers, "Content-Location",
  244.                                          FALSE, FALSE);
  245.         if (!location) {
  246.             char* tmp = MimeHeaders_get(child->headers, "Content-ID",
  247.                                         FALSE, FALSE);
  248.             if (tmp) {
  249.                 char* tmp2 = tmp;
  250.                 if (*tmp2 == '<') {
  251.                     int length;
  252.                     tmp2++;
  253.                     length = XP_STRLEN(tmp2);
  254.                     if (length > 0 && tmp2[length - 1] == '>') {
  255.                         tmp2[length - 1] = '\0';
  256.                     }
  257.                 }
  258.                 location = PR_smprintf("cid:%s", tmp2);
  259.                 XP_FREE(tmp);
  260.             }
  261.         }
  262.         if (location) {
  263.             char* base_url = MimeHeaders_get(child->headers, "Content-Base",
  264.                                              FALSE, FALSE);
  265.             char* absolute =
  266.                 NET_MakeAbsoluteURL(base_url ? base_url : relobj->base_url,
  267.                                     location);
  268.             FREEIF(base_url);
  269.             XP_FREE(location);
  270.             if (absolute) {
  271.                 char* partnum = mime_part_address(child);
  272.                 if (partnum) {
  273.                     char* part;
  274.                     part = mime_set_url_part(obj->options->url, partnum,
  275.                                              FALSE);
  276.                     if (part) {
  277.                         /* If there's a space in the url, escape the url.
  278.                            (This happens primarily on Windows and Unix.) */
  279.                         char *temp = part;
  280.                         if (XP_STRCHR(part, ' ') || XP_STRCHR(part, '>') || XP_STRCHR(part, '%'))
  281.                           temp = escape_for_mrel_subst(part);
  282.                         XP_Puthash(relobj->hash, absolute, temp);
  283.  
  284.                         /* The value string that is added to the hashtable will be deleted
  285.                            by the hashtable at destruction time. So if we created an
  286.                            escaped string for the hashtable, we have to delete the
  287.                            part URL that was given to us. */
  288.                         if (temp != part) XP_FREE(part);
  289.                     }
  290.                     XP_FREE(partnum);
  291.                 }
  292.             }
  293.         }
  294.     } else {
  295.         /* Ah-hah!  We're the head object.  */
  296.         char* base_url;
  297.         relobj->head_loaded = TRUE;
  298.         relobj->headobj = child;
  299.         relobj->buffered_hdrs = MimeHeaders_copy(child->headers);
  300.         base_url = MimeHeaders_get(child->headers, "Content-Base",
  301.                                    FALSE, FALSE);
  302.         if (base_url) {
  303.             /* If the head object has a base_url associated with it, use
  304.                that instead of any base_url that may have been associated
  305.                with the multipart/related. */
  306.             FREEIF(relobj->base_url);
  307.             relobj->base_url = base_url;
  308.         }
  309.  
  310.     }
  311.     if (obj->options && !obj->options->write_html_p
  312. #ifdef MIME_DRAFTS
  313.         && !obj->options->decompose_file_p
  314. #endif /* MIME_DRAFTS */
  315.         )
  316.       {
  317.         return TRUE;
  318.       }
  319.  
  320.     return FALSE;            /* Don't actually parse this child; we'll handle
  321.                                all that at eof time. */
  322. }
  323.  
  324. static int
  325. MimeMultipartRelated_parse_child_line (MimeObject *obj,
  326.                                        char *line, int32 length,
  327.                                        XP_Bool first_line_p)
  328. {
  329.     MimeContainer *cont = (MimeContainer *) obj;
  330.     MimeMultipartRelated *relobj = (MimeMultipartRelated *) obj;
  331.     int status;
  332.     MimeObject *kid;
  333.  
  334.     if (obj->options && !obj->options->write_html_p
  335. #ifdef MIME_DRAFTS
  336.         && !obj->options->decompose_file_p
  337. #endif /* MIME_DRAFTS */
  338.         )
  339.       {
  340.         /* Oh, just go do the normal thing... */
  341.         return ((MimeMultipartClass*)&MIME_SUPERCLASS)->
  342.             parse_child_line(obj, line, length, first_line_p);
  343.       }
  344.  
  345.     /* Throw it away if this isn't the head object.  (Someday, maybe we'll
  346.        cache it instead.) */
  347.     XP_ASSERT(cont->nchildren > 0);
  348.     if (cont->nchildren <= 0)
  349.         return -1;
  350.     kid = cont->children[cont->nchildren-1];
  351.     XP_ASSERT(kid);
  352.     if (!kid) return -1;
  353.     if (kid != relobj->headobj) return 0;
  354.  
  355.     /* Buffer this up (###tw much code duplication from mimemalt.c) */
  356.     /* If we don't yet have a buffer (either memory or file) try and make a
  357.        memory buffer. */
  358.     if (!relobj->head_buffer && !relobj->file_buffer_name) {
  359.         int target_size = 1024 * 50;       /* try for 50k */
  360.         while (target_size > 0) {
  361.             relobj->head_buffer = (char *) XP_ALLOC(target_size);
  362.             if (relobj->head_buffer) break;  /* got it! */
  363.             target_size -= (1024 * 5);     /* decrease it and try again */
  364.         }
  365.  
  366.         if (relobj->head_buffer) {
  367.             relobj->head_buffer_size = target_size;
  368.         } else {
  369.             relobj->head_buffer_size = 0;
  370.         }
  371.  
  372.         relobj->head_buffer_fp = 0;
  373.     }
  374.  
  375.     /* Ok, if at this point we still don't have either kind of buffer, try and
  376.        make a file buffer. */
  377.     if (!relobj->head_buffer && !relobj->file_buffer_name) {
  378.         relobj->file_buffer_name = WH_TempName(xpTemporary, "nsma");
  379.         if (!relobj->file_buffer_name) return MK_OUT_OF_MEMORY;
  380.  
  381.         relobj->file_stream = XP_FileOpen(relobj->file_buffer_name,
  382.                                           xpTemporary,
  383.                                           XP_FILE_WRITE_BIN);
  384.         if (!relobj->file_stream) {
  385.             return MK_UNABLE_TO_OPEN_TMP_FILE;
  386.         }
  387.     }
  388.   
  389.     XP_ASSERT(relobj->head_buffer || relobj->file_stream);
  390.  
  391.  
  392.     /* If this line will fit in the memory buffer, put it there.
  393.      */
  394.     if (relobj->head_buffer &&
  395.         relobj->head_buffer_fp + length < relobj->head_buffer_size) {
  396.         XP_MEMCPY(relobj->head_buffer + relobj->head_buffer_fp, line, length);
  397.         relobj->head_buffer_fp += length;
  398.     } else {
  399.         /* Otherwise it won't fit; write it to the file instead. */
  400.  
  401.         /* If the file isn't open yet, open it, and dump the memory buffer
  402.            to it. */
  403.         if (!relobj->file_stream) {
  404.             if (!relobj->file_buffer_name) {
  405.                 relobj->file_buffer_name = WH_TempName (xpTemporary, "nsma");
  406.             }
  407.             if (!relobj->file_buffer_name) return MK_OUT_OF_MEMORY;
  408.  
  409.             relobj->file_stream = XP_FileOpen(relobj->file_buffer_name,
  410.                                             xpTemporary,
  411.                                             XP_FILE_WRITE_BIN);
  412.             if (!relobj->file_stream) {
  413.                 return MK_UNABLE_TO_OPEN_TMP_FILE;
  414.             }
  415.  
  416.             if (relobj->head_buffer && relobj->head_buffer_fp) {
  417.                 status = XP_FileWrite (relobj->head_buffer,
  418.                                        relobj->head_buffer_fp,
  419.                                        relobj->file_stream);
  420.                 if (status < 0) return status;
  421.             }
  422.  
  423.             FREEIF(relobj->head_buffer);
  424.             relobj->head_buffer_fp = 0;
  425.             relobj->head_buffer_size = 0;
  426.         }
  427.  
  428.         /* Dump this line to the file. */
  429.         status = XP_FileWrite (line, length, relobj->file_stream);
  430.         if (status < 0) return status;
  431.     }
  432.  
  433.     return 0;
  434. }
  435.  
  436.  
  437.  
  438.  
  439. static int
  440. real_write(MimeMultipartRelated* relobj, char* buf, int32 size)
  441. {
  442.   MimeObject* obj = (MimeObject*) relobj;
  443.   void* closure = relobj->real_output_closure;
  444.  
  445. #ifdef MIME_DRAFTS
  446.   if ( obj->options &&
  447.        obj->options->decompose_file_p &&
  448.        obj->options->decompose_file_output_fn )
  449.     {
  450.       return obj->options->decompose_file_output_fn 
  451.         (buf, size, obj->options->stream_closure);
  452.     }
  453.   else
  454. #endif /* MIME_DRAFTS */
  455.     {
  456.       if (!closure) {
  457.         MimeObject* obj = (MimeObject*) relobj;
  458.         closure = obj->options->stream_closure;
  459.       }
  460.       return relobj->real_output_fn(buf, size, closure);
  461.     }
  462. }
  463.  
  464.  
  465. static int
  466. push_tag(MimeMultipartRelated* relobj, const char* buf, int32 size)
  467. {
  468.     if (size + relobj->curtag_length > relobj->curtag_max) {
  469.         relobj->curtag_max += 2 * size;
  470.         if (relobj->curtag_max < 1024) relobj->curtag_max = 1024;
  471.         if (!relobj->curtag) {
  472.             relobj->curtag = (char*) XP_ALLOC(relobj->curtag_max);
  473.         } else {
  474.             relobj->curtag = (char*) XP_REALLOC(relobj->curtag,
  475.                                                 relobj->curtag_max);
  476.         }
  477.         if (!relobj->curtag) return MK_OUT_OF_MEMORY;
  478.     }
  479.     XP_MEMCPY(relobj->curtag + relobj->curtag_length, buf, size);
  480.     relobj->curtag_length += size;
  481.     return 0;
  482. }
  483.  
  484. static int
  485. flush_tag(MimeMultipartRelated* relobj)
  486. {
  487.     int length = relobj->curtag_length;
  488.     char* buf;
  489.     int status;
  490.     
  491.     if (relobj->curtag == NULL || length == 0) return 0;
  492.  
  493.     status = push_tag(relobj, "", 1);    /* Push on a trailing NULL. */
  494.     if (status < 0) return status;
  495.     buf = relobj->curtag;
  496.     XP_ASSERT(*buf == '<' && buf[length - 1] == '>');
  497.     while (*buf) {
  498.         char c;
  499.         char* absolute;
  500.         char* part_url;
  501.         char* ptr = buf;
  502.         char *ptr2;
  503.         XP_Bool isquote = FALSE;
  504.         while (*ptr && *ptr != '=') ptr++;
  505.         if (*ptr == '=') {
  506.             ptr++;
  507.             if (*ptr == '"') {
  508.                 isquote = TRUE;
  509.                 /* Take up the double quote and leading space here as well. */
  510.                 /* Safe because there's a '>' at the end */
  511.                 do {ptr++;} while (XP_IS_SPACE(*ptr));
  512.             }
  513.         }
  514.         status = real_write(relobj, buf, ptr - buf);
  515.         if (status < 0) return status;
  516.         buf = ptr;
  517.         if (!*buf) break;
  518.         if (isquote) 
  519.         {
  520.             ptr = mime_strnchr(buf, '"', length - (buf - relobj->curtag));
  521.         } else {
  522.             for (ptr = buf; *ptr ; ptr++) {
  523.                 if (*ptr == '>' || XP_IS_SPACE(*ptr)) break;
  524.             }
  525.             XP_ASSERT(*ptr);
  526.         }
  527.         if (!ptr || !*ptr) break;
  528.  
  529.         while(buf < ptr)
  530.         {
  531.             /* ### mwelch    For each word in the value string, see if
  532.                             the word is a cid: URL. If so, attempt to
  533.                             substitute the appropriate mailbox part URL in
  534.                             its place. */
  535.             ptr2=buf; /* walk from the left end rightward */
  536.             while((ptr2<ptr) && (!XP_IS_SPACE(*ptr2)))
  537.                 ptr2++;
  538.             /* Compare the beginning of the word with "cid:". Yuck. */
  539.             if (((ptr2 - buf) > 4) && 
  540.                 (buf[0]=='c' && buf[1]=='i' && buf[2]=='d' && buf[3]==':'))
  541.             {
  542.                 /* Null terminate the word so we can... */
  543.                 c = *ptr2;
  544.                 *ptr2 = '\0';
  545.                 
  546.                 /* Construct a URL out of the word. */
  547.                 absolute = NET_MakeAbsoluteURL(relobj->base_url, buf);
  548.  
  549.                 /* See if we have a mailbox part URL
  550.                    corresponding to this cid. */
  551.                 part_url = absolute ? XP_Gethash(relobj->hash, buf, NULL)
  552.                     : NULL;
  553.                 FREEIF(absolute);
  554.                 
  555.                 /*If we found a mailbox part URL, write that out instead.*/
  556.                 if (part_url)
  557.                 {
  558.                     status = real_write(relobj, part_url, XP_STRLEN(part_url));
  559.                     if (status < 0) return status;
  560.                     buf = ptr2; /* skip over the cid: URL we substituted */
  561.                 }
  562.                 
  563.                 /* Restore the character that we nulled. */
  564.                 *ptr2 = c;
  565.             }
  566.  
  567.             /* Advance to the beginning of the next word, or to
  568.                the end of the value string. */
  569.             while((ptr2<ptr) && (XP_IS_SPACE(*ptr2)))
  570.                 ptr2++;
  571.  
  572.             /* Write whatever original text remains after
  573.                cid: URL substitution. */
  574.             status = real_write(relobj, buf, ptr2-buf);
  575.             if (status < 0) return status;
  576.             buf = ptr2;
  577.         }
  578.     }
  579.     if (buf && *buf) {
  580.         status = real_write(relobj, buf, XP_STRLEN(buf));
  581.         if (status < 0) return status;
  582.     }
  583.     relobj->curtag_length = 0;
  584.     return 0;
  585. }
  586.  
  587.  
  588.  
  589. static int
  590. mime_multipart_related_output_fn(char* buf, int32 size, void *stream_closure)
  591. {
  592.     MimeMultipartRelated *relobj = (MimeMultipartRelated *) stream_closure;
  593.     char* ptr;
  594.     int32 delta;
  595.     int status;
  596.     while (size > 0) {
  597.         if (relobj->curtag_length > 0) {
  598.             ptr = mime_strnchr(buf, '>', size);
  599.             if (!ptr) {
  600.                 return push_tag(relobj, buf, size);
  601.             }
  602.             delta = ptr - buf + 1;
  603.             status = push_tag(relobj, buf, delta);
  604.             if (status < 0) return status;
  605.             status = flush_tag(relobj);
  606.             if (status < 0) return status;
  607.             buf += delta;
  608.             size -= delta;
  609.         }
  610.         ptr = mime_strnchr(buf, '<', size);
  611.         if (ptr && ptr - buf >= size) ptr = 0;
  612.         if (!ptr) {
  613.             return real_write(relobj, buf, size);
  614.         }
  615.         delta = ptr - buf;
  616.         status = real_write(relobj, buf, delta);
  617.         if (status < 0) return status;
  618.         buf += delta;
  619.         size -= delta;
  620.         XP_ASSERT(relobj->curtag_length == 0);
  621.         status = push_tag(relobj, buf, 1);
  622.         if (status < 0) return status;
  623.         XP_ASSERT(relobj->curtag_length == 1);
  624.         buf++;
  625.         size--;
  626.     }
  627.     return 0;
  628. }
  629.  
  630.  
  631. static int
  632. MimeMultipartRelated_parse_eof (MimeObject *obj, XP_Bool abort_p)
  633. {
  634.     /* OK, all the necessary data has been collected.  We now have to spew out
  635.        the HTML.  We let it go through all the normal mechanisms (which
  636.        includes content-encoding handling), and intercept the output data to do
  637.        translation of the tags.  Whee. */
  638.  
  639.     MimeMultipartRelated *relobj = (MimeMultipartRelated *) obj;
  640.     int status = 0;
  641.     MimeObject *body;
  642.     char* ct;
  643.     const char* dct;
  644.  
  645.     status = ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_eof(obj, abort_p);
  646.     if (status < 0) goto FAIL;
  647.  
  648.     if (!relobj->headobj) return 0;
  649.  
  650.     ct = (relobj->buffered_hdrs
  651.           ? MimeHeaders_get (relobj->buffered_hdrs, HEADER_CONTENT_TYPE,
  652.                              TRUE, FALSE)
  653.           : 0);
  654.     dct = (((MimeMultipartClass *) obj->class)->default_part_type);
  655.  
  656.     relobj->real_output_fn = obj->options->output_fn;
  657.     relobj->real_output_closure = obj->options->output_closure;
  658.  
  659.     obj->options->output_fn = mime_multipart_related_output_fn;
  660.     obj->options->output_closure = obj;
  661.  
  662.     body = mime_create(((ct && *ct) ? ct : (dct ? dct : TEXT_HTML)),
  663.                        relobj->buffered_hdrs, obj->options);
  664.     if (!body) {
  665.         status = MK_OUT_OF_MEMORY;
  666.         goto FAIL;
  667.     }
  668.     status = ((MimeContainerClass *) obj->class)->add_child(obj, body);
  669.     if (status < 0) {
  670.         mime_free(body);
  671.         goto FAIL;
  672.     }
  673.  
  674. #ifdef MIME_DRAFTS
  675.     if ( obj->options && 
  676.        obj->options->decompose_file_p &&
  677.        obj->options->decompose_file_init_fn &&
  678.        (relobj->file_buffer_name || relobj->head_buffer))
  679.     {
  680.       status = obj->options->decompose_file_init_fn ( obj->options->stream_closure,
  681.                                                       relobj->buffered_hdrs );
  682.       if (status < 0) return status;
  683.     }
  684. #endif /* MIME_DRAFTS */
  685.  
  686.  
  687.     /* Now that we've added this new object to our list of children,
  688.        start its parser going. */
  689.     status = body->class->parse_begin(body);
  690.     if (status < 0) goto FAIL;
  691.     
  692.     if (relobj->head_buffer) {
  693.         /* Read it out of memory. */
  694.         XP_ASSERT(!relobj->file_buffer_name && !relobj->file_stream);
  695.  
  696.         status = body->class->parse_buffer(relobj->head_buffer,
  697.                                                relobj->head_buffer_fp,
  698.                                                body);
  699.     } else if (relobj->file_buffer_name) {
  700.         /* Read it off disk.
  701.          */
  702.         char *buf;
  703.         int32 buf_size = 10 * 1024;  /* 10k; tune this? */
  704.  
  705.         XP_ASSERT(relobj->head_buffer_size == 0 &&
  706.                   relobj->head_buffer_fp == 0);
  707.         XP_ASSERT(relobj->file_buffer_name);
  708.         if (!relobj->file_buffer_name) {
  709.             status = -1;
  710.             goto FAIL;
  711.         }
  712.  
  713.         buf = (char *) XP_ALLOC(buf_size);
  714.         if (!buf) {
  715.             status = MK_OUT_OF_MEMORY;
  716.             goto FAIL;
  717.         }
  718.  
  719.         if (relobj->file_stream) {
  720.             XP_FileClose(relobj->file_stream);
  721.         }
  722.         relobj->file_stream = XP_FileOpen(relobj->file_buffer_name,
  723.                                           xpTemporary,
  724.                                           XP_FILE_READ_BIN);
  725.         if (!relobj->file_stream) {
  726.             XP_FREE(buf);
  727.             status = MK_UNABLE_TO_OPEN_TMP_FILE;
  728.             goto FAIL;
  729.         }
  730.  
  731.         while(1) {
  732.             int32 rstatus = XP_FileRead(buf, buf_size - 1,
  733.                                         relobj->file_stream);
  734.             if (rstatus <= 0) {
  735.                 status = rstatus;
  736.                 break;
  737.             } else {
  738.                 /* It would be really nice to be able to yield here, and let
  739.                    some user events and other input sources get processed.
  740.                    Oh well. */
  741.  
  742.                 status = body->class->parse_buffer(buf, rstatus, body);
  743.                 if (status < 0) break;
  744.             }
  745.         }
  746.         XP_FREE(buf);
  747.     }
  748.  
  749.   if (status < 0) goto FAIL;
  750.  
  751.   /* Done parsing. */
  752.   status = body->class->parse_eof(body, FALSE);
  753.   if (status < 0) goto FAIL;
  754.   status = body->class->parse_end(body, FALSE);
  755.   if (status < 0) goto FAIL;
  756.  
  757. FAIL:
  758.  
  759. #ifdef MIME_DRAFTS
  760.   if ( obj->options &&
  761.        obj->options->decompose_file_p &&
  762.        obj->options->decompose_file_close_fn &&
  763.        (relobj->file_buffer_name || relobj->head_buffer)) {
  764.     status = obj->options->decompose_file_close_fn ( obj->options->stream_closure );
  765.     if (status < 0) return status;
  766.   }
  767. #endif /* MIME_DRAFTS */
  768.  
  769.   relobj->headobj = NULL;
  770.   obj->options->output_fn = relobj->real_output_fn;
  771.   obj->options->output_closure = relobj->real_output_closure;
  772.  
  773.   return status;
  774. }    
  775.  
  776.  
  777.  
  778.  
  779. static int
  780. MimeMultipartRelatedClassInitialize(MimeMultipartRelatedClass *class)
  781. {
  782.     MimeObjectClass    *oclass = (MimeObjectClass *) class;
  783.     MimeMultipartClass *mclass = (MimeMultipartClass *) class;
  784.     XP_ASSERT(!oclass->class_initialized);
  785.     oclass->initialize       = MimeMultipartRelated_initialize;
  786.     oclass->finalize         = MimeMultipartRelated_finalize;
  787.     oclass->parse_eof        = MimeMultipartRelated_parse_eof;
  788.     mclass->output_child_p   = MimeMultipartRelated_output_child_p;
  789.     mclass->parse_child_line = MimeMultipartRelated_parse_child_line;
  790.     return 0;
  791. }
  792.