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
//! Connection module
//!
//! This module provides functionality for managing HTTP connections.
use crate::{
cert::{Certificate, Identity},
dns::DnsResolver,
proto::ProtocolSettings,
response::DeboaResponse,
Result,
};
use http::{Request, Version};
use hyper_body_utils::HttpBody;
use std::{future::Future, net::IpAddr};
use std::{net::SocketAddr, time::Duration};
/// Builder for connection configuration.
pub struct ConnectionConfigBuilder<'a, I, C> {
scheme: &'a str,
host: &'a str,
port: u16,
protocol_version: Version,
connection_timeout: Duration,
identity: Option<&'a I>,
certificate: Option<&'a C>,
skip_cert_verification: bool,
client_bind_addr: IpAddr,
prior_knowledge: bool,
protocol_settings: ProtocolSettings,
}
impl<'a, I, C> ConnectionConfigBuilder<'a, I, C>
where
I: Identity,
C: Certificate,
{
/// Create a new connection configuration builder.
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
scheme: "http",
host: "",
port: 80,
protocol_version: Version::HTTP_2,
protocol_settings: ProtocolSettings::default(),
connection_timeout: Duration::from_secs(30),
identity: None,
certificate: None,
skip_cert_verification: false,
client_bind_addr: "0.0.0.0"
.parse()
.unwrap(),
prior_knowledge: false,
}
}
/// Set the scheme for the connection.
pub fn scheme(mut self, scheme: &'a str) -> Self {
self.scheme = scheme;
self
}
/// Set the host for the connection.
pub fn host(mut self, host: &'a str) -> Self {
self.host = host;
self
}
/// Set the port for the connection.
pub fn port(mut self, port: u16) -> Self {
self.port = port;
self
}
/// Set the protocol for the connection.
pub fn protocol_version(mut self, protocol_version: Version) -> Self {
self.protocol_version = protocol_version;
self
}
/// Set the connection timeout for the connection.
pub fn connection_timeout(mut self, connection_timeout: Duration) -> Self {
self.connection_timeout = connection_timeout;
self
}
/// Set the identity for the connection.
pub fn identity(mut self, identity: Option<&'a I>) -> Self {
self.identity = identity;
self
}
/// Set the certificate for the connection.
pub fn certificate(mut self, certificate: Option<&'a C>) -> Self {
self.certificate = certificate;
self
}
/// Set whether to skip certificate verification.
pub fn skip_cert_verification(mut self, skip_cert_verification: bool) -> Self {
self.skip_cert_verification = skip_cert_verification;
self
}
/// Set the client bind address for the connection.
pub fn client_bind_addr(mut self, client_bind_addr: IpAddr) -> Self {
self.client_bind_addr = client_bind_addr;
self
}
/// Set whether to prior knowdlege of protocol support.
pub fn prior_knowledge(mut self, prior_knowledge: bool) -> Self {
self.prior_knowledge = prior_knowledge;
self
}
/// Set the protocol settings the connection.
pub fn protocol_settings(mut self, protocol_settings: ProtocolSettings) -> Self {
self.protocol_settings = protocol_settings;
self
}
/// Build the connection configuration.
pub fn build(self) -> ConnectionConfig<'a, I, C> {
ConnectionConfig {
scheme: self.scheme,
host: self.host,
port: self.port,
protocol_version: self.protocol_version,
connection_timeout: self.connection_timeout,
identity: self.identity,
certificate: self.certificate,
skip_cert_verification: self.skip_cert_verification,
client_bind_addr: self.client_bind_addr,
prior_knowledge: self.prior_knowledge,
protocol_settings: self.protocol_settings,
}
}
}
/// Connection configuration.
pub struct ConnectionConfig<'a, I, C> {
scheme: &'a str,
host: &'a str,
port: u16,
protocol_version: Version,
connection_timeout: Duration,
identity: Option<&'a I>,
certificate: Option<&'a C>,
skip_cert_verification: bool,
client_bind_addr: IpAddr,
prior_knowledge: bool,
protocol_settings: ProtocolSettings,
}
impl<'a, I, C> ConnectionConfig<'a, I, C>
where
I: Identity,
C: Certificate,
{
/// Create a new connection configuration builder.
pub fn builder() -> ConnectionConfigBuilder<'a, I, C> {
ConnectionConfigBuilder::new()
}
/// Get the scheme for the connection.
pub fn scheme(&self) -> &str {
self.scheme
}
/// Get the host for the connection.
pub fn host(&self) -> &str {
self.host
}
/// Get the port for the connection.
pub fn port(&self) -> u16 {
self.port
}
/// Get the protocol for the connection.
pub fn protocol_version(&self) -> &Version {
&self.protocol_version
}
/// Get the connection timeout for the connection.
pub fn connection_timeout(&self) -> Duration {
self.connection_timeout
}
/// Get the identity for the connection.
pub fn identity(&self) -> Option<&I> {
self.identity
}
/// Get the certificate for the connection.
pub fn certificate(&self) -> Option<&C> {
self.certificate
}
/// Get whether to skip certificate verification.
pub fn skip_cert_verification(&self) -> bool {
self.skip_cert_verification
}
/// Get the client bind address for the connection.
pub fn client_bind_addr(&self) -> &IpAddr {
&self.client_bind_addr
}
/// Get whether to consider prior knowdlege of protocol support.
pub fn prior_knowledge(&self) -> bool {
self.prior_knowledge
}
/// Get the protocol settings for the connection.
pub fn protocol_settings(&self) -> &ProtocolSettings {
&self.protocol_settings
}
}
/// Trait that represents an HTTP connection.
pub trait HttpConnection {
/// The sender to use.
type Sender;
/// Get the sender.
fn sender(&mut self) -> &mut Self::Sender;
}
/// Trait that represents the HTTP connection pool.
pub trait HttpConnectionPool {
/// The identity type.
type Identity: crate::cert::Identity;
/// The certificate type.
type Certificate: crate::cert::Certificate;
/// The connection dispatcher type.
type ConnectionDispather: HttpConnectionDispatcher;
/// The connection cache type.
type ConnectionCache;
/// Allow get connections.
///
/// # Returns
///
/// * `&Self::ConnectionCache` - The connections.
///
fn connections(&self) -> &Self::ConnectionCache;
/// Returns the number of connections.
///
/// # Returns
///
/// * `u32` - The number of connections.
///
fn connection_count(&self) -> u32;
/// Allow create a new connection.
///
/// # Arguments
///
/// * `config` - The connection configuration.
/// * `dns_resolver` - The DNS resolver to use.
///
/// # Returns
///
/// * `Result<&mut Self::ConnectionDispather>` - The connection or error.
///
fn create_connection<D>(
&mut self,
config: &mut ConnectionConfig<Self::Identity, Self::Certificate>,
dns_resolver: &D,
) -> impl Future<Output = Result<&mut Self::ConnectionDispather>>
where
D: DnsResolver;
}
/// Trait for plain connection factory
pub trait PlainConnectionFactory {
/// Output connection type
type Connection;
/// Connect with TcpStream
fn connect(&mut self, addr: SocketAddr) -> impl Future<Output = Result<Self::Connection>>;
}
/// Trait for secure connection factory
pub trait SecureConnectionFactory<'a, I, C> {
/// Output connection type
type Connection;
/// Connect with TcpStream
fn connect(
&mut self,
addr: SocketAddr,
config: ConnectionConfig<'a, I, C>,
) -> impl Future<Output = Result<Self::Connection>>;
}
/// Trait that represents the HTTP connection dispatcher.
pub trait HttpConnectionDispatcher {
/// Send a request through the connection.
///
/// # Arguments
///
/// * `request` - The request to send.
///
/// # Returns
///
/// * `Result<DeboaResponse>` - The response from the server.
fn send_request(
&mut self,
request: Request<HttpBody>,
timeout: Duration,
) -> impl Future<Output = Result<DeboaResponse>>;
}
/// Trait that represents the HTTP connection.
///
/// # Type Parameters
///
/// * `Connection` - The connection type.
/// * `RuntimeStream` - The runtime stream type.
///
pub trait ProtoConnection {
/// The connection type.
type Connection: HttpConnection;
/// The runtime stream type.
type RuntimeStream;
/// Create a new connection.
///
/// # Arguments
///
/// * `stream` - The runtime stream to use.
///
/// # Errors
///
/// * `DeboaError` - If the connection fails.
///
/// # Returns
///
/// * `Result<Self::Connection>` - The connection or error.
///
fn connect(stream: Self::RuntimeStream) -> impl Future<Output = Result<Self::Connection>>;
/// Get connection protocol.
///
/// # Returns
///
/// * `Version` - The connection protocol.
///
fn protocol_version(&self) -> Version;
}