forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.hh
82 lines (70 loc) · 2.7 KB
/
http.hh
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
/*
* Copyright (C) 2023-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include <seastar/core/seastar.hh>
#include <seastar/core/shared_future.hh>
#include <seastar/coroutine/all.hh>
#include <seastar/http/client.hh>
#include <seastar/net/dns.hh>
#include <seastar/net/tls.hh>
#include "seastarx.hh"
#include "utils/log.hh"
namespace utils::http {
class dns_connection_factory : public seastar::http::experimental::connection_factory {
protected:
std::string _host;
int _port;
logging::logger& _logger;
struct state {
bool initialized = false;
socket_address addr;
::shared_ptr<tls::certificate_credentials> creds;
};
lw_shared_ptr<state> _state;
shared_future<> _done;
// This method can out-live the factory instance, in case `make()` is never called before the instance is destroyed.
static future<> initialize(lw_shared_ptr<state> state, std::string host, int port, bool use_https, logging::logger& logger) {
co_await coroutine::all(
[state, host, port] () -> future<> {
auto hent = co_await net::dns::get_host_by_name(host, net::inet_address::family::INET);
state->addr = socket_address(hent.addr_list.front(), port);
},
[state, use_https] () -> future<> {
if (use_https) {
tls::credentials_builder cbuild;
co_await cbuild.set_system_trust();
state->creds = cbuild.build_certificate_credentials();
}
}
);
state->initialized = true;
logger.debug("Initialized factory, address={} tls={}", state->addr, state->creds == nullptr ? "no" : "yes");
}
public:
dns_connection_factory(std::string host, int port, bool use_https, logging::logger& logger)
: _host(std::move(host))
, _port(port)
, _logger(logger)
, _state(make_lw_shared<state>())
, _done(initialize(_state, _host, _port, use_https, _logger))
{
}
virtual future<connected_socket> make(abort_source*) override {
if (!_state->initialized) {
_logger.debug("Waiting for factory to initialize");
co_await _done.get_future();
}
if (_state->creds) {
_logger.debug("Making new HTTPS connection addr={} host={}", _state->addr, _host);
co_return co_await tls::connect(_state->creds, _state->addr, tls::tls_options{.server_name = _host});
} else {
_logger.debug("Making new HTTP connection");
co_return co_await seastar::connect(_state->addr, {}, transport::TCP);
}
}
};
} // namespace utils::http