aquatic_http: in Connection.inner, replace Either with new enum

This commit is contained in:
Joakim Frostegård 2020-07-04 13:28:55 +02:00
parent 73b1646c71
commit 1ea1f0b749
2 changed files with 42 additions and 18 deletions

View file

@ -3,7 +3,6 @@ use std::io::ErrorKind;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::sync::Arc; use std::sync::Arc;
use either::Either;
use hashbrown::HashMap; use hashbrown::HashMap;
use mio::Token; use mio::Token;
use mio::net::TcpStream; use mio::net::TcpStream;
@ -192,9 +191,15 @@ impl <'a>TlsHandshakeMachine {
} }
pub enum ConnectionInner {
Established(EstablishedConnection),
InProgress(TlsHandshakeMachine),
}
pub struct Connection { pub struct Connection {
pub valid_until: ValidUntil, pub valid_until: ValidUntil,
pub inner: Either<EstablishedConnection, TlsHandshakeMachine>, pub inner: ConnectionInner,
} }
@ -207,9 +212,9 @@ impl Connection {
) -> Self { ) -> Self {
// Setup handshake machine if TLS is requested // Setup handshake machine if TLS is requested
let inner = if let Some(tls_acceptor) = opt_tls_acceptor { let inner = if let Some(tls_acceptor) = opt_tls_acceptor {
Either::Right(TlsHandshakeMachine::new(tls_acceptor.clone(), tcp_stream)) ConnectionInner::InProgress(TlsHandshakeMachine::new(tls_acceptor.clone(), tcp_stream))
} else { } else {
Either::Left(EstablishedConnection::new(Stream::TcpStream(tcp_stream))) ConnectionInner::Established(EstablishedConnection::new(Stream::TcpStream(tcp_stream)))
}; };
Self { Self {
@ -217,6 +222,22 @@ impl Connection {
inner, inner,
} }
} }
pub fn get_established(&mut self) -> Option<&mut EstablishedConnection> {
if let ConnectionInner::Established(ref mut established) = self.inner {
Some(established)
} else {
None
}
}
pub fn get_in_progress(self) -> Option<TlsHandshakeMachine> {
if let ConnectionInner::InProgress(machine) = self.inner {
Some(machine)
} else {
None
}
}
} }

View file

@ -5,7 +5,6 @@ use std::io::ErrorKind;
use std::sync::Arc; use std::sync::Arc;
use std::vec::Drain; use std::vec::Drain;
use either::Either;
use hashbrown::HashMap; use hashbrown::HashMap;
use log::{info, debug, error}; use log::{info, debug, error};
use native_tls::TlsAcceptor; use native_tls::TlsAcceptor;
@ -178,17 +177,20 @@ pub fn run_handshakes_and_read_requests(
valid_until: ValidUntil, valid_until: ValidUntil,
){ ){
loop { loop {
let opt_connection = connections.get_mut(&poll_token); // Get connection, updating valid_until
let opt_connection = {
if let Some(connection) = connections.get_mut(&poll_token) {
connection.valid_until = valid_until;
let opt_established = if let Some(connection) = opt_connection { Some(connection)
connection.valid_until = valid_until; } else {
None
connection.inner.as_mut().left() }
} else {
None
}; };
if let Some(established) = opt_established { if let Some(established) = opt_connection
.and_then(Connection::get_established)
{
match established.read_request(){ match established.read_request(){
Ok(request) => { Ok(request) => {
let meta = ConnectionMeta { let meta = ConnectionMeta {
@ -246,13 +248,13 @@ pub fn run_handshakes_and_read_requests(
}, },
} }
} else if let Some(handshake_machine) = connections.remove(&poll_token) } else if let Some(handshake_machine) = connections.remove(&poll_token)
.and_then(|c| c.inner.right()) .and_then(Connection::get_in_progress)
{ {
match handshake_machine.establish_tls(){ match handshake_machine.establish_tls(){
Ok(established) => { Ok(established) => {
let connection = Connection { let connection = Connection {
valid_until, valid_until,
inner: Either::Left(established) inner: ConnectionInner::Established(established)
}; };
connections.insert(poll_token, connection); connections.insert(poll_token, connection);
@ -260,17 +262,18 @@ pub fn run_handshakes_and_read_requests(
Err(TlsHandshakeMachineError::WouldBlock(machine)) => { Err(TlsHandshakeMachineError::WouldBlock(machine)) => {
let connection = Connection { let connection = Connection {
valid_until, valid_until,
inner: Either::Right(machine) inner: ConnectionInner::InProgress(machine)
}; };
connections.insert(poll_token, connection); connections.insert(poll_token, connection);
// Break and wait for more data
break break
}, },
Err(TlsHandshakeMachineError::Failure(err)) => { Err(TlsHandshakeMachineError::Failure(err)) => {
info!("tls handshake error: {}", err); info!("tls handshake error: {}", err);
// TLS negotiation error occured // TLS negotiation failed
break break
} }
} }
@ -287,7 +290,7 @@ pub fn send_responses(
){ ){
for (meta, response) in local_responses.chain(response_channel_receiver){ for (meta, response) in local_responses.chain(response_channel_receiver){
if let Some(established) = connections.get_mut(&meta.poll_token) if let Some(established) = connections.get_mut(&meta.poll_token)
.and_then(|c| c.inner.as_mut().left()) .and_then(Connection::get_established)
{ {
if established.peer_addr != meta.peer_addr { if established.peer_addr != meta.peer_addr {
info!("socket worker error: peer socket addrs didn't match"); info!("socket worker error: peer socket addrs didn't match");