From abeb180044e33c529d1aa736958321d2e0ef11ec Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 23 Apr 2014 14:48:34 +0100 Subject: [PATCH] Fix issue with read event in http server. --- src/libutil/http.c | 18 +++++++++++++++++- src/libutil/http.h | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/libutil/http.c b/src/libutil/http.c index b360a5a1d..1b635b44f 100644 --- a/src/libutil/http.c +++ b/src/libutil/http.c @@ -505,6 +505,7 @@ rspamd_http_on_message_complete (http_parser* parser) if (ret == 0) { rspamd_http_connection_ref (conn); ret = conn->finish_handler (conn, priv->msg); + conn->finished = TRUE; rspamd_http_connection_unref (conn); } @@ -565,6 +566,7 @@ rspamd_http_write_helper (struct rspamd_http_connection *conn) if (priv->wr_pos >= priv->wr_total) { rspamd_http_connection_ref (conn); conn->finish_handler (conn, priv->msg); + conn->finished = TRUE; rspamd_http_connection_unref (conn); } else { @@ -593,6 +595,18 @@ rspamd_http_event_handler (int fd, short what, gpointer ud) g_error_free (err); return; } + else if (r == 0) { + if (conn->finished) { + rspamd_http_connection_unref (conn); + return; + } + else { + err = g_error_new (HTTP_ERROR, errno, "IO read error: unexpected EOF"); + conn->error_handler (conn, err); + g_error_free (err); + return; + } + } else { buf->len = r; rspamd_http_connection_ref (conn); @@ -643,6 +657,7 @@ rspamd_http_connection_new (rspamd_http_body_handler_t body_handler, new->finish_handler = finish_handler; new->fd = -1; new->ref = 1; + new->finished = FALSE; /* Init priv */ priv = g_slice_alloc0 (sizeof (struct rspamd_http_connection_private)); @@ -674,7 +689,7 @@ rspamd_http_connection_reset (struct rspamd_http_connection *conn) rspamd_http_message_free (msg); priv->msg = NULL; } - + conn->finished = FALSE; /* Clear priv */ event_del (&priv->ev); if (priv->buf != NULL) { @@ -862,6 +877,7 @@ rspamd_http_connection_write_message (struct rspamd_http_connection *conn, priv->out[i++].iov_len = bodylen; } + event_del (&priv->ev); event_set (&priv->ev, fd, EV_WRITE, rspamd_http_event_handler, conn); event_base_set (base, &priv->ev); event_add (&priv->ev, priv->ptv); diff --git a/src/libutil/http.h b/src/libutil/http.h index 8af4429c6..a76135616 100644 --- a/src/libutil/http.h +++ b/src/libutil/http.h @@ -101,6 +101,7 @@ struct rspamd_http_connection { gpointer ud; enum rspamd_http_options opts; enum rspamd_http_connection_type type; + gboolean finished; gint fd; gint ref; }; -- 2.39.5