/*
* $Id$
*
- * This is the MIME parser brought over from the Citadel server source code.
- * We use it to handle HTTP uploads, which are sent in MIME format. In the
- * future we'll use it to output MIME messages as well.
+ * This is the MIME parser for Citadel. Sometimes it actually works.
*
* Copyright (c) 1998-2001 by Art Cancro
* This code is distributed under the terms of the GNU General Public License.
#include "mime_parser.h"
-
void extract_key(char *target, char *source, char *key)
{
int a, b;
}
-
-/*
- * Utility function to "readline" from memory
- * (returns new pointer)
- */
-char *memreadline(char *start, char *buf, int maxlen)
-{
- char ch;
- char *ptr;
- int len = 0; /* tally our own length to avoid strlen() delays */
-
- ptr = start;
- memset(buf, 0, maxlen);
-
- while (1) {
- ch = *ptr++;
- if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
- buf[strlen(buf) + 1] = 0;
- buf[strlen(buf)] = ch;
- ++len;
- }
- if ((ch == 10) || (ch == 0)) {
- return ptr;
- }
- }
-}
-
-
/*
* For non-multipart messages, we need to generate a quickie partnum of "1"
* to return to callback functions. Some callbacks demand it.
{
char *ptr;
- char *part_start, *part_end;
+ char *part_start, *part_end = NULL;
char buf[SIZ];
char header[SIZ];
char boundary[SIZ];
memset(disposition, 0, sizeof disposition);
content_length = 0;
+ /* If the caller didn't supply an endpointer, generate one by measure */
+ if (content_end == NULL) {
+ content_end = &content_start[strlen(content_start)];
+ }
+
/* Learn interesting things from the headers */
strcpy(header, "");
do {
ptr = memreadline(ptr, buf, sizeof buf);
- /* if (*ptr == 0)
- return; premature end of message */
- if (content_end != NULL)
- if (ptr >= content_end)
- return;
+ if (ptr >= content_end)
+ return;
for (i = 0; i < strlen(buf); ++i)
if (isspace(buf[i]))
NULL, content_type,
0, encoding, userdata);
}
- /***********
- if (CallBack != NULL) {
- CallBack("", "", fixed_partnum(partnum),
- "", NULL, content_type,
- 0, encoding, userdata);
- }
- ***********/
/* Figure out where the boundaries are */
sprintf(startary, "--%s", boundary);
sprintf(endary, "--%s--", boundary);
do {
- part_end = ptr;
- if (content_end != NULL)
- if (ptr >= content_end) goto END_MULTI;
+ /* if (ptr >= content_end) goto END_MULTI; */
- if ( (!strncasecmp(buf, startary, strlen(startary)))
- || (!strncasecmp(buf, endary, strlen(endary))) ) {
+ if ( (!strncasecmp(ptr, startary, strlen(startary)))
+ || (!strncasecmp(ptr, endary, strlen(endary))) ) {
fprintf(stderr, "hit boundary!\n");
if (part_start != NULL) {
if (strlen(partnum) > 0) {
userdata,
dont_decode);
}
- part_start = ptr;
ptr = memreadline(ptr, buf, sizeof(buf));
+ part_start = ptr;
}
else {
+ part_end = ptr;
++ptr;
}
- } while ( (strcasecmp(buf, endary)) && (ptr <= content_end) );
-END_MULTI: if (PostMultiPartCallBack != NULL) {
+ } while ( (strcasecmp(ptr, endary)) && (ptr <= content_end) );
+ if (PostMultiPartCallBack != NULL) {
PostMultiPartCallBack("", "", partnum, "", NULL,
content_type, 0, encoding, userdata);
}
fprintf(stderr, "doing non-multipart thing\n");
part_start = ptr;
length = 0;
- if (content_end == NULL) {
- while (*ptr != 0) {
- ++length;
- part_end = ptr++;
- }
- }
- else {
- while (ptr < content_end) {
- ++ptr;
- ++length;
- }
- part_end = content_end;
+ while (ptr < content_end) {
+ ++ptr;
+ ++length;
}
+ part_end = content_end;
/* Truncate if the header told us to */
if ( (content_length > 0) && (length > content_length) ) {