summaryrefslogtreecommitdiffstats
path: root/src/lib-http/http-client-host.c
blob: 647ab663a1365f0077d80f0f46654115c9f476e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "net.h"
#include "str.h"
#include "hash.h"
#include "array.h"
#include "llist.h"
#include "ioloop.h"
#include "istream.h"
#include "ostream.h"
#include "time-util.h"
#include "dns-lookup.h"
#include "http-response-parser.h"

#include "http-client-private.h"

#define HTTP_CLIENT_HOST_MINIMUM_IDLE_TIMEOUT_MSECS 100

static void http_client_host_lookup_done(struct http_client_host *host);
static void
http_client_host_lookup_failure(struct http_client_host *host,
				const char *error);
static bool http_client_host_is_idle(struct http_client_host *host);
static void http_client_host_free_shared(struct http_client_host **_host);

/*
 * Host (shared)
 */

static void
http_client_host_shared_idle_timeout(struct http_client_host_shared *hshared)
{
	e_debug(hshared->event, "Idle host timed out");
	http_client_host_shared_free(&hshared);
}

static void
http_client_host_shared_check_idle(struct http_client_host_shared *hshared)
{
	struct http_client_host *host;
	int timeout = 0;

	if (hshared->destroyed)
		return;
	if (hshared->to_idle != NULL)
		return;

	host = hshared->hosts_list;
	while (host != NULL) {
		if (!http_client_host_is_idle(host))
			return;
		host = host->shared_next;
	}

	if (!hshared->unix_local && !hshared->explicit_ip &&
	    hshared->ips_timeout.tv_sec > 0) {
		timeout = timeval_diff_msecs(&hshared->ips_timeout,
					     &ioloop_timeval);
	}

	if (timeout <= HTTP_CLIENT_HOST_MINIMUM_IDLE_TIMEOUT_MSECS)
		timeout = HTTP_CLIENT_HOST_MINIMUM_IDLE_TIMEOUT_MSECS;

	hshared->to_idle = timeout_add_to(hshared->cctx->ioloop, timeout,
					  http_client_host_shared_idle_timeout,
					  hshared);

	e_debug(hshared->event, "Host is idle (timeout = %u msecs)", timeout);
}

static void
http_client_host_shared_lookup_failure(struct http_client_host_shared *hshared,
				       const char *error)
{
	struct http_client_host *host;

	e_debug(hshared->event, "DNS lookup failed: %s", error);

	error = t_strdup_printf("Failed to lookup host %s: %s",
				hshared->name, error);

	host = hshared->hosts_list;
	while (host != NULL) {
		http_client_host_lookup_failure(host, error);
		host = host->shared_next;
	}

	http_client_host_shared_check_idle(hshared);
}

static void
http_client_host_shared_lookup_success(struct http_client_host_shared *hshared,
				       const struct ip_addr *ips,
				       unsigned int ips_count)
{
	struct http_client_context *cctx = hshared->cctx;

	i_assert(ips_count > 0);

	e_debug(hshared->event,
		"DNS lookup successful; got %d IPs", ips_count);

	hshared->ips = i_realloc_type(hshared->ips, struct ip_addr,
				      hshared->ips_count, ips_count);
	hshared->ips_count = ips_count;
	memcpy(hshared->ips, ips, sizeof(struct ip_addr) * ips_count);

	hshared->ips_timeout = ioloop_timeval;
	i_assert(cctx->dns_ttl_msecs > 0);
	timeval_add_msecs(&hshared->ips_timeout, cctx->dns_ttl_msecs);
}

static void
http_client_host_shared_dns_callback(const struct dns_lookup_result *result,
				     struct http_client_host_shared *hshared)
{
	struct http_client_host *host;

	hshared->dns_lookup = NULL;

	if (result->ret != 0) {
		/* Lookup failed */
		http_client_host_shared_lookup_failure(hshared, result->error);
		return;
	}

	http_client_host_shared_lookup_success(hshared, result->ips,
					       result->ips_count);

	/* Notify all sessions */
	host = hshared->hosts_list;
	while (host != NULL) {
		http_client_host_lookup_done(host);
		host = host->shared_next;
	}
}

static void
http_client_host_shared_lookup(struct http_client_host_shared *hshared)
{
	struct http_client_context *cctx = hshared->cctx;
	struct dns_lookup_settings dns_set;
	int ret;

	i_assert(!hshared->explicit_ip);
	i_assert(hshared->dns_lookup == NULL);

	if (cctx->dns_client != NULL) {
		e_debug(hshared->event, "Performing asynchronous DNS lookup");
		(void)dns_client_lookup(cctx->dns_client, hshared->name,
					http_client_host_shared_dns_callback,
					hshared, &hshared->dns_lookup);
	} 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 = cctx->dns_client_socket_path;
		dns_set.timeout_msecs = cctx->dns_lookup_timeout_msecs;
		dns_set.ioloop = cctx->ioloop;
		dns_set.event_parent = hshared->event;
		(void)dns_lookup(hshared->name, &dns_set,
				 http_client_host_shared_dns_callback,
				 hshared, &hshared->dns_lookup);
	} else {
		struct ip_addr *ips;
		unsigned int ips_count;

		ret = net_gethostbyname(hshared->name, &ips, &ips_count);
		if (ret != 0) {
			http_client_host_shared_lookup_failure(
				hshared, net_gethosterror(ret));
			return;
		}

		http_client_host_shared_lookup_success(hshared, ips, ips_count);
	}
}

static int
http_client_host_shared_refresh(struct http_client_host_shared *hshared)
{
	if (hshared->unix_local)
		return 0;
	if (hshared->explicit_ip)
		return 0;

	if (hshared->dns_lookup != NULL)
		return -1;

	if (hshared->ips_count == 0) {
		e_debug(hshared->event, "Need to perform DNS lookup");
	} else {
		if (timeval_cmp(&hshared->ips_timeout, &ioloop_timeval) > 0)
			return 0;

		e_debug(hshared->event, "IPs have expired; "
			"need to refresh DNS lookup");
	}

	http_client_host_shared_lookup(hshared);
	if (hshared->dns_lookup != NULL)
		return -1;
	return (hshared->ips_count > 0 ? 1 : -1);
}

static struct http_client_host_shared *
http_client_host_shared_create(struct http_client_context *cctx,
			       const char *name)
{
	struct http_client_host_shared *hshared;

	// FIXME: limit the maximum number of inactive cached hosts
	hshared = i_new(struct http_client_host_shared, 1);
	hshared->cctx = cctx;
	hshared->name = i_strdup(name);
	hshared->event = event_create(cctx->event);
	event_set_append_log_prefix(hshared->event,
				    t_strdup_printf("host %s: ", name));
	DLLIST_PREPEND(&cctx->hosts_list, hshared);

	return hshared;
}

static struct http_client_host_shared *
http_client_host_shared_get(struct http_client_context *cctx,
			    const struct http_url *host_url)
{
	struct http_client_host_shared *hshared;

	if (host_url == NULL) {
		hshared = cctx->unix_host;
		if (hshared == NULL) {
			hshared = http_client_host_shared_create(
				cctx, "[unix]");
			hshared->name = i_strdup("[unix]");
			hshared->unix_local = TRUE;

			cctx->unix_host = hshared;

			e_debug(hshared->event, "Unix host created");
		}

	} else {
		const char *hostname = host_url->host.name;
		struct ip_addr ip = host_url->host.ip;

		hshared = hash_table_lookup(cctx->hosts, hostname);
		if (hshared == NULL) {
			hshared = http_client_host_shared_create(
				cctx, hostname);
			hostname = hshared->name;
			hash_table_insert(cctx->hosts, hostname, hshared);

			if (ip.family != 0 ||
			    net_addr2ip(hshared->name, &ip) == 0) {
				hshared->ips_count = 1;
				hshared->ips = i_new(struct ip_addr,
						     hshared->ips_count);
				hshared->ips[0] = ip;
				hshared->explicit_ip = TRUE;
			}

			e_debug(hshared->event, "Host created");
		}
	}
	return hshared;
}

void http_client_host_shared_free(struct http_client_host_shared **_hshared)
{
	struct http_client_host_shared *hshared = *_hshared;
	struct http_client_context *cctx = hshared->cctx;
	struct http_client_host *host;
	const char *hostname = hshared->name;

	if (hshared->destroyed)
		return;
	hshared->destroyed = TRUE;

	e_debug(hshared->event, "Host destroy");

	timeout_remove(&hshared->to_idle);

	DLLIST_REMOVE(&cctx->hosts_list, hshared);
	if (hshared == cctx->unix_host)
		cctx->unix_host = NULL;
	else
		hash_table_remove(cctx->hosts, hostname);

	if (hshared->dns_lookup != NULL)
		dns_lookup_abort(&hshared->dns_lookup);

	/* Drop client sessions */
	while (hshared->hosts_list != NULL) {
		host = hshared->hosts_list;
		http_client_host_free_shared(&host);
	}

	event_unref(&hshared->event);
	i_free(hshared->ips);
	i_free(hshared->name);
	i_free(hshared);

	*_hshared = NULL;
}

static void
http_client_host_shared_request_submitted(
	struct http_client_host_shared *hshared)
{
	/* Cancel host idle timeout */
	timeout_remove(&hshared->to_idle);
}

void http_client_host_shared_switch_ioloop(
	struct http_client_host_shared *hshared)
{
	struct http_client_context *cctx = hshared->cctx;

	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);
}

/*
 * Host
 */

struct http_client_host *
http_client_host_get(struct http_client *client,
		     const struct http_url *host_url)
{
	struct http_client_host_shared *hshared;
	struct http_client_host *host;

	hshared = http_client_host_shared_get(client->cctx, host_url);

	host = hshared->hosts_list;
	while (host != NULL) {
		if (host->client == client)
			break;
		host = host->shared_next;
	}

	if (host == NULL) {
		host = i_new(struct http_client_host, 1);
		host->client = client;
		host->shared = hshared;
		i_array_init(&host->queues, 4);
		DLLIST_PREPEND_FULL(&hshared->hosts_list,
				    host, shared_prev, shared_next);
		DLLIST_PREPEND_FULL(&client->hosts_list, host,
				    client_prev, client_next);

		e_debug(hshared->event, "Host session created");
	}

	return host;
}

static void http_client_host_free_shared(struct http_client_host **_host)
{
	struct http_client_host *host = *_host;
	struct http_client *client = host->client;
	struct http_client_host_shared *hshared = host->shared;
	struct http_client_queue *queue;
	ARRAY_TYPE(http_client_queue) queues;

	*_host = NULL;

	e_debug(hshared->event, "Host session destroy");

	DLLIST_REMOVE_FULL(&hshared->hosts_list, host,
			   shared_prev, shared_next);
	DLLIST_REMOVE_FULL(&client->hosts_list, host,
			   client_prev, client_next);

	/* Drop request queues */
	t_array_init(&queues, array_count(&host->queues));
	array_copy(&queues.arr, 0, &host->queues.arr, 0,
		   array_count(&host->queues));
	array_clear(&host->queues);
	array_foreach_elem(&queues, queue)
		http_client_queue_free(queue);
	array_free(&host->queues);

	i_free(host);
}

void http_client_host_free(struct http_client_host **_host)
{
	struct http_client_host *host = *_host;
	struct http_client_host_shared *hshared = host->shared;

	http_client_host_free_shared(_host);

	http_client_host_shared_check_idle(hshared);
}

static void http_client_host_lookup_done(struct http_client_host *host)
{
	struct http_client *client = host->client;
	struct http_client_queue *queue;
	unsigned int requests = 0;

	/* Notify all queues */
	array_foreach_elem(&host->queues, queue)
		requests += http_client_queue_host_lookup_done(queue);

	if (requests == 0 && client->waiting)
		io_loop_stop(client->ioloop);
}

static void
http_client_host_lookup_failure(struct http_client_host *host,
				const char *error)
{
	struct http_client_queue *queue;

	array_foreach_elem(&host->queues, queue)
		http_client_queue_host_lookup_failure(queue, error);
}

void http_client_host_submit_request(struct http_client_host *host,
				     struct http_client_request *req)
{
	struct http_client *client = req->client;
	struct http_client_queue *queue;
	struct http_client_peer_addr addr;
	const char *error;

	req->host = host;

	http_client_request_get_peer_addr(req, &addr);
	if (http_client_peer_addr_is_https(&addr) &&
	    client->ssl_ctx == NULL) {
		if (http_client_init_ssl_ctx(client, &error) < 0) {
			http_client_request_error(
				&req, HTTP_CLIENT_REQUEST_ERROR_CONNECT_FAILED,
				error);
			return;
		}
	}

	/* Add request to queue */
	queue = http_client_queue_get(host, &addr);
	http_client_queue_submit_request(queue, req);

	/* Update shared host object (idle timeout) */
	http_client_host_shared_request_submitted(host->shared);

	/* Queue will trigger host lookup once the request is activated
	   (may be delayed) */
}

static bool http_client_host_is_idle(struct http_client_host *host)
{
	struct http_client_queue *queue;
	unsigned int requests = 0;

	array_foreach_elem(&host->queues, queue)
		requests += http_client_queue_requests_active(queue);

	return (requests == 0);
}

void http_client_host_check_idle(struct http_client_host *host)
{
	http_client_host_shared_check_idle(host->shared);
}

int http_client_host_refresh(struct http_client_host *host)
{
	return http_client_host_shared_refresh(host->shared);
}

bool http_client_host_get_ip_idx(struct http_client_host *host,
				 const struct ip_addr *ip, unsigned int *idx_r)
{
	struct http_client_host_shared *hshared = host->shared;
	unsigned int i;

	for (i = 0; i < hshared->ips_count; i++) {
		if (net_ip_compare(&hshared->ips[i], ip)) {
			*idx_r = i;
			return TRUE;
		}
	}
	return FALSE;
}

void http_client_host_switch_ioloop(struct http_client_host *host)
{
	struct http_client_queue *queue;

	array_foreach_elem(&host->queues, queue)
		http_client_queue_switch_ioloop(queue);
}