From cc409f853b0c627fc7d5c4cbb895c4195d88ebaa Mon Sep 17 00:00:00 2001 From: Aram Peres Date: Thu, 5 Aug 2021 01:38:27 -0400 Subject: [PATCH] Add debug to blocking --- rups/src/blocking/client.rs | 8 +++++++- rups/src/blocking/stream.rs | 25 +++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/rups/src/blocking/client.rs b/rups/src/blocking/client.rs index 9fe00b9..4d8fae7 100644 --- a/rups/src/blocking/client.rs +++ b/rups/src/blocking/client.rs @@ -24,9 +24,15 @@ impl Client { let tcp_stream = TcpStream::connect_timeout(&host.addr, config.timeout)?; let mut client = Client { config: config.clone(), - stream: ConnectionStream::Tcp(tcp_stream).buffered(), + stream: ConnectionStream::Tcp(tcp_stream), }; + if config.debug { + client.stream = client.stream.debug(); + } + + client.stream = client.stream.buffered(); + client = client.enable_ssl()?; Ok(client) diff --git a/rups/src/blocking/stream.rs b/rups/src/blocking/stream.rs index db0d1c6..522102b 100644 --- a/rups/src/blocking/stream.rs +++ b/rups/src/blocking/stream.rs @@ -15,6 +15,11 @@ pub enum ConnectionStream { /// It can then be un-wrapped with `.unbuffered()`. Buffered(Box>), + /// A stream wrapped with a debug logging mechanism. + /// + /// Use `.debug()` to wrap any stream with this wrapper. + Debug(Box), + /// A client stream wrapped with SSL using `rustls`. #[cfg(feature = "ssl")] SslClient(Box>), @@ -129,7 +134,7 @@ impl ConnectionStream { } /// Wraps the current stream with a `BufReader`. - pub fn buffered(self) -> ConnectionStream { + pub fn buffered(self) -> Self { Self::Buffered(Box::new(BufReader::new(self))) } @@ -137,13 +142,18 @@ impl ConnectionStream { /// If the current stream is not buffered, it returns itself (no-op). /// /// Note that, if the stream is buffered, any un-consumed data will be discarded. - pub fn unbuffered(self) -> ConnectionStream { + pub fn unbuffered(self) -> Self { if let Self::Buffered(buf) = self { buf.into_inner() } else { self } } + + /// Wraps the current stream with a debug logging mechanism. + pub fn debug(self) -> Self { + Self::Debug(Box::new(self)) + } } impl Read for ConnectionStream { @@ -151,6 +161,11 @@ impl Read for ConnectionStream { match self { Self::Tcp(stream) => stream.read(buf), Self::Buffered(reader) => reader.read(buf), + Self::Debug(stream) => { + let str = String::from_utf8_lossy(buf).trim().to_string(); + println!("DEBUG::READ <- '{}'", str); // TODO: Replace with logger? + stream.read(buf) + } #[cfg(feature = "ssl")] Self::SslClient(stream) => stream.read(buf), #[cfg(feature = "ssl")] @@ -184,6 +199,11 @@ impl Write for ConnectionStream { match self { Self::Tcp(stream) => stream.write(buf), Self::Buffered(reader) => reader.get_mut().write(buf), + Self::Debug(stream) => { + let str = String::from_utf8_lossy(buf).trim().to_string(); + println!("DEBUG::WRITE -> '{}'", str); // TODO: Replace with logger? + stream.write(buf) + } #[cfg(feature = "ssl")] Self::SslClient(stream) => stream.write(buf), #[cfg(feature = "ssl")] @@ -201,6 +221,7 @@ impl Write for ConnectionStream { match self { Self::Tcp(stream) => stream.flush(), Self::Buffered(reader) => reader.get_mut().flush(), + Self::Debug(stream) => stream.flush(), #[cfg(feature = "ssl")] Self::SslClient(stream) => stream.flush(), #[cfg(feature = "ssl")]