Skip to main content

rama/http/client/proxy_connector/
not_enabled.rs

1use crate::{
2    Service,
3    error::{BoxError, BoxErrorExt as _},
4    extensions::{Extensions, ExtensionsRef},
5    http::{Request, Response, io::upgrade::Upgraded},
6    net::client::{ConnectionError, ConnectionErrorKind, EstablishedClientConnection},
7};
8use std::{fmt, marker::PhantomData};
9
10#[cfg(doc)]
11use super::ProxyConnector;
12
13/// A proxy method a [`ProxyConnector`] was not given, which refuses the routes asking for
14/// it: a client serves a proxied route the way it was asked to, or not at all.
15///
16/// `Over` is the connector towards proxies the method would have been built on, so a slot
17/// has the same shape whether or not its method is enabled.
18pub struct ProxyMethodNotEnabled<Over = ()> {
19    reason: &'static str,
20    _phantom: PhantomData<fn() -> Over>,
21}
22
23impl<Over> ProxyMethodNotEnabled<Over> {
24    pub(in crate::http::client) const SOCKS5: Self = Self::new("socks5 proxying is not enabled");
25    pub(in crate::http::client) const HTTP_CONNECT: Self =
26        Self::new("http connect proxying is not enabled");
27    pub(in crate::http::client) const HTTP_CONNECT_UDP: Self =
28        Self::new("http connect-udp proxying is not enabled");
29    pub(in crate::http::client) const HTTP_FORWARD: Self =
30        Self::new("http forward proxying is not enabled");
31
32    const fn new(reason: &'static str) -> Self {
33        Self {
34            reason,
35            _phantom: PhantomData,
36        }
37    }
38}
39
40impl<Over> fmt::Debug for ProxyMethodNotEnabled<Over> {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        f.debug_struct("ProxyMethodNotEnabled")
43            .field("reason", &self.reason)
44            .finish()
45    }
46}
47
48impl<Over> Clone for ProxyMethodNotEnabled<Over> {
49    fn clone(&self) -> Self {
50        *self
51    }
52}
53
54impl<Over> Copy for ProxyMethodNotEnabled<Over> {}
55
56impl<Over: 'static, Input: Send + 'static> Service<Input> for ProxyMethodNotEnabled<Over> {
57    type Output = EstablishedClientConnection<NoProxyConnection, Input>;
58    type Error = ConnectionError;
59
60    async fn serve(&self, _: Input) -> Result<Self::Output, Self::Error> {
61        Err(ConnectionError::local(
62            BoxError::from_static_str(self.reason),
63            ConnectionErrorKind::Unavailable,
64        ))
65    }
66}
67
68/// The connection of a proxy method which is not enabled: it is never established.
69#[derive(Debug, Clone)]
70pub enum NoProxyConnection {}
71
72impl From<NoProxyConnection> for Upgraded {
73    fn from(conn: NoProxyConnection) -> Self {
74        match conn {}
75    }
76}
77
78impl ExtensionsRef for NoProxyConnection {
79    fn extensions(&self) -> &Extensions {
80        match *self {}
81    }
82}
83
84impl<Body: Send + 'static> Service<Request<Body>> for NoProxyConnection {
85    type Output = Response;
86    type Error = BoxError;
87
88    async fn serve(&self, _: Request<Body>) -> Result<Self::Output, Self::Error> {
89        match *self {}
90    }
91}
92
93#[cfg(feature = "udp")]
94#[warn(clippy::missing_trait_methods)]
95impl crate::net::stream::Socket for NoProxyConnection {
96    fn local_addr(&self) -> std::io::Result<crate::net::address::SocketAddress> {
97        match *self {}
98    }
99
100    fn peer_addr(&self) -> std::io::Result<crate::net::address::SocketAddress> {
101        match *self {}
102    }
103}
104
105#[cfg(feature = "udp")]
106#[warn(clippy::missing_trait_methods)]
107impl crate::udp::DatagramSocket for NoProxyConnection {
108    type Sender = Self;
109
110    fn create_sender(&self) -> Self::Sender {
111        match *self {}
112    }
113
114    fn poll_recv(
115        &mut self,
116        _: &mut std::task::Context<'_>,
117        _: &mut [std::io::IoSliceMut<'_>],
118        _: &mut [crate::udp::DatagramMetadata],
119    ) -> std::task::Poll<Result<usize, crate::udp::DatagramError>> {
120        match *self {}
121    }
122
123    fn capabilities(&self) -> crate::udp::DatagramCapabilities {
124        match *self {}
125    }
126}
127
128#[cfg(feature = "udp")]
129#[warn(clippy::missing_trait_methods)]
130impl crate::udp::DatagramSender for NoProxyConnection {
131    fn poll_send(
132        &mut self,
133        _: &mut std::task::Context<'_>,
134        _: &crate::udp::SendDatagram<'_>,
135    ) -> std::task::Poll<Result<(), crate::udp::DatagramError>> {
136        match *self {}
137    }
138
139    fn poll_send_batch(
140        &mut self,
141        _: &mut std::task::Context<'_>,
142        _: &[crate::udp::SendDatagram<'_>],
143    ) -> std::task::Poll<Result<usize, crate::udp::DatagramError>> {
144        match *self {}
145    }
146
147    fn capabilities(&self) -> crate::udp::DatagramCapabilities {
148        match *self {}
149    }
150}
151
152#[warn(clippy::missing_trait_methods)]
153impl tokio::io::AsyncRead for NoProxyConnection {
154    fn poll_read(
155        self: std::pin::Pin<&mut Self>,
156        _: &mut std::task::Context<'_>,
157        _: &mut tokio::io::ReadBuf<'_>,
158    ) -> std::task::Poll<std::io::Result<()>> {
159        match *self {}
160    }
161}
162
163#[warn(clippy::missing_trait_methods)]
164impl tokio::io::AsyncWrite for NoProxyConnection {
165    fn poll_write(
166        self: std::pin::Pin<&mut Self>,
167        _: &mut std::task::Context<'_>,
168        _: &[u8],
169    ) -> std::task::Poll<std::io::Result<usize>> {
170        match *self {}
171    }
172
173    fn poll_flush(
174        self: std::pin::Pin<&mut Self>,
175        _: &mut std::task::Context<'_>,
176    ) -> std::task::Poll<std::io::Result<()>> {
177        match *self {}
178    }
179
180    fn poll_shutdown(
181        self: std::pin::Pin<&mut Self>,
182        _: &mut std::task::Context<'_>,
183    ) -> std::task::Poll<std::io::Result<()>> {
184        match *self {}
185    }
186
187    fn poll_write_vectored(
188        self: std::pin::Pin<&mut Self>,
189        _: &mut std::task::Context<'_>,
190        _: &[std::io::IoSlice<'_>],
191    ) -> std::task::Poll<std::io::Result<usize>> {
192        match *self {}
193    }
194
195    fn is_write_vectored(&self) -> bool {
196        match *self {}
197    }
198}