From: Vsevolod Stakhov Date: Wed, 28 Mar 2018 12:25:34 +0000 (+0100) Subject: [Feature] Allow to fold headers on stop characters X-Git-Tag: 1.7.3~96 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a709da98c462abc9f6b08e4288525ad92698685f;p=thirdparty%2Frspamd.git [Feature] Allow to fold headers on stop characters --- diff --git a/src/client/rspamc.c b/src/client/rspamc.c index 2bd1376072..a8139d1cab 100644 --- a/src/client/rspamc.c +++ b/src/client/rspamc.c @@ -1339,9 +1339,9 @@ rspamc_mime_output (FILE *out, ucl_object_t *result, GString *input, folded_symbuf = rspamd_header_value_fold ("X-Spam-Symbols", symbuf->str, - 0, nl_type); + 0, nl_type, ","); rspamd_printf_gstring (added_headers, "X-Spam-Symbols: %v%s", - folded_symbuf, line_end); + folded_symbuf, line_end, ","); g_string_free (folded_symbuf, TRUE); g_string_free (symbuf, TRUE); diff --git a/src/libserver/protocol.c b/src/libserver/protocol.c index 77bbe04bfd..421e36574c 100644 --- a/src/libserver/protocol.c +++ b/src/libserver/protocol.c @@ -1127,11 +1127,11 @@ rspamd_protocol_write_ucl (struct rspamd_task *task, if (task->flags & RSPAMD_TASK_FLAG_MILTER) { folded_header = rspamd_header_value_fold ("DKIM-Signature", - dkim_sig->str, 80, RSPAMD_TASK_NEWLINES_LF); + dkim_sig->str, 80, RSPAMD_TASK_NEWLINES_LF, NULL); } else { folded_header = rspamd_header_value_fold ("DKIM-Signature", - dkim_sig->str, 80, task->nlines_type); + dkim_sig->str, 80, task->nlines_type, NULL); } /* * According to milter docs, we need to be extra careful diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c index 7d346354fc..238f95c87f 100644 --- a/src/libutil/str_util.c +++ b/src/libutil/str_util.c @@ -918,7 +918,8 @@ GString * rspamd_header_value_fold (const gchar *name, const gchar *value, guint fold_max, - enum rspamd_newlines_type how) + enum rspamd_newlines_type how, + const gchar *fold_on_chars) { GString *res; const guint default_fold_max = 76; @@ -1019,6 +1020,13 @@ rspamd_header_value_fold (const gchar *name, cur_len ++; } } + else if (fold_on_chars && strchr (fold_on_chars, *p) != NULL) { + fold_type = fold_after; + state = fold_token; + next_state = read_token; + + p ++; + } else { p ++; cur_len ++; diff --git a/src/libutil/str_util.h b/src/libutil/str_util.h index 68ec5f0bda..5f0695c2a0 100644 --- a/src/libutil/str_util.h +++ b/src/libutil/str_util.h @@ -242,12 +242,16 @@ gint rspamd_strings_levenshtein_distance (const gchar *s1, gsize s1len, * Fold header using rfc822 rules, return new GString from the previous one * @param name name of header (used just for folding) * @param value value of header + * @param fold_max + * @param how + * @param fold_on_chars * @return new GString with the folded value */ GString *rspamd_header_value_fold (const gchar *name, const gchar *value, guint fold_max, - enum rspamd_newlines_type how); + enum rspamd_newlines_type how, + const gchar *fold_on_chars); /** * Search for a substring `srch` in the text `in` using Apostolico-Crochemore algorithm diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index a14de69f3a..46f103f1a8 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -135,11 +135,13 @@ LUA_FUNCTION_DEF (util, levenshtein_distance); LUA_FUNCTION_DEF (util, parse_addr); /*** - * @function util.fold_header(name, value) + * @function util.fold_header(name, value, [how, [stop_chars]]) * Fold rfc822 header according to the folding rules * * @param {string} name name of the header * @param {string} value value of the header + * @param {string} how "cr" for \r, "lf" for \n and "crlf" for \r\n (default) + * @param {string} stop_chars also fold header when the * @return {string} Folded value of the header */ LUA_FUNCTION_DEF (util, fold_header); @@ -1178,7 +1180,7 @@ lua_util_parse_addr (lua_State *L) static gint lua_util_fold_header (lua_State *L) { - const gchar *name, *value, *how; + const gchar *name, *value, *how, *stop_chars = NULL; GString *folded; name = luaL_checkstring (L, 1); @@ -1187,24 +1189,29 @@ lua_util_fold_header (lua_State *L) if (name && value) { if (lua_isstring (L, 3)) { + how = lua_tostring (L, 3); + if (lua_isstring (L, 4)) { + stop_chars = lua_tostring (L, 4); + } + if (strcmp (how, "cr") == 0) { folded = rspamd_header_value_fold (name, value, 0, - RSPAMD_TASK_NEWLINES_CR); + RSPAMD_TASK_NEWLINES_CR, stop_chars); } else if (strcmp (how, "lf") == 0) { folded = rspamd_header_value_fold (name, value, 0, - RSPAMD_TASK_NEWLINES_LF); + RSPAMD_TASK_NEWLINES_LF, stop_chars); } else { folded = rspamd_header_value_fold (name, value, 0, - RSPAMD_TASK_NEWLINES_CRLF); + RSPAMD_TASK_NEWLINES_CRLF, stop_chars); } } else { folded = rspamd_header_value_fold (name, value, 0, - RSPAMD_TASK_NEWLINES_CRLF); + RSPAMD_TASK_NEWLINES_CRLF, stop_chars); } if (folded) {