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.
52 * according to RFC2045 section 6.7
54 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
63 decoded[0] = encoded[0];
64 while (sourcepos <= sourcelen){
65 check = &decoded[destpos];
66 decoded[destpos] = encoded[sourcepos];
67 if ((ignore_last == 0) && (decoded[destpos-1] == '='))
69 if ((*check == '\0') ||
73 decoded[destpos - 1] = '\0';
76 else if (sourcelen - sourcepos > 2)
78 sscanf(&encoded[sourcepos], "%02x", &ch);
79 decoded[destpos - 1] = ch;
96 * Given a message or message-part body and a length, handle any necessary
97 * decoding and pass the request up the stack.
99 void mime_decode(char *partnum,
100 char *part_start, size_t length,
101 char *content_type, char *charset, char *encoding,
103 char *name, char *filename,
115 void (*PreMultiPartCallBack)
126 void (*PostMultiPartCallBack)
143 size_t bytes_decoded = 0;
145 /* Some encodings aren't really encodings */
146 if (!strcasecmp(encoding, "7bit"))
147 strcpy(encoding, "");
148 if (!strcasecmp(encoding, "8bit"))
149 strcpy(encoding, "");
150 if (!strcasecmp(encoding, "binary"))
151 strcpy(encoding, "");
153 /* If this part is not encoded, send as-is */
154 if ( (strlen(encoding) == 0) || (dont_decode)) {
155 if (CallBack != NULL) {
156 CallBack(name, filename, fixed_partnum(partnum),
157 disposition, part_start,
158 content_type, charset, length, encoding, userdata);
163 /* Fail silently if we hit an unknown encoding. */
164 if ((strcasecmp(encoding, "base64"))
165 && (strcasecmp(encoding, "quoted-printable"))) {
170 * Allocate a buffer for the decoded data. The output buffer is slightly
171 * larger than the input buffer; this assumes that the decoded data
172 * will never be significantly larger than the encoded data. This is a
173 * safe assumption with base64, uuencode, and quoted-printable.
175 decoded = malloc(length + 32768);
176 if (decoded == NULL) {
180 if (!strcasecmp(encoding, "base64")) {
181 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
183 else if (!strcasecmp(encoding, "quoted-printable")) {
184 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
187 if (bytes_decoded > 0) if (CallBack != NULL) {
188 CallBack(name, filename, fixed_partnum(partnum),
189 disposition, decoded,
190 content_type, charset, bytes_decoded, "binary", userdata);
197 * Break out the components of a multipart message
198 * (This function expects to be fed HEADERS + CONTENT)
199 * Note: NULL can be supplied as content_end; in this case, the message is
200 * considered to have ended when the parser encounters a 0x00 byte.
202 void the_mime_parser(char *partnum,
203 char *content_start, char *content_end,
215 void (*PreMultiPartCallBack)
226 void (*PostMultiPartCallBack)
244 char *part_start, *part_end = NULL;
249 size_t startary_len = 0;
254 size_t content_length;
258 char *content_type_name;
259 char *content_disposition_name;
265 char nested_partnum[SIZ];
270 boundary = malloc(SIZ);
271 memset(boundary, 0, SIZ);
273 startary = malloc(SIZ);
274 memset(startary, 0, SIZ);
276 endary = malloc(SIZ);
277 memset(endary, 0, SIZ);
279 header = malloc(SIZ);
280 memset(header, 0, SIZ);
282 content_type = malloc(SIZ);
283 memset(content_type, 0, SIZ);
285 charset = malloc(SIZ);
286 memset(charset, 0, SIZ);
288 encoding = malloc(SIZ);
289 memset(encoding, 0, SIZ);
291 content_type_name = malloc(SIZ);
292 memset(content_type_name, 0, SIZ);
294 content_disposition_name = malloc(SIZ);
295 memset(content_disposition_name, 0, SIZ);
297 filename = malloc(SIZ);
298 memset(filename, 0, SIZ);
300 disposition = malloc(SIZ);
301 memset(disposition, 0, SIZ);
303 /* If the caller didn't supply an endpointer, generate one by measure */
304 if (content_end == NULL) {
305 content_end = &content_start[strlen(content_start)];
308 /* Learn interesting things from the headers */
311 ptr = memreadline(ptr, buf, SIZ);
312 if (ptr >= content_end) {
316 for (i = 0; i < strlen(buf); ++i) {
317 if (isspace(buf[i])) {
322 if (!isspace(buf[0])) {
323 if (!strncasecmp(header, "Content-type: ", 14)) {
324 strcpy(content_type, &header[14]);
325 extract_key(content_type_name, content_type, "name");
326 extract_key(charset, content_type, "charset");
327 /* Deal with weird headers */
328 if (strchr(content_type, ' '))
329 *(strchr(content_type, ' ')) = '\0';
330 if (strchr(content_type, ';'))
331 *(strchr(content_type, ';')) = '\0';
333 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
334 strcpy(disposition, &header[21]);
335 extract_key(content_disposition_name, disposition, "name");
336 extract_key(filename, disposition, "filename");
338 if (!strncasecmp(header, "Content-length: ", 16)) {
339 content_length = (size_t) atol(&header[16]);
341 if (!strncasecmp(header,
342 "Content-transfer-encoding: ", 27))
343 strcpy(encoding, &header[27]);
344 if (strlen(boundary) == 0)
345 extract_key(boundary, header, "boundary");
348 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;
395 if (strlen(partnum) > 0) {
396 snprintf(nested_partnum,
397 sizeof nested_partnum,
402 snprintf(nested_partnum,
403 sizeof nested_partnum,
406 the_mime_parser(nested_partnum,
407 part_start, part_end,
409 PreMultiPartCallBack,
410 PostMultiPartCallBack,
415 if (next_boundary != NULL) {
416 /* If we pass out of scope, don't attempt to
417 * read past the end boundary. */
418 if (!strcmp(next_boundary, endary)) {
422 /* Set up for the next part. */
423 part_start = strstr(next_boundary, "\n");
429 /* Invalid end of multipart. Bail out! */
432 } while ( (ptr < content_end) && (next_boundary != NULL) );
434 if (PostMultiPartCallBack != NULL) {
435 PostMultiPartCallBack("", "", partnum, "", NULL,
436 content_type, charset, 0, encoding, userdata);
441 /* If it's not a multipart message, then do something with it */
445 while (ptr < content_end) {
449 part_end = content_end;
450 /* fix an off-by-one error */
454 /* Truncate if the header told us to */
455 if ( (content_length > 0) && (length > content_length) ) {
456 length = content_length;
459 /* Sometimes the "name" field is tacked on to Content-type,
460 * and sometimes it's tacked on to Content-disposition. Use
461 * whichever one we have.
463 if (strlen(content_disposition_name) > strlen(content_type_name)) {
464 name = content_disposition_name;
467 name = content_type_name;
470 /* lprintf(CTDL_DEBUG, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
471 partnum, length, content_type, charset, encoding); */
473 /* Ok, we've got a non-multipart part here, so do something with it.
477 content_type, charset, encoding, disposition,
479 CallBack, NULL, NULL,
480 userdata, dont_decode
484 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
486 if (!strcasecmp(content_type, "message/rfc822")) {
488 if (PreMultiPartCallBack != NULL) {
489 PreMultiPartCallBack("", "", partnum, "",
490 NULL, content_type, charset,
491 0, encoding, userdata);
493 if (CallBack != NULL) {
494 if (strlen(partnum) > 0) {
495 snprintf(nested_partnum,
496 sizeof nested_partnum,
501 snprintf(nested_partnum,
502 sizeof nested_partnum,
505 the_mime_parser(nested_partnum,
506 part_start, part_end,
508 PreMultiPartCallBack,
509 PostMultiPartCallBack,
514 if (PostMultiPartCallBack != NULL) {
515 PostMultiPartCallBack("", "", partnum, "", NULL,
516 content_type, charset, 0, encoding, userdata);
524 end_parser: /* free the buffers! end the oppression!! */
532 free(content_type_name);
533 free(content_disposition_name);
541 * Entry point for the MIME parser.
542 * (This function expects to be fed HEADERS + CONTENT)
543 * Note: NULL can be supplied as content_end; in this case, the message is
544 * considered to have ended when the parser encounters a 0x00 byte.
546 void mime_parser(char *content_start,
561 void (*PreMultiPartCallBack)
573 void (*PostMultiPartCallBack)
590 the_mime_parser("", content_start, content_end,
592 PreMultiPartCallBack,
593 PostMultiPartCallBack,
594 userdata, dont_decode);