diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-02-25 15:23:22 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-02-25 15:23:22 +0000 |
commit | d8833f06bd6272520a4d6994b54c0df43ca3c6f0 (patch) | |
tree | 03656d17fd1979d4f456717264ac43c04a7cd09b /src/libutil | |
parent | fcf1b617ed850e7cbab47b37b37eda6a7db3b52c (diff) | |
download | rspamd-d8833f06bd6272520a4d6994b54c0df43ca3c6f0.tar.gz rspamd-d8833f06bd6272520a4d6994b54c0df43ca3c6f0.zip |
[Project] HTTP: Rework HTTP connections API to simplify its usage
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/http_connection.c | 75 | ||||
-rw-r--r-- | src/libutil/http_connection.h | 32 | ||||
-rw-r--r-- | src/libutil/http_router.c | 16 | ||||
-rw-r--r-- | src/libutil/map.c | 6 |
4 files changed, 61 insertions, 68 deletions
diff --git a/src/libutil/http_connection.c b/src/libutil/http_connection.c index fd052951b..1c4bec380 100644 --- a/src/libutil/http_connection.c +++ b/src/libutil/http_connection.c @@ -687,13 +687,11 @@ rspamd_http_on_message_complete (http_parser * parser) static void rspamd_http_simple_client_helper (struct rspamd_http_connection *conn) { - struct event_base *base; struct rspamd_http_connection_private *priv; gpointer ssl; gint request_method; priv = conn->priv; - base = conn->priv->ev.ev_base; ssl = priv->ssl; priv->ssl = NULL; request_method = priv->msg->method; @@ -702,12 +700,12 @@ rspamd_http_simple_client_helper (struct rspamd_http_connection *conn) /* Plan read message */ if (conn->opts & RSPAMD_HTTP_CLIENT_SHARED) { - rspamd_http_connection_read_message_shared (conn, conn->ud, conn->fd, - conn->priv->ptv, base); + rspamd_http_connection_read_message_shared (conn, conn->ud, + conn->priv->ptv); } else { - rspamd_http_connection_read_message (conn, conn->ud, conn->fd, - conn->priv->ptv, base); + rspamd_http_connection_read_message (conn, conn->ud, + conn->priv->ptv); } priv->msg->method = request_method; @@ -1036,6 +1034,7 @@ rspamd_http_parser_reset (struct rspamd_http_connection *conn) struct rspamd_http_connection * rspamd_http_connection_new ( struct rspamd_http_context *ctx, + gint fd, rspamd_http_body_handler_t body_handler, rspamd_http_error_handler_t error_handler, rspamd_http_finish_handler_t finish_handler, @@ -1055,7 +1054,7 @@ rspamd_http_connection_new ( conn->body_handler = body_handler; conn->error_handler = error_handler; conn->finish_handler = finish_handler; - conn->fd = -1; + conn->fd = fd; conn->ref = 1; conn->finished = FALSE; @@ -1303,13 +1302,12 @@ rspamd_http_connection_free (struct rspamd_http_connection *conn) static void rspamd_http_connection_read_message_common (struct rspamd_http_connection *conn, - gpointer ud, gint fd, struct timeval *timeout, struct event_base *base, + gpointer ud, struct timeval *timeout, gint flags) { struct rspamd_http_connection_private *priv = conn->priv; struct rspamd_http_message *req; - conn->fd = fd; conn->ud = ud; req = rspamd_http_new_message ( conn->type == RSPAMD_HTTP_SERVER ? HTTP_REQUEST : HTTP_RESPONSE); @@ -1341,13 +1339,12 @@ rspamd_http_connection_read_message_common (struct rspamd_http_connection *conn, priv->flags |= RSPAMD_HTTP_CONN_FLAG_NEW_HEADER; event_set (&priv->ev, - fd, + conn->fd, EV_READ | EV_PERSIST, rspamd_http_event_handler, conn); - if (base != NULL) { - event_base_set (base, &priv->ev); - } + + event_base_set (priv->ctx->ev_base, &priv->ev); priv->flags &= ~RSPAMD_HTTP_CONN_FLAG_RESETED; event_add (&priv->ev, priv->ptv); @@ -1355,16 +1352,16 @@ rspamd_http_connection_read_message_common (struct rspamd_http_connection *conn, void rspamd_http_connection_read_message (struct rspamd_http_connection *conn, - gpointer ud, gint fd, struct timeval *timeout, struct event_base *base) + gpointer ud, struct timeval *timeout) { - rspamd_http_connection_read_message_common (conn, ud, fd, timeout, base, 0); + rspamd_http_connection_read_message_common (conn, ud, timeout, 0); } void rspamd_http_connection_read_message_shared (struct rspamd_http_connection *conn, - gpointer ud, gint fd, struct timeval *timeout, struct event_base *base) + gpointer ud, struct timeval *timeout) { - rspamd_http_connection_read_message_common (conn, ud, fd, timeout, base, + rspamd_http_connection_read_message_common (conn, ud, timeout, RSPAMD_HTTP_FLAG_SHMEM); } @@ -1720,9 +1717,12 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted, static void rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, const gchar *host, const gchar *mime_type, - gpointer ud, gint fd, struct timeval *timeout, struct event_base *base, - gboolean allow_shared) + struct rspamd_http_message *msg, + const gchar *host, + const gchar *mime_type, + gpointer ud, + struct timeval *timeout, + gboolean allow_shared) { struct rspamd_http_connection_private *priv = conn->priv; struct rspamd_http_header *hdr, *htmp, *hcur; @@ -1737,7 +1737,6 @@ rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn enum rspamd_cryptobox_mode mode; GError *err; - conn->fd = fd; conn->ud = ud; priv->msg = msg; @@ -2043,9 +2042,7 @@ rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn gpointer ssl_ctx = (msg->flags & RSPAMD_HTTP_FLAG_SSL_NOVERIFY) ? priv->ctx->ssl_ctx_noverify : priv->ctx->ssl_ctx; - if (base != NULL) { - event_base_set (base, &priv->ev); - } + event_base_set (priv->ctx->ev_base, &priv->ev); if (!ssl_ctx) { err = g_error_new (HTTP_ERROR, errno, "ssl message requested " @@ -2062,11 +2059,11 @@ rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn rspamd_ssl_connection_free (priv->ssl); } - priv->ssl = rspamd_ssl_connection_new (ssl_ctx, base, + priv->ssl = rspamd_ssl_connection_new (ssl_ctx, priv->ctx->ev_base, !(msg->flags & RSPAMD_HTTP_FLAG_SSL_NOVERIFY)); g_assert (priv->ssl != NULL); - if (!rspamd_ssl_connect_fd (priv->ssl, fd, host, &priv->ev, + if (!rspamd_ssl_connect_fd (priv->ssl, conn->fd, host, &priv->ev, priv->ptv, rspamd_http_event_handler, rspamd_http_ssl_err_handler, conn)) { @@ -2083,11 +2080,8 @@ rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn } } else { - event_set (&priv->ev, fd, EV_WRITE, rspamd_http_event_handler, conn); - - if (base != NULL) { - event_base_set (base, &priv->ev); - } + event_set (&priv->ev, conn->fd, EV_WRITE, rspamd_http_event_handler, conn); + event_base_set (priv->ctx->ev_base, &priv->ev); event_add (&priv->ev, priv->ptv); } @@ -2095,20 +2089,26 @@ rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn void rspamd_http_connection_write_message (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, const gchar *host, const gchar *mime_type, - gpointer ud, gint fd, struct timeval *timeout, struct event_base *base) + struct rspamd_http_message *msg, + const gchar *host, + const gchar *mime_type, + gpointer ud, + struct timeval *timeout) { rspamd_http_connection_write_message_common (conn, msg, host, mime_type, - ud, fd, timeout, base, FALSE); + ud, timeout, FALSE); } void rspamd_http_connection_write_message_shared (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, const gchar *host, const gchar *mime_type, - gpointer ud, gint fd, struct timeval *timeout, struct event_base *base) + struct rspamd_http_message *msg, + const gchar *host, + const gchar *mime_type, + gpointer ud, + struct timeval *timeout) { rspamd_http_connection_write_message_common (conn, msg, host, mime_type, - ud, fd, timeout, base, TRUE); + ud, timeout, TRUE); } @@ -2134,7 +2134,6 @@ rspamd_http_message_free (struct rspamd_http_message *msg) } } - rspamd_http_message_storage_cleanup (msg); if (msg->url != NULL) { diff --git a/src/libutil/http_connection.h b/src/libutil/http_connection.h index 4e9f9f800..5fadf4509 100644 --- a/src/libutil/http_connection.h +++ b/src/libutil/http_connection.h @@ -74,14 +74,19 @@ struct rspamd_storage_shmem { */ #define RSPAMD_HTTP_FLAG_SSL_NOVERIFY (1 << 6) /** + * Do not verify server's certificate + */ +#define RSPAMD_HTTP_FLAG_KEEPALIVE (1 << 7) +/** * Options for HTTP connection */ enum rspamd_http_options { - RSPAMD_HTTP_BODY_PARTIAL = 0x1, /**< Call body handler on all body data portions *///!< RSPAMD_HTTP_BODY_PARTIAL - RSPAMD_HTTP_CLIENT_SIMPLE = 0x1u << 1, /**< Read HTTP client reply automatically */ //!< RSPAMD_HTTP_CLIENT_SIMPLE - RSPAMD_HTTP_CLIENT_ENCRYPTED = 0x1u << 2, /**< Encrypt data for client */ //!< RSPAMD_HTTP_CLIENT_ENCRYPTED - RSPAMD_HTTP_CLIENT_SHARED = 0x1u << 3, /**< Store reply in shared memory */ //!< RSPAMD_HTTP_CLIENT_SHARED - RSPAMD_HTTP_REQUIRE_ENCRYPTION = 0x1u << 4 + RSPAMD_HTTP_BODY_PARTIAL = 1, /**< Call body handler on all body data portions */ + RSPAMD_HTTP_CLIENT_SIMPLE = 1u << 1, /**< Read HTTP client reply automatically */ + RSPAMD_HTTP_CLIENT_ENCRYPTED = 1u << 2, /**< Encrypt data for client */ + RSPAMD_HTTP_CLIENT_SHARED = 1u << 3, /**< Store reply in shared memory */ + RSPAMD_HTTP_REQUIRE_ENCRYPTION = 1u << 4, + RSPAMD_HTTP_CLIENT_KEEP_ALIVE = 1u << 5, }; typedef int (*rspamd_http_body_handler_t) (struct rspamd_http_connection *conn, @@ -120,6 +125,7 @@ struct rspamd_http_connection { */ struct rspamd_http_connection *rspamd_http_connection_new ( struct rspamd_http_context *ctx, + gint fd, rspamd_http_body_handler_t body_handler, rspamd_http_error_handler_t error_handler, rspamd_http_finish_handler_t finish_handler, @@ -159,16 +165,12 @@ gboolean rspamd_http_connection_is_encrypted (struct rspamd_http_connection *con void rspamd_http_connection_read_message ( struct rspamd_http_connection *conn, gpointer ud, - gint fd, - struct timeval *timeout, - struct event_base *base); + struct timeval *timeout); void rspamd_http_connection_read_message_shared ( struct rspamd_http_connection *conn, gpointer ud, - gint fd, - struct timeval *timeout, - struct event_base *base); + struct timeval *timeout); /** * Send reply using initialised connection @@ -183,9 +185,7 @@ void rspamd_http_connection_write_message ( const gchar *host, const gchar *mime_type, gpointer ud, - gint fd, - struct timeval *timeout, - struct event_base *base); + struct timeval *timeout); void rspamd_http_connection_write_message_shared ( struct rspamd_http_connection *conn, @@ -193,9 +193,7 @@ void rspamd_http_connection_write_message_shared ( const gchar *host, const gchar *mime_type, gpointer ud, - gint fd, - struct timeval *timeout, - struct event_base *base); + struct timeval *timeout); /** * Free connection structure diff --git a/src/libutil/http_router.c b/src/libutil/http_router.c index e9dc48962..570d3d5c6 100644 --- a/src/libutil/http_router.c +++ b/src/libutil/http_router.c @@ -92,9 +92,7 @@ rspamd_http_router_error_handler (struct rspamd_http_connection *conn, NULL, "text/plain", entry, - entry->conn->fd, - entry->rt->ptv, - entry->rt->ev_base); + entry->rt->ptv); entry->is_reply = TRUE; } } @@ -211,8 +209,8 @@ rspamd_http_router_try_file (struct rspamd_http_connection_entry *entry, msg_debug ("requested file %s", realbuf); rspamd_http_connection_write_message (entry->conn, reply_msg, NULL, - rspamd_http_router_detect_ct (realbuf), entry, entry->conn->fd, - entry->rt->ptv, entry->rt->ev_base); + rspamd_http_router_detect_ct (realbuf), entry, + entry->rt->ptv); return TRUE; } @@ -237,9 +235,7 @@ rspamd_http_router_send_error (GError *err, NULL, "text/plain", entry, - entry->conn->fd, - entry->rt->ptv, - entry->rt->ev_base); + entry->rt->ptv); } @@ -511,6 +507,7 @@ rspamd_http_router_handle_socket (struct rspamd_http_connection_router *router, conn->is_reply = FALSE; conn->conn = rspamd_http_connection_new (router->ctx, + fd, NULL, rspamd_http_router_error_handler, rspamd_http_router_finish_handler, @@ -521,8 +518,7 @@ rspamd_http_router_handle_socket (struct rspamd_http_connection_router *router, rspamd_http_connection_set_key (conn->conn, router->key); } - rspamd_http_connection_read_message (conn->conn, conn, fd, router->ptv, - router->ev_base); + rspamd_http_connection_read_message (conn->conn, conn, router->ptv); DL_PREPEND (router->conns, conn); } diff --git a/src/libutil/map.c b/src/libutil/map.c index 1413b2953..eca45599e 100644 --- a/src/libutil/map.c +++ b/src/libutil/map.c @@ -138,9 +138,7 @@ write_http_request (struct http_callback_data *cbd) cbd->data->host, NULL, cbd, - cbd->fd, - &cbd->tv, - cbd->ev_base); + &cbd->tv); } else { msg_err_map ("cannot connect to %s: %s", cbd->data->host, @@ -1277,6 +1275,7 @@ rspamd_map_dns_callback (struct rdns_reply *reply, void *arg) if (cbd->fd != -1) { cbd->stage = map_load_file; cbd->conn = rspamd_http_connection_new (NULL, + cbd->fd, NULL, http_map_error, http_map_finish, @@ -1651,6 +1650,7 @@ check: cbd->conn = rspamd_http_connection_new ( NULL, NULL, + cbd->fd, http_map_error, http_map_finish, flags, |