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
use std::rc::Rc;
use std::task::{Context, Poll};
use std::time::Duration;

use futures::future::{err, Either, Ready};

use crate::codec::{AsyncRead, AsyncWrite};
use crate::connect::{self, Connect as TcpConnect, Connector as TcpConnector};
use crate::http::{Protocol, Uri};
use crate::service::{apply_fn, boxed, Service};
use crate::util::timeout::{TimeoutError, TimeoutService};

use super::connection::Connection;
use super::error::ConnectError;
use super::pool::ConnectionPool;
use super::Connect;

#[cfg(feature = "openssl")]
use crate::connect::openssl::SslConnector as OpensslConnector;

#[cfg(feature = "rustls")]
use crate::connect::rustls::ClientConfig;
#[cfg(feature = "rustls")]
use std::sync::Arc;

type BoxedConnector =
    boxed::BoxService<TcpConnect<Uri>, (Box<dyn Io>, Protocol), ConnectError>;

/// Manages http client network connectivity.
///
/// The `Connector` type uses a builder-like combinator pattern for service
/// construction that finishes by calling the `.finish()` method.
///
/// ```rust,no_run
/// use std::time::Duration;
/// use ntex::http::client::Connector;
///
/// let connector = Connector::default()
///      .timeout(Duration::from_secs(5))
///      .finish();
/// ```
pub struct Connector {
    timeout: Duration,
    conn_lifetime: Duration,
    conn_keep_alive: Duration,
    disconnect_timeout: Duration,
    limit: usize,
    connector: BoxedConnector,
    ssl_connector: Option<BoxedConnector>,
    #[allow(dead_code)]
    resolver: connect::AsyncResolver,
}

trait Io: AsyncRead + AsyncWrite + Unpin {}
impl<T: AsyncRead + AsyncWrite + Unpin> Io for T {}

impl Default for Connector {
    fn default() -> Self {
        Connector::new(connect::default_resolver())
    }
}

impl Connector {
    pub fn new(resolver: connect::AsyncResolver) -> Connector {
        let conn = Connector {
            connector: boxed::service(
                TcpConnector::new(resolver.clone())
                    .map(|io| (Box::new(io) as Box<dyn Io>, Protocol::Http1))
                    .map_err(ConnectError::from),
            ),
            ssl_connector: None,
            timeout: Duration::from_secs(1),
            conn_lifetime: Duration::from_secs(75),
            conn_keep_alive: Duration::from_secs(15),
            disconnect_timeout: Duration::from_millis(3000),
            limit: 100,
            resolver,
        };

        #[cfg(feature = "openssl")]
        {
            use crate::connect::openssl::SslMethod;

            let mut ssl = OpensslConnector::builder(SslMethod::tls()).unwrap();
            let _ = ssl
                .set_alpn_protos(b"\x02h2\x08http/1.1")
                .map_err(|e| error!("Can not set ALPN protocol: {:?}", e));
            conn.openssl(ssl.build())
        }
        #[cfg(all(not(feature = "openssl"), feature = "rustls"))]
        {
            let protos = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
            let mut config = ClientConfig::new();
            config.set_protocols(&protos);
            config
                .root_store
                .add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
            conn.rustls(Arc::new(config))
        }
        #[cfg(not(any(feature = "openssl", feature = "rustls")))]
        {
            conn
        }
    }
}

impl Connector {
    /// Connection timeout, i.e. max time to connect to remote host including dns name resolution.
    /// Set to 1 second by default.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    #[deprecated(since = "0.1.4", note = "Please use `openssl()` method instead")]
    #[doc(hidden)]
    #[cfg(feature = "openssl")]
    pub fn ssl(self, connector: OpensslConnector) -> Self {
        self.openssl(connector)
    }

    #[cfg(feature = "openssl")]
    /// Use custom `SslConnector` instance.
    pub fn openssl(self, connector: OpensslConnector) -> Self {
        use crate::connect::openssl::OpensslConnector;

        let resolver = self.resolver.clone();

        const H2: &[u8] = b"h2";
        self.secure_connector(OpensslConnector::with_resolver(connector, resolver).map(
            |sock| {
                let h2 = sock
                    .ssl()
                    .selected_alpn_protocol()
                    .map(|protos| protos.windows(2).any(|w| w == H2))
                    .unwrap_or(false);
                if h2 {
                    (sock, Protocol::Http2)
                } else {
                    (sock, Protocol::Http1)
                }
            },
        ))
    }

    #[cfg(feature = "rustls")]
    pub fn rustls(self, connector: Arc<ClientConfig>) -> Self {
        use crate::connect::rustls::{RustlsConnector, Session};

        let resolver = self.resolver.clone();

        const H2: &[u8] = b"h2";
        self.secure_connector(RustlsConnector::with_resolver(connector, resolver).map(
            |sock| {
                let h2 = sock
                    .get_ref()
                    .1
                    .get_alpn_protocol()
                    .map(|protos| protos.windows(2).any(|w| w == H2))
                    .unwrap_or(false);
                if h2 {
                    (Box::new(sock) as Box<dyn Io>, Protocol::Http2)
                } else {
                    (Box::new(sock) as Box<dyn Io>, Protocol::Http1)
                }
            },
        ))
    }

    /// Set total number of simultaneous connections per type of scheme.
    ///
    /// If limit is 0, the connector has no limit.
    /// The default limit size is 100.
    pub fn limit(mut self, limit: usize) -> Self {
        self.limit = limit;
        self
    }

    /// Set keep-alive period for opened connection.
    ///
    /// Keep-alive period is the period between connection usage. If
    /// the delay between repeated usages of the same connection
    /// exceeds this period, the connection is closed.
    /// Default keep-alive period is 15 seconds.
    pub fn keep_alive(mut self, dur: Duration) -> Self {
        self.conn_keep_alive = dur;
        self
    }

    /// Set max lifetime period for connection.
    ///
    /// Connection lifetime is max lifetime of any opened connection
    /// until it is closed regardless of keep-alive period.
    /// Default lifetime period is 75 seconds.
    pub fn lifetime(mut self, dur: Duration) -> Self {
        self.conn_lifetime = dur;
        self
    }

    /// Set server connection disconnect timeout in milliseconds.
    ///
    /// Defines a timeout for disconnect connection. If a disconnect procedure does not complete
    /// within this time, the socket get dropped. This timeout affects only secure connections.
    ///
    /// To disable timeout set value to 0.
    ///
    /// By default disconnect timeout is set to 3000 milliseconds.
    pub fn disconnect_timeout(mut self, dur: Duration) -> Self {
        self.disconnect_timeout = dur;
        self
    }

    /// Use custom connector to open un-secured connections.
    pub fn connector<T, U>(mut self, connector: T) -> Self
    where
        U: AsyncRead + AsyncWrite + Unpin + 'static,
        T: Service<
                Request = TcpConnect<Uri>,
                Response = (U, Protocol),
                Error = crate::connect::ConnectError,
            > + 'static,
    {
        self.connector = boxed::service(
            connector
                .map(|(io, proto)| (Box::new(io) as Box<dyn Io>, proto))
                .map_err(ConnectError::from),
        );
        self
    }

    /// Use custom connector to open secure connections.
    pub fn secure_connector<T, U>(mut self, connector: T) -> Self
    where
        U: AsyncRead + AsyncWrite + Unpin + 'static,
        T: Service<
                Request = TcpConnect<Uri>,
                Response = (U, Protocol),
                Error = crate::connect::ConnectError,
            > + 'static,
    {
        self.ssl_connector = Some(boxed::service(
            connector
                .map(|(io, proto)| (Box::new(io) as Box<dyn Io>, proto))
                .map_err(ConnectError::from),
        ));
        self
    }

    /// Finish configuration process and create connector service.
    /// The Connector builder always concludes by calling `finish()` last in
    /// its combinator chain.
    pub fn finish(
        self,
    ) -> impl Service<Request = Connect, Response = impl Connection, Error = ConnectError>
           + Clone {
        let tcp_service = connector(self.connector, self.timeout);

        let ssl_pool = if let Some(ssl_connector) = self.ssl_connector {
            let srv = connector(ssl_connector, self.timeout);
            Some(ConnectionPool::new(
                srv,
                self.conn_lifetime,
                self.conn_keep_alive,
                Some(self.disconnect_timeout),
                self.limit,
            ))
        } else {
            None
        };

        Rc::new(InnerConnector {
            tcp_pool: ConnectionPool::new(
                tcp_service,
                self.conn_lifetime,
                self.conn_keep_alive,
                None,
                self.limit,
            ),
            ssl_pool,
        })
    }
}

fn connector(
    connector: BoxedConnector,
    timeout: Duration,
) -> impl Service<
    Request = Connect,
    Response = (Box<dyn Io>, Protocol),
    Error = ConnectError,
    Future = impl Unpin,
> + Unpin {
    TimeoutService::new(
        timeout,
        apply_fn(connector, |msg: Connect, srv| {
            srv.call(TcpConnect::new(msg.uri).set_addr(msg.addr))
        })
        .map_err(ConnectError::from),
    )
    .map_err(|e| match e {
        TimeoutError::Service(e) => e,
        TimeoutError::Timeout => ConnectError::Timeout,
    })
}

type Pool<T> = ConnectionPool<T, Box<dyn Io>>;

struct InnerConnector<T> {
    tcp_pool: Pool<T>,
    ssl_pool: Option<Pool<T>>,
}

impl<T> Service for InnerConnector<T>
where
    T: Service<
            Request = Connect,
            Response = (Box<dyn Io>, Protocol),
            Error = ConnectError,
        > + Unpin
        + 'static,
    T::Future: Unpin,
{
    type Request = Connect;
    type Response = <Pool<T> as Service>::Response;
    type Error = ConnectError;
    type Future =
        Either<<Pool<T> as Service>::Future, Ready<Result<Self::Response, Self::Error>>>;

    #[inline]
    fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        let ready = self.tcp_pool.poll_ready(cx)?.is_ready();
        let ready = if let Some(ref ssl_pool) = self.ssl_pool {
            ssl_pool.poll_ready(cx)?.is_ready() && ready
        } else {
            ready
        };
        if ready {
            Poll::Ready(Ok(()))
        } else {
            Poll::Pending
        }
    }

    #[inline]
    fn poll_shutdown(&self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
        let ready = self.tcp_pool.poll_shutdown(cx, is_error).is_ready();
        let ready = if let Some(ref ssl_pool) = self.ssl_pool {
            ssl_pool.poll_shutdown(cx, is_error).is_ready() && ready
        } else {
            ready
        };
        if ready {
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }

    fn call(&self, req: Connect) -> Self::Future {
        match req.uri.scheme_str() {
            Some("https") | Some("wss") => {
                if let Some(ref conn) = self.ssl_pool {
                    Either::Left(conn.call(req))
                } else {
                    Either::Right(err(ConnectError::SslIsNotSupported))
                }
            }
            _ => Either::Left(self.tcp_pool.call(req)),
        }
    }
}