@@ -14,10 +14,11 @@ use crate::helpers::{Data, DataFactory};
1414use crate :: request:: Request ;
1515use crate :: response:: Response ;
1616use crate :: service:: HttpService ;
17+ use crate :: { ConnectCallback , Extensions } ;
1718
18- /// A http service builder
19+ /// A HTTP service builder
1920///
20- /// This type can be used to construct an instance of `http service` through a
21+ /// This type can be used to construct an instance of [`HttpService`] through a
2122/// builder-like pattern.
2223pub struct HttpServiceBuilder < T , S , X = ExpectHandler , U = UpgradeHandler < T > > {
2324 keep_alive : KeepAlive ,
@@ -27,7 +28,9 @@ pub struct HttpServiceBuilder<T, S, X = ExpectHandler, U = UpgradeHandler<T>> {
2728 local_addr : Option < net:: SocketAddr > ,
2829 expect : X ,
2930 upgrade : Option < U > ,
31+ // DEPRECATED: in favor of on_connect_ext
3032 on_connect : Option < Rc < dyn Fn ( & T ) -> Box < dyn DataFactory > > > ,
33+ on_connect_ext : Option < Rc < ConnectCallback < T > > > ,
3134 _t : PhantomData < ( T , S ) > ,
3235}
3336
4952 expect : ExpectHandler ,
5053 upgrade : None ,
5154 on_connect : None ,
55+ on_connect_ext : None ,
5256 _t : PhantomData ,
5357 }
5458 }
@@ -138,6 +142,7 @@ where
138142 expect : expect. into_factory ( ) ,
139143 upgrade : self . upgrade ,
140144 on_connect : self . on_connect ,
145+ on_connect_ext : self . on_connect_ext ,
141146 _t : PhantomData ,
142147 }
143148 }
@@ -167,14 +172,16 @@ where
167172 expect : self . expect ,
168173 upgrade : Some ( upgrade. into_factory ( ) ) ,
169174 on_connect : self . on_connect ,
175+ on_connect_ext : self . on_connect_ext ,
170176 _t : PhantomData ,
171177 }
172178 }
173179
174180 /// Set on-connect callback.
175181 ///
176- /// It get called once per connection and result of the call
177- /// get stored to the request's extensions.
182+ /// Called once per connection. Return value of the call is stored in request extensions.
183+ ///
184+ /// *SOFT DEPRECATED*: Prefer the `on_connect_ext` style callback.
178185 pub fn on_connect < F , I > ( mut self , f : F ) -> Self
179186 where
180187 F : Fn ( & T ) -> I + ' static ,
@@ -184,7 +191,20 @@ where
184191 self
185192 }
186193
187- /// Finish service configuration and create *http service* for HTTP/1 protocol.
194+ /// Sets the callback to be run on connection establishment.
195+ ///
196+ /// Has mutable access to a data container that will be merged into request extensions.
197+ /// This enables transport layer data (like client certificates) to be accessed in middleware
198+ /// and handlers.
199+ pub fn on_connect_ext < F > ( mut self , f : F ) -> Self
200+ where
201+ F : Fn ( & T , & mut Extensions ) + ' static ,
202+ {
203+ self . on_connect_ext = Some ( Rc :: new ( f) ) ;
204+ self
205+ }
206+
207+ /// Finish service configuration and create a HTTP Service for HTTP/1 protocol.
188208 pub fn h1 < F , B > ( self , service : F ) -> H1Service < T , S , B , X , U >
189209 where
190210 B : MessageBody ,
@@ -200,13 +220,15 @@ where
200220 self . secure ,
201221 self . local_addr ,
202222 ) ;
223+
203224 H1Service :: with_config ( cfg, service. into_factory ( ) )
204225 . expect ( self . expect )
205226 . upgrade ( self . upgrade )
206227 . on_connect ( self . on_connect )
228+ . on_connect_ext ( self . on_connect_ext )
207229 }
208230
209- /// Finish service configuration and create *http service* for HTTP/2 protocol.
231+ /// Finish service configuration and create a HTTP service for HTTP/2 protocol.
210232 pub fn h2 < F , B > ( self , service : F ) -> H2Service < T , S , B >
211233 where
212234 B : MessageBody + ' static ,
@@ -223,7 +245,10 @@ where
223245 self . secure ,
224246 self . local_addr ,
225247 ) ;
226- H2Service :: with_config ( cfg, service. into_factory ( ) ) . on_connect ( self . on_connect )
248+
249+ H2Service :: with_config ( cfg, service. into_factory ( ) )
250+ . on_connect ( self . on_connect )
251+ . on_connect_ext ( self . on_connect_ext )
227252 }
228253
229254 /// Finish service configuration and create `HttpService` instance.
@@ -243,9 +268,11 @@ where
243268 self . secure ,
244269 self . local_addr ,
245270 ) ;
271+
246272 HttpService :: with_config ( cfg, service. into_factory ( ) )
247273 . expect ( self . expect )
248274 . upgrade ( self . upgrade )
249275 . on_connect ( self . on_connect )
276+ . on_connect_ext ( self . on_connect_ext )
250277 }
251278}
0 commit comments