diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2022-01-05 21:36:58 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2022-01-05 21:36:58 +0000 |
commit | 7fd6b2b76699f3456f6b06ab1ee1d0bb6d8d9069 (patch) | |
tree | d569a2ebc2ebc0f03f9893dc1287cf3153fed09a /src/libserver/http | |
parent | d90f7a622feb794283bd300c937f302d0f7c00b2 (diff) | |
download | rspamd-7fd6b2b76699f3456f6b06ab1ee1d0bb6d8d9069.tar.gz rspamd-7fd6b2b76699f3456f6b06ab1ee1d0bb6d8d9069.zip |
[Fix] Fix HTTP(s) client timeout
Diffstat (limited to 'src/libserver/http')
-rw-r--r-- | src/libserver/http/http_connection.c | 88 |
1 files changed, 47 insertions, 41 deletions
diff --git a/src/libserver/http/http_connection.c b/src/libserver/http/http_connection.c index afd685ae0..d24732819 100644 --- a/src/libserver/http/http_connection.c +++ b/src/libserver/http/http_connection.c @@ -91,6 +91,8 @@ static const rspamd_ftok_t last_modified_header = { .len = 13 }; +static void rspamd_http_event_handler (int fd, short what, gpointer ud); +static void rspamd_http_ssl_err_handler (gpointer ud, GError *err); #define HTTP_ERROR http_error_quark () @@ -1030,54 +1032,48 @@ rspamd_http_event_handler (int fd, short what, gpointer ud) } } else if (what == EV_TIMEOUT) { - /* Let's try to read from the socket first */ - r = rspamd_http_try_read (fd, conn, priv, pbuf, &d); + if (!priv->ssl) { + /* Let's try to read from the socket first */ + r = rspamd_http_try_read(fd, conn, priv, pbuf, &d); + + if (r > 0) { + if (http_parser_execute(&priv->parser, &priv->parser_cb, + d, r) != (size_t) r || priv->parser.http_errno != 0) { + err = g_error_new(HTTP_ERROR, 400, + "HTTP parser error: %s", + http_errno_description(priv->parser.http_errno)); + + if (!conn->finished) { + conn->error_handler(conn, err); + } + else { + msg_err ("got error after HTTP request is finished: %e", err); + } - if (r > 0) { - if (http_parser_execute (&priv->parser, &priv->parser_cb, - d, r) != (size_t)r || priv->parser.http_errno != 0) { - err = g_error_new (HTTP_ERROR, 400, - "HTTP parser error: %s", - http_errno_description (priv->parser.http_errno)); + g_error_free(err); - if (!conn->finished) { - conn->error_handler (conn, err); - } - else { - msg_err ("got error after HTTP request is finished: %e", err); - } + REF_RELEASE (pbuf); + rspamd_http_connection_unref(conn); - g_error_free (err); + return; + } + } + else { + err = g_error_new(HTTP_ERROR, 408, + "IO timeout"); + conn->error_handler(conn, err); + g_error_free(err); REF_RELEASE (pbuf); - rspamd_http_connection_unref (conn); + rspamd_http_connection_unref(conn); return; } } - else if (r == 0) { - if (!conn->finished && !priv->ssl) { - err = g_error_new (HTTP_ERROR, 408, - "IO timeout"); - conn->error_handler (conn, err); - g_error_free (err); - - } - REF_RELEASE (pbuf); - rspamd_http_connection_unref (conn); - - return; - } else { - if (!priv->ssl) { - err = g_error_new(HTTP_ERROR, 408, - "IO timeout"); - conn->error_handler(conn, err); - g_error_free(err); - } - + /* In case of SSL we disable this logic as we already came from SSL handler */ REF_RELEASE (pbuf); - rspamd_http_connection_unref (conn); + rspamd_http_connection_unref(conn); return; } @@ -1536,9 +1532,18 @@ rspamd_http_connection_read_message_common (struct rspamd_http_connection *conn, priv->buf->data = rspamd_fstring_sized_new (8192); priv->flags |= RSPAMD_HTTP_CONN_FLAG_NEW_HEADER; - rspamd_ev_watcher_init (&priv->ev, conn->fd, EV_READ, - rspamd_http_event_handler, conn); - rspamd_ev_watcher_start (priv->ctx->event_loop, &priv->ev, priv->timeout); + if (!priv->ssl) { + rspamd_ev_watcher_init(&priv->ev, conn->fd, EV_READ, + rspamd_http_event_handler, conn); + rspamd_ev_watcher_start(priv->ctx->event_loop, &priv->ev, priv->timeout); + } + else { + rspamd_ssl_connection_restore_handlers (priv->ssl, + rspamd_http_event_handler, + rspamd_http_ssl_err_handler, + conn, + EV_READ); + } priv->flags &= ~RSPAMD_HTTP_CONN_FLAG_RESETED; } @@ -2351,7 +2356,8 @@ rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn rspamd_ssl_connection_restore_handlers (priv->ssl, rspamd_http_event_handler, rspamd_http_ssl_err_handler, - conn); + conn, + EV_WRITE); } } } |