@@ -50,9 +50,8 @@ use std::task::{Context, Poll};
5050
5151pub use rustls;
5252
53- use rustls:: pki_types:: ServerName ;
5453use rustls:: server:: AcceptedAlert ;
55- use rustls:: { ClientConfig , ClientConnection , CommonState , ServerConfig , ServerConnection } ;
54+ use rustls:: { CommonState , ServerConfig , ServerConnection } ;
5655use tokio:: io:: { AsyncBufRead , AsyncRead , AsyncWrite , ReadBuf } ;
5756
5857macro_rules! ready {
@@ -65,148 +64,17 @@ macro_rules! ready {
6564}
6665
6766pub mod client;
67+ pub use client:: { TlsConnector , TlsConnectorWithAlpn } ;
6868mod common;
6969use common:: { MidHandshake , TlsState } ;
7070pub mod server;
7171
72- /// A wrapper around a `rustls::ClientConfig`, providing an async `connect` method.
73- #[ derive( Clone ) ]
74- pub struct TlsConnector {
75- inner : Arc < ClientConfig > ,
76- #[ cfg( feature = "early-data" ) ]
77- early_data : bool ,
78- }
79-
80- impl TlsConnector {
81- /// Enable 0-RTT.
82- ///
83- /// If you want to use 0-RTT,
84- /// You must also set `ClientConfig.enable_early_data` to `true`.
85- #[ cfg( feature = "early-data" ) ]
86- pub fn early_data ( mut self , flag : bool ) -> TlsConnector {
87- self . early_data = flag;
88- self
89- }
90-
91- #[ inline]
92- pub fn connect < IO > ( & self , domain : ServerName < ' static > , stream : IO ) -> Connect < IO >
93- where
94- IO : AsyncRead + AsyncWrite + Unpin ,
95- {
96- self . connect_impl ( domain, stream, None , |_| ( ) )
97- }
98-
99- #[ inline]
100- pub fn connect_with < IO , F > ( & self , domain : ServerName < ' static > , stream : IO , f : F ) -> Connect < IO >
101- where
102- IO : AsyncRead + AsyncWrite + Unpin ,
103- F : FnOnce ( & mut ClientConnection ) ,
104- {
105- self . connect_impl ( domain, stream, None , f)
106- }
107-
108- fn connect_impl < IO , F > (
109- & self ,
110- domain : ServerName < ' static > ,
111- stream : IO ,
112- alpn_protocols : Option < Vec < Vec < u8 > > > ,
113- f : F ,
114- ) -> Connect < IO >
115- where
116- IO : AsyncRead + AsyncWrite + Unpin ,
117- F : FnOnce ( & mut ClientConnection ) ,
118- {
119- let alpn = alpn_protocols. unwrap_or_else ( || self . inner . alpn_protocols . clone ( ) ) ;
120- let mut session = match ClientConnection :: new_with_alpn ( self . inner . clone ( ) , domain, alpn) {
121- Ok ( session) => session,
122- Err ( error) => {
123- return Connect ( MidHandshake :: Error {
124- io : stream,
125- // TODO(eliza): should this really return an `io::Error`?
126- // Probably not...
127- error : io:: Error :: new ( io:: ErrorKind :: Other , error) ,
128- } ) ;
129- }
130- } ;
131- f ( & mut session) ;
132-
133- Connect ( MidHandshake :: Handshaking ( client:: TlsStream {
134- io : stream,
135-
136- #[ cfg( not( feature = "early-data" ) ) ]
137- state : TlsState :: Stream ,
138-
139- #[ cfg( feature = "early-data" ) ]
140- state : if self . early_data && session. early_data ( ) . is_some ( ) {
141- TlsState :: EarlyData ( 0 , Vec :: new ( ) )
142- } else {
143- TlsState :: Stream
144- } ,
145-
146- need_flush : false ,
147-
148- #[ cfg( feature = "early-data" ) ]
149- early_waker : None ,
150-
151- session,
152- } ) )
153- }
154-
155- pub fn with_alpn ( & self , alpn_protocols : Vec < Vec < u8 > > ) -> TlsConnectorWithAlpn < ' _ > {
156- TlsConnectorWithAlpn {
157- inner : self ,
158- alpn_protocols,
159- }
160- }
161-
162- /// Get a read-only reference to underlying config
163- pub fn config ( & self ) -> & Arc < ClientConfig > {
164- & self . inner
165- }
166- }
167-
168- pub struct TlsConnectorWithAlpn < ' c > {
169- inner : & ' c TlsConnector ,
170- alpn_protocols : Vec < Vec < u8 > > ,
171- }
172-
173- impl TlsConnectorWithAlpn < ' _ > {
174- #[ inline]
175- pub fn connect < IO > ( self , domain : ServerName < ' static > , stream : IO ) -> Connect < IO >
176- where
177- IO : AsyncRead + AsyncWrite + Unpin ,
178- {
179- self . inner
180- . connect_impl ( domain, stream, Some ( self . alpn_protocols ) , |_| ( ) )
181- }
182-
183- #[ inline]
184- pub fn connect_with < IO , F > ( self , domain : ServerName < ' static > , stream : IO , f : F ) -> Connect < IO >
185- where
186- IO : AsyncRead + AsyncWrite + Unpin ,
187- F : FnOnce ( & mut ClientConnection ) ,
188- {
189- self . inner
190- . connect_impl ( domain, stream, Some ( self . alpn_protocols ) , f)
191- }
192- }
193-
19472/// A wrapper around a `rustls::ServerConfig`, providing an async `accept` method.
19573#[ derive( Clone ) ]
19674pub struct TlsAcceptor {
19775 inner : Arc < ServerConfig > ,
19876}
19977
200- impl From < Arc < ClientConfig > > for TlsConnector {
201- fn from ( inner : Arc < ClientConfig > ) -> TlsConnector {
202- TlsConnector {
203- inner,
204- #[ cfg( feature = "early-data" ) ]
205- early_data : false ,
206- }
207- }
208- }
209-
21078impl From < Arc < ServerConfig > > for TlsAcceptor {
21179 fn from ( inner : Arc < ServerConfig > ) -> TlsAcceptor {
21280 TlsAcceptor { inner }
0 commit comments