From 8ebfb1d0ce9309fa71e575a3d0ae30a85a3e31c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Sat, 4 Jul 2020 15:04:31 +0200 Subject: [PATCH] aquatic_http network: avoid infinite loop case; minor other changes --- aquatic_http/src/lib/network/connection.rs | 5 +++++ aquatic_http/src/lib/network/mod.rs | 25 +++++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/aquatic_http/src/lib/network/connection.rs b/aquatic_http/src/lib/network/connection.rs index 2b602e4..5fd7f2c 100644 --- a/aquatic_http/src/lib/network/connection.rs +++ b/aquatic_http/src/lib/network/connection.rs @@ -170,6 +170,8 @@ impl <'a>TlsHandshakeMachine { Stream::TlsStream(stream) ); + ::log::debug!("established tcp connection"); + Ok(established) }, Err(native_tls::HandshakeError::WouldBlock(handshake)) => { @@ -217,6 +219,8 @@ impl Connection { TlsHandshakeMachine::new(tls_acceptor.clone(), tcp_stream) ) } else { + ::log::debug!("established tcp connection"); + ConnectionInner::Established( EstablishedConnection::new(Stream::TcpStream(tcp_stream)) ) @@ -259,6 +263,7 @@ impl Connection { } } + /// Takes ownership since TlsStream needs ownership of TcpStream #[inline] pub fn get_in_progress(self) -> Option { if let ConnectionInner::InProgress(machine) = self.inner { diff --git a/aquatic_http/src/lib/network/mod.rs b/aquatic_http/src/lib/network/mod.rs index fdaa351..74d4072 100644 --- a/aquatic_http/src/lib/network/mod.rs +++ b/aquatic_http/src/lib/network/mod.rs @@ -97,7 +97,7 @@ pub fn run_poll_loop( &opt_tls_acceptor, ); } else { - run_handshakes_and_read_requests( + handle_connection_read_event( socket_worker_index, &request_channel_sender, &mut local_responses, @@ -172,7 +172,7 @@ fn accept_new_streams( /// On the stream given by poll_token, get TLS up and running if requested, /// then read requests and pass on through channel. -pub fn run_handshakes_and_read_requests( +pub fn handle_connection_read_event( socket_worker_index: usize, request_channel_sender: &RequestChannelSender, local_responses: &mut Vec<(ConnectionMeta, Response)>, @@ -182,19 +182,18 @@ pub fn run_handshakes_and_read_requests( ){ loop { // Get connection, updating valid_until - let opt_connection = { - if let Some(connection) = connections.get_mut(&poll_token) { - connection.valid_until = valid_until; - - Some(connection) - } else { - None - } + let connection = if let Some(c) = connections.get_mut(&poll_token){ + c + } else { + // If there is no connection, there is no stream, so there + // shouldn't be any (relevant) poll events. In other words, it's + // safe to return here + return }; - if let Some(established) = opt_connection - .and_then(Connection::get_established) - { + connection.valid_until = valid_until; + + if let Some(established) = connection.get_established(){ match established.read_request(){ Ok(request) => { let meta = ConnectionMeta {