5 * \defgroup MIME This is the MIME parser for Citadel.
7 * Copyright (c) 1998-2005 by Art Cancro
8 * This code is distributed under the terms of the GNU General Public License.
9 * \ingroup WebcitHttpServer
13 #include "webserver.h"
14 #include "mime_parser.h"
16 void extract_key(char *target, char *source, char *key)
20 strcpy(target, source);
21 for (a = 0; a < strlen(target); ++a) {
22 if ((!strncasecmp(&target[a], key, strlen(key)))
23 && (target[a + strlen(key)] == '=')) {
24 strcpy(target, &target[a + strlen(key) + 1]);
26 strcpy(target, &target[1]);
27 for (b = 0; b < strlen(target); ++b)
38 * For non-multipart messages, we need to generate a quickie partnum of "1"
39 * to return to callback functions. Some callbacks demand it.
41 char *fixed_partnum(char *supplied_partnum) {
42 if (supplied_partnum == NULL) return "1";
43 if (strlen(supplied_partnum)==0) return "1";
44 return supplied_partnum;
50 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
51 * according to RFC2045 section 6.7
53 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
55 int decoded_length = 0;
58 while (pos < sourcelen)
60 if (!strncmp(&encoded[pos], "=\r\n", 3))
64 else if (!strncmp(&encoded[pos], "=\n", 2))
68 else if (encoded[pos] == '=')
71 sscanf(&encoded[pos+1], "%02x", &ch);
73 decoded[decoded_length++] = ch;
77 decoded[decoded_length++] = encoded[pos];
81 decoded[decoded_length] = 0;
82 return(decoded_length);
87 * Given a message or message-part body and a length, handle any necessary
88 * decoding and pass the request up the stack.
90 void mime_decode(char *partnum,
91 char *part_start, size_t length,
92 char *content_type, char *charset, char *encoding,
94 char *name, char *filename,
106 void (*PreMultiPartCallBack)
117 void (*PostMultiPartCallBack)
134 size_t bytes_decoded = 0;
136 /* Some encodings aren't really encodings */
137 if (!strcasecmp(encoding, "7bit"))
138 strcpy(encoding, "");
139 if (!strcasecmp(encoding, "8bit"))
140 strcpy(encoding, "");
141 if (!strcasecmp(encoding, "binary"))
142 strcpy(encoding, "");
144 /* If this part is not encoded, send as-is */
145 if ( (strlen(encoding) == 0) || (dont_decode)) {
146 if (CallBack != NULL) {
147 CallBack(name, filename, fixed_partnum(partnum),
148 disposition, part_start,
149 content_type, charset, length, encoding, userdata);
154 /* Fail silently if we hit an unknown encoding. */
155 if ((strcasecmp(encoding, "base64"))
156 && (strcasecmp(encoding, "quoted-printable"))) {
161 * Allocate a buffer for the decoded data. The output buffer is slightly
162 * larger than the input buffer; this assumes that the decoded data
163 * will never be significantly larger than the encoded data. This is a
164 * safe assumption with base64, uuencode, and quoted-printable.
166 decoded = malloc(length + 32768);
167 if (decoded == NULL) {
171 if (!strcasecmp(encoding, "base64")) {
172 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
174 else if (!strcasecmp(encoding, "quoted-printable")) {
175 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
178 if (bytes_decoded > 0) if (CallBack != NULL) {
179 CallBack(name, filename, fixed_partnum(partnum),
180 disposition, decoded,
181 content_type, charset, bytes_decoded, "binary", userdata);
188 * Break out the components of a multipart message
189 * (This function expects to be fed HEADERS + CONTENT)
190 * Note: NULL can be supplied as content_end; in this case, the message is
191 * considered to have ended when the parser encounters a 0x00 byte.
193 void the_mime_parser(char *partnum,
194 char *content_start, char *content_end,
206 void (*PreMultiPartCallBack)
217 void (*PostMultiPartCallBack)
235 char *part_start, *part_end = NULL;
240 size_t startary_len = 0;
245 size_t content_length;
249 char *content_type_name;
250 char *content_disposition_name;
256 char nested_partnum[256];
258 char *evaluate_crlf_ptr = NULL;
263 boundary = malloc(SIZ);
264 memset(boundary, 0, SIZ);
266 startary = malloc(SIZ);
267 memset(startary, 0, SIZ);
269 endary = malloc(SIZ);
270 memset(endary, 0, SIZ);
272 header = malloc(SIZ);
273 memset(header, 0, SIZ);
275 content_type = malloc(SIZ);
276 memset(content_type, 0, SIZ);
278 charset = malloc(SIZ);
279 memset(charset, 0, SIZ);
281 encoding = malloc(SIZ);
282 memset(encoding, 0, SIZ);
284 content_type_name = malloc(SIZ);
285 memset(content_type_name, 0, SIZ);
287 content_disposition_name = malloc(SIZ);
288 memset(content_disposition_name, 0, SIZ);
290 filename = malloc(SIZ);
291 memset(filename, 0, SIZ);
293 disposition = malloc(SIZ);
294 memset(disposition, 0, SIZ);
296 /* If the caller didn't supply an endpointer, generate one by measure */
297 if (content_end == NULL) {
298 content_end = &content_start[strlen(content_start)];
301 /* Learn interesting things from the headers */
304 ptr = memreadline(ptr, buf, SIZ);
305 if (ptr >= content_end) {
309 for (i = 0; i < strlen(buf); ++i) {
310 if (isspace(buf[i])) {
315 if (!isspace(buf[0])) {
316 if (!strncasecmp(header, "Content-type:", 13)) {
317 strcpy(content_type, &header[13]);
318 striplt(content_type);
319 extract_key(content_type_name, content_type, "name");
320 extract_key(charset, content_type, "charset");
321 /* Deal with weird headers */
322 if (strchr(content_type, ' '))
323 *(strchr(content_type, ' ')) = '\0';
324 if (strchr(content_type, ';'))
325 *(strchr(content_type, ';')) = '\0';
327 if (!strncasecmp(header, "Content-Disposition:", 20)) {
328 strcpy(disposition, &header[20]);
329 striplt(disposition);
330 extract_key(content_disposition_name, disposition, "name");
331 extract_key(filename, disposition, "filename");
333 if (!strncasecmp(header, "Content-length: ", 15)) {
335 safestrncpy(clbuf, &header[15], sizeof clbuf);
337 content_length = (size_t) atol(clbuf);
339 if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
340 strcpy(encoding, &header[26]);
343 if (strlen(boundary) == 0)
344 extract_key(boundary, header, "boundary");
347 if ((strlen(header) + strlen(buf) + 2) < SIZ) {
350 } while ((strlen(buf) > 0) && (*ptr != 0));
352 if (strchr(disposition, ';'))
353 *(strchr(disposition, ';')) = '\0';
354 striplt(disposition);
355 if (strchr(content_type, ';'))
356 *(strchr(content_type, ';')) = '\0';
357 striplt(content_type);
359 if (strlen(boundary) > 0) {
365 /* If this is a multipart message, then recursively process it */
369 /* Tell the client about this message's multipartedness */
370 if (PreMultiPartCallBack != NULL) {
371 PreMultiPartCallBack("", "", partnum, "",
372 NULL, content_type, charset,
373 0, encoding, userdata);
376 /* Figure out where the boundaries are */
377 snprintf(startary, SIZ, "--%s", boundary);
378 snprintf(endary, SIZ, "--%s--", boundary);
379 startary_len = strlen(startary);
383 next_boundary = NULL;
384 for (srch=ptr; srch<content_end; ++srch) {
385 if (!memcmp(srch, startary, startary_len)) {
386 next_boundary = srch;
391 if ( (part_start != NULL) && (next_boundary != NULL) ) {
392 part_end = next_boundary;
393 --part_end; /* omit the trailing LF */
395 --part_end; /* omit the trailing CR */
398 if (strlen(partnum) > 0) {
399 snprintf(nested_partnum,
400 sizeof nested_partnum,
405 snprintf(nested_partnum,
406 sizeof nested_partnum,
409 the_mime_parser(nested_partnum,
410 part_start, part_end,
412 PreMultiPartCallBack,
413 PostMultiPartCallBack,
418 if (next_boundary != NULL) {
419 /* If we pass out of scope, don't attempt to
420 * read past the end boundary. */
421 if (!strcmp(next_boundary, endary)) {
425 /* Set up for the next part. */
426 part_start = strstr(next_boundary, "\n");
428 /* Determine whether newlines are LF or CRLF */
429 evaluate_crlf_ptr = part_start;
431 if (!memcmp(evaluate_crlf_ptr, "\r\n", 2)) {
438 /* Advance past the LF ... now we're in the next part */
444 /* Invalid end of multipart. Bail out! */
447 } while ( (ptr < content_end) && (next_boundary != NULL) );
449 if (PostMultiPartCallBack != NULL) {
450 PostMultiPartCallBack("", "", partnum, "", NULL,
451 content_type, charset, 0, encoding, userdata);
456 /* If it's not a multipart message, then do something with it */
460 while (ptr < content_end) {
464 part_end = content_end;
467 * I thought there was an off-by-one error here, but there isn't.
468 * This probably means that there's an off-by-one error somewhere
469 * else ... or maybe only in certain messages?
474 /* Truncate if the header told us to */
475 if ( (content_length > 0) && (length > content_length) ) {
476 length = content_length;
479 /* Sometimes the "name" field is tacked on to Content-type,
480 * and sometimes it's tacked on to Content-disposition. Use
481 * whichever one we have.
483 if (strlen(content_disposition_name) > strlen(content_type_name)) {
484 name = content_disposition_name;
487 name = content_type_name;
490 /* lprintf(CTDL_DEBUG, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
491 partnum, length, content_type, charset, encoding); */
493 /* Ok, we've got a non-multipart part here, so do something with it.
497 content_type, charset, encoding, disposition,
499 CallBack, NULL, NULL,
500 userdata, dont_decode
504 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
506 if (!strcasecmp(content_type, "message/rfc822")) {
508 if (PreMultiPartCallBack != NULL) {
509 PreMultiPartCallBack("", "", partnum, "",
510 NULL, content_type, charset,
511 0, encoding, userdata);
513 if (CallBack != NULL) {
514 if (strlen(partnum) > 0) {
515 snprintf(nested_partnum,
516 sizeof nested_partnum,
521 snprintf(nested_partnum,
522 sizeof nested_partnum,
525 the_mime_parser(nested_partnum,
526 part_start, part_end,
528 PreMultiPartCallBack,
529 PostMultiPartCallBack,
534 if (PostMultiPartCallBack != NULL) {
535 PostMultiPartCallBack("", "", partnum, "", NULL,
536 content_type, charset, 0, encoding, userdata);
544 end_parser: /* free the buffers! end the oppression!! */
552 free(content_type_name);
553 free(content_disposition_name);
561 * Entry point for the MIME parser.
562 * (This function expects to be fed HEADERS + CONTENT)
563 * Note: NULL can be supplied as content_end; in this case, the message is
564 * considered to have ended when the parser encounters a 0x00 byte.
566 void mime_parser(char *content_start,
581 void (*PreMultiPartCallBack)
593 void (*PostMultiPartCallBack)
610 the_mime_parser("", content_start, content_end,
612 PreMultiPartCallBack,
613 PostMultiPartCallBack,
614 userdata, dont_decode);