From ef2e13df274aaf0f2e50c03c63db33d65d9a0275 Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 28 Oct 2026 22:38:54 +0100 Subject: v0.2.6: preserve buffered lines and add request read timeouts --- src/bin/server/tls.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'src/bin/server') diff --git a/src/bin/server/tls.rs b/src/bin/server/tls.rs index acf3315..62a25db 100644 --- a/src/bin/server/tls.rs +++ b/src/bin/server/tls.rs @@ -1,6 +1,8 @@ use std::io::{Read, Write}; use std::net::TcpStream; +use zagan_bet::protocol::LineBuffer; + #[derive(Clone)] pub struct TlsServer { acceptor: native_tls::TlsAcceptor, @@ -42,27 +44,25 @@ impl TlsServer { pub struct TlsStream { stream: native_tls::TlsStream, + lines: LineBuffer, } impl TlsStream { pub fn new(stream: native_tls::TlsStream) -> Self { - Self { stream } + Self { + stream, + lines: LineBuffer::new(), + } } pub fn read_line(&mut self, s: &mut String) -> std::io::Result { - let mut buf = [0u8; 1024]; - loop { - match self.stream.read(&mut buf) { - Ok(0) => return Ok(0), - Ok(n) => { - let chunk = std::str::from_utf8(&buf[..n]).unwrap_or(""); - s.push_str(chunk); - if s.contains('\n') { - return Ok(n); - } - } - Err(e) => return Err(e), + match self.lines.read_line(&mut self.stream)? { + Some(line) => { + s.push_str(&line); + s.push('\n'); + Ok(line.len() + 1) } + None => Ok(0), } } -- cgit v1.2.3