]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/mime_parser.c
* post increment...
[citadel.git] / libcitadel / lib / mime_parser.c
index 3fdcf583501af7c63da76d25888427095d61a937..6db2074a4cf49789573a998bd2447aab28f4a65b 100644 (file)
 #include "libcitadel.h"
 #include "libcitadellocal.h"
 
-void extract_key(char *target, char *source, long sourcelen, char *key, long keylen)
+void extract_key(char *target, char *source, long sourcelen, char *key, long keylen, char KeyEnd)
 {
-       char *lookat;
-       char *ptr;
-       //char looking_for[256];
+       char *sptr, *ptr = NULL;
        int double_quotes = 0;
+       long RealKeyLen = keylen;
 
-       lookat = source;
-       //snprintf(looking_for, sizeof looking_for, "%s=", key);
-
-       while ((lookat != NULL)) {
-               ptr = bmstrcasestr_len(lookat, sourcelen, key, keylen);
-               lookat = ptr;
-               if ((ptr != NULL) &&
-                   (*(ptr + keylen) == '='))
-                       lookat = NULL;
-
+       sptr = source;
 
+       while (sptr != NULL)
+       {
+               ptr = bmstrcasestr_len(sptr, sourcelen - (sptr - source), 
+                                      key, keylen);
+               if(ptr != NULL)
+               {
+                       while (isspace(*(ptr + RealKeyLen)))
+                               RealKeyLen ++;
+                       if (*(ptr + RealKeyLen) == KeyEnd)
+                       {
+                               sptr = NULL;
+                               RealKeyLen ++;                          
+                       }
+                       else
+                       {
+                               sptr = ptr + RealKeyLen + 1;
+                       }
+               }
+               else 
+                       sptr = ptr;
        }
-       //ptr = bmstrcasestr(source, looking_for);
        if (ptr == NULL) {
-               strcpy(target, "");
+               *target = '\0';
                return;
        }
-       strcpy(target, (ptr + keylen + 1));
+       strcpy(target, (ptr + RealKeyLen));
 
-       for (ptr=target; (*ptr != 0); ++ptr) {
+       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 == ';')) {
@@ -405,9 +414,9 @@ void the_mime_parser(char *partnum,
                                memcpy (content_type, &header[13], headerlen - 12);
                                content_type_len = striplt (content_type);
 
-                               extract_key(content_type_name, content_type, content_type_len, HKEY("name"));
-                               extract_key(charset,           content_type, content_type_len, HKEY("charset"));
-                               extract_key(boundary,          header,       headerlen,        HKEY("boundary"));
+                               extract_key(content_type_name, content_type, content_type_len, HKEY("name"), '=');
+                               extract_key(charset,           content_type, content_type_len, HKEY("charset"), '=');
+                               extract_key(boundary,          header,       headerlen,        HKEY("boundary"), '=');
 
                                /* Deal with weird headers */
                                if (strchr(content_type, ' '))
@@ -415,24 +424,24 @@ void the_mime_parser(char *partnum,
                                if (strchr(content_type, ';'))
                                        *(strchr(content_type, ';')) = '\0';
                        }
-                       if (!strncasecmp(header, "Content-Disposition:", 20)) {
+                       else if (!strncasecmp(header, "Content-Disposition:", 20)) {
                                memcpy (disposition, &header[20], headerlen - 19);
                                disposition_len = striplt(disposition);
-                               extract_key(content_disposition_name, disposition, disposition_len,  HKEY("name"));
-                               extract_key(filename,                 disposition, disposition_len, HKEY("filename"));
+                               extract_key(content_disposition_name, disposition, disposition_len,  HKEY("name"), '=');
+                               extract_key(filename,                 disposition, disposition_len, HKEY("filename"), '=');
                        }
-                       if (!strncasecmp(header, "Content-ID:", 11)) {
+                       else if (!strncasecmp(header, "Content-ID:", 11)) {
                                strcpy(id, &header[11]);
                                striplt(id);
                                stripallbut(id, '<', '>');
                        }
-                       if (!strncasecmp(header, "Content-length: ", 15)) {
+                       else if (!strncasecmp(header, "Content-length: ", 15)) {
                                char clbuf[10];
                                safestrncpy(clbuf, &header[15], sizeof clbuf);
                                striplt(clbuf);
                                content_length = (size_t) atol(clbuf);
                        }
-                       if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
+                       else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
                                strcpy(encoding, &header[26]);
                                striplt(encoding);
                        }