File tree Expand file tree Collapse file tree 3 files changed +20
-2
lines changed Expand file tree Collapse file tree 3 files changed +20
-2
lines changed Original file line number Diff line number Diff line change @@ -309,6 +309,18 @@ impl<State> Request<State> {
309309 pub fn is_empty ( & self ) -> Option < bool > {
310310 Some ( self . request . len ( ) ? == 0 )
311311 }
312+
313+ /// Peer address of the underlying transport
314+ #[ must_use]
315+ pub fn peer_addr ( & self ) -> Option < & str > {
316+ self . request . peer_addr ( )
317+ }
318+
319+ /// Local address of the underlying transport
320+ #[ must_use]
321+ pub fn local_addr ( & self ) -> Option < & str > {
322+ self . request . local_addr ( )
323+ }
312324}
313325
314326impl < State > AsMut < http:: Request > for Request < State > {
Original file line number Diff line number Diff line change @@ -296,8 +296,12 @@ impl<State: Send + Sync + 'static> Server<State> {
296296 while let Some ( stream) = incoming. next ( ) . await {
297297 let stream = stream?;
298298 let this = self . clone ( ) ;
299+ let local_addr = stream. local_addr ( ) . ok ( ) ;
300+ let peer_addr = stream. peer_addr ( ) . ok ( ) ;
299301 task:: spawn ( async move {
300- let res = async_h1:: accept ( stream, |req| async {
302+ let res = async_h1:: accept ( stream, |mut req| async {
303+ req. set_local_addr ( local_addr) ;
304+ req. set_peer_addr ( peer_addr) ;
301305 let res = this. respond ( req) . await ;
302306 let res = res. map_err ( |_| io:: Error :: from ( io:: ErrorKind :: Other ) ) ?;
303307 Ok ( res)
Original file line number Diff line number Diff line change @@ -12,8 +12,10 @@ fn hello_world() -> Result<(), http_types::Error> {
1212 let port = test_utils:: find_port ( ) . await ;
1313 let server = task:: spawn ( async move {
1414 let mut app = tide:: new ( ) ;
15- app. at ( "/" ) . get ( |mut req : Request < ( ) > | async move {
15+ app. at ( "/" ) . get ( move |mut req : Request < ( ) > | async move {
1616 assert_eq ! ( req. body_string( ) . await . unwrap( ) , "nori" . to_string( ) ) ;
17+ assert ! ( req. local_addr( ) . unwrap( ) . contains( & port. to_string( ) ) ) ;
18+ assert ! ( req. peer_addr( ) . is_some( ) ) ;
1719 let res = Response :: new ( StatusCode :: Ok ) . body_string ( "says hello" . to_string ( ) ) ;
1820 Ok ( res)
1921 } ) ;
You can’t perform that action at this time.
0 commit comments