]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-mail: Added message_decoder_current_content_type()
authorTimo Sirainen <tss@iki.fi>
Wed, 14 Jan 2015 23:22:04 +0000 (01:22 +0200)
committerTimo Sirainen <tss@iki.fi>
Wed, 14 Jan 2015 23:22:04 +0000 (01:22 +0200)
src/lib-mail/message-decoder.c
src/lib-mail/test-message-decoder.c

index fdf98a152ad800d58c0204e2c24e9ed4709a4fed..ee1e5157318579e06dc9d320d5dcb4a6b0e99779 100644 (file)
@@ -31,7 +31,7 @@ struct message_decoder_context {
 
        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 @@ void message_decoder_deinit(struct message_decoder_context **_ctx)
        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 @@ parse_content_type(struct message_decoder_context *ctx,
        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 @@ bool message_decoder_decode_next_block(struct message_decoder_context *ctx,
        }
 }
 
+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);
index 9544150738ec0862af544d34c9e5f0314ce42912..148f50ae844ec05a3b380621dcd77817269a01ea 100644 (file)
@@ -83,10 +83,74 @@ static void test_message_decoder(void)
        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 = &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);