From 5ac509220ed1874a7dd2fe244d1b8e6f39febf06 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 20 Aug 2014 14:11:58 +0100 Subject: [PATCH] Add HTTP_CLIENT_SIMPLE option. --- src/libutil/http.c | 36 +++++++++++++++++++++++++++++------- src/libutil/http.h | 7 ++++--- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/libutil/http.c b/src/libutil/http.c index 3cfc433e1..1a36e6142 100644 --- a/src/libutil/http.c +++ b/src/libutil/http.c @@ -28,6 +28,8 @@ #include "printf.h" #include "logger.h" +#include + struct rspamd_http_connection_private { GString *buf; gboolean new_header; @@ -549,6 +551,18 @@ rspamd_http_on_message_complete (http_parser * parser) return ret; } +static void +rspamd_http_simple_client_helper (struct rspamd_http_connection *conn) +{ + struct event_base *base; + + base = event_get_base (&conn->priv->ev); + rspamd_http_connection_reset (conn); + /* Plan read message */ + rspamd_http_connection_read_message (conn, conn->ud, conn->fd, + conn->priv->ptv, base); +} + static void rspamd_http_write_helper (struct rspamd_http_connection *conn) { @@ -562,10 +576,7 @@ rspamd_http_write_helper (struct rspamd_http_connection *conn) priv = conn->priv; if (priv->wr_pos == priv->wr_total) { - rspamd_http_connection_ref (conn); - conn->finish_handler (conn, priv->msg); - rspamd_http_connection_unref (conn); - return; + goto call_finish_handler; } start = &priv->out[0]; @@ -603,14 +614,25 @@ rspamd_http_write_helper (struct rspamd_http_connection *conn) } if (priv->wr_pos >= priv->wr_total) { + goto call_finish_handler; + } + else { + /* Want to write more */ + event_add (&priv->ev, priv->ptv); + } + + return; + +call_finish_handler: + if ((conn->opts & RSPAMD_HTTP_CLIENT_SIMPLE) == 0) { rspamd_http_connection_ref (conn); conn->finish_handler (conn, priv->msg); conn->finished = TRUE; rspamd_http_connection_unref (conn); } else { - /* Want to write more */ - event_add (&priv->ev, priv->ptv); + /* Plan read message */ + rspamd_http_simple_client_helper (conn); } } @@ -685,7 +707,7 @@ struct rspamd_http_connection * rspamd_http_connection_new (rspamd_http_body_handler_t body_handler, rspamd_http_error_handler_t error_handler, rspamd_http_finish_handler_t finish_handler, - enum rspamd_http_options opts, + unsigned opts, enum rspamd_http_connection_type type) { struct rspamd_http_connection *new; diff --git a/src/libutil/http.h b/src/libutil/http.h index c6a142e3b..eefadee18 100644 --- a/src/libutil/http.h +++ b/src/libutil/http.h @@ -67,7 +67,8 @@ struct rspamd_http_message { * Options for HTTP connection */ enum rspamd_http_options { - RSPAMD_HTTP_BODY_PARTIAL = 0x1 //!< RSPAMD_HTTP_BODY_PARTIAL + RSPAMD_HTTP_BODY_PARTIAL = 0x1, /**< Call body handler on all body data portions */ + RSPAMD_HTTP_CLIENT_SIMPLE = 0x2 /**< Read HTTP client reply automatically */ }; struct rspamd_http_connection_private; @@ -104,7 +105,7 @@ struct rspamd_http_connection { rspamd_http_error_handler_t error_handler; rspamd_http_finish_handler_t finish_handler; gpointer ud; - enum rspamd_http_options opts; + unsigned opts; enum rspamd_http_connection_type type; gboolean finished; gint fd; @@ -140,7 +141,7 @@ struct rspamd_http_connection * rspamd_http_connection_new ( rspamd_http_body_handler_t body_handler, rspamd_http_error_handler_t error_handler, rspamd_http_finish_handler_t finish_handler, - enum rspamd_http_options opts, + unsigned opts, enum rspamd_http_connection_type type); /** -- 2.39.5