]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-http: client: Use merged DNS settings from all clients connected to a shared...
authorStephan Bosch <stephan.bosch@dovecot.fi>
Fri, 29 Dec 2017 01:15:06 +0000 (02:15 +0100)
committerVille Savolainen <ville.savolainen@dovecot.fi>
Mon, 12 Mar 2018 07:09:09 +0000 (09:09 +0200)
src/lib-http/http-client-host.c
src/lib-http/http-client-private.h
src/lib-http/http-client.c

index 3ab0a49e192770e406b6f0e141893175f87d082f..9de3437b75f151cf237873d1d5a12f8ad8ae8276 100644 (file)
@@ -90,7 +90,7 @@ static void
 http_client_host_shared_dns_callback(const struct dns_lookup_result *result,
                              struct http_client_host_shared *hshared)
 {
-       const struct http_client_settings *set = &hshared->cctx->set;
+       struct http_client_context *cctx = hshared->cctx;
        struct http_client_host *host;
 
        hshared->dns_lookup = NULL;
@@ -111,7 +111,8 @@ http_client_host_shared_dns_callback(const struct dns_lookup_result *result,
        memcpy(hshared->ips, result->ips, sizeof(*hshared->ips) * hshared->ips_count);
 
        hshared->ips_timeout = ioloop_timeval;
-       timeval_add_msecs(&hshared->ips_timeout, set->dns_ttl_msecs);
+       i_assert(cctx->dns_ttl_msecs > 0);
+       timeval_add_msecs(&hshared->ips_timeout, cctx->dns_ttl_msecs);
 
        /* notify all sessions */
        host = hshared->hosts_list;
@@ -124,7 +125,7 @@ http_client_host_shared_dns_callback(const struct dns_lookup_result *result,
 static void http_client_host_shared_lookup
 (struct http_client_host_shared *hshared)
 {
-       const struct http_client_settings *set = &hshared->cctx->set;
+       struct http_client_context *cctx = hshared->cctx;
        struct dns_lookup_settings dns_set;
        struct ip_addr *ips;
        int ret;
@@ -132,22 +133,16 @@ static void http_client_host_shared_lookup
        i_assert(!hshared->explicit_ip);
        i_assert(hshared->dns_lookup == NULL);
 
-       if (set->dns_client != NULL) {
+       if (cctx->dns_client != NULL) {
                e_debug(hshared->event, "Performing asynchronous DNS lookup");
-               (void)dns_client_lookup(set->dns_client, hshared->name,
+               (void)dns_client_lookup(cctx->dns_client, hshared->name,
                        http_client_host_shared_dns_callback, hshared, &hshared->dns_lookup);
-       } else if (set->dns_client_socket_path != NULL) {
+       } else if (cctx->dns_client_socket_path != NULL) {
+               i_assert(cctx->dns_lookup_timeout_msecs > 0);
                e_debug(hshared->event, "Performing asynchronous DNS lookup");
                i_zero(&dns_set);
-               dns_set.dns_client_socket_path = set->dns_client_socket_path;
-               if (set->connect_timeout_msecs > 0)
-                       dns_set.timeout_msecs = set->connect_timeout_msecs;
-               else if (set->request_timeout_msecs > 0)
-                       dns_set.timeout_msecs = set->request_timeout_msecs;
-               else {
-                       dns_set.timeout_msecs =
-                               HTTP_CLIENT_DEFAULT_DNS_LOOKUP_TIMEOUT_MSECS;
-               }
+               dns_set.dns_client_socket_path = cctx->dns_client_socket_path;
+               dns_set.timeout_msecs = cctx->dns_lookup_timeout_msecs;
                (void)dns_lookup(hshared->name, &dns_set,
                                 http_client_host_shared_dns_callback, hshared, &hshared->dns_lookup);
        } else {
@@ -170,7 +165,8 @@ static void http_client_host_shared_lookup
 
        if (hshared->ips_count > 0) {
                hshared->ips_timeout = ioloop_timeval;
-               timeval_add_msecs(&hshared->ips_timeout, set->dns_ttl_msecs);
+               i_assert(cctx->dns_ttl_msecs > 0);
+               timeval_add_msecs(&hshared->ips_timeout, cctx->dns_ttl_msecs);
        }
 }
 
@@ -304,9 +300,9 @@ http_client_host_shared_request_submitted(
 void http_client_host_shared_switch_ioloop(
        struct http_client_host_shared *hshared)
 {
-       const struct http_client_settings *set = &hshared->cctx->set;
+       struct http_client_context *cctx = hshared->cctx;
 
-       if (hshared->dns_lookup != NULL && set->dns_client == NULL)
+       if (hshared->dns_lookup != NULL && cctx->dns_client == NULL)
                dns_lookup_switch_ioloop(hshared->dns_lookup);
        if (hshared->to_idle != NULL)
                hshared->to_idle = io_loop_move_timeout(&hshared->to_idle);
index 09d99a8b90702a94a625858975e348ede40c20c3..ef40ada011fe293162408242f45cacb1c0c5dbc8 100644 (file)
@@ -390,6 +390,11 @@ struct http_client_context {
 
        struct http_client_settings set;
 
+       struct dns_client *dns_client;
+       const char *dns_client_socket_path;
+       unsigned int dns_ttl_msecs;
+       unsigned int dns_lookup_timeout_msecs;
+
        struct http_client *clients_list;
        struct connection_list *conn_list;
 
index a967126293522ff462b3c11dade18ac7cf61eec5..380f98a9bf3997dc9265027c36ee56945a39403b 100644 (file)
@@ -144,6 +144,11 @@ http_client_init_shared(struct http_client_context *cctx,
        /* merge provided settings with context defaults */
        client->set = cctx->set;
        if (set != NULL) {
+               client->set.dns_client = set->dns_client;
+               client->set.dns_client_socket_path =
+                       p_strdup_empty(pool, set->dns_client_socket_path);
+               client->set.dns_ttl_msecs = set->dns_ttl_msecs;
+
                if (set->user_agent != NULL && *set->user_agent != '\0')
                        client->set.user_agent = p_strdup_empty(pool, set->user_agent);
                if (set->rawlog_dir != NULL && *set->rawlog_dir != '\0')
@@ -537,6 +542,16 @@ void http_client_context_unref(struct http_client_context **_cctx)
        pool_unref(&cctx->pool);
 }
 
+static unsigned int
+http_client_get_dns_lookup_timeout_msecs(const struct http_client_settings *set)
+{
+       if (set->connect_timeout_msecs > 0)
+               return set->connect_timeout_msecs;
+       if (set->request_timeout_msecs > 0)
+               return set->request_timeout_msecs;
+       return HTTP_CLIENT_DEFAULT_DNS_LOOKUP_TIMEOUT_MSECS;
+}
+
 static void
 http_client_context_update_settings(struct http_client_context *cctx)
 {
@@ -544,11 +559,36 @@ http_client_context_update_settings(struct http_client_context *cctx)
        bool debug;
 
        /* revert back to context settings */
+       cctx->dns_client = cctx->set.dns_client;
+       cctx->dns_client_socket_path = cctx->set.dns_client_socket_path;
+       cctx->dns_ttl_msecs = cctx->set.dns_ttl_msecs;
+       cctx->dns_lookup_timeout_msecs =
+               http_client_get_dns_lookup_timeout_msecs(&cctx->set);
        debug = cctx->set.debug;
 
-       /* merge with available client settings */
+       i_assert(cctx->dns_ttl_msecs > 0);
+       i_assert(cctx->dns_lookup_timeout_msecs > 0);
+
+       /* override with available client settings */
        for (client = cctx->clients_list; client != NULL;
             client = client->next) {
+               unsigned dns_lookup_timeout_msecs =
+                       http_client_get_dns_lookup_timeout_msecs(&client->set);
+
+               if (cctx->dns_client == NULL)
+                       cctx->dns_client = client->set.dns_client;
+               if (cctx->dns_client_socket_path == NULL) {
+                       cctx->dns_client_socket_path =
+                               client->set.dns_client_socket_path;
+               }
+               if (client->set.dns_ttl_msecs != 0 &&
+                   cctx->dns_ttl_msecs > client->set.dns_ttl_msecs)
+                       cctx->dns_ttl_msecs = client->set.dns_ttl_msecs;
+               if (dns_lookup_timeout_msecs != 0 &&
+                   cctx->dns_lookup_timeout_msecs > dns_lookup_timeout_msecs) {
+                       cctx->dns_lookup_timeout_msecs =
+                               dns_lookup_timeout_msecs;
+               }
                debug = debug || client->set.debug;
        }