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: script:
- cargo test - cargo test
- cargo test --features early-data
- cd examples/server - cd examples/server
- cargo check - cargo check
- cd ../../examples/client - cd ../../examples/client

View File

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

View File

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

View File

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

View File

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