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"
17 void extract_key(char *target, char *source, char *key)
21 strcpy(target, source);
22 for (a = 0; a < strlen(target); ++a) {
23 if ((!strncasecmp(&target[a], key, strlen(key)))
24 && (target[a + strlen(key)] == '=')) {
25 strcpy(target, &target[a + strlen(key) + 1]);
27 strcpy(target, &target[1]);
28 for (b = 0; b < strlen(target); ++b)
39 * For non-multipart messages, we need to generate a quickie partnum of "1"
40 * to return to callback functions. Some callbacks demand it.
42 char *fixed_partnum(char *supplied_partnum) {
43 if (supplied_partnum == NULL) return "1";
44 if (strlen(supplied_partnum)==0) return "1";
45 return supplied_partnum;
51 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
53 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
56 int soft_line_break = 0;
58 int decoded_length = 0;
63 memset(buf, '\0', SIZ);
66 for (i = 0; i < sourcelen; ++i) {
67 size_t buflen = strlen(buf);
68 buf[buf_length++] = encoded[i];
70 if ( (encoded[i] == '\n')
72 || (i == (sourcelen-1)) ) {
73 buf[buf_length++] = 0;
75 /*** begin -- process one line ***/
77 if (buf[buflen-1] == '\n') {
81 if (buf[buflen-1] == '\r') {
85 while (isspace(buf[buflen-1])) {
92 if ((buf[0] == '=') && (buf[1] == '\0')) {
96 } else if ((buflen>=3) && (buf[0]=='=')) {
97 sscanf(&buf[1], "%02x", &ch);
98 decoded[decoded_length++] = ch;
102 decoded[decoded_length++] = buf[0];
103 strcpy(buf, &buf[1]);
107 if (soft_line_break == 0) {
108 decoded[decoded_length++] = '\r';
109 decoded[decoded_length++] = '\n';
112 /*** end -- process one line ***/
116 decoded[decoded_length++] = 0;
117 return(decoded_length);
121 * Given a message or message-part body and a length, handle any necessary
122 * decoding and pass the request up the stack.
124 void mime_decode(char *partnum,
125 char *part_start, size_t length,
126 char *content_type, char *charset, char *encoding,
128 char *name, char *filename,
140 void (*PreMultiPartCallBack)
151 void (*PostMultiPartCallBack)
168 size_t bytes_decoded = 0;
170 /* Some encodings aren't really encodings */
171 if (!strcasecmp(encoding, "7bit"))
172 strcpy(encoding, "");
173 if (!strcasecmp(encoding, "8bit"))
174 strcpy(encoding, "");
175 if (!strcasecmp(encoding, "binary"))
176 strcpy(encoding, "");
178 /* If this part is not encoded, send as-is */
179 if ( (strlen(encoding) == 0) || (dont_decode)) {
180 if (CallBack != NULL) {
181 CallBack(name, filename, fixed_partnum(partnum),
182 disposition, part_start,
183 content_type, charset, length, encoding, userdata);
188 /* Fail silently if we hit an unknown encoding. */
189 if ((strcasecmp(encoding, "base64"))
190 && (strcasecmp(encoding, "quoted-printable"))) {
195 * Allocate a buffer for the decoded data. The output buffer is slightly
196 * larger than the input buffer; this assumes that the decoded data
197 * will never be significantly larger than the encoded data. This is a
198 * safe assumption with base64, uuencode, and quoted-printable.
200 decoded = malloc(length + 32768);
201 if (decoded == NULL) {
205 if (!strcasecmp(encoding, "base64")) {
206 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
208 else if (!strcasecmp(encoding, "quoted-printable")) {
209 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
212 if (bytes_decoded > 0) if (CallBack != NULL) {
213 CallBack(name, filename, fixed_partnum(partnum),
214 disposition, decoded,
215 content_type, charset, bytes_decoded, "binary", userdata);
222 * Break out the components of a multipart message
223 * (This function expects to be fed HEADERS + CONTENT)
224 * Note: NULL can be supplied as content_end; in this case, the message is
225 * considered to have ended when the parser encounters a 0x00 byte.
227 void the_mime_parser(char *partnum,
228 char *content_start, char *content_end,
240 void (*PreMultiPartCallBack)
251 void (*PostMultiPartCallBack)
269 char *part_start, *part_end = NULL;
274 size_t startary_len = 0;
279 size_t content_length;
283 char *content_type_name;
284 char *content_disposition_name;
290 char nested_partnum[SIZ];
295 boundary = malloc(SIZ);
296 memset(boundary, 0, SIZ);
298 startary = malloc(SIZ);
299 memset(startary, 0, SIZ);
301 endary = malloc(SIZ);
302 memset(endary, 0, SIZ);
304 header = malloc(SIZ);
305 memset(header, 0, SIZ);
307 content_type = malloc(SIZ);
308 memset(content_type, 0, SIZ);
310 charset = malloc(SIZ);
311 memset(charset, 0, SIZ);
313 encoding = malloc(SIZ);
314 memset(encoding, 0, SIZ);
316 content_type_name = malloc(SIZ);
317 memset(content_type_name, 0, SIZ);
319 content_disposition_name = malloc(SIZ);
320 memset(content_disposition_name, 0, SIZ);
322 filename = malloc(SIZ);
323 memset(filename, 0, SIZ);
325 disposition = malloc(SIZ);
326 memset(disposition, 0, SIZ);
328 /* If the caller didn't supply an endpointer, generate one by measure */
329 if (content_end == NULL) {
330 content_end = &content_start[strlen(content_start)];
333 /* Learn interesting things from the headers */
336 ptr = memreadline(ptr, buf, SIZ);
337 if (ptr >= content_end) {
341 for (i = 0; i < strlen(buf); ++i) {
342 if (isspace(buf[i])) {
347 if (!isspace(buf[0])) {
348 if (!strncasecmp(header, "Content-type: ", 14)) {
349 strcpy(content_type, &header[14]);
350 extract_key(content_type_name, content_type, "name");
351 extract_key(charset, content_type, "charset");
352 /* Deal with weird headers */
353 if (strchr(content_type, ' '))
354 *(strchr(content_type, ' ')) = '\0';
355 if (strchr(content_type, ';'))
356 *(strchr(content_type, ';')) = '\0';
358 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
359 strcpy(disposition, &header[21]);
360 extract_key(content_disposition_name, disposition, "name");
361 extract_key(filename, disposition, "filename");
363 if (!strncasecmp(header, "Content-length: ", 16)) {
364 content_length = (size_t) atol(&header[16]);
366 if (!strncasecmp(header,
367 "Content-transfer-encoding: ", 27))
368 strcpy(encoding, &header[27]);
369 if (strlen(boundary) == 0)
370 extract_key(boundary, header, "boundary");
373 if ((strlen(header) + strlen(buf) + 2) < SIZ)
375 } while ((strlen(buf) > 0) && (*ptr != 0));
377 if (strchr(disposition, ';'))
378 *(strchr(disposition, ';')) = '\0';
379 striplt(disposition);
380 if (strchr(content_type, ';'))
381 *(strchr(content_type, ';')) = '\0';
382 striplt(content_type);
384 if (strlen(boundary) > 0) {
390 /* If this is a multipart message, then recursively process it */
394 /* Tell the client about this message's multipartedness */
395 if (PreMultiPartCallBack != NULL) {
396 PreMultiPartCallBack("", "", partnum, "",
397 NULL, content_type, charset,
398 0, encoding, userdata);
401 /* Figure out where the boundaries are */
402 snprintf(startary, SIZ, "--%s", boundary);
403 snprintf(endary, SIZ, "--%s--", boundary);
404 startary_len = strlen(startary);
408 next_boundary = NULL;
409 for (srch=ptr; srch<content_end; ++srch) {
410 if (!memcmp(srch, startary, startary_len)) {
411 next_boundary = srch;
416 if ( (part_start != NULL) && (next_boundary != NULL) ) {
417 part_end = next_boundary;
420 if (strlen(partnum) > 0) {
421 snprintf(nested_partnum,
422 sizeof nested_partnum,
427 snprintf(nested_partnum,
428 sizeof nested_partnum,
431 the_mime_parser(nested_partnum,
432 part_start, part_end,
434 PreMultiPartCallBack,
435 PostMultiPartCallBack,
440 if (next_boundary != NULL) {
441 /* If we pass out of scope, don't attempt to
442 * read past the end boundary. */
443 if (!strcmp(next_boundary, endary)) {
447 /* Set up for the next part. */
448 part_start = strstr(next_boundary, "\n");
454 /* Invalid end of multipart. Bail out! */
457 } while ( (ptr < content_end) && (next_boundary != NULL) );
459 if (PostMultiPartCallBack != NULL) {
460 PostMultiPartCallBack("", "", partnum, "", NULL,
461 content_type, charset, 0, encoding, userdata);
466 /* If it's not a multipart message, then do something with it */
470 while (ptr < content_end) {
474 part_end = content_end;
475 /* fix an off-by-one error */
479 /* Truncate if the header told us to */
480 if ( (content_length > 0) && (length > content_length) ) {
481 length = content_length;
484 /* Sometimes the "name" field is tacked on to Content-type,
485 * and sometimes it's tacked on to Content-disposition. Use
486 * whichever one we have.
488 if (strlen(content_disposition_name) > strlen(content_type_name)) {
489 name = content_disposition_name;
492 name = content_type_name;
495 /* lprintf(CTDL_DEBUG, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
496 partnum, length, content_type, charset, encoding); */
498 /* Ok, we've got a non-multipart part here, so do something with it.
502 content_type, charset, encoding, disposition,
504 CallBack, NULL, NULL,
505 userdata, dont_decode
509 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
511 if (!strcasecmp(content_type, "message/rfc822")) {
513 if (PreMultiPartCallBack != NULL) {
514 PreMultiPartCallBack("", "", partnum, "",
515 NULL, content_type, charset,
516 0, encoding, userdata);
518 if (CallBack != NULL) {
519 if (strlen(partnum) > 0) {
520 snprintf(nested_partnum,
521 sizeof nested_partnum,
526 snprintf(nested_partnum,
527 sizeof nested_partnum,
530 the_mime_parser(nested_partnum,
531 part_start, part_end,
533 PreMultiPartCallBack,
534 PostMultiPartCallBack,
539 if (PostMultiPartCallBack != NULL) {
540 PostMultiPartCallBack("", "", partnum, "", NULL,
541 content_type, charset, 0, encoding, userdata);
549 end_parser: /* free the buffers! end the oppression!! */
557 free(content_type_name);
558 free(content_disposition_name);
566 * Entry point for the MIME parser.
567 * (This function expects to be fed HEADERS + CONTENT)
568 * Note: NULL can be supplied as content_end; in this case, the message is
569 * considered to have ended when the parser encounters a 0x00 byte.
571 void mime_parser(char *content_start,
586 void (*PreMultiPartCallBack)
598 void (*PostMultiPartCallBack)
615 the_mime_parser("", content_start, content_end,
617 PreMultiPartCallBack,
618 PostMultiPartCallBack,
619 userdata, dont_decode);