Updated extract_key() in mime_parser.c
authorArt Cancro <ajc@citadel.org>
Fri, 26 Oct 2007 20:22:32 +0000 (20:22 +0000)
committerArt Cancro <ajc@citadel.org>
Fri, 26 Oct 2007 20:22:32 +0000 (20:22 +0000)
We now handle the rare situation in which the 'boundary'
key is not the last key in the content type declaration.  This
fixes the occasional 'Unexpected end of message (2)' error in
WebCit.

TODO: copy updated mime parser to webcit

citadel/mime_parser.c
citadel/msgbase.c

index 6b1f3c40e41bc7a315145601e81d9773600707d1..9569d68eb07f4f9c8c2fc42d40b8a3bfa6a0a1ae 100644 (file)
@@ -30,6 +30,7 @@ void extract_key(char *target, char *source, char *key)
 {
        char *ptr;
        char looking_for[256];
+       int double_quotes = 0;
 
        snprintf(looking_for, sizeof looking_for, "%s=", key);
 
@@ -41,8 +42,21 @@ void extract_key(char *target, char *source, char *key)
        strcpy(target, (ptr + strlen(looking_for)));
 
        for (ptr=target; (*ptr != 0); ++ptr) {
+
+               /* A semicolon means we've hit the end of the key, unless we're inside double quotes */
+               if ( (double_quotes != 1) && (*ptr == ';')) {
+                       *ptr = 0;
+               }
+
+               /* if we find double quotes, we've got a great set of string boundaries */
                if (*ptr == '\"') {
-                       strcpy(ptr, ptr+1);
+                       ++double_quotes;
+                       if (double_quotes == 1) {
+                               strcpy(ptr, ptr+1);
+                       }
+                       else {
+                               *ptr = 0;
+                       }
                }
        }
 }
@@ -335,6 +349,7 @@ void the_mime_parser(char *partnum,
                                striplt(content_type);
                                extract_key(content_type_name, content_type, "name");
                                extract_key(charset, content_type, "charset");
+                               extract_key(boundary, header, "boundary");
                                /* Deal with weird headers */
                                if (strchr(content_type, ' '))
                                        *(strchr(content_type, ' ')) = '\0';
@@ -357,8 +372,6 @@ void the_mime_parser(char *partnum,
                                strcpy(encoding, &header[26]);
                                striplt(encoding);
                        }
-                       if (strlen(boundary) == 0)
-                               extract_key(boundary, header, "boundary");
                        strcpy(header, "");
                        headerlen = 0;
                }
index 90d62a53b2f77f6754fd8294798c76ddfc5a590f..b260db4cbe3e347a58c25b2cfe4660147a3d2c4a 100644 (file)
@@ -1034,9 +1034,7 @@ struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
         */
        ch = *mptr++;
        if (ch != 255) {
-               lprintf(CTDL_ERR,
-                       "Message %ld appears to be corrupted.\n",
-                       msgnum);
+               lprintf(CTDL_ERR, "Message %ld appears to be corrupted.\n", msgnum);
                cdb_free(dmsgtext);
                return NULL;
        }
@@ -1199,8 +1197,7 @@ void fixed_output(char *name, char *filename, char *partnum, char *disp,
         * we've already printed another section, skip this one.
         */     
        if ( (ma->is_ma) && (ma->did_print) ) {
-               lprintf(CTDL_DEBUG, "Skipping part %s (%s)\n",
-                       partnum, cbtype);
+               lprintf(CTDL_DEBUG, "Skipping part %s (%s)\n", partnum, cbtype);
                return;
        }
        ma->did_print = 1;
@@ -1268,6 +1265,7 @@ void choose_preferred(char *name, char *filename, char *partnum, char *disp,
                        extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
                        if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
                                if (i < ma->chosen_pref) {
+                                       lprintf(CTDL_DEBUG, "Setting chosen part: <%s>\n", partnum);
                                        safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
                                        ma->chosen_pref = i;
                                }