/
http-client-host.c
360 lines (291 loc) · 8.95 KB
/
http-client-host.c
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
/* Copyright (c) 2013-2017 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
/*
* Logging
*/
static inline void
http_client_host_debug(struct http_client_host *host,
const char *format, ...) ATTR_FORMAT(2, 3);
static inline void
http_client_host_debug(struct http_client_host *host,
const char *format, ...)
{
va_list args;
if (host->client->set.debug) {
va_start(args, format);
i_debug("http-client: host %s: %s",
host->name, t_strdup_vprintf(format, args));
va_end(args);
}
}
/*
* Host
*/
static void
http_client_host_idle_timeout(struct http_client_host *host)
{
http_client_host_debug(host, "Idle host timed out");
http_client_host_free(&host);
}
void http_client_host_check_idle(struct http_client_host *host)
{
struct http_client_queue *const *queue_idx;
unsigned int requests = 0;
int timeout = 0;
if (host->to_idle != NULL)
return;
array_foreach(&host->queues, queue_idx) {
requests += http_client_queue_requests_active(*queue_idx);
}
if (requests > 0)
return;
if (!host->unix_local && !host->explicit_ip &&
host->ips_timeout.tv_sec > 0) {
timeout = timeval_diff_msecs
(&host->ips_timeout, &ioloop_timeval);
}
if (timeout <= HTTP_CLIENT_HOST_MINIMUM_IDLE_TIMEOUT_MSECS)
timeout = HTTP_CLIENT_HOST_MINIMUM_IDLE_TIMEOUT_MSECS;
host->to_idle = timeout_add_short(timeout,
http_client_host_idle_timeout, host);
http_client_host_debug(host,
"Host is idle (timeout = %u msecs)", timeout);
}
static void
http_client_host_lookup_failure(struct http_client_host *host,
const char *error)
{
struct http_client_queue *const *queue_idx;
error = t_strdup_printf("Failed to lookup host %s: %s",
host->name, error);
array_foreach_modifiable(&host->queues, queue_idx) {
http_client_queue_host_lookup_failure(*queue_idx, error);
}
http_client_host_check_idle(host);
}
static void
http_client_host_dns_callback(const struct dns_lookup_result *result,
struct http_client_host *host)
{
struct http_client *client = host->client;
struct http_client_queue *const *queue_idx;
unsigned int requests = 0;
host->dns_lookup = NULL;
if (result->ret != 0) {
/* lookup failed */
http_client_host_lookup_failure(host, result->error);
return;
}
http_client_host_debug(host,
"DNS lookup successful; got %d IPs", result->ips_count);
i_assert(result->ips_count > 0);
host->ips = i_realloc_type(host->ips, struct ip_addr,
host->ips_count, result->ips_count);
host->ips_count = result->ips_count;
memcpy(host->ips, result->ips, sizeof(*host->ips) * host->ips_count);
host->ips_timeout = ioloop_timeval;
timeval_add_msecs(&host->ips_timeout, client->set.dns_ttl_msecs);
/* make connections to requested ports */
array_foreach_modifiable(&host->queues, queue_idx) {
requests += http_client_queue_host_lookup_done(*queue_idx);
}
if (requests == 0 && host->client->ioloop != NULL)
io_loop_stop(host->client->ioloop);
}
static void http_client_host_lookup
(struct http_client_host *host)
{
struct http_client *client = host->client;
struct dns_lookup_settings dns_set;
struct ip_addr *ips;
int ret;
i_assert(!host->explicit_ip);
i_assert(host->dns_lookup == NULL);
if (client->set.dns_client != NULL) {
http_client_host_debug(host,
"Performing asynchronous DNS lookup");
(void)dns_client_lookup(client->set.dns_client, host->name,
http_client_host_dns_callback, host, &host->dns_lookup);
} else if (client->set.dns_client_socket_path != NULL) {
http_client_host_debug(host,
"Performing asynchronous DNS lookup");
i_zero(&dns_set);
dns_set.dns_client_socket_path =
client->set.dns_client_socket_path;
if (client->set.connect_timeout_msecs > 0)
dns_set.timeout_msecs = client->set.connect_timeout_msecs;
else if (client->set.request_timeout_msecs > 0)
dns_set.timeout_msecs = client->set.request_timeout_msecs;
else {
dns_set.timeout_msecs =
HTTP_CLIENT_DEFAULT_DNS_LOOKUP_TIMEOUT_MSECS;
}
(void)dns_lookup(host->name, &dns_set,
http_client_host_dns_callback, host, &host->dns_lookup);
} else {
unsigned int ips_count;
ret = net_gethostbyname(host->name, &ips, &ips_count);
if (ret != 0) {
http_client_host_lookup_failure(host, net_gethosterror(ret));
return;
}
http_client_host_debug(host,
"DNS lookup successful; got %d IPs", ips_count);
i_free(host->ips);
host->ips_count = ips_count;
host->ips = i_new(struct ip_addr, ips_count);
memcpy(host->ips, ips, ips_count * sizeof(*ips));
host->ips_timeout = ioloop_timeval;
timeval_add_msecs(&host->ips_timeout, client->set.dns_ttl_msecs);
}
}
int http_client_host_refresh(struct http_client_host *host)
{
if (host->unix_local)
return 0;
if (host->explicit_ip)
return 0;
if (host->dns_lookup != NULL)
return -1;
if (host->ips_count > 0 &&
timeval_cmp(&host->ips_timeout, &ioloop_timeval) > 0)
return 0;
if (host->to_idle != NULL)
return 0;
http_client_host_debug(host,
"IPs have expired; need to refresh DNS lookup");
http_client_host_lookup(host);
if (host->dns_lookup != NULL)
return -1;
return (host->ips_count > 0 ? 1 : -1);
}
static struct http_client_host *http_client_host_create
(struct http_client *client)
{
struct http_client_host *host;
// FIXME: limit the maximum number of inactive cached hosts
host = i_new(struct http_client_host, 1);
host->client = client;
i_array_init(&host->queues, 4);
DLLIST_PREPEND(&client->hosts_list, host);
return host;
}
struct http_client_host *http_client_host_get
(struct http_client *client, const struct http_url *host_url)
{
struct http_client_host *host;
if (host_url == NULL) {
host = client->unix_host;
if (host == NULL) {
host = http_client_host_create(client);
host->name = i_strdup("[unix]");
host->unix_local = TRUE;
client->unix_host = host;
http_client_host_debug(host, "Unix host created");
}
} else {
const char *hostname = host_url->host.name;
struct ip_addr ip = host_url->host.ip;
host = hash_table_lookup(client->hosts, hostname);
if (host == NULL) {
host = http_client_host_create(client);
host->name = i_strdup(hostname);
hostname = host->name;
hash_table_insert(client->hosts, hostname, host);
if (ip.family != 0 || net_addr2ip(host->name, &ip) == 0) {
host->ips_count = 1;
host->ips = i_new(struct ip_addr, host->ips_count);
host->ips[0] = ip;
host->explicit_ip = TRUE;
}
http_client_host_debug(host, "Host created");
}
}
return host;
}
void http_client_host_submit_request(struct http_client_host *host,
struct http_client_request *req)
{
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) &&
host->client->ssl_ctx == NULL) {
if (http_client_init_ssl_ctx(host->client, &error) < 0) {
http_client_request_error(&req,
HTTP_CLIENT_REQUEST_ERROR_CONNECT_FAILED, error);
return;
}
}
/* add request to queue (grouped by tcp port) */
queue = http_client_queue_get(host, &addr);
http_client_queue_submit_request(queue, req);
/* cancel host idle timeout */
timeout_remove(&host->to_idle);
}
void http_client_host_free(struct http_client_host **_host)
{
struct http_client_host *host = *_host;
struct http_client_queue *const *queue_idx;
ARRAY_TYPE(http_client_queue) queues;
const char *hostname = host->name;
http_client_host_debug(host, "Host destroy");
if (host->to_idle != NULL)
timeout_remove(&host->to_idle);
DLLIST_REMOVE(&host->client->hosts_list, host);
if (host == host->client->unix_host)
host->client->unix_host = NULL;
else
hash_table_remove(host->client->hosts, hostname);
if (host->dns_lookup != NULL)
dns_lookup_abort(&host->dns_lookup);
/* 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(&queues, queue_idx) {
http_client_queue_free(*queue_idx);
}
array_free(&host->queues);
i_free(host->ips);
i_free(host->name);
i_free(host);
}
bool http_client_host_get_ip_idx(struct http_client_host *host,
const struct ip_addr *ip, unsigned int *idx_r)
{
unsigned int i;
for (i = 0; i < host->ips_count; i++) {
if (net_ip_compare(&host->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 *const *queue_idx;
if (host->dns_lookup != NULL && host->client->set.dns_client == NULL)
dns_lookup_switch_ioloop(host->dns_lookup);
array_foreach(&host->queues, queue_idx)
http_client_queue_switch_ioloop(*queue_idx);
if (host->to_idle != NULL)
host->to_idle = io_loop_move_timeout(&host->to_idle);
}