1 #ifndef HTTP_CLIENT_PRIVATE_H
2 #define HTTP_CLIENT_PRIVATE_H
3 
4 #include "connection.h"
5 
6 #include "http-url.h"
7 #include "http-client.h"
8 
9 /*
10  * Defaults
11  */
12 
13 #define HTTP_CLIENT_CONTINUE_TIMEOUT_MSECS (1000*2)
14 #define HTTP_CLIENT_DEFAULT_REQUEST_TIMEOUT_MSECS (1000*60*1)
15 #define HTTP_CLIENT_DEFAULT_DNS_LOOKUP_TIMEOUT_MSECS (1000*10)
16 #define HTTP_CLIENT_DEFAULT_BACKOFF_TIME_MSECS (100)
17 #define HTTP_CLIENT_DEFAULT_BACKOFF_MAX_TIME_MSECS (1000*60)
18 #define HTTP_CLIENT_DEFAULT_DNS_TTL_MSECS (1000*60*30)
19 #define HTTP_CLIENT_MIN_IDLE_TIMEOUT_MSECS 50
20 
21 /*
22  * Types
23  */
24 
25 struct http_client_connection;
26 struct http_client_peer_pool;
27 struct http_client_peer_shared;
28 struct http_client_peer;
29 struct http_client_queue;
30 struct http_client_host_shared;
31 struct http_client_host;
32 
33 ARRAY_DEFINE_TYPE(http_client_request, struct http_client_request *);
34 ARRAY_DEFINE_TYPE(http_client_connection, struct http_client_connection *);
35 ARRAY_DEFINE_TYPE(http_client_peer, struct http_client_peer *);
36 ARRAY_DEFINE_TYPE(http_client_peer_shared, struct http_client_peer_shared *);
37 ARRAY_DEFINE_TYPE(http_client_peer_pool, struct http_client_peer_pool *);
38 ARRAY_DEFINE_TYPE(http_client_queue, struct http_client_queue *);
39 ARRAY_DEFINE_TYPE(http_client_host, struct http_client_host_shared *);
40 
41 HASH_TABLE_DEFINE_TYPE(http_client_peer_shared,
42 		       const struct http_client_peer_addr *,
43 		       struct http_client_peer_shared *);
44 HASH_TABLE_DEFINE_TYPE(http_client_host_shared, const char *,
45 		       struct http_client_host_shared *);
46 
47 enum http_client_peer_addr_type {
48 	HTTP_CLIENT_PEER_ADDR_HTTP = 0,
49 	HTTP_CLIENT_PEER_ADDR_HTTPS,
50 	HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL,
51 	HTTP_CLIENT_PEER_ADDR_RAW,
52 	HTTP_CLIENT_PEER_ADDR_UNIX,
53 };
54 
55 struct http_client_peer_addr {
56 	enum http_client_peer_addr_type type;
57 	union {
58 		struct {
59 			const char *https_name; /* TLS SNI */
60 			struct ip_addr ip;
61 			in_port_t port;
62 		} tcp;
63 		struct {
64 			const char *path;
65 		} un;
66 	} a;
67 };
68 
69 /*
70  * Objects
71  */
72 
73 struct http_client_request {
74 	pool_t pool;
75 	unsigned int refcount;
76 	const char *label;
77 	unsigned int id;
78 
79 	struct http_client_request *prev, *next;
80 
81 	const char *method, *target;
82 	struct http_url origin_url;
83 	const char *username, *password;
84 
85 	const char *host_socket;
86 	const struct http_url *host_url;
87 	const char *authority;
88 
89 	struct http_client *client;
90 	struct http_client_host *host;
91 	struct http_client_queue *queue;
92 	struct http_client_peer *peer;
93 	struct http_client_connection *conn;
94 
95 	struct event *event;
96 	const char *const *event_headers;
97 	unsigned int last_status;
98 
99 	string_t *headers;
100 	time_t date;
101 
102 	struct istream *payload_input;
103 	uoff_t payload_size, payload_offset;
104 	struct ostream *payload_output;
105 
106 	/* Time when request can be sent the next time. This is set by
107 	   http_client_request_delay*(). Default is 0 = immediately. Retries
108 	   can update this. */
109 	struct timeval release_time;
110 	/* Time when http_client_request_submit() was called. */
111 	struct timeval submit_time;
112 	/* Time when the request was first sent to the server. The HTTP
113 	   connection already exists at this time. */
114 	struct timeval first_sent_time;
115 	/* Time when the request was last sent to the server (if it was
116 	   retried). */
117 	struct timeval sent_time;
118 	/* Time when the HTTP response header was last received. */
119 	struct timeval response_time;
120 	/* Time when the request will be aborted. Set by
121 	   http_client_request_set_timeout(). */
122 	struct timeval timeout_time;
123 	unsigned int timeout_msecs;
124 	unsigned int attempt_timeout_msecs;
125 	unsigned int max_attempts;
126 
127 	uoff_t response_offset, request_offset;
128 	uoff_t bytes_in, bytes_out;
129 
130 	unsigned int attempts, send_attempts;
131 	unsigned int redirects;
132 	uint64_t sent_global_ioloop_usecs;
133 	uint64_t sent_http_ioloop_usecs;
134 	uint64_t sent_lock_usecs;
135 
136 	unsigned int delayed_error_status;
137 	const char *delayed_error;
138 
139 	http_client_request_callback_t *callback;
140 	void *context;
141 
142 	void (*destroy_callback)(void *);
143 	void *destroy_context;
144 
145 	enum http_request_state state;
146 
147 	bool have_hdr_authorization:1;
148 	bool have_hdr_body_spec:1;
149 	bool have_hdr_connection:1;
150 	bool have_hdr_date:1;
151 	bool have_hdr_expect:1;
152 	bool have_hdr_host:1;
153 	bool have_hdr_user_agent:1;
154 
155 	bool payload_sync:1;
156 	bool payload_sync_continue:1;
157 	bool payload_chunked:1;
158 	bool payload_wait:1;
159 	bool payload_finished:1;
160 	bool payload_empty:1;
161 	bool urgent:1;
162 	bool submitted:1;
163 	bool listed:1;
164 	bool connect_tunnel:1;
165 	bool connect_direct:1;
166 	bool ssl_tunnel:1;
167 	bool preserve_exact_reason:1;
168 };
169 
170 struct http_client_connection {
171 	struct connection conn;
172 	struct event *event;
173 	unsigned int refcount;
174 
175 	struct http_client_peer_pool *ppool;
176 	struct http_client_peer *peer;
177 
178 	int connect_errno;
179 	struct timeval connect_start_timestamp;
180 	struct timeval connected_timestamp;
181 	struct http_client_request *connect_request;
182 
183 	struct ssl_iostream *ssl_iostream;
184 	struct http_response_parser *http_parser;
185 	struct timeout *to_connect, *to_input, *to_idle, *to_response;
186 	struct timeout *to_requests;
187 
188 	struct http_client_request *pending_request;
189 	struct istream *incoming_payload;
190 	struct io *io_req_payload;
191 	struct ioloop *last_ioloop;
192 	struct io_wait_timer *io_wait_timer;
193 
194 	/* Requests that have been sent, waiting for response */
195 	ARRAY_TYPE(http_client_request) request_wait_list;
196 
197 	bool connected:1;           /* Connection is connected */
198 	bool idle:1;		    /* Connection is idle */
199 	bool tunneling:1;           /* Last sent request turns this
200 	                               connection into tunnel */
201 	bool connect_succeeded:1;   /* Connection succeeded including SSL */
202 	bool connect_failed:1;      /* Connection failed */
203 	bool lost_prematurely:1;    /* Lost connection before receiving any data */
204 	bool closing:1;
205 	bool disconnected:1;
206 	bool close_indicated:1;
207 	bool output_locked:1;       /* Output is locked; no pipelining */
208 	bool output_broken:1;       /* Output is broken; no more requests */
209 	bool in_req_callback:1;     /* Performing request callback (busy) */
210 	bool debug:1;
211 };
212 
213 struct http_client_peer_shared {
214 	unsigned int refcount;
215 	struct http_client_peer_addr addr;
216 	char *addr_name;
217 	struct event *event;
218 
219 	char *label;
220 
221 	struct http_client_context *cctx;
222 	struct http_client_peer_shared *prev, *next;
223 
224 	struct http_client_peer_pool *pools_list;
225 
226 	struct http_client_peer *peers_list;
227 	unsigned int peers_count;
228 
229 	/* Connection retry */
230 	struct timeval last_failure;
231 	struct timeout *to_backoff;
232 	unsigned int backoff_initial_time_msecs;
233 	unsigned int backoff_current_time_msecs;
234 	unsigned int backoff_max_time_msecs;
235 
236 	bool no_payload_sync:1;   /* Expect: 100-continue failed before */
237 	bool seen_100_response:1; /* Expect: 100-continue succeeded before */
238 	bool allows_pipelining:1; /* Peer is known to allow persistent
239 	                             connections */
240 };
241 
242 struct http_client_peer_pool {
243 	unsigned int refcount;
244 	struct http_client_peer_shared *peer;
245 	struct http_client_peer_pool *prev, *next;
246 	struct event *event;
247 
248 	/* All connections to this peer */
249 	ARRAY_TYPE(http_client_connection) conns;
250 
251 	/* Pending connections (not ready connecting) */
252 	ARRAY_TYPE(http_client_connection) pending_conns;
253 
254 	/* Available connections to this peer */
255 	ARRAY_TYPE(http_client_connection) idle_conns;
256 
257 	/* Distinguishing settings for these connections */
258 	struct ssl_iostream_context *ssl_ctx;
259 	char *rawlog_dir;
260 	struct pcap_output *pcap_output;
261 
262 	bool destroyed:1;         /* Peer pool is being destroyed */
263 };
264 
265 struct http_client_peer {
266 	unsigned int refcount;
267 	struct http_client_peer_shared *shared;
268 	struct http_client_peer *shared_prev, *shared_next;
269 
270 	struct http_client *client;
271 	struct http_client_peer *client_prev, *client_next;
272 
273 	struct http_client_peer_pool *ppool;
274 	struct event *event;
275 
276 	/* Queues using this peer */
277 	ARRAY_TYPE(http_client_queue) queues;
278 
279 	/* Active connections to this peer */
280 	ARRAY_TYPE(http_client_connection) conns;
281 	/* Pending connections (not ready connecting) */
282 	ARRAY_TYPE(http_client_connection) pending_conns;
283 
284 	/* Zero time-out for consolidating request handling */
285 	struct timeout *to_req_handling;
286 
287 	bool connect_failed:1;    /* Last connection attempt failed */
288 	bool connect_backoff:1;   /* Peer is waiting for backoff timout*/
289 	bool disconnected:1;      /* Peer is already disconnected */
290 	bool handling_requests:1; /* Currently running request handler */
291 };
292 
293 struct http_client_queue {
294 	struct http_client *client;
295 	struct http_client_queue *prev, *next;
296 
297 	struct http_client_host *host;
298 	char *name;
299 	struct event *event;
300 
301 	struct http_client_peer_addr addr;
302 	char *addr_name;
303 
304 	/* Current index in host->ips */
305 	unsigned int ips_connect_idx;
306 	/* The first IP that started the current round of connection attempts.
307 	   initially 0, and later set to the ip index of the last successful
308 	   connected IP */
309 	unsigned int ips_connect_start_idx;
310 
311 	struct timeval first_connect_time;
312 	unsigned int connect_attempts;
313 
314 	/* Peers we are trying to connect to;
315 	   this can be more than one when soft connect timeouts are enabled */
316 	ARRAY_TYPE(http_client_peer) pending_peers;
317 
318 	/* Currently active peer */
319 	struct http_client_peer *cur_peer;
320 
321 	/* All requests associated to this queue
322 	   (ordered by earliest timeout first) */
323 	ARRAY_TYPE(http_client_request) requests;
324 
325 	/* Delayed requests waiting to be released after delay */
326 	ARRAY_TYPE(http_client_request) delayed_requests;
327 
328 	/* Requests pending in queue to be picked up by connections */
329 	ARRAY_TYPE(http_client_request) queued_requests, queued_urgent_requests;
330 
331 	struct timeout *to_connect, *to_request, *to_delayed;
332 };
333 
334 struct http_client_host_shared {
335 	struct http_client_host_shared *prev, *next;
336 
337 	struct http_client_context *cctx;
338 	char *name;
339 	struct event *event;
340 
341 	/* The ip addresses DNS returned for this host */
342 	unsigned int ips_count;
343 	struct ip_addr *ips;
344 	struct timeval ips_timeout;
345 
346 	/* Private instance for each client that uses this host */
347 	struct http_client_host *hosts_list;
348 
349 	/* Active DNS lookup */
350 	struct dns_lookup *dns_lookup;
351 
352 	/* Timeouts */
353 	struct timeout *to_idle;
354 
355 	bool destroyed:1;	/* Shared host object is being destroyed */
356 	bool unix_local:1;
357 	bool explicit_ip:1;
358 };
359 
360 struct http_client_host {
361 	struct http_client_host_shared *shared;
362 	struct http_client_host *shared_prev, *shared_next;
363 
364 	struct http_client *client;
365 	struct http_client_host *client_prev, *client_next;
366 
367 	/* Separate queue for each host port */
368 	ARRAY_TYPE(http_client_queue) queues;
369 };
370 
371 struct http_client {
372 	pool_t pool;
373 	struct http_client_context *cctx;
374 	struct http_client_settings set;
375 
376 	struct http_client *prev, *next;
377 
378 	struct event *event;
379 	struct ioloop *ioloop;
380 	struct ssl_iostream_context *ssl_ctx;
381 
382 	/* List of failed requests that are waiting for ioloop */
383 	ARRAY(struct http_client_request *) delayed_failing_requests;
384 	struct timeout *to_failing_requests;
385 
386 	struct http_client_host *hosts_list;
387 	struct http_client_peer *peers_list;
388 
389 	struct http_client_request *requests_list;
390 	unsigned int requests_count;
391 
392 	bool waiting:1;
393 };
394 
395 struct http_client_context {
396 	pool_t pool;
397 	unsigned int refcount;
398 	struct event *event;
399 	struct ioloop *ioloop;
400 
401 	struct http_client_settings set;
402 
403 	struct dns_client *dns_client;
404 	const char *dns_client_socket_path;
405 	unsigned int dns_ttl_msecs;
406 	unsigned int dns_lookup_timeout_msecs;
407 
408 	struct http_client *clients_list;
409 	struct connection_list *conn_list;
410 
411 	HASH_TABLE_TYPE(http_client_peer_shared) peers;
412 	struct http_client_peer_shared *peers_list;
413 	HASH_TABLE_TYPE(http_client_host_shared) hosts;
414 	struct http_client_host_shared *unix_host;
415 	struct http_client_host_shared *hosts_list;
416 };
417 
418 /*
419  * Peer address
420  */
421 
422 static inline bool
http_client_peer_addr_is_https(const struct http_client_peer_addr * addr)423 http_client_peer_addr_is_https(const struct http_client_peer_addr *addr)
424 {
425 	switch (addr->type) {
426 	case HTTP_CLIENT_PEER_ADDR_HTTPS:
427 	case HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL:
428 		return TRUE;
429 	default:
430 		break;
431 	}
432 	return FALSE;
433 }
434 
435 static inline const char *
http_client_peer_addr_get_https_name(const struct http_client_peer_addr * addr)436 http_client_peer_addr_get_https_name(const struct http_client_peer_addr *addr)
437 {
438 	switch (addr->type) {
439 	case HTTP_CLIENT_PEER_ADDR_HTTPS:
440 	case HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL:
441 		return addr->a.tcp.https_name;
442 	default:
443 		break;
444 	}
445 	return NULL;
446 }
447 
448 static inline const char *
http_client_peer_addr2str(const struct http_client_peer_addr * addr)449 http_client_peer_addr2str(const struct http_client_peer_addr *addr)
450 {
451 	switch (addr->type) {
452 	case HTTP_CLIENT_PEER_ADDR_HTTP:
453 	case HTTP_CLIENT_PEER_ADDR_HTTPS:
454 	case HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL:
455 	case HTTP_CLIENT_PEER_ADDR_RAW:
456 		if (addr->a.tcp.ip.family == AF_INET6) {
457 			return t_strdup_printf("[%s]:%u",
458 					       net_ip2addr(&addr->a.tcp.ip),
459 					       addr->a.tcp.port);
460 		}
461 		return t_strdup_printf("%s:%u",
462 				       net_ip2addr(&addr->a.tcp.ip),
463 				       addr->a.tcp.port);
464 	case HTTP_CLIENT_PEER_ADDR_UNIX:
465 		return t_strdup_printf("unix:%s", addr->a.un.path);
466 	default:
467 		break;
468 	}
469 	i_unreached();
470 	return "";
471 }
472 
473 /*
474  * Request
475  */
476 
477 static inline bool
http_client_request_to_proxy(const struct http_client_request * req)478 http_client_request_to_proxy(const struct http_client_request *req)
479 {
480 	return (req->host_url != &req->origin_url);
481 }
482 
483 const char *http_client_request_label(struct http_client_request *req);
484 
485 void http_client_request_ref(struct http_client_request *req);
486 /* Returns FALSE if unrefing destroyed the request entirely */
487 bool http_client_request_unref(struct http_client_request **_req);
488 void http_client_request_destroy(struct http_client_request **_req);
489 
490 void http_client_request_get_peer_addr(const struct http_client_request *req,
491 				       struct http_client_peer_addr *addr);
492 enum http_response_payload_type
493 http_client_request_get_payload_type(struct http_client_request *req);
494 int http_client_request_send(struct http_client_request *req, bool pipelined);
495 int http_client_request_send_more(struct http_client_request *req,
496 				  bool pipelined);
497 
498 bool http_client_request_callback(struct http_client_request *req,
499 				  struct http_response *response);
500 void http_client_request_connect_callback(struct http_client_request *req,
501 					  const struct http_client_tunnel *tunnel,
502 					  struct http_response *response);
503 
504 void http_client_request_resubmit(struct http_client_request *req);
505 void http_client_request_retry(struct http_client_request *req,
506 			       unsigned int status, const char *error);
507 void http_client_request_error_delayed(struct http_client_request **_req);
508 void http_client_request_error(struct http_client_request **req,
509 			       unsigned int status, const char *error);
510 void http_client_request_redirect(struct http_client_request *req,
511 				  unsigned int status, const char *location);
512 void http_client_request_finish(struct http_client_request *req);
513 
514 /*
515  * Connection
516  */
517 
518 struct connection_list *http_client_connection_list_init(void);
519 
520 struct http_client_connection *
521 http_client_connection_create(struct http_client_peer *peer);
522 void http_client_connection_ref(struct http_client_connection *conn);
523 /* Returns FALSE if unrefing destroyed the connection entirely */
524 bool http_client_connection_unref(struct http_client_connection **_conn);
525 void http_client_connection_close(struct http_client_connection **_conn);
526 
527 void http_client_connection_lost(struct http_client_connection **_conn,
528 				 const char *error) ATTR_NULL(2);
529 
530 void http_client_connection_peer_closed(struct http_client_connection **_conn);
531 void http_client_connection_request_destroyed(
532 	struct http_client_connection *conn, struct http_client_request *req);
533 
534 void http_client_connection_handle_output_error(
535 	struct http_client_connection *conn);
536 int http_client_connection_output(struct http_client_connection *conn);
537 void http_client_connection_start_request_timeout(
538 	struct http_client_connection *conn);
539 void http_client_connection_reset_request_timeout(
540 	struct http_client_connection *conn);
541 void http_client_connection_stop_request_timeout(
542 	struct http_client_connection *conn);
543 unsigned int
544 http_client_connection_count_pending(struct http_client_connection *conn);
545 int http_client_connection_check_ready(struct http_client_connection *conn);
546 bool http_client_connection_is_idle(struct http_client_connection *conn);
547 bool http_client_connection_is_active(struct http_client_connection *conn);
548 int http_client_connection_next_request(struct http_client_connection *conn);
549 void http_client_connection_check_idle(struct http_client_connection *conn);
550 void http_client_connection_switch_ioloop(struct http_client_connection *conn);
551 void http_client_connection_start_tunnel(struct http_client_connection **_conn,
552 					 struct http_client_tunnel *tunnel);
553 void http_client_connection_lost_peer(struct http_client_connection *conn);
554 void http_client_connection_claim_idle(struct http_client_connection *conn,
555 				       struct http_client_peer *peer);
556 
557 /*
558  * Peer
559  */
560 
561 /* address */
562 
563 unsigned int
564 http_client_peer_addr_hash(const struct http_client_peer_addr *peer) ATTR_PURE;
565 int http_client_peer_addr_cmp(const struct http_client_peer_addr *peer1,
566 			      const struct http_client_peer_addr *peer2)
567 			      ATTR_PURE;
568 
569 /* connection pool */
570 
571 void http_client_peer_pool_ref(struct http_client_peer_pool *ppool);
572 void http_client_peer_pool_unref(struct http_client_peer_pool **_ppool);
573 
574 void http_client_peer_pool_close(struct http_client_peer_pool **_ppool);
575 
576 /* peer (shared) */
577 
578 const char *
579 http_client_peer_shared_label(struct http_client_peer_shared *pshared);
580 
581 void http_client_peer_shared_ref(struct http_client_peer_shared *pshared);
582 void http_client_peer_shared_unref(struct http_client_peer_shared **_pshared);
583 void http_client_peer_shared_close(struct http_client_peer_shared **_pshared);
584 
585 void http_client_peer_shared_switch_ioloop(
586 	struct http_client_peer_shared *pshared);
587 
588 unsigned int
589 http_client_peer_shared_max_connections(
590 	struct http_client_peer_shared *pshared);
591 
592 /* peer */
593 
594 struct http_client_peer *
595 http_client_peer_get(struct http_client *client,
596 		     const struct http_client_peer_addr *addr);
597 void http_client_peer_ref(struct http_client_peer *peer);
598 bool http_client_peer_unref(struct http_client_peer **_peer);
599 void http_client_peer_close(struct http_client_peer **_peer);
600 
601 bool http_client_peer_have_queue(struct http_client_peer *peer,
602 				 struct http_client_queue *queue);
603 void http_client_peer_link_queue(struct http_client_peer *peer,
604 				 struct http_client_queue *queue);
605 void http_client_peer_unlink_queue(struct http_client_peer *peer,
606 				   struct http_client_queue *queue);
607 struct http_client_request *
608 http_client_peer_claim_request(struct http_client_peer *peer, bool no_urgent);
609 void http_client_peer_trigger_request_handler(struct http_client_peer *peer);
610 void http_client_peer_connection_success(struct http_client_peer *peer);
611 void http_client_peer_connection_failure(struct http_client_peer *peer,
612 					 const char *reason);
613 void http_client_peer_connection_lost(struct http_client_peer *peer,
614 				      bool premature);
615 bool http_client_peer_is_connected(struct http_client_peer *peer);
616 unsigned int
617 http_client_peer_idle_connections(struct http_client_peer *peer);
618 unsigned int
619 http_client_peer_active_connections(struct http_client_peer *peer);
620 unsigned int
621 http_client_peer_pending_connections(struct http_client_peer *peer);
622 void http_client_peer_switch_ioloop(struct http_client_peer *peer);
623 
624 /*
625  * Queue
626  */
627 
628 struct http_client_queue *
629 http_client_queue_get(struct http_client_host *host,
630 		      const struct http_client_peer_addr *addr);
631 void http_client_queue_free(struct http_client_queue *queue);
632 void http_client_queue_connection_setup(struct http_client_queue *queue);
633 unsigned int
634 http_client_queue_host_lookup_done(struct http_client_queue *queue);
635 void http_client_queue_host_lookup_failure(struct http_client_queue *queue,
636 					   const char *error);
637 void http_client_queue_submit_request(struct http_client_queue *queue,
638 				      struct http_client_request *req);
639 void http_client_queue_drop_request(struct http_client_queue *queue,
640 				    struct http_client_request *req);
641 struct http_client_request *
642 http_client_queue_claim_request(struct http_client_queue *queue,
643 				const struct http_client_peer_addr *addr,
644 				bool no_urgent);
645 unsigned int
646 http_client_queue_requests_pending(struct http_client_queue *queue,
647 				   unsigned int *num_urgent_r) ATTR_NULL(2);
648 unsigned int http_client_queue_requests_active(struct http_client_queue *queue);
649 void http_client_queue_connection_success(struct http_client_queue *queue,
650 					  struct http_client_peer *peer);
651 void http_client_queue_connection_failure(struct http_client_queue *queue,
652 					  struct http_client_peer *peer,
653 					  const char *reason);
654 void http_client_queue_peer_disconnected(struct http_client_queue *queue,
655 					 struct http_client_peer *peer);
656 void http_client_queue_switch_ioloop(struct http_client_queue *queue);
657 
658 /*
659  * Host
660  */
661 
662 /* host (shared) */
663 
664 void http_client_host_shared_free(struct http_client_host_shared **_hshared);
665 void http_client_host_shared_switch_ioloop(
666 	struct http_client_host_shared *hshared);
667 
668 /* host */
669 
670 static inline unsigned int
http_client_host_get_ips_count(struct http_client_host * host)671 http_client_host_get_ips_count(struct http_client_host *host)
672 {
673 	return host->shared->ips_count;
674 }
675 
676 static inline const struct ip_addr *
http_client_host_get_ip(struct http_client_host * host,unsigned int idx)677 http_client_host_get_ip(struct http_client_host *host, unsigned int idx)
678 {
679 	i_assert(idx < host->shared->ips_count);
680 	return &host->shared->ips[idx];
681 }
682 
683 static inline bool
http_client_host_ready(struct http_client_host * host)684 http_client_host_ready(struct http_client_host *host)
685 {
686 	return host->shared->dns_lookup == NULL;
687 }
688 
689 struct http_client_host *
690 http_client_host_get(struct http_client *client,
691 		     const struct http_url *host_url);
692 void http_client_host_free(struct http_client_host **_host);
693 void http_client_host_submit_request(struct http_client_host *host,
694 				     struct http_client_request *req);
695 void http_client_host_switch_ioloop(struct http_client_host *host);
696 void http_client_host_check_idle(struct http_client_host *host);
697 int http_client_host_refresh(struct http_client_host *host);
698 bool http_client_host_get_ip_idx(struct http_client_host *host,
699 				 const struct ip_addr *ip, unsigned int *idx_r);
700 
701 /*
702  * Client
703  */
704 
705 int http_client_init_ssl_ctx(struct http_client *client, const char **error_r);
706 
707 void http_client_delay_request_error(struct http_client *client,
708 				     struct http_client_request *req);
709 void http_client_remove_request_error(struct http_client *client,
710 				      struct http_client_request *req);
711 
712 /*
713  * Client shared context
714  */
715 
716 void http_client_context_switch_ioloop(struct http_client_context *cctx);
717 
718 #endif
719