Skip to content

Commit

Permalink
lib-mail: header filter should call callback for added EOH
Browse files Browse the repository at this point in the history
If we add a EOH because there wasn't one and
HEADER_FILTER_ADD_MISSING_EOH was specified, we should invoke the
callback for it.  Otherwise, it is unnecessarily difficult for consumers
to add a header since there is no way to know if EOH will be present
ahead of time.
  • Loading branch information
Josef 'Jeff' Sipek authored and sirainen committed Dec 29, 2016
1 parent e41845d commit 34f7cc3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/lib-mail/istream-header-filter.c
Expand Up @@ -306,10 +306,28 @@ static ssize_t read_header(struct header_filter_istream *mstream)
return -1;
}
if (!mstream->seen_eoh && mstream->add_missing_eoh) {
bool matched = FALSE;

mstream->seen_eoh = TRUE;

if (!mstream->last_added_newline)
add_eol(mstream, mstream->last_orig_crlf);
add_eol(mstream, mstream->last_orig_crlf);

if (mstream->callback != NULL) {
struct message_header_line fake_eoh_hdr = {
.eoh = TRUE,
.name = "",
};
mstream->callback(mstream, &fake_eoh_hdr,
&matched, mstream->context);
mstream->callbacks_called = TRUE;
}

if (matched) {
mstream->seen_eoh = FALSE;
} else {
add_eol(mstream, mstream->last_orig_crlf);
}
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/lib-mail/test-istream-header-filter.c
Expand Up @@ -10,6 +10,7 @@
struct run_ctx {
header_filter_callback *callback;
bool null_hdr_seen;
bool eoh_seen;
bool callback_called;
};

Expand All @@ -19,6 +20,8 @@ static void run_callback(struct header_filter_istream *input,
{
if (hdr == NULL)
ctx->null_hdr_seen = TRUE;
if (hdr != NULL && hdr->eoh)
ctx->eoh_seen = TRUE;
if (ctx->callback != NULL)
ctx->callback(input, hdr, matched, NULL);
ctx->callback_called = TRUE;
Expand All @@ -31,13 +34,15 @@ test_istream_run_prep(struct run_ctx *run_ctx,
i_zero(run_ctx);
run_ctx->callback = callback;
run_ctx->null_hdr_seen = FALSE;
run_ctx->eoh_seen = FALSE;
run_ctx->callback_called = FALSE;
}

static void
test_istream_run_check(struct run_ctx *run_ctx,
struct istream *filter,
const char *output,
enum header_filter_flags flags,
bool first,
size_t *size_r)
{
Expand All @@ -49,6 +54,9 @@ test_istream_run_check(struct run_ctx *run_ctx,
else
test_assert(run_ctx->null_hdr_seen == run_ctx->callback_called);

if (first && ((flags & HEADER_FILTER_ADD_MISSING_EOH) != 0))
test_assert(run_ctx->eoh_seen);

data = i_stream_get_data(filter, size_r);
test_assert(*size_r == strlen(output) &&
memcmp(data, output, *size_r) == 0);
Expand Down Expand Up @@ -81,15 +89,15 @@ test_istream_run(struct istream *test_istream,
test_assert(i_stream_read(filter) > 0);
test_assert(i_stream_read(filter) == -1);

test_istream_run_check(&run_ctx, filter, output, TRUE, &size);
test_istream_run_check(&run_ctx, filter, output, flags, TRUE, &size);

/* run again to make sure it's still correct the second time */
test_istream_run_prep(&run_ctx, callback);

i_stream_skip(filter, size);
i_stream_seek(filter, 0);
while (i_stream_read(filter) > 0) ;
test_istream_run_check(&run_ctx, filter, output, FALSE, &size);
test_istream_run_check(&run_ctx, filter, output, flags, FALSE, &size);
i_stream_unref(&filter);
}

Expand Down

0 comments on commit 34f7cc3

Please sign in to comment.