]> git.proxmox.com Git - proxmox.git/commitdiff
rest-server: PeerAddr trait, drop proxmox-http dep
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Tue, 24 Jan 2023 10:15:22 +0000 (11:15 +0100)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Thu, 2 Mar 2023 14:44:35 +0000 (15:44 +0100)
We pulled in proxmox-http with the client feature solely to
implement the `Service` trait on
`SslStream<RateLimitedStream<TcpStream>>`.

All those `Service` impls are the same: provide a peer
address and return an `ApiService`.
Let's put the `peer_addr()` call into a trait and build from
there.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
proxmox-rest-server/Cargo.toml
proxmox-rest-server/src/rest.rs

index 8238c1d249d44ec9008cdcb0d6e807205fa10c26..244739274239bf9c1d9a3785c71cd19707a05986 100644 (file)
@@ -38,7 +38,6 @@ proxmox-async.workspace = true
 proxmox-compression.workspace = true
 proxmox-io.workspace = true
 proxmox-lang.workspace = true
-proxmox-http = { workspace = true, features = [ "client" ] }
 proxmox-router.workspace = true
 proxmox-schema = { workspace = true, features = [ "api-macro", "upid-api-impl" ] }
 proxmox-time.workspace = true
index 96c35f090574ceb48b3bc77c14e8f9a6b058260d..57e97623ce7231c34a99d4b21583fa791f8d1ca0 100644 (file)
@@ -28,8 +28,6 @@ use proxmox_router::{
 };
 use proxmox_schema::{ObjectSchemaType, ParameterSchema};
 
-use proxmox_http::client::RateLimitedStream;
-
 use proxmox_async::stream::AsyncReaderStream;
 use proxmox_compression::{DeflateEncoder, Level};
 
@@ -78,9 +76,7 @@ impl RestServer {
     }
 }
 
-impl Service<&Pin<Box<tokio_openssl::SslStream<RateLimitedStream<tokio::net::TcpStream>>>>>
-    for RestServer
-{
+impl<T: PeerAddress> Service<&T> for RestServer {
     type Response = ApiService;
     type Error = Error;
     type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
@@ -89,82 +85,45 @@ impl Service<&Pin<Box<tokio_openssl::SslStream<RateLimitedStream<tokio::net::Tcp
         Poll::Ready(Ok(()))
     }
 
-    fn call(
-        &mut self,
-        ctx: &Pin<Box<tokio_openssl::SslStream<RateLimitedStream<tokio::net::TcpStream>>>>,
-    ) -> Self::Future {
-        match ctx.get_ref().peer_addr() {
-            Err(err) => future::err(format_err!("unable to get peer address - {}", err)).boxed(),
-            Ok(peer) => future::ok(ApiService {
+    fn call(&mut self, ctx: &T) -> Self::Future {
+        let result = match ctx.peer_addr() {
+            Err(err) => Err(format_err!("unable to get peer address - {}", err)),
+            Ok(peer) => Ok(ApiService {
                 peer,
                 api_config: self.api_config.clone(),
-            })
-            .boxed(),
-        }
+            }),
+        };
+        Box::pin(async move { result })
     }
 }
 
-impl Service<&Pin<Box<tokio_openssl::SslStream<tokio::net::TcpStream>>>> for RestServer {
-    type Response = ApiService;
-    type Error = Error;
-    type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
-
-    fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
-        Poll::Ready(Ok(()))
-    }
-
-    fn call(
-        &mut self,
-        ctx: &Pin<Box<tokio_openssl::SslStream<tokio::net::TcpStream>>>,
-    ) -> Self::Future {
-        match ctx.get_ref().peer_addr() {
-            Err(err) => future::err(format_err!("unable to get peer address - {}", err)).boxed(),
-            Ok(peer) => future::ok(ApiService {
-                peer,
-                api_config: self.api_config.clone(),
-            })
-            .boxed(),
-        }
-    }
+pub trait PeerAddress {
+    fn peer_addr(&self) -> Result<std::net::SocketAddr, Error>;
 }
 
-impl Service<&hyper::server::conn::AddrStream> for RestServer {
-    type Response = ApiService;
-    type Error = Error;
-    type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
-
-    fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
-        Poll::Ready(Ok(()))
+impl<T: PeerAddress> PeerAddress for tokio_openssl::SslStream<T> {
+    fn peer_addr(&self) -> Result<std::net::SocketAddr, Error> {
+        self.get_ref().peer_addr()
     }
+}
 
-    fn call(&mut self, ctx: &hyper::server::conn::AddrStream) -> Self::Future {
-        let peer = ctx.remote_addr();
-        future::ok(ApiService {
-            peer,
-            api_config: self.api_config.clone(),
-        })
-        .boxed()
+impl PeerAddress for tokio::net::TcpStream {
+    fn peer_addr(&self) -> Result<std::net::SocketAddr, Error> {
+        Ok(self.peer_addr()?)
     }
 }
 
-impl Service<&tokio::net::UnixStream> for RestServer {
-    type Response = ApiService;
-    type Error = Error;
-    type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
-
-    fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
-        Poll::Ready(Ok(()))
+impl PeerAddress for hyper::server::conn::AddrStream {
+    fn peer_addr(&self) -> Result<std::net::SocketAddr, Error> {
+        Ok(self.remote_addr())
     }
+}
 
-    fn call(&mut self, _ctx: &tokio::net::UnixStream) -> Self::Future {
+impl PeerAddress for tokio::net::UnixStream {
+    fn peer_addr(&self) -> Result<std::net::SocketAddr, Error> {
         // TODO: Find a way to actually represent the vsock peer in the ApiService struct - for now
         // it doesn't really matter, so just use a fake IP address
-        let fake_peer = "0.0.0.0:807".parse().unwrap();
-        future::ok(ApiService {
-            peer: fake_peer,
-            api_config: self.api_config.clone(),
-        })
-        .boxed()
+        Ok(([0, 0, 0, 0], 807).into())
     }
 }