make 0-RTT optional

This commit is contained in:
quininer 2019-02-25 23:48:06 +08:00
parent 02ff36428c
commit 485cf84639
5 changed files with 43 additions and 16 deletions

View File

@ -14,6 +14,7 @@ matrix:
script:
- cargo test
- cargo test --features early-data
- cd examples/server
- cargo check
- cd ../../examples/client

View File

@ -22,6 +22,9 @@ iovec = "0.1"
rustls = "0.15"
webpki = "0.19"
[features]
early-data = []
[dev-dependencies]
tokio = "0.1.6"
lazy_static = "1"

View File

@ -14,6 +14,7 @@ build: false
test_script:
- 'cargo test'
- 'cargo test --features early-data'
- 'cd examples/server'
- 'cargo check'
- 'cd ../../examples/client'

View File

@ -1,5 +1,4 @@
use super::*;
use std::io::Write;
use rustls::Session;
@ -10,12 +9,14 @@ pub struct TlsStream<IO> {
pub(crate) io: IO,
pub(crate) session: ClientSession,
pub(crate) state: TlsState,
#[cfg(feature = "early-data")]
pub(crate) early_data: (usize, Vec<u8>)
}
#[derive(Debug)]
pub(crate) enum TlsState {
EarlyData,
#[cfg(feature = "early-data")] EarlyData,
Stream,
Eof,
Shutdown
@ -23,7 +24,7 @@ pub(crate) enum TlsState {
pub(crate) enum MidHandshake<IO> {
Handshaking(TlsStream<IO>),
EarlyData(TlsStream<IO>),
#[cfg(feature = "early-data")] EarlyData(TlsStream<IO>),
End
}
@ -66,8 +67,9 @@ where IO: AsyncRead + AsyncWrite,
}
match mem::replace(self, MidHandshake::End) {
MidHandshake::Handshaking(stream)
| MidHandshake::EarlyData(stream) => Ok(Async::Ready(stream)),
MidHandshake::Handshaking(stream) => Ok(Async::Ready(stream)),
#[cfg(feature = "early-data")]
MidHandshake::EarlyData(stream) => Ok(Async::Ready(stream)),
MidHandshake::End => panic!()
}
}
@ -80,7 +82,10 @@ where IO: AsyncRead + AsyncWrite
let mut stream = Stream::new(&mut self.io, &mut self.session);
match self.state {
#[cfg(feature = "early-data")]
TlsState::EarlyData => {
use std::io::Write;
let (pos, data) = &mut self.early_data;
// complete handshake
@ -126,6 +131,7 @@ where IO: AsyncRead + AsyncWrite
let mut stream = Stream::new(&mut self.io, &mut self.session);
match self.state {
#[cfg(feature = "early-data")]
TlsState::EarlyData => {
let (pos, data) = &mut self.early_data;

View File

@ -28,6 +28,7 @@ use common::Stream;
#[derive(Clone)]
pub struct TlsConnector {
inner: Arc<ClientConfig>,
#[cfg(feature = "early-data")]
early_data: bool
}
@ -39,7 +40,11 @@ pub struct TlsAcceptor {
impl From<Arc<ClientConfig>> for TlsConnector {
fn from(inner: Arc<ClientConfig>) -> TlsConnector {
TlsConnector { inner, early_data: false }
TlsConnector {
inner,
#[cfg(feature = "early-data")]
early_data: false
}
}
}
@ -54,6 +59,7 @@ impl TlsConnector {
///
/// Note that you want to use 0-RTT.
/// You must set `enable_early_data` to `true` in `ClientConfig`.
#[cfg(feature = "early-data")]
pub fn early_data(mut self, flag: bool) -> TlsConnector {
self.early_data = flag;
self
@ -75,19 +81,28 @@ impl TlsConnector {
let mut session = ClientSession::new(&self.inner, domain);
f(&mut session);
Connect(if self.early_data {
client::MidHandshake::EarlyData(client::TlsStream {
session, io: stream,
state: client::TlsState::EarlyData,
early_data: (0, Vec::new())
})
} else {
client::MidHandshake::Handshaking(client::TlsStream {
#[cfg(not(feature = "early-data"))] {
Connect(client::MidHandshake::Handshaking(client::TlsStream {
session, io: stream,
state: client::TlsState::Stream,
early_data: (0, Vec::new())
}))
}
#[cfg(feature = "early-data")] {
Connect(if self.early_data {
client::MidHandshake::EarlyData(client::TlsStream {
session, io: stream,
state: client::TlsState::EarlyData,
early_data: (0, Vec::new())
})
} else {
client::MidHandshake::Handshaking(client::TlsStream {
session, io: stream,
state: client::TlsState::Stream,
early_data: (0, Vec::new())
})
})
})
}
}
}
@ -143,5 +158,6 @@ impl<IO: AsyncRead + AsyncWrite> Future for Accept<IO> {
}
}
#[cfg(feature = "early-data")]
#[cfg(test)]
mod test_0rtt;