diff options
| author | salmonik <salmonik@tuta.io> | 2026-10-28 22:38:54 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-10-28 22:38:54 +0100 |
| commit | ef2e13df274aaf0f2e50c03c63db33d65d9a0275 (patch) | |
| tree | 991fa12e70ec11e84e8f6c6e7bc0a60b0a150759 /src/bin/server/tls.rs | |
| parent | v0.2.5: fix terminal width, ping timing and output formatting (diff) | |
| download | zagan-bet-ef2e13df274aaf0f2e50c03c63db33d65d9a0275.tar.gz zagan-bet-ef2e13df274aaf0f2e50c03c63db33d65d9a0275.zip | |
v0.2.6: preserve buffered lines and add request read timeouts
Diffstat (limited to '')
| -rw-r--r-- | src/bin/server/tls.rs | 26 |
1 files changed, 13 insertions, 13 deletions
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 @@ | |||
| 1 | use std::io::{Read, Write}; | 1 | use std::io::{Read, Write}; |
| 2 | use std::net::TcpStream; | 2 | use std::net::TcpStream; |
| 3 | 3 | ||
| 4 | use zagan_bet::protocol::LineBuffer; | ||
| 5 | |||
| 4 | #[derive(Clone)] | 6 | #[derive(Clone)] |
| 5 | pub struct TlsServer { | 7 | pub struct TlsServer { |
| 6 | acceptor: native_tls::TlsAcceptor, | 8 | acceptor: native_tls::TlsAcceptor, |
| @@ -42,27 +44,25 @@ impl TlsServer { | |||
| 42 | 44 | ||
| 43 | pub struct TlsStream { | 45 | pub struct TlsStream { |
| 44 | stream: native_tls::TlsStream<TcpStream>, | 46 | stream: native_tls::TlsStream<TcpStream>, |
| 47 | lines: LineBuffer, | ||
| 45 | } | 48 | } |
| 46 | 49 | ||
| 47 | impl TlsStream { | 50 | impl TlsStream { |
| 48 | pub fn new(stream: native_tls::TlsStream<TcpStream>) -> Self { | 51 | pub fn new(stream: native_tls::TlsStream<TcpStream>) -> Self { |
| 49 | Self { stream } | 52 | Self { |
| 53 | stream, | ||
| 54 | lines: LineBuffer::new(), | ||
| 55 | } | ||
| 50 | } | 56 | } |
| 51 | 57 | ||
| 52 | pub fn read_line(&mut self, s: &mut String) -> std::io::Result<usize> { | 58 | pub fn read_line(&mut self, s: &mut String) -> std::io::Result<usize> { |
| 53 | let mut buf = [0u8; 1024]; | 59 | match self.lines.read_line(&mut self.stream)? { |
| 54 | loop { | 60 | Some(line) => { |
| 55 | match self.stream.read(&mut buf) { | 61 | s.push_str(&line); |
| 56 | Ok(0) => return Ok(0), | 62 | s.push('\n'); |
| 57 | Ok(n) => { | 63 | Ok(line.len() + 1) |
| 58 | let chunk = std::str::from_utf8(&buf[..n]).unwrap_or(""); | ||
| 59 | s.push_str(chunk); | ||
| 60 | if s.contains('\n') { | ||
| 61 | return Ok(n); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | Err(e) => return Err(e), | ||
| 65 | } | 64 | } |
| 65 | None => Ok(0), | ||
| 66 | } | 66 | } |
| 67 | } | 67 | } |
| 68 | 68 | ||
