changeset 21361:8902229240d5

lib-mail: header filter should call callback for added EOH 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.
author Josef 'Jeff' Sipek <jeff.sipek@dovecot.fi>
date Tue, 27 Dec 2016 15:03:51 -0500
parents 063e6955617b
children ef9c844a24bd
files src/lib-mail/istream-header-filter.c src/lib-mail/test-istream-header-filter.c
diffstat 2 files changed, 29 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-mail/istream-header-filter.c	Wed Dec 28 10:49:49 2016 -0500
+++ b/src/lib-mail/istream-header-filter.c	Tue Dec 27 15:03:51 2016 -0500
@@ -306,10 +306,28 @@
 			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);
+			}
 		}
 	}
 
--- a/src/lib-mail/test-istream-header-filter.c	Wed Dec 28 10:49:49 2016 -0500
+++ b/src/lib-mail/test-istream-header-filter.c	Tue Dec 27 15:03:51 2016 -0500
@@ -10,6 +10,7 @@
 struct run_ctx {
 	header_filter_callback *callback;
 	bool null_hdr_seen;
+	bool eoh_seen;
 	bool callback_called;
 };
 
@@ -19,6 +20,8 @@
 {
 	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;
@@ -31,6 +34,7 @@
 	i_zero(run_ctx);
 	run_ctx->callback = callback;
 	run_ctx->null_hdr_seen = FALSE;
+	run_ctx->eoh_seen = FALSE;
 	run_ctx->callback_called = FALSE;
 }
 
@@ -38,6 +42,7 @@
 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)
 {
@@ -49,6 +54,9 @@
 	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);
@@ -81,7 +89,7 @@
 	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);
@@ -89,7 +97,7 @@
 	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);
 }