hyperdriver 0.12.8

The missing middle for Hyper - Servers and Clients with ergonomic APIs
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! Tower middleware for collecting connection information after a handshake has been completed.
//!
//! This middleware applies to the request stack, but recieves the connection info from the acceptor stack.

use std::{fmt, task::Poll};

use hyper::{Request, Response};
use tower::{Layer, Service};

use chateau::info::{Address, ConnectionInfo, HasConnectionInfo};
use chateau::services::ServiceRef;

#[cfg(feature = "tls")]
pub use tls::{
    ConnectionWithTlsInfo, MakeServiceTlsConnectionInfoLayer, MakeServiceTlsConnectionInfoService,
};

/// A middleware which adds connection information to the request extensions.
///
/// This layer is meant to be applied to the "make service" part of the stack:
/// ```rust
/// # use std::convert::Infallible;
/// # use hyperdriver::Body;
/// # use hyperdriver::info::ConnectionInfo;
/// # use hyperdriver::server::conn::MakeServiceConnectionInfoLayer;
/// # use tower::Layer;
/// # use std::net::SocketAddr;
/// # use tower::service_fn;
/// use tower::make::Shared;
///
/// # async fn make_service_with_layer() {
///
/// let service = service_fn(|req: http::Request<Body>| async move {
///    let info = req.extensions().get::<ConnectionInfo<SocketAddr>>().unwrap();
///    println!("Connection info: {:?}", info);
///    Ok::<_, Infallible>(http::Response::new(Body::from("Hello, World!")))
/// });
///
/// let make_service = MakeServiceConnectionInfoLayer::default().layer(Shared::new(service));
/// # }
///
///
#[derive(Clone, Default)]
pub struct MakeServiceConnectionInfoLayer {
    _priv: (),
}

impl MakeServiceConnectionInfoLayer {
    /// Create a new `MakeServiceConnectionInfoLayer`.
    pub fn new() -> Self {
        Self { _priv: () }
    }
}

impl fmt::Debug for MakeServiceConnectionInfoLayer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MakeServiceConnectionInfoLayer").finish()
    }
}

impl<S> Layer<S> for MakeServiceConnectionInfoLayer {
    type Service = MakeServiceConnectionInfoService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        MakeServiceConnectionInfoService::new(inner)
    }
}

/// A service which adds connection information to the request extensions.
///
/// This is applied to the "make service" part of the stack.
///
/// See [`MakeServiceConnectionInfoLayer`] for more details.
#[derive(Debug, Clone)]
pub struct MakeServiceConnectionInfoService<C> {
    inner: C,
}

impl<C> MakeServiceConnectionInfoService<C> {
    /// Create a new `StartConnectionInfoService` wrapping `inner` service,
    /// and applying `info` to the request extensions.
    pub fn new(inner: C) -> Self {
        Self { inner }
    }
}

impl<C, IO> Service<&IO> for MakeServiceConnectionInfoService<C>
where
    C: ServiceRef<IO> + Clone + Send + 'static,
    IO: HasConnectionInfo + Send + 'static,
    IO::Addr: Clone + Send + Sync + 'static,
{
    type Response = ConnectionWithInfo<C::Response, IO::Addr>;

    type Error = C::Error;

    type Future = future::MakeServiceConnectionInfoFuture<C, IO>;

    fn poll_ready(
        &mut self,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, stream: &IO) -> Self::Future {
        let inner = self.inner.clone();
        let mut inner = std::mem::replace(&mut self.inner, inner);
        let info = stream.info();
        tracing::trace!("prepared connection info from stream");
        future::MakeServiceConnectionInfoFuture::new(inner.call(stream), info)
    }
}

mod future {

    use pin_project::pin_project;
    use std::future::Future;

    use chateau::services::ServiceRef;

    use super::*;

    #[pin_project]
    #[derive(Debug)]
    pub struct MakeServiceConnectionInfoFuture<S, IO>
    where
        S: ServiceRef<IO>,
        IO: HasConnectionInfo,
    {
        #[pin]
        inner: S::Future,
        info: Option<ConnectionInfo<IO::Addr>>,
    }

    impl<S, IO> MakeServiceConnectionInfoFuture<S, IO>
    where
        S: ServiceRef<IO>,
        IO: HasConnectionInfo,
    {
        pub(super) fn new(inner: S::Future, info: ConnectionInfo<IO::Addr>) -> Self {
            Self {
                inner,
                info: Some(info),
            }
        }
    }

    impl<S, IO> Future for MakeServiceConnectionInfoFuture<S, IO>
    where
        S: ServiceRef<IO>,
        IO: HasConnectionInfo,
    {
        type Output = Result<ConnectionWithInfo<S::Response, IO::Addr>, S::Error>;

        fn poll(
            self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> Poll<Self::Output> {
            let this = self.project();

            match this.inner.poll(cx) {
                Poll::Ready(Ok(inner)) => Poll::Ready(Ok(ConnectionWithInfo {
                    inner,
                    info: this.info.take(),
                })),
                Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
                Poll::Pending => Poll::Pending,
            }
        }
    }
}

/// Interior service which adds connection information to the request extensions.
///
/// This service wraps the request/response service, not the connector service.
#[derive(Debug, Clone)]
pub struct ConnectionWithInfo<S, A> {
    inner: S,
    info: Option<ConnectionInfo<A>>,
}

impl<S, A, BIn, BOut> Service<Request<BIn>> for ConnectionWithInfo<S, A>
where
    S: Service<Request<BIn>, Response = Response<BOut>> + Clone + Send + 'static,
    S::Future: Send,
    S::Error: fmt::Display,
    BIn: Send + 'static,
    A: Address + Send + Sync + 'static,
{
    type Response = S::Response;
    type Error = S::Error;
    type Future = S::Future;

    fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, mut req: Request<BIn>) -> Self::Future {
        let next = self.inner.clone();
        let mut inner = std::mem::replace(&mut self.inner, next);

        if let Some(info) = self.info.take() {
            tracing::trace!(
                "Inserting connection info {}",
                std::any::type_name_of_val(&info),
            );
            req.extensions_mut().insert(info.erase());
        } else {
            tracing::error!("Connection called twice, info is not available");
        }
        inner.call(req)
    }
}

/// Tower middleware for collecting TLS connection information after a
/// handshake has been completed, and attaching it to request extensions.
///
/// This is analogous to [`MakeServiceConnectionInfoLayer`] and
/// [`ConnectionWithInfo`], but for [`chateau::info::TlsConnectionInfo`].
///
/// It exists because `chateau::server::conn::tls::info::TlsConnectionInfoLayer`
/// is generic over the request type, and so has no way to insert the TLS
/// connection information into request extensions. This layer is specialized
/// to `hyper::Request`/`hyper::Response`, so it can do exactly that.
#[cfg(feature = "tls")]
mod tls {
    use std::{fmt, task::Poll};

    use hyper::{Request, Response};
    use tower::{Layer, Service};

    use chateau::info::tls::TlsConnectionInfoReceiver;
    use chateau::services::ServiceRef;
    use chateau::stream::tls::TlsHandshakeInfo;

    use crate::BoxFuture;

    /// A middleware which adds TLS connection information to the request extensions.
    ///
    /// This layer is meant to be applied to the "make service" part of the stack, after
    /// the request/response service has already been established (for example, via
    /// [`crate::server::ServerConnectionInfoExt::with_tls_connection_info`]).
    #[derive(Clone, Default)]
    pub struct MakeServiceTlsConnectionInfoLayer {
        _priv: (),
    }

    impl MakeServiceTlsConnectionInfoLayer {
        /// Create a new `MakeServiceTlsConnectionInfoLayer`.
        pub fn new() -> Self {
            Self { _priv: () }
        }
    }

    impl fmt::Debug for MakeServiceTlsConnectionInfoLayer {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.debug_struct("MakeServiceTlsConnectionInfoLayer").finish()
        }
    }

    impl<S> Layer<S> for MakeServiceTlsConnectionInfoLayer {
        type Service = MakeServiceTlsConnectionInfoService<S>;

        fn layer(&self, inner: S) -> Self::Service {
            MakeServiceTlsConnectionInfoService::new(inner)
        }
    }

    /// A service which adds TLS connection information to the request extensions.
    ///
    /// This is applied to the "make service" part of the stack.
    ///
    /// See [`MakeServiceTlsConnectionInfoLayer`] for more details.
    #[derive(Debug, Clone)]
    pub struct MakeServiceTlsConnectionInfoService<C> {
        inner: C,
    }

    impl<C> MakeServiceTlsConnectionInfoService<C> {
        /// Create a new `MakeServiceTlsConnectionInfoService` wrapping `inner` service.
        pub fn new(inner: C) -> Self {
            Self { inner }
        }
    }

    impl<C, IO> Service<&IO> for MakeServiceTlsConnectionInfoService<C>
    where
        C: ServiceRef<IO> + Clone + Send + 'static,
        IO: TlsHandshakeInfo,
    {
        type Response = ConnectionWithTlsInfo<C::Response>;

        type Error = C::Error;

        type Future = future::MakeServiceTlsConnectionInfoFuture<C, IO>;

        fn poll_ready(
            &mut self,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<Result<(), Self::Error>> {
            self.inner.poll_ready(cx)
        }

        fn call(&mut self, stream: &IO) -> Self::Future {
            let inner = self.inner.clone();
            let mut inner = std::mem::replace(&mut self.inner, inner);
            let rx = stream.recv();
            tracing::trace!("captured TLS connection info receiver from stream");
            future::MakeServiceTlsConnectionInfoFuture::new(inner.call(stream), rx)
        }
    }

    mod future {
        use std::{future::Future, task::Poll};

        use pin_project::pin_project;

        use chateau::info::tls::TlsConnectionInfoReceiver;
        use chateau::services::ServiceRef;

        use super::ConnectionWithTlsInfo;

        #[pin_project]
        #[derive(Debug)]
        pub struct MakeServiceTlsConnectionInfoFuture<S, IO>
        where
            S: ServiceRef<IO>,
        {
            #[pin]
            inner: S::Future,
            rx: Option<TlsConnectionInfoReceiver>,
        }

        impl<S, IO> MakeServiceTlsConnectionInfoFuture<S, IO>
        where
            S: ServiceRef<IO>,
        {
            pub(super) fn new(inner: S::Future, rx: TlsConnectionInfoReceiver) -> Self {
                Self {
                    inner,
                    rx: Some(rx),
                }
            }
        }

        impl<S, IO> Future for MakeServiceTlsConnectionInfoFuture<S, IO>
        where
            S: ServiceRef<IO>,
        {
            type Output = Result<ConnectionWithTlsInfo<S::Response>, S::Error>;

            fn poll(
                self: std::pin::Pin<&mut Self>,
                cx: &mut std::task::Context<'_>,
            ) -> Poll<Self::Output> {
                let this = self.project();

                match this.inner.poll(cx) {
                    Poll::Ready(Ok(inner)) => Poll::Ready(Ok(ConnectionWithTlsInfo {
                        inner,
                        rx: this.rx.take().expect("future polled after completion"),
                    })),
                    Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
                    Poll::Pending => Poll::Pending,
                }
            }
        }
    }

    /// Interior service which adds TLS connection information to the request extensions.
    ///
    /// This service wraps the request/response service, not the connector service.
    #[derive(Debug, Clone)]
    pub struct ConnectionWithTlsInfo<S> {
        inner: S,
        rx: TlsConnectionInfoReceiver,
    }

    impl<S, BIn, BOut> Service<Request<BIn>> for ConnectionWithTlsInfo<S>
    where
        S: Service<Request<BIn>, Response = Response<BOut>> + Clone + Send + 'static,
        S::Future: Send,
        S::Error: fmt::Display,
        BIn: Send + 'static,
    {
        type Response = S::Response;
        type Error = S::Error;
        type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

        fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
            self.inner.poll_ready(cx)
        }

        fn call(&mut self, mut req: Request<BIn>) -> Self::Future {
            let rx = self.rx.clone();
            let next = self.inner.clone();
            let mut inner = std::mem::replace(&mut self.inner, next);

            Box::pin(async move {
                match rx.recv().await {
                    Some(info) => {
                        tracing::trace!(?info, "inserting TLS connection info");
                        req.extensions_mut().insert(info);
                    }
                    None => {
                        tracing::trace!(
                            "no TLS connection info available for this request (non-TLS connection?)"
                        );
                    }
                }
                inner.call(req).await
            })
        }
    }
}

#[cfg(test)]
mod tests {

    use std::convert::Infallible;

    use tower::{ServiceBuilder, make::Shared};

    use crate::{info::DuplexAddr, server::conn::AcceptExt as _};

    use super::*;

    #[tokio::test]
    async fn connection_info_from_service() {
        let service = tower::service_fn(|req: http::Request<crate::Body>| {
            let info = req.extensions().get::<ConnectionInfo>().unwrap();
            assert_eq!(
                info.remote_addr_as::<DuplexAddr>(),
                Some(&DuplexAddr::new())
            );
            async { Ok::<_, Infallible>(Response::new(())) }
        });

        let mut make_service = ServiceBuilder::new()
            .layer(MakeServiceConnectionInfoLayer::new())
            .service(Shared::new(service));

        let (client, incoming) = chateau::stream::duplex::pair();

        let (_, conn) = tokio::try_join!(client.connect(1024), incoming.accept()).unwrap();

        let req = http::Request::new(crate::Body::empty());
        let mut svc = tower::Service::call(&mut make_service, &conn)
            .await
            .unwrap();

        svc.call(req).await.unwrap();
    }

    #[cfg(all(feature = "tls", feature = "client", feature = "stream"))]
    #[tokio::test]
    async fn tls_connection_info_from_service() {
        use chateau::client::conn::Transport as _;
        use chateau::client::conn::transport::duplex::DuplexTransport;
        use chateau::info::TlsConnectionInfo;
        use chateau::stream::tls::TlsHandshakeStream as _;

        use crate::client::conn::HttpTlsTransport;

        crate::fixtures::tls_install_default();

        let service = tower::service_fn(|req: http::Request<crate::Body>| {
            let info = req.extensions().get::<TlsConnectionInfo>().unwrap();
            assert!(matches!(
                info.alpn.as_deref(),
                Some("h2") | Some("http/1.1")
            ));
            async { Ok::<_, Infallible>(Response::new(())) }
        });

        let mut make_service = ServiceBuilder::new()
            .layer(MakeServiceTlsConnectionInfoLayer::new())
            .service(Shared::new(service));

        let (client, incoming) = chateau::stream::duplex::pair();

        let acceptor = crate::server::conn::Acceptor::from(incoming)
            .with_tls(crate::fixtures::tls_server_config().into());

        let mut transport = HttpTlsTransport::new(
            DuplexTransport::new(1024, client),
            crate::fixtures::tls_client_config().into(),
        );

        let req = http::Request::get("https://example.com").body(()).unwrap();

        let client_task = async move {
            let mut stream = transport.connect(&req).await.unwrap();
            stream.finish_handshake().await.unwrap();
            stream
        };

        let server_task = async move {
            let mut conn = acceptor.accept().await.unwrap();

            let mut svc = tower::Service::call(&mut make_service, &conn)
                .await
                .unwrap();

            conn.finish_handshake().await.unwrap();

            let req = http::Request::new(crate::Body::empty());
            svc.call(req).await.unwrap();
            conn
        };

        let (stream, conn) = tokio::join!(client_task, server_task);
        drop((stream, conn));
    }
}