|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +// This code was copied and then modified from Tokio's Axum. |
| 7 | + |
| 8 | +/* Copyright (c) 2021 Tower Contributors |
| 9 | + * |
| 10 | + * Permission is hereby granted, free of charge, to any |
| 11 | + * person obtaining a copy of this software and associated |
| 12 | + * documentation files (the "Software"), to deal in the |
| 13 | + * Software without restriction, including without |
| 14 | + * limitation the rights to use, copy, modify, merge, |
| 15 | + * publish, distribute, sublicense, and/or sell copies of |
| 16 | + * the Software, and to permit persons to whom the Software |
| 17 | + * is furnished to do so, subject to the following |
| 18 | + * conditions: |
| 19 | + * |
| 20 | + * The above copyright notice and this permission notice |
| 21 | + * shall be included in all copies or substantial portions |
| 22 | + * of the Software. |
| 23 | + * |
| 24 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF |
| 25 | + * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 26 | + * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 27 | + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 28 | + * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 29 | + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 30 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR |
| 31 | + * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 32 | + * DEALINGS IN THE SOFTWARE. |
| 33 | + */ |
| 34 | + |
| 35 | +use std::{ |
| 36 | + convert::Infallible, |
| 37 | + fmt, |
| 38 | + future::ready, |
| 39 | + marker::PhantomData, |
| 40 | + net::SocketAddr, |
| 41 | + task::{Context, Poll}, |
| 42 | +}; |
| 43 | + |
| 44 | +use http::request::Parts; |
| 45 | +use hyper::server::conn::AddrStream; |
| 46 | +use tower::{Layer, Service}; |
| 47 | +use tower_http::add_extension::{AddExtension, AddExtensionLayer}; |
| 48 | + |
| 49 | +use crate::{request::FromParts, Extension}; |
| 50 | + |
| 51 | +/// A [`MakeService`] created from a router. |
| 52 | +/// |
| 53 | +/// See [`Router::into_make_service_with_connect_info`] for more details. |
| 54 | +/// |
| 55 | +/// [`MakeService`]: tower::make::MakeService |
| 56 | +/// [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info |
| 57 | +pub struct IntoMakeServiceWithConnectInfo<S, C> { |
| 58 | + inner: S, |
| 59 | + _connect_info: PhantomData<fn() -> C>, |
| 60 | +} |
| 61 | + |
| 62 | +impl<S, C> IntoMakeServiceWithConnectInfo<S, C> { |
| 63 | + pub fn new(svc: S) -> Self { |
| 64 | + Self { |
| 65 | + inner: svc, |
| 66 | + _connect_info: PhantomData, |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl<S, C> fmt::Debug for IntoMakeServiceWithConnectInfo<S, C> |
| 72 | +where |
| 73 | + S: fmt::Debug, |
| 74 | +{ |
| 75 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 76 | + f.debug_struct("IntoMakeServiceWithConnectInfo") |
| 77 | + .field("inner", &self.inner) |
| 78 | + .finish() |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl<S, C> Clone for IntoMakeServiceWithConnectInfo<S, C> |
| 83 | +where |
| 84 | + S: Clone, |
| 85 | +{ |
| 86 | + fn clone(&self) -> Self { |
| 87 | + Self { |
| 88 | + inner: self.inner.clone(), |
| 89 | + _connect_info: PhantomData, |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/// Trait that connected IO resources implement and use to produce information |
| 95 | +/// about the connection. |
| 96 | +/// |
| 97 | +/// The goal for this trait is to allow users to implement custom IO types that |
| 98 | +/// can still provide the same connection metadata. |
| 99 | +/// |
| 100 | +/// See [`Router::into_make_service_with_connect_info`] for more details. |
| 101 | +/// |
| 102 | +/// [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info |
| 103 | +pub trait Connected<T>: Clone { |
| 104 | + /// Create type holding information about the connection. |
| 105 | + fn connect_info(target: T) -> Self; |
| 106 | +} |
| 107 | + |
| 108 | +impl Connected<&AddrStream> for SocketAddr { |
| 109 | + fn connect_info(target: &AddrStream) -> Self { |
| 110 | + target.remote_addr() |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +impl<S, C, T> Service<T> for IntoMakeServiceWithConnectInfo<S, C> |
| 115 | +where |
| 116 | + S: Clone, |
| 117 | + C: Connected<T>, |
| 118 | +{ |
| 119 | + type Response = AddExtension<S, ConnectInfo<C>>; |
| 120 | + type Error = Infallible; |
| 121 | + type Future = ResponseFuture<S, C>; |
| 122 | + |
| 123 | + #[inline] |
| 124 | + fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
| 125 | + Poll::Ready(Ok(())) |
| 126 | + } |
| 127 | + |
| 128 | + fn call(&mut self, target: T) -> Self::Future { |
| 129 | + let connect_info = ConnectInfo(C::connect_info(target)); |
| 130 | + let svc = AddExtensionLayer::new(connect_info).layer(self.inner.clone()); |
| 131 | + ResponseFuture::new(ready(Ok(svc))) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +opaque_future! { |
| 136 | + /// Response future for [`IntoMakeServiceWithConnectInfo`]. |
| 137 | + pub type ResponseFuture<S, C> = |
| 138 | + std::future::Ready<Result<AddExtension<S, ConnectInfo<C>>, Infallible>>; |
| 139 | +} |
| 140 | + |
| 141 | +/// Extractor for getting connection information produced by a `Connected`. |
| 142 | +/// |
| 143 | +/// Note this extractor requires you to use |
| 144 | +/// [`Router::into_make_service_with_connect_info`] to run your app |
| 145 | +/// otherwise it will fail at runtime. |
| 146 | +/// |
| 147 | +/// See [`Router::into_make_service_with_connect_info`] for more details. |
| 148 | +/// |
| 149 | +/// [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info |
| 150 | +#[derive(Clone, Debug)] |
| 151 | +pub struct ConnectInfo<T>(pub T); |
| 152 | + |
| 153 | +impl<P, T> FromParts<P> for ConnectInfo<T> |
| 154 | +where |
| 155 | + T: Send + Sync + 'static, |
| 156 | +{ |
| 157 | + type Rejection = <Extension<Self> as FromParts<P>>::Rejection; |
| 158 | + |
| 159 | + fn from_parts(parts: &mut Parts) -> Result<Self, Self::Rejection> { |
| 160 | + let Extension(connect_info) = <Extension<Self> as FromParts<P>>::from_parts(parts)?; |
| 161 | + Ok(connect_info) |
| 162 | + } |
| 163 | +} |
0 commit comments