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"
18 void extract_key(char *target, char *source, char *key)
22 strcpy(target, source);
23 for (a = 0; a < strlen(target); ++a) {
24 if ((!strncasecmp(&target[a], key, strlen(key)))
25 && (target[a + strlen(key)] == '=')) {
26 strcpy(target, &target[a + strlen(key) + 1]);
28 strcpy(target, &target[1]);
29 for (b = 0; b < strlen(target); ++b)
40 * For non-multipart messages, we need to generate a quickie partnum of "1"
41 * to return to callback functions. Some callbacks demand it.
43 char *fixed_partnum(char *supplied_partnum) {
44 if (supplied_partnum == NULL) return "1";
45 if (strlen(supplied_partnum)==0) return "1";
46 return supplied_partnum;
52 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
53 * according to RFC2045 section 6.7
55 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
57 int decoded_length = 0;
60 while (pos < sourcelen)
62 if (!strncmp(&encoded[pos], "=\r\n", 3))
66 else if (!strncmp(&encoded[pos], "=\n", 2))
70 else if (encoded[pos] == '=')
73 sscanf(&encoded[pos+1], "%02x", &ch);
75 decoded[decoded_length++] = ch;
79 decoded[decoded_length++] = encoded[pos];
83 decoded[decoded_length] = 0;
84 return(decoded_length);
89 * Given a message or message-part body and a length, handle any necessary
90 * decoding and pass the request up the stack.
92 void mime_decode(char *partnum,
93 char *part_start, size_t length,
94 char *content_type, char *charset, char *encoding,
96 char *name, char *filename,
108 void (*PreMultiPartCallBack)
119 void (*PostMultiPartCallBack)
136 size_t bytes_decoded = 0;
138 /* Some encodings aren't really encodings */
139 if (!strcasecmp(encoding, "7bit"))
140 strcpy(encoding, "");
141 if (!strcasecmp(encoding, "8bit"))
142 strcpy(encoding, "");
143 if (!strcasecmp(encoding, "binary"))
144 strcpy(encoding, "");
146 /* If this part is not encoded, send as-is */
147 if ( (strlen(encoding) == 0) || (dont_decode)) {
148 if (CallBack != NULL) {
149 CallBack(name, filename, fixed_partnum(partnum),
150 disposition, part_start,
151 content_type, charset, length, encoding, userdata);
156 /* Fail silently if we hit an unknown encoding. */
157 if ((strcasecmp(encoding, "base64"))
158 && (strcasecmp(encoding, "quoted-printable"))) {
163 * Allocate a buffer for the decoded data. The output buffer is slightly
164 * larger than the input buffer; this assumes that the decoded data
165 * will never be significantly larger than the encoded data. This is a
166 * safe assumption with base64, uuencode, and quoted-printable.
168 decoded = malloc(length + 32768);
169 if (decoded == NULL) {
173 if (!strcasecmp(encoding, "base64")) {
174 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
176 else if (!strcasecmp(encoding, "quoted-printable")) {
177 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
180 if (bytes_decoded > 0) if (CallBack != NULL) {
181 CallBack(name, filename, fixed_partnum(partnum),
182 disposition, decoded,
183 content_type, charset, bytes_decoded, "binary", userdata);
190 * Break out the components of a multipart message
191 * (This function expects to be fed HEADERS + CONTENT)
192 * Note: NULL can be supplied as content_end; in this case, the message is
193 * considered to have ended when the parser encounters a 0x00 byte.
195 void the_mime_parser(char *partnum,
196 char *content_start, char *content_end,
208 void (*PreMultiPartCallBack)
219 void (*PostMultiPartCallBack)
237 char *part_start, *part_end = NULL;
242 size_t startary_len = 0;
247 size_t content_length;
251 char *content_type_name;
252 char *content_disposition_name;
258 char nested_partnum[SIZ];
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: ", 14)) {
317 strcpy(content_type, &header[14]);
318 extract_key(content_type_name, content_type, "name");
319 extract_key(charset, content_type, "charset");
320 /* Deal with weird headers */
321 if (strchr(content_type, ' '))
322 *(strchr(content_type, ' ')) = '\0';
323 if (strchr(content_type, ';'))
324 *(strchr(content_type, ';')) = '\0';
326 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
327 strcpy(disposition, &header[21]);
328 extract_key(content_disposition_name, disposition, "name");
329 extract_key(filename, disposition, "filename");
331 if (!strncasecmp(header, "Content-length: ", 16)) {
332 content_length = (size_t) atol(&header[16]);
334 if (!strncasecmp(header,
335 "Content-transfer-encoding: ", 27))
336 strcpy(encoding, &header[27]);
337 if (strlen(boundary) == 0)
338 extract_key(boundary, header, "boundary");
341 if ((strlen(header) + strlen(buf) + 2) < SIZ)
343 } while ((strlen(buf) > 0) && (*ptr != 0));
345 if (strchr(disposition, ';'))
346 *(strchr(disposition, ';')) = '\0';
347 striplt(disposition);
348 if (strchr(content_type, ';'))
349 *(strchr(content_type, ';')) = '\0';
350 striplt(content_type);
352 if (strlen(boundary) > 0) {
358 /* If this is a multipart message, then recursively process it */
362 /* Tell the client about this message's multipartedness */
363 if (PreMultiPartCallBack != NULL) {
364 PreMultiPartCallBack("", "", partnum, "",
365 NULL, content_type, charset,
366 0, encoding, userdata);
369 /* Figure out where the boundaries are */
370 snprintf(startary, SIZ, "--%s", boundary);
371 snprintf(endary, SIZ, "--%s--", boundary);
372 startary_len = strlen(startary);
376 next_boundary = NULL;
377 for (srch=ptr; srch<content_end; ++srch) {
378 if (!memcmp(srch, startary, startary_len)) {
379 next_boundary = srch;
384 if ( (part_start != NULL) && (next_boundary != NULL) ) {
385 part_end = next_boundary;
388 if (strlen(partnum) > 0) {
389 snprintf(nested_partnum,
390 sizeof nested_partnum,
395 snprintf(nested_partnum,
396 sizeof nested_partnum,
399 the_mime_parser(nested_partnum,
400 part_start, part_end,
402 PreMultiPartCallBack,
403 PostMultiPartCallBack,
408 if (next_boundary != NULL) {
409 /* If we pass out of scope, don't attempt to
410 * read past the end boundary. */
411 if (!strcmp(next_boundary, endary)) {
415 /* Set up for the next part. */
416 part_start = strstr(next_boundary, "\n");
422 /* Invalid end of multipart. Bail out! */
425 } while ( (ptr < content_end) && (next_boundary != NULL) );
427 if (PostMultiPartCallBack != NULL) {
428 PostMultiPartCallBack("", "", partnum, "", NULL,
429 content_type, charset, 0, encoding, userdata);
434 /* If it's not a multipart message, then do something with it */
438 while (ptr < content_end) {
442 part_end = content_end;
445 * I thought there was an off-by-one error here, but there isn't.
446 * This probably means that there's an off-by-one error somewhere
447 * else ... or maybe only in certain messages?
452 /* Truncate if the header told us to */
453 if ( (content_length > 0) && (length > content_length) ) {
454 length = content_length;
457 /* Sometimes the "name" field is tacked on to Content-type,
458 * and sometimes it's tacked on to Content-disposition. Use
459 * whichever one we have.
461 if (strlen(content_disposition_name) > strlen(content_type_name)) {
462 name = content_disposition_name;
465 name = content_type_name;
468 /* lprintf(CTDL_DEBUG, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
469 partnum, length, content_type, charset, encoding); */
471 /* Ok, we've got a non-multipart part here, so do something with it.
475 content_type, charset, encoding, disposition,
477 CallBack, NULL, NULL,
478 userdata, dont_decode
482 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
484 if (!strcasecmp(content_type, "message/rfc822")) {
486 if (PreMultiPartCallBack != NULL) {
487 PreMultiPartCallBack("", "", partnum, "",
488 NULL, content_type, charset,
489 0, encoding, userdata);
491 if (CallBack != NULL) {
492 if (strlen(partnum) > 0) {
493 snprintf(nested_partnum,
494 sizeof nested_partnum,
499 snprintf(nested_partnum,
500 sizeof nested_partnum,
503 the_mime_parser(nested_partnum,
504 part_start, part_end,
506 PreMultiPartCallBack,
507 PostMultiPartCallBack,
512 if (PostMultiPartCallBack != NULL) {
513 PostMultiPartCallBack("", "", partnum, "", NULL,
514 content_type, charset, 0, encoding, userdata);
522 end_parser: /* free the buffers! end the oppression!! */
530 free(content_type_name);
531 free(content_disposition_name);
539 * Entry point for the MIME parser.
540 * (This function expects to be fed HEADERS + CONTENT)
541 * Note: NULL can be supplied as content_end; in this case, the message is
542 * considered to have ended when the parser encounters a 0x00 byte.
544 void mime_parser(char *content_start,
559 void (*PreMultiPartCallBack)
571 void (*PostMultiPartCallBack)
588 the_mime_parser("", content_start, content_end,
590 PreMultiPartCallBack,
591 PostMultiPartCallBack,
592 userdata, dont_decode);