dovecot-2.2: lib-mail: Added message_decoder_current_content_type()

dovecot at dovecot.org dovecot at dovecot.org
Wed Jan 14 23:23:41 UTC 2015


details:   http://hg.dovecot.org/dovecot-2.2/rev/55555824f636
changeset: 18154:55555824f636
user:      Timo Sirainen <tss at iki.fi>
date:      Thu Jan 15 01:22:04 2015 +0200
description:
lib-mail: Added message_decoder_current_content_type()

diffstat:

 src/lib-mail/message-decoder.c      |  15 ++++++-
 src/lib-mail/test-message-decoder.c |  64 +++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+), 3 deletions(-)

diffs (132 lines):

diff -r 7250ccf9ab4c -r 55555824f636 src/lib-mail/message-decoder.c
--- a/src/lib-mail/message-decoder.c	Thu Jan 15 01:11:34 2015 +0200
+++ b/src/lib-mail/message-decoder.c	Thu Jan 15 01:22:04 2015 +0200
@@ -31,7 +31,7 @@
 
 	buffer_t *encoding_buf;
 
-	char *content_charset;
+	char *content_type, *content_charset;
 	enum message_cte message_cte;
 
 	unsigned int binary_input:1;
@@ -69,6 +69,7 @@
 	buffer_free(&ctx->buf);
 	buffer_free(&ctx->buf2);
 	i_free(ctx->charset_trans_charset);
+	i_free(ctx->content_type);
 	i_free(ctx->content_charset);
 	i_free(ctx);
 }
@@ -124,14 +125,15 @@
 	const char *const *results;
 	string_t *str;
 
-	if (ctx->content_charset != NULL)
+	if (ctx->content_type != NULL)
 		return;
 
 	rfc822_parser_init(&parser, hdr->full_value, hdr->full_value_len, NULL);
 	rfc822_skip_lwsp(&parser);
 	str = t_str_new(64);
-	if (rfc822_parse_content_type(&parser, str) <= 0)
+	if (rfc822_parse_content_type(&parser, str) < 0)
 		return;
+	ctx->content_type = i_strdup(str_c(str));
 
 	rfc2231_parse(&parser, &results);
 	for (; *results != NULL; results += 2) {
@@ -376,8 +378,15 @@
 	}
 }
 
+const char *
+message_decoder_current_content_type(struct message_decoder_context *ctx)
+{
+	return ctx->content_type;
+}
+
 void message_decoder_decode_reset(struct message_decoder_context *ctx)
 {
+	i_free_and_null(ctx->content_type);
 	i_free_and_null(ctx->content_charset);
 	ctx->message_cte = MESSAGE_CTE_78BIT;
 	buffer_set_used_size(ctx->encoding_buf, 0);
diff -r 7250ccf9ab4c -r 55555824f636 src/lib-mail/test-message-decoder.c
--- a/src/lib-mail/test-message-decoder.c	Thu Jan 15 01:11:34 2015 +0200
+++ b/src/lib-mail/test-message-decoder.c	Thu Jan 15 01:22:04 2015 +0200
@@ -83,10 +83,74 @@
 	test_end();
 }
 
+static void test_message_decoder_current_content_type(void)
+{
+	struct message_decoder_context *ctx;
+	struct message_part part, part2, part3;
+	struct message_header_line hdr;
+	struct message_block input, output;
+
+	test_begin("message_decoder_current_content_type()");
+
+	memset(&part, 0, sizeof(part));
+	part2 = part3 = part;
+
+	memset(&input, 0, sizeof(input));
+	memset(&output, 0xff, sizeof(output));
+	input.part = ∂
+
+	ctx = message_decoder_init(NULL, 0);
+	test_assert(message_decoder_current_content_type(ctx) == NULL);
+
+	/* multipart/mixed */
+	memset(&hdr, 0, sizeof(hdr));
+	hdr.name = "Content-Type";
+	hdr.name_len = strlen(hdr.name);
+	hdr.full_value = (const void *)"multipart/mixed; boundary=x";
+	hdr.full_value_len = strlen((const char *)hdr.full_value);
+	input.hdr = &hdr;
+	test_assert(message_decoder_decode_next_block(ctx, &input, &output));
+
+	input.hdr = NULL;
+	test_assert(message_decoder_decode_next_block(ctx, &input, &output));
+	test_assert(strcmp(message_decoder_current_content_type(ctx), "multipart/mixed") == 0);
+
+	/* child 1 */
+	input.part = &part2;
+	hdr.full_value = (const void *)"text/plain";
+	hdr.full_value_len = strlen((const char *)hdr.full_value);
+	input.hdr = &hdr;
+	test_assert(message_decoder_decode_next_block(ctx, &input, &output));
+
+	input.hdr = NULL;
+	test_assert(message_decoder_decode_next_block(ctx, &input, &output));
+	test_assert(strcmp(message_decoder_current_content_type(ctx), "text/plain") == 0);
+
+	/* child 2 */
+	input.part = &part3;
+	hdr.full_value = (const void *)"application/pdf";
+	hdr.full_value_len = strlen((const char *)hdr.full_value);
+	input.hdr = &hdr;
+	test_assert(message_decoder_decode_next_block(ctx, &input, &output));
+
+	input.hdr = NULL;
+	test_assert(message_decoder_decode_next_block(ctx, &input, &output));
+	test_assert(strcmp(message_decoder_current_content_type(ctx), "application/pdf") == 0);
+
+	/* reset */
+	message_decoder_decode_reset(ctx);
+	test_assert(message_decoder_current_content_type(ctx) == NULL);
+
+	message_decoder_deinit(&ctx);
+
+	test_end();
+}
+
 int main(void)
 {
 	static void (*test_functions[])(void) = {
 		test_message_decoder,
+		test_message_decoder_current_content_type,
 		NULL
 	};
 	return test_run(test_functions);


More information about the dovecot-cvs mailing list