@@ -7,6 +7,7 @@ use hyper::service::Service;
77use hyper:: { Request , Response , StatusCode } ;
88use std:: fmt;
99use std:: future:: Future ;
10+ use std:: net:: SocketAddr ;
1011use std:: ops:: { Deref , DerefMut } ;
1112use std:: task:: { Context , Poll } ;
1213
@@ -27,6 +28,32 @@ impl<B: Default> NotFound<B> for B {
2728 }
2829}
2930
31+ /// Connection which has a remote address, which can thus be composited.
32+ pub trait HasRemoteAddr {
33+ /// Get the remote address for the connection to pass
34+ /// to the composited service
35+ fn remote_addr ( & self ) -> Option < SocketAddr > ;
36+ }
37+
38+ impl < ' a > HasRemoteAddr for & ' a Option < SocketAddr > {
39+ fn remote_addr ( & self ) -> Option < SocketAddr > {
40+ * * self
41+ }
42+ }
43+
44+ impl < ' a > HasRemoteAddr for & ' a hyper:: server:: conn:: AddrStream {
45+ fn remote_addr ( & self ) -> Option < SocketAddr > {
46+ Some ( hyper:: server:: conn:: AddrStream :: remote_addr ( self ) )
47+ }
48+ }
49+
50+ #[ cfg( feature = "uds" ) ]
51+ impl HasRemoteAddr for & ' a tokio:: net:: UnixStream {
52+ fn remote_addr ( & self ) -> Option < SocketAddr > {
53+ None
54+ }
55+ }
56+
3057/// Trait implemented by services which can be composited.
3158///
3259/// Wraps tower_service::Service
@@ -151,98 +178,10 @@ where
151178 }
152179}
153180
154- use std:: net:: SocketAddr ;
155-
156- impl < ' a , ReqBody , ResBody , Error , MakeError > Service < & ' a Option < SocketAddr > >
157- for CompositeMakeService < Option < SocketAddr > , ReqBody , ResBody , Error , MakeError >
158- where
159- ReqBody : ' static ,
160- ResBody : NotFound < ResBody > + ' static ,
161- MakeError : Send + ' static ,
162- Error : ' static ,
163- {
164- type Error = MakeError ;
165- type Response = CompositeService < ReqBody , ResBody , Error > ;
166- type Future = BoxFuture < ' static , Result < Self :: Response , Self :: Error > > ;
167-
168- fn poll_ready ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
169- for service in & mut self . 0 {
170- match service. 1 . poll_ready ( cx) {
171- Poll :: Ready ( Ok ( _) ) => { }
172- Poll :: Ready ( Err ( e) ) => {
173- return Poll :: Ready ( Err ( e) ) ;
174- }
175- Poll :: Pending => {
176- return Poll :: Pending ;
177- }
178- }
179- }
180- Poll :: Ready ( Ok ( ( ) ) )
181- }
182-
183- fn call ( & mut self , target : & ' a Option < SocketAddr > ) -> Self :: Future {
184- let mut services = Vec :: with_capacity ( self . 0 . len ( ) ) ;
185- for ( path, service) in & mut self . 0 {
186- let path: & ' static str = path;
187- services. push ( service. call ( * target) . map_ok ( move |s| ( path, s) ) ) ;
188- }
189- Box :: pin ( futures:: future:: join_all ( services) . map ( |results| {
190- let services: Result < Vec < _ > , MakeError > = results. into_iter ( ) . collect ( ) ;
191-
192- Ok ( CompositeService ( services?) )
193- } ) )
194- }
195- }
196-
197- impl < ' a , ReqBody , ResBody , Error , MakeError > Service < & ' a hyper:: server:: conn:: AddrStream >
198- for CompositeMakeService < Option < SocketAddr > , ReqBody , ResBody , Error , MakeError >
199- where
200- ReqBody : ' static ,
201- ResBody : NotFound < ResBody > + ' static ,
202- MakeError : Send + ' static ,
203- Error : ' static ,
204- {
205- type Error = MakeError ;
206- type Response = CompositeService < ReqBody , ResBody , Error > ;
207- type Future = BoxFuture < ' static , Result < Self :: Response , Self :: Error > > ;
208-
209- fn poll_ready ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
210- for service in & mut self . 0 {
211- match service. 1 . poll_ready ( cx) {
212- Poll :: Ready ( Ok ( _) ) => { }
213- Poll :: Ready ( Err ( e) ) => {
214- return Poll :: Ready ( Err ( e) ) ;
215- }
216- Poll :: Pending => {
217- return Poll :: Pending ;
218- }
219- }
220- }
221- Poll :: Ready ( Ok ( ( ) ) )
222- }
223-
224- fn call ( & mut self , target : & ' a hyper:: server:: conn:: AddrStream ) -> Self :: Future {
225- let mut services = Vec :: with_capacity ( self . 0 . len ( ) ) ;
226- for ( path, service) in & mut self . 0 {
227- let path: & ' static str = path;
228- services. push (
229- service
230- . call ( Some ( target. remote_addr ( ) ) )
231- . map_ok ( move |s| ( path, s) ) ,
232- ) ;
233- }
234- Box :: pin ( futures:: future:: join_all ( services) . map ( |results| {
235- let services: Result < Vec < _ > , MakeError > = results. into_iter ( ) . collect ( ) ;
236-
237- Ok ( CompositeService ( services?) )
238- } ) )
239- }
240- }
241-
242- #[ cfg( feature = "uds" ) ]
243- impl < ' a , ReqBody , ResBody , Error , MakeError > Service < & ' a tokio:: net:: UnixStream >
181+ impl < ReqBody , ResBody , Error , MakeError , Connection > Service < Connection >
244182 for CompositeMakeService < Option < SocketAddr > , ReqBody , ResBody , Error , MakeError >
245183where
184+ Connection : HasRemoteAddr ,
246185 ReqBody : ' static ,
247186 ResBody : NotFound < ResBody > + ' static ,
248187 MakeError : Send + ' static ,
@@ -267,15 +206,12 @@ where
267206 Poll :: Ready ( Ok ( ( ) ) )
268207 }
269208
270- fn call ( & mut self , _target : & ' a tokio :: net :: UnixStream ) -> Self :: Future {
209+ fn call ( & mut self , target : Connection ) -> Self :: Future {
271210 let mut services = Vec :: with_capacity ( self . 0 . len ( ) ) ;
211+ let addr = target. remote_addr ( ) ;
272212 for ( path, service) in & mut self . 0 {
273213 let path: & ' static str = path;
274- services. push (
275- service
276- . call ( None )
277- . map_ok ( move |s| ( path, s) ) ,
278- ) ;
214+ services. push ( service. call ( addr) . map_ok ( move |s| ( path, s) ) ) ;
279215 }
280216 Box :: pin ( futures:: future:: join_all ( services) . map ( |results| {
281217 let services: Result < Vec < _ > , MakeError > = results. into_iter ( ) . collect ( ) ;
0 commit comments