aws_smithy_http_server/routing/into_make_service_with_connect_info.rs
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//! The [`IntoMakeServiceWithConnectInfo`] is a service factory which adjoins [`ConnectInfo`] to the requests.
36
37use std::{
38 convert::Infallible,
39 fmt,
40 future::ready,
41 marker::PhantomData,
42 net::SocketAddr,
43 task::{Context, Poll},
44};
45
46use hyper::server::conn::AddrStream;
47use tower::{Layer, Service};
48use tower_http::add_extension::{AddExtension, AddExtensionLayer};
49
50use crate::request::connect_info::ConnectInfo;
51
52/// A [`MakeService`] used to insert [`ConnectInfo<T>`] into [`http::Request`]s.
53///
54/// The `T` must be derivable from the underlying IO resource using the [`Connected`] trait.
55///
56/// [`MakeService`]: tower::make::MakeService
57pub struct IntoMakeServiceWithConnectInfo<S, C> {
58 inner: S,
59 _connect_info: PhantomData<fn() -> C>,
60}
61
62impl<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
71impl<S, C> fmt::Debug for IntoMakeServiceWithConnectInfo<S, C>
72where
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
82impl<S, C> Clone for IntoMakeServiceWithConnectInfo<S, C>
83where
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.
99pub trait Connected<T>: Clone {
100 /// Create type holding information about the connection.
101 fn connect_info(target: T) -> Self;
102}
103
104impl Connected<&AddrStream> for SocketAddr {
105 fn connect_info(target: &AddrStream) -> Self {
106 target.remote_addr()
107 }
108}
109
110impl<S, C, T> Service<T> for IntoMakeServiceWithConnectInfo<S, C>
111where
112 S: Clone,
113 C: Connected<T>,
114{
115 type Response = AddExtension<S, ConnectInfo<C>>;
116 type Error = Infallible;
117 type Future = ResponseFuture<S, C>;
118
119 #[inline]
120 fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
121 Poll::Ready(Ok(()))
122 }
123
124 fn call(&mut self, target: T) -> Self::Future {
125 let connect_info = ConnectInfo(C::connect_info(target));
126 let svc = AddExtensionLayer::new(connect_info).layer(self.inner.clone());
127 ResponseFuture::new(ready(Ok(svc)))
128 }
129}
130
131opaque_future! {
132 /// Response future for [`IntoMakeServiceWithConnectInfo`].
133 pub type ResponseFuture<S, C> =
134 std::future::Ready<Result<AddExtension<S, ConnectInfo<C>>, Infallible>>;
135}