]> source.dussan.org Git - rspamd.git/commitdiff
[Rework] Another iteration of HTTP interfaces rework
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 18 Mar 2019 12:00:00 +0000 (12:00 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 18 Mar 2019 12:00:00 +0000 (12:00 +0000)
13 files changed:
src/client/rspamdclient.c
src/fuzzy_storage.c
src/libserver/rspamd_control.c
src/libutil/http_connection.c
src/libutil/http_connection.h
src/libutil/http_router.c
src/libutil/map.c
src/libutil/map_private.h
src/lua/lua_http.c
src/plugins/surbl.c
src/rspamadm/control.c
src/rspamd_proxy.c
src/worker.c

index a2ff85458409ebfbac7089473816ddf9967302a2..5f831ee642f8447ba6eb9f9487d37f35069709ac 100644 (file)
@@ -255,13 +255,12 @@ rspamd_client_init (struct rspamd_http_context *http_ctx,
        conn->ev_base = ev_base;
        conn->fd = fd;
        conn->req_sent = FALSE;
-       conn->http_conn = rspamd_http_connection_new (http_ctx,
-                       fd,
+       conn->http_conn = rspamd_http_connection_new_client_socket (http_ctx,
                        rspamd_client_body_handler,
                        rspamd_client_error_handler,
                        rspamd_client_finish_handler,
                        0,
-                       RSPAMD_HTTP_CLIENT);
+                       fd);
 
        conn->server_name = g_string_new (name);
        if (port != 0) {
index 2054633ade426b3d84152217b14e337e8fd6dbc1..96fb09c2b430078dfe1cbc706bbcd277d4b2d79e 100644 (file)
@@ -618,14 +618,13 @@ rspamd_fuzzy_send_update_mirror (struct rspamd_fuzzy_storage_ctx *ctx,
        msg = rspamd_http_new_message (HTTP_REQUEST);
        rspamd_printf_fstring (&msg->url, "/update_v1/%s", m->name);
 
-       conn->http_conn = rspamd_http_connection_new (
+       conn->http_conn = rspamd_http_connection_new_client_socket (
                        ctx->http_ctx,
-                       conn->sock,
                        NULL,
                        fuzzy_mirror_error_handler,
                        fuzzy_mirror_finish_handler,
                        RSPAMD_HTTP_CLIENT_SIMPLE,
-                       RSPAMD_HTTP_CLIENT);
+                       conn->sock);
 
        rspamd_http_connection_set_key (conn->http_conn,
                        ctx->sync_keypair);
@@ -1992,14 +1991,13 @@ accept_fuzzy_mirror_socket (gint fd, short what, void *arg)
        session->name = rspamd_inet_address_to_string (addr);
        rspamd_random_hex (session->uid, sizeof (session->uid) - 1);
        session->uid[sizeof (session->uid) - 1] = '\0';
-       http_conn = rspamd_http_connection_new (
+       http_conn = rspamd_http_connection_new_server (
                        ctx->http_ctx,
                        nfd,
                        NULL,
                        rspamd_fuzzy_mirror_error_handler,
                        rspamd_fuzzy_mirror_finish_handler,
-                       0,
-                       RSPAMD_HTTP_SERVER);
+                       0);
 
        rspamd_http_connection_set_key (http_conn, ctx->sync_keypair);
        session->ctx = ctx;
index 149ad42459df61992ad2e6c884f98013f5651a91..12d37cdbca8c7deb193154d8af0b9500964c8d9f 100644 (file)
@@ -518,13 +518,12 @@ rspamd_control_process_client_socket (struct rspamd_main *rspamd_main,
        session = g_malloc0 (sizeof (*session));
 
        session->fd = fd;
-       session->conn = rspamd_http_connection_new (rspamd_main->http_ctx,
+       session->conn = rspamd_http_connection_new_server (rspamd_main->http_ctx,
                        fd,
                        NULL,
                        rspamd_control_error_handler,
                        rspamd_control_finish_handler,
-                       0,
-                       RSPAMD_HTTP_SERVER);
+                       0);
        session->rspamd_main = rspamd_main;
        session->addr = addr;
        rspamd_http_connection_read_message (session->conn, session,
index 42b934d2a338a529250d421b04d1e7a4b3872fca..417784789cfc66a0473ddc91ffe7f3f0185da821 100644 (file)
@@ -1077,14 +1077,12 @@ 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,
-               unsigned opts,
-               enum rspamd_http_connection_type type)
+rspamd_http_connection_new_server (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,
+                                                                  unsigned opts)
 {
        struct rspamd_http_connection *conn;
        struct rspamd_http_connection_private *priv;
@@ -1095,7 +1093,7 @@ rspamd_http_connection_new (
 
        conn = g_malloc0 (sizeof (struct rspamd_http_connection));
        conn->opts = opts;
-       conn->type = type;
+       conn->type = RSPAMD_HTTP_SERVER;
        conn->body_handler = body_handler;
        conn->error_handler = error_handler;
        conn->finish_handler = finish_handler;
@@ -1112,14 +1110,49 @@ rspamd_http_connection_new (
        conn->priv = priv;
        priv->ctx = ctx;
 
-       if (conn->type == RSPAMD_HTTP_CLIENT) {
-               priv->cache = ctx->client_kp_cache;
-               if (ctx->client_kp) {
-                       priv->local_key = rspamd_keypair_ref (ctx->client_kp);
-               }
+       priv->cache = ctx->server_kp_cache;
+
+       rspamd_http_parser_reset (conn);
+       priv->parser.data = conn;
+
+       return conn;
+}
+
+struct rspamd_http_connection *
+rspamd_http_connection_new_client_socket (struct rspamd_http_context *ctx,
+                                                                  rspamd_http_body_handler_t body_handler,
+                                                                  rspamd_http_error_handler_t error_handler,
+                                                                  rspamd_http_finish_handler_t finish_handler,
+                                                                  unsigned opts,
+                                                                  gint fd)
+{
+       struct rspamd_http_connection *conn;
+       struct rspamd_http_connection_private *priv;
+
+       g_assert (error_handler != NULL && finish_handler == NULL);
+
+       conn = g_malloc0 (sizeof (struct rspamd_http_connection));
+       conn->opts = opts;
+       conn->type = RSPAMD_HTTP_CLIENT;
+       conn->body_handler = body_handler;
+       conn->error_handler = error_handler;
+       conn->finish_handler = finish_handler;
+       conn->fd = fd;
+       conn->ref = 1;
+       conn->finished = FALSE;
+
+       /* Init priv */
+       if (ctx == NULL) {
+               ctx = rspamd_http_context_default ();
        }
-       else {
-               priv->cache = ctx->server_kp_cache;
+
+       priv = g_malloc0 (sizeof (struct rspamd_http_connection_private));
+       conn->priv = priv;
+       priv->ctx = ctx;
+
+       priv->cache = ctx->client_kp_cache;
+       if (ctx->client_kp) {
+               priv->local_key = rspamd_keypair_ref (ctx->client_kp);
        }
 
        rspamd_http_parser_reset (conn);
@@ -1128,16 +1161,42 @@ rspamd_http_connection_new (
        return conn;
 }
 
+struct rspamd_http_connection *
+rspamd_http_connection_new_client (struct rspamd_http_context *ctx,
+                                                                  rspamd_http_body_handler_t body_handler,
+                                                                  rspamd_http_error_handler_t error_handler,
+                                                                  rspamd_http_finish_handler_t finish_handler,
+                                                                  unsigned opts,
+                                                                  rspamd_inet_addr_t *addr)
+{
+       gint fd;
+
+       if (error_handler == NULL || finish_handler == NULL) {
+               return NULL;
+       }
+
+       fd = rspamd_inet_address_connect (addr, SOCK_STREAM, TRUE);
+
+       if (fd == -1) {
+               msg_info ("cannot connect to %s: %s", rspamd_inet_address_to_string (addr),
+                               strerror (errno));
+               return NULL;
+       }
+
+       return rspamd_http_connection_new_client_socket (ctx,
+                       body_handler, error_handler, finish_handler,
+                       opts | RSPAMD_HTTP_OWN_SOCKET, fd);
+}
+
 struct rspamd_http_connection *
 rspamd_http_connection_new_keepalive (struct rspamd_http_context *ctx,
-               rspamd_http_body_handler_t body_handler,
-               rspamd_http_error_handler_t error_handler,
-               rspamd_http_finish_handler_t finish_handler,
-               rspamd_inet_addr_t *addr,
-               const gchar *host)
+                                                                         rspamd_http_body_handler_t body_handler,
+                                                                         rspamd_http_error_handler_t error_handler,
+                                                                         rspamd_http_finish_handler_t finish_handler,
+                                                                         rspamd_inet_addr_t *addr,
+                                                                         const gchar *host)
 {
        struct rspamd_http_connection *conn;
-       gint fd;
 
        if (error_handler == NULL || finish_handler == NULL) {
                return NULL;
@@ -1153,18 +1212,10 @@ rspamd_http_connection_new_keepalive (struct rspamd_http_context *ctx,
                return conn;
        }
 
-       fd = rspamd_inet_address_connect (addr, SOCK_STREAM, TRUE);
-
-       if (fd == -1) {
-               msg_info ("cannot connect to %s: %s", rspamd_inet_address_to_string (addr),
-                               host);
-               return NULL;
-       }
-
-       conn = rspamd_http_connection_new (ctx, fd, body_handler, error_handler,
-                       finish_handler,
+       conn = rspamd_http_connection_new_client (ctx,
+                       body_handler, error_handler, finish_handler,
                        RSPAMD_HTTP_CLIENT_SIMPLE|RSPAMD_HTTP_CLIENT_KEEP_ALIVE,
-                       RSPAMD_HTTP_CLIENT);
+                       addr);
 
        if (conn) {
                rspamd_http_context_prepare_keepalive (ctx, conn, addr, host);
@@ -1387,7 +1438,7 @@ rspamd_http_connection_free (struct rspamd_http_connection *conn)
                g_free (priv);
        }
 
-       if (conn->opts & RSPAMD_HTTP_CLIENT_KEEP_ALIVE) {
+       if (conn->opts & RSPAMD_HTTP_OWN_SOCKET) {
                /* Fd is owned by a connection */
                close (conn->fd);
        }
index a327eec0d5677b5fa4ff602c9030dd4978693aa0..da354ac41305ccd7579400c3a50d2fadafe16f98 100644 (file)
@@ -85,6 +85,7 @@ enum rspamd_http_options {
        RSPAMD_HTTP_CLIENT_SHARED = 1u << 3, /**< Store reply in shared memory */
        RSPAMD_HTTP_REQUIRE_ENCRYPTION = 1u << 4,
        RSPAMD_HTTP_CLIENT_KEEP_ALIVE = 1u << 5,
+       RSPAMD_HTTP_OWN_SOCKET = 1u << 6,
 };
 
 typedef int (*rspamd_http_body_handler_t) (struct rspamd_http_connection *conn,
@@ -118,20 +119,33 @@ struct rspamd_http_connection {
 };
 
 /**
- * Create new http connection
- * @param handler_t handler_t for body
- * @param opts options
- * @return new connection structure
+ * Creates a new HTTP server connection from an opened FD returned by accept function
+ * @param ctx
+ * @param fd
+ * @param body_handler
+ * @param error_handler
+ * @param finish_handler
+ * @param opts
+ * @return
  */
-struct rspamd_http_connection *rspamd_http_connection_new (
+struct rspamd_http_connection *rspamd_http_connection_new_server (
                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,
-               unsigned opts,
-               enum rspamd_http_connection_type type);
+               unsigned opts);
 
+/**
+ * Creates or reuses a new keepalive client connection identified by hostname and inet_addr
+ * @param ctx
+ * @param body_handler
+ * @param error_handler
+ * @param finish_handler
+ * @param addr
+ * @param host
+ * @return
+ */
 struct rspamd_http_connection *rspamd_http_connection_new_keepalive (
                struct rspamd_http_context *ctx,
                rspamd_http_body_handler_t body_handler,
@@ -140,6 +154,41 @@ struct rspamd_http_connection *rspamd_http_connection_new_keepalive (
                rspamd_inet_addr_t *addr,
                const gchar *host);
 
+/**
+ * Creates an ordinary connection using the address specified (if proxy is not set)
+ * @param ctx
+ * @param body_handler
+ * @param error_handler
+ * @param finish_handler
+ * @param opts
+ * @param addr
+ * @return
+ */
+struct rspamd_http_connection *rspamd_http_connection_new_client (
+               struct rspamd_http_context *ctx,
+               rspamd_http_body_handler_t body_handler,
+               rspamd_http_error_handler_t error_handler,
+               rspamd_http_finish_handler_t finish_handler,
+               unsigned opts,
+               rspamd_inet_addr_t *addr);
+
+/**
+ * Creates an ordinary client connection using ready file descriptor (ignores proxy)
+ * @param ctx
+ * @param body_handler
+ * @param error_handler
+ * @param finish_handler
+ * @param opts
+ * @param addr
+ * @return
+ */
+struct rspamd_http_connection *rspamd_http_connection_new_client_socket (
+               struct rspamd_http_context *ctx,
+               rspamd_http_body_handler_t body_handler,
+               rspamd_http_error_handler_t error_handler,
+               rspamd_http_finish_handler_t finish_handler,
+               unsigned opts,
+               gint fd);
 
 /**
  * Set key pointed by an opaque pointer
index 570d3d5c6735ef526cd930d4d19cf5826db50cbe..ec0eeb7b486a76f5e7a9fd6ec03b4749dce5cca6 100644 (file)
@@ -506,13 +506,12 @@ rspamd_http_router_handle_socket (struct rspamd_http_connection_router *router,
        conn->ud = ud;
        conn->is_reply = FALSE;
 
-       conn->conn = rspamd_http_connection_new (router->ctx,
+       conn->conn = rspamd_http_connection_new_server (router->ctx,
                        fd,
                        NULL,
                        rspamd_http_router_error_handler,
                        rspamd_http_router_finish_handler,
-                       0,
-                       RSPAMD_HTTP_SERVER);
+                       0);
 
        if (router->key) {
                rspamd_http_connection_set_key (conn->conn, router->key);
index 0f7ff4f489cbbadf08e5f73f220fb9dce0d24371..fc414ab00a90a5aefb40e189d545480f9a00d698 100644 (file)
@@ -75,76 +75,62 @@ write_http_request (struct http_callback_data *cbd)
        struct rspamd_map *map;
 
        map = cbd->map;
+       msg = rspamd_http_new_message (HTTP_REQUEST);
 
-       if (cbd->fd != -1) {
-               close (cbd->fd);
+       if (cbd->bk->protocol == MAP_PROTO_HTTPS) {
+               msg->flags |= RSPAMD_HTTP_FLAG_SSL;
        }
 
-       cbd->fd = rspamd_inet_address_connect (cbd->addr, SOCK_STREAM, TRUE);
-
-       if (cbd->fd != -1) {
-               msg = rspamd_http_new_message (HTTP_REQUEST);
-
-               if (cbd->bk->protocol == MAP_PROTO_HTTPS) {
-                       msg->flags |= RSPAMD_HTTP_FLAG_SSL;
-               }
+       if (cbd->check) {
+               msg->method = HTTP_HEAD;
+       }
 
-               if (cbd->check) {
-                       msg->method = HTTP_HEAD;
-               }
+       if (cbd->stage == map_load_file) {
+               msg->url = rspamd_fstring_append (msg->url,
+                               cbd->data->path, strlen (cbd->data->path));
 
-               if (cbd->stage == map_load_file) {
-                       msg->url = rspamd_fstring_append (msg->url,
-                                       cbd->data->path, strlen (cbd->data->path));
-
-                       if (cbd->check && cbd->stage == map_load_file) {
-                               if (cbd->data->last_modified != 0) {
-                                       rspamd_http_date_format (datebuf, sizeof (datebuf),
-                                                       cbd->data->last_modified);
-                                       rspamd_http_message_add_header (msg, "If-Modified-Since",
-                                                       datebuf);
-                               }
-                               if (cbd->data->etag) {
-                                       rspamd_http_message_add_header_len (msg, "If-None-Match",
-                                                       cbd->data->etag->str, cbd->data->etag->len);
-                               }
+               if (cbd->check && cbd->stage == map_load_file) {
+                       if (cbd->data->last_modified != 0) {
+                               rspamd_http_date_format (datebuf, sizeof (datebuf),
+                                               cbd->data->last_modified);
+                               rspamd_http_message_add_header (msg, "If-Modified-Since",
+                                               datebuf);
+                       }
+                       if (cbd->data->etag) {
+                               rspamd_http_message_add_header_len (msg, "If-None-Match",
+                                               cbd->data->etag->str, cbd->data->etag->len);
                        }
                }
-               else if (cbd->stage == map_load_pubkey) {
-                       msg->url = rspamd_fstring_append (msg->url,
-                                       cbd->data->path, strlen (cbd->data->path));
-                       msg->url = rspamd_fstring_append (msg->url, ".pub", 4);
-               }
-               else if (cbd->stage == map_load_signature) {
-                       msg->url = rspamd_fstring_append (msg->url,
-                                       cbd->data->path, strlen (cbd->data->path));
-                       msg->url = rspamd_fstring_append (msg->url, ".sig", 4);
-               }
-               else {
-                       g_assert_not_reached ();
-               }
-
-               msg->url = rspamd_fstring_append (msg->url, cbd->data->rest,
-                                       strlen (cbd->data->rest));
-
-               if (cbd->data->userinfo) {
-                       rspamd_http_message_add_header (msg, "Authorization",
-                                       cbd->data->userinfo);
-               }
-
-               MAP_RETAIN (cbd, "http_callback_data");
-               rspamd_http_connection_write_message (cbd->conn,
-                               msg,
-                               cbd->data->host,
-                               NULL,
-                               cbd,
-                               &cbd->tv);
+       }
+       else if (cbd->stage == map_load_pubkey) {
+               msg->url = rspamd_fstring_append (msg->url,
+                               cbd->data->path, strlen (cbd->data->path));
+               msg->url = rspamd_fstring_append (msg->url, ".pub", 4);
+       }
+       else if (cbd->stage == map_load_signature) {
+               msg->url = rspamd_fstring_append (msg->url,
+                               cbd->data->path, strlen (cbd->data->path));
+               msg->url = rspamd_fstring_append (msg->url, ".sig", 4);
        }
        else {
-               msg_err_map ("cannot connect to %s: %s", cbd->data->host,
-                               strerror (errno));
-               cbd->periodic->errored = TRUE;
+               g_assert_not_reached ();
        }
+
+       msg->url = rspamd_fstring_append (msg->url, cbd->data->rest,
+                       strlen (cbd->data->rest));
+
+       if (cbd->data->userinfo) {
+               rspamd_http_message_add_header (msg, "Authorization",
+                               cbd->data->userinfo);
+       }
+
+       MAP_RETAIN (cbd, "http_callback_data");
+       rspamd_http_connection_write_message (cbd->conn,
+                       msg,
+                       cbd->data->host,
+                       NULL,
+                       cbd,
+                       &cbd->tv);
 }
 
 static gboolean
@@ -282,10 +268,6 @@ free_http_cbdata_common (struct http_callback_data *cbd, gboolean plan_new)
                cbd->conn = NULL;
        }
 
-       if (cbd->fd != -1) {
-               close (cbd->fd);
-       }
-
        if (cbd->addr) {
                rspamd_inet_address_free (cbd->addr);
        }
@@ -517,7 +499,13 @@ http_map_finish (struct rspamd_http_connection *conn,
                                        }
                                }
 
-                               rspamd_http_connection_reset (cbd->conn);
+                               rspamd_http_connection_unref (cbd->conn);
+                               cbd->conn = rspamd_http_connection_new_client (NULL,
+                                               NULL,
+                                               http_map_error,
+                                               http_map_finish,
+                                               RSPAMD_HTTP_CLIENT_SIMPLE|RSPAMD_HTTP_CLIENT_SHARED,
+                                               cbd->addr);
                                write_http_request (cbd);
                                MAP_RELEASE (cbd, "http_callback_data");
 
@@ -563,7 +551,13 @@ http_map_finish (struct rspamd_http_connection *conn,
                        }
 
                        cbd->stage = map_load_signature;
-                       rspamd_http_connection_reset (cbd->conn);
+                       rspamd_http_connection_unref (cbd->conn);
+                       cbd->conn = rspamd_http_connection_new_client (NULL,
+                                       NULL,
+                                       http_map_error,
+                                       http_map_finish,
+                                       RSPAMD_HTTP_CLIENT_SIMPLE|RSPAMD_HTTP_CLIENT_SHARED,
+                                       cbd->addr);
                        write_http_request (cbd);
                        MAP_RELEASE (cbd, "http_callback_data");
 
@@ -1264,20 +1258,15 @@ rspamd_map_dns_callback (struct rdns_reply *reply, void *arg)
 
                        if (cbd->addr != NULL) {
                                rspamd_inet_address_set_port (cbd->addr, cbd->data->port);
-                               /* Try to open a socket */
-                               cbd->fd = rspamd_inet_address_connect (cbd->addr, SOCK_STREAM,
-                                               TRUE);
-
-                               if (cbd->fd != -1) {
+                               cbd->conn = rspamd_http_connection_new_client (NULL,
+                                               NULL,
+                                               http_map_error,
+                                               http_map_finish,
+                                               flags,
+                                               cbd->addr);
+
+                               if (cbd->conn != NULL) {
                                        cbd->stage = map_load_file;
-                                       cbd->conn = rspamd_http_connection_new (NULL,
-                                                       cbd->fd,
-                                                       NULL,
-                                                       http_map_error,
-                                                       http_map_finish,
-                                                       flags,
-                                                       RSPAMD_HTTP_CLIENT);
-
                                        write_http_request (cbd);
                                }
                                else {
@@ -1623,7 +1612,6 @@ check:
        cbd->ev_base = map->ev_base;
        cbd->map = map;
        cbd->data = data;
-       cbd->fd = -1;
        cbd->check = check;
        cbd->periodic = periodic;
        MAP_RETAIN (periodic, "periodic");
@@ -1638,20 +1626,16 @@ check:
        /* Send both A and AAAA requests */
        if (rspamd_parse_inet_address (&cbd->addr, data->host, strlen (data->host))) {
                rspamd_inet_address_set_port (cbd->addr, cbd->data->port);
-               cbd->fd = rspamd_inet_address_connect (cbd->addr, SOCK_STREAM,
-                               TRUE);
+               cbd->conn = rspamd_http_connection_new_client (
+                               NULL,
+                               NULL,
+                               http_map_error,
+                               http_map_finish,
+                               flags,
+                               cbd->addr);
 
-               if (cbd->fd != -1) {
+               if (cbd->conn != NULL) {
                        cbd->stage = map_load_file;
-                       cbd->conn = rspamd_http_connection_new (
-                                       NULL,
-                                       cbd->fd,
-                                       NULL,
-                                       http_map_error,
-                                       http_map_finish,
-                                       flags,
-                                       RSPAMD_HTTP_CLIENT);
-
                        write_http_request (cbd);
                        MAP_RELEASE (cbd, "http_callback_data");
                }
index 68415d0e0c55c4f910dbaf8bef8ccfcf9430005b..b32f0e39014ca35c9f787498b295699ed483d9f4 100644 (file)
@@ -200,7 +200,6 @@ struct http_callback_data {
        gsize pubkey_len;
 
        enum rspamd_map_http_stage stage;
-       gint fd;
        struct timeval tv;
 
        ref_entry_t ref;
index e518e6dab7fc5b4b738982dc1a5f9544be9c779a..19cf8f2c99aff00e6bf61f4e894b9ad37ac46d0b 100644 (file)
@@ -368,8 +368,6 @@ lua_http_resume_handler (struct rspamd_http_connection *conn,
 static gboolean
 lua_http_make_connection (struct lua_http_cbdata *cbd)
 {
-       int fd;
-
        rspamd_inet_address_set_port (cbd->addr, cbd->msg->port);
 
        if (cbd->flags & RSPAMD_LUA_HTTP_FLAG_KEEP_ALIVE) {
@@ -384,22 +382,14 @@ lua_http_make_connection (struct lua_http_cbdata *cbd)
                                cbd->host);
        }
        else {
-               fd = rspamd_inet_address_connect (cbd->addr, SOCK_STREAM, TRUE);
-
-               if (fd == -1) {
-                       msg_info ("cannot connect to %V", cbd->msg->host);
-                       return FALSE;
-               }
-
-               cbd->fd = fd;
-               cbd->conn = rspamd_http_connection_new (
+               cbd->fd = -1;
+               cbd->conn = rspamd_http_connection_new_client (
                                NULL, /* Default context */
-                               fd,
                                NULL,
                                lua_http_error_handler,
                                lua_http_finish_handler,
                                RSPAMD_HTTP_CLIENT_SIMPLE,
-                               RSPAMD_HTTP_CLIENT);
+                               cbd->addr);
        }
 
        if (cbd->conn) {
index d2632cb8129b27dcdb59e12953e928d856f110a5..63a7dd544fb7aa4e0e3b72016cc87b611a6e1c03 100644 (file)
@@ -125,7 +125,6 @@ struct redirector_param {
        GHashTable *tree;
        struct suffix_item *suffix;
        struct rspamd_symcache_item *item;
-       gint sock;
        guint redirector_requests;
 };
 
@@ -1689,7 +1688,6 @@ free_redirector_session (void *ud)
        }
 
        rspamd_http_connection_unref (param->conn);
-       close (param->sock);
 }
 
 static void
@@ -1780,7 +1778,6 @@ static void
 register_redirector_call (struct rspamd_url *url, struct rspamd_task *task,
        const gchar *rule)
 {
-       gint s = -1;
        struct redirector_param *param;
        struct timeval *timeout;
        struct upstream *selected;
@@ -1793,11 +1790,15 @@ register_redirector_call (struct rspamd_url *url, struct rspamd_task *task,
                                RSPAMD_UPSTREAM_ROUND_ROBIN, url->host, url->hostlen);
 
                if (selected) {
-                       s = rspamd_inet_address_connect (rspamd_upstream_addr_next (selected),
-                                       SOCK_STREAM, TRUE);
+                       param->conn = rspamd_http_connection_new_client (NULL,
+                                       NULL,
+                                       surbl_redirector_error,
+                                       surbl_redirector_finish,
+                                       RSPAMD_HTTP_CLIENT_SIMPLE,
+                                       rspamd_upstream_addr_next (selected));
                }
 
-               if (s == -1) {
+               if (param->conn == NULL) {
                        msg_info_surbl ("<%s> cannot create tcp socket failed: %s",
                                        task->message_id,
                                        strerror (errno));
@@ -1810,17 +1811,9 @@ register_redirector_call (struct rspamd_url *url, struct rspamd_task *task,
                                                sizeof (struct redirector_param));
                param->url = url;
                param->task = task;
-               param->conn = rspamd_http_connection_new (NULL,
-                               s,
-                               NULL,
-                               surbl_redirector_error,
-                               surbl_redirector_finish,
-                               RSPAMD_HTTP_CLIENT_SIMPLE,
-                               RSPAMD_HTTP_CLIENT);
                param->ctx = surbl_module_ctx;
                msg = rspamd_http_new_message (HTTP_REQUEST);
                msg->url = rspamd_fstring_assign (msg->url, url->string, url->urllen);
-               param->sock = s;
                param->redirector = selected;
                timeout = rspamd_mempool_alloc (task->task_pool, sizeof (struct timeval));
                double_to_tv (surbl_module_ctx->read_timeout, timeout);
index 0a242e943485521c61d24cf4696d7ffc063d7305..1311622c37a3ff50d1ac70b221bfd92ba6db3f5d 100644 (file)
@@ -175,7 +175,6 @@ rspamadm_control (gint argc, gchar **argv, const struct rspamadm_command *_cmd)
        rspamd_inet_addr_t *addr;
        struct timeval tv;
        static struct rspamadm_control_cbdata cbdata;
-       gint sock;
 
        context = g_option_context_new (
                        "control - manage rspamd main control interface");
@@ -230,22 +229,14 @@ rspamadm_control (gint argc, gchar **argv, const struct rspamadm_command *_cmd)
                exit (1);
        }
 
-       sock = rspamd_inet_address_connect (addr, SOCK_STREAM, TRUE);
 
-       if (sock == -1) {
-               rspamd_fprintf (stderr, "cannot connect to: %s\n", control_path);
-               rspamd_inet_address_free (addr);
-               exit (1);
-       }
-
-       conn = rspamd_http_connection_new (
+       conn = rspamd_http_connection_new_client (
                        rspamd_main->http_ctx, /* Default context */
-                       sock,
                        NULL,
                        rspamd_control_error_handler,
                        rspamd_control_finish_handler,
                        RSPAMD_HTTP_CLIENT_SIMPLE,
-                       RSPAMD_HTTP_CLIENT);
+                       addr);
        msg = rspamd_http_new_message (HTTP_REQUEST);
        msg->url = rspamd_fstring_new_init (path, strlen (path));
        double_to_tv (timeout, &tv);
@@ -261,5 +252,4 @@ rspamadm_control (gint argc, gchar **argv, const struct rspamadm_command *_cmd)
 
        rspamd_http_connection_unref (conn);
        rspamd_inet_address_free (addr);
-       close (sock);
 }
index b6ede29b960b73b628beb74f1da54ac3068b7462..e834266731fbe6a6d365af7f093bbea8cc0711d5 100644 (file)
@@ -1404,14 +1404,13 @@ proxy_open_mirror_connections (struct rspamd_proxy_session *session)
                        rspamd_http_message_add_header (msg, "Settings-ID", m->settings_id);
                }
 
-               bk_conn->backend_conn = rspamd_http_connection_new (
+               bk_conn->backend_conn = rspamd_http_connection_new_client_socket (
                                session->ctx->http_ctx,
-                               bk_conn->backend_sock,
                                NULL,
                                proxy_backend_mirror_error_handler,
                                proxy_backend_mirror_finish_handler,
                                RSPAMD_HTTP_CLIENT_SIMPLE,
-                               RSPAMD_HTTP_CLIENT);
+                               bk_conn->backend_sock);
 
                if (m->key) {
                        msg->peer_key = rspamd_pubkey_ref (m->key);
@@ -1831,14 +1830,13 @@ retry:
                        goto err; /* No fallback here */
                }
 
-               session->master_conn->backend_conn = rspamd_http_connection_new (
+               session->master_conn->backend_conn = rspamd_http_connection_new_client_socket (
                                session->ctx->http_ctx,
-                               session->master_conn->backend_sock,
                                NULL,
                                proxy_backend_master_error_handler,
                                proxy_backend_master_finish_handler,
                                RSPAMD_HTTP_CLIENT_SIMPLE,
-                               RSPAMD_HTTP_CLIENT);
+                               session->master_conn->backend_sock);
                session->master_conn->flags &= ~RSPAMD_BACKEND_CLOSED;
                session->master_conn->parser_from_ref = backend->parser_from_ref;
                session->master_conn->parser_to_ref = backend->parser_to_ref;
@@ -2082,14 +2080,13 @@ proxy_accept_socket (gint fd, short what, void *arg)
        }
 
        if (!ctx->milter) {
-               session->client_conn = rspamd_http_connection_new (
+               session->client_conn = rspamd_http_connection_new_server (
                                ctx->http_ctx,
                                nfd,
                                NULL,
                                proxy_client_error_handler,
                                proxy_client_finish_handler,
-                               0,
-                               RSPAMD_HTTP_SERVER);
+                               0);
 
                if (ctx->key) {
                        rspamd_http_connection_set_key (session->client_conn, ctx->key);
index d81be54a15e6df8db1b27151c013662b0e207310..77614ec1390d3dbef19d343ba2dcd6b477447549 100644 (file)
@@ -412,14 +412,13 @@ accept_socket (gint fd, short what, void *arg)
                http_opts = RSPAMD_HTTP_REQUIRE_ENCRYPTION;
        }
 
-       task->http_conn = rspamd_http_connection_new (
+       task->http_conn = rspamd_http_connection_new_server (
                        ctx->http_ctx,
                        nfd,
                        rspamd_worker_body_handler,
                        rspamd_worker_error_handler,
                        rspamd_worker_finish_handler,
-                       http_opts,
-                       RSPAMD_HTTP_SERVER);
+                       http_opts);
        rspamd_http_connection_set_max_size (task->http_conn, task->cfg->max_message);
        worker->nconns++;
        rspamd_mempool_add_destructor (task->task_pool,