diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2022-07-09 20:39:33 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2022-07-09 20:39:33 +0100 |
commit | 3bf1e3a890638b4b908da3597bae4f434fc79144 (patch) | |
tree | b067d3a821da6efb0540043431163f72349c0633 | |
parent | 1da2169a07b950ee3e04a597a4ee88080cb0e1a7 (diff) | |
download | rspamd-3bf1e3a890638b4b908da3597bae4f434fc79144.tar.gz rspamd-3bf1e3a890638b4b908da3597bae4f434fc79144.zip |
[Minor] Fix several issues in the parsing of the timeout in Keep-Alive header
-rw-r--r-- | src/libserver/http/http_context.c | 63 |
1 files changed, 38 insertions, 25 deletions
diff --git a/src/libserver/http/http_context.c b/src/libserver/http/http_context.c index 87ee9268e..81e22bb52 100644 --- a/src/libserver/http/http_context.c +++ b/src/libserver/http/http_context.c @@ -572,6 +572,40 @@ rspamd_http_keepalive_handler (gint fd, short what, gpointer ud) g_free (cbdata); } +/* Non-static for unit testing */ +long +rspamd_http_parse_keepalive_timeout (const rspamd_ftok_t *tok) +{ + long timeout = -1; + goffset pos = rspamd_substring_search (tok->begin, + tok->len, "timeout=", sizeof ("timeout=") - 1); + + if (pos != -1) { + pos += sizeof ("timeout=") - 1; + + gchar *end_pos = memchr (tok->begin + pos, ',', tok->len - pos); + glong real_timeout; + + if (end_pos) { + if (rspamd_strtol (tok->begin + pos + 1, + (end_pos - tok->begin) - pos - 1, &real_timeout) && + real_timeout > 0) { + timeout = real_timeout; + msg_debug_http_context ("got timeout attr %.2f", timeout); + } + } + else { + if (rspamd_strtol (tok->begin + pos + 1, + tok->len - pos, &real_timeout) && real_timeout > 0) { + timeout = real_timeout; + msg_debug_http_context ("got timeout attr %.2f", timeout); + } + } + } + + return timeout; +} + void rspamd_http_context_push_keepalive (struct rspamd_http_context *ctx, struct rspamd_http_connection *conn, @@ -609,31 +643,10 @@ rspamd_http_context_push_keepalive (struct rspamd_http_context *ctx, tok = rspamd_http_message_find_header (msg, "Keep-Alive"); if (tok) { - goffset pos = rspamd_substring_search_caseless (tok->begin, - tok->len, "timeout=", sizeof ("timeout=") - 1); - - if (pos != -1) { - pos += sizeof ("timeout="); - - gchar *end_pos = memchr (tok->begin + pos, ',', tok->len - pos); - glong real_timeout; - - if (end_pos) { - if (rspamd_strtol (tok->begin + pos + 1, - (end_pos - tok->begin) - pos - 1, &real_timeout) && - real_timeout > 0) { - timeout = real_timeout; - msg_debug_http_context ("got timeout attr %.2f", timeout); - } - } - else { - if (rspamd_strtol (tok->begin + pos + 1, - tok->len - pos - 1, &real_timeout) && - real_timeout > 0) { - timeout = real_timeout; - msg_debug_http_context ("got timeout attr %.2f", timeout); - } - } + long maybe_timeout = rspamd_http_parse_keepalive_timeout(tok); + + if (maybe_timeout > 0) { + timeout = maybe_timeout; } } } |