use crate::prelude::*; #[ cfg (feature = "hss-handler") ] pub trait Handler where Self : Send + Sync + 'static, { type Future : Future>> + Send + 'static; type ResponseBody : BodyTrait + Send + Sync + 'static; type ResponseBodyError : Error + Send + Sync + 'static; fn handle (&self, _request : Request) -> Self::Future; fn wrap (self) -> HandlerWrapper where Self : Sized { HandlerWrapper (self) } } #[ cfg (feature = "hss-handler") ] pub struct HandlerWrapper (H); #[ cfg (feature = "hss-handler") ] impl hyper::Service> for HandlerWrapper where H : Handler, { type Future = H::Future; type Response = Response; type Error = ServerError; fn poll_ready (&mut self, _context : &mut Context<'_>) -> Poll { Poll::Ready (Ok (())) } fn call (&mut self, _request : Request) -> Self::Future { self.0.handle (_request) } } #[ cfg (feature = "hss-handler") ] pub struct HandlerFnAsync where C : Fn (Request) -> F + Send + Sync + 'static, F : Future>> + Send + 'static, RB : BodyTrait + Send + Sync + 'static, RB::Error : Error + Send + Sync + 'static, { function : C, phantom : PhantomData, } #[ cfg (feature = "hss-handler") ] impl Handler for HandlerFnAsync where C : Fn (Request) -> F + Send + Sync + 'static, F : Future>> + Send + 'static, RB : BodyTrait + Send + Sync + 'static, RB::Error : Error + Send + Sync + 'static, { type Future = F; type ResponseBody = RB; type ResponseBodyError = RB::Error; fn handle (&self, _request : Request) -> Self::Future { (self.function) (_request) } } #[ cfg (feature = "hss-handler") ] impl From for HandlerFnAsync where C : Fn (Request) -> F + Send + Sync + 'static, F : Future>> + Send + 'static, RB : BodyTrait + Send + Sync + 'static, RB::Error : Error + Send + Sync + 'static, { fn from (_function : C) -> Self { Self { function : _function, phantom : PhantomData, } } } #[ cfg (feature = "hss-handler") ] pub struct HandlerFnSync where C : Fn (Request) -> ServerResult> + Send + Sync + 'static, RB : BodyTrait + Send + Sync + 'static, RB::Error : Error + Send + Sync + 'static, { function : C, phantom : PhantomData, } #[ cfg (feature = "hss-handler") ] impl Handler for HandlerFnSync where C : Fn (Request) -> ServerResult> + Send + Sync + 'static, RB : BodyTrait + Send + Sync + 'static, RB::Error : Error + Send + Sync + 'static, { type Future = future::Ready>>; type ResponseBody = RB; type ResponseBodyError = RB::Error; fn handle (&self, _request : Request) -> Self::Future { future::ready ((self.function) (_request)) } } #[ cfg (feature = "hss-handler") ] impl From for HandlerFnSync where C : Fn (Request) -> ServerResult> + Send + Sync + 'static, RB : BodyTrait + Send + Sync + 'static, RB::Error : Error + Send + Sync + 'static, { fn from (_function : C) -> Self { Self { function : _function, phantom : PhantomData, } } } #[ cfg (feature = "hss-handler") ] pub trait HandlerDyn where Self : Send + Sync + 'static, { fn handle (&self, _request : Request) -> HandlerFutureDynBox; } #[ cfg (feature = "hss-handler") ] impl HandlerDyn for H where H : Handler + Send + Sync + 'static, H::Future : Future>> + Send + 'static, { fn handle (&self, _request : Request) -> HandlerFutureDynBox { let _future = Handler::handle (self, _request); let _future = _future.map_ok (|_response| _response.map (BodyDynBox::new)); HandlerFutureDynBox::new (_future) } } #[ derive (Clone) ] #[ cfg (feature = "hss-handler") ] pub struct HandlerDynArc (Arc); #[ cfg (feature = "hss-handler") ] impl HandlerDynArc { pub fn new (_handler : impl HandlerDyn) -> Self { HandlerDynArc (Arc::new (_handler)) } pub fn delegate (&self, _request : Request) -> HandlerFutureDynBox { self.0.handle (_request) } pub fn from_arc (_handler : Arc) -> Self { HandlerDynArc (_handler) } pub fn into_arc (self) -> Arc { self.0 } pub fn clone_arc (&self) -> Arc { self.0.clone () } } #[ cfg (feature = "hss-handler") ] impl Handler for HandlerDynArc { type Future = HandlerFutureDynBox; type ResponseBody = BodyDynBox; type ResponseBodyError = ServerError; fn handle (&self, _request : Request) -> Self::Future { self.delegate (_request) } } #[ cfg (feature = "hss-handler") ] pub struct HandlerFutureDynBox (Pin>> + Send>>); #[ cfg (feature = "hss-handler") ] impl Future for HandlerFutureDynBox { type Output = ServerResult>; fn poll (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll { let _self = Pin::into_inner (self); _self.0.as_mut () .poll (_context) } } #[ cfg (feature = "hss-handler") ] impl HandlerFutureDynBox { pub fn new (_future : F) -> Self where F : Future>> + Send + 'static { Self (Box::pin (_future)) } pub fn ready (_result : ServerResult>) -> Self { Self::new (future::ready (_result)) } pub fn ready_response (_response : Response) -> Self { Self::ready (Ok (_response)) } pub fn ready_error (_error : ServerError) -> Self { Self::ready (Err (_error)) } } #[ cfg (feature = "hss-handler") ] #[ cfg (feature = "hss-extensions") ] impl From> for HandlerFutureDynBox where B : BodyTrait + Send + Sync + 'static, B::Error : Error + Send + Sync + 'static, { fn from (_response : Response) -> Self { Self::ready_response (_response.map (BodyDynBox::new)) } } #[ cfg (feature = "hss-handler") ] #[ cfg (feature = "hss-extensions") ] impl From for HandlerFutureDynBox { fn from (_error : ServerError) -> Self { Self::ready_error (_error) } } #[ cfg (feature = "hss-handler") ] #[ cfg (feature = "hss-extensions") ] pub trait HandlerSimpleAsync where Self : Send + Sync + 'static, { type Future : Future>> + Send + 'static; fn handle (&self, _request : Request) -> Self::Future; fn wrap (self) -> HandlerSimpleAsyncWrapper where Self : Sized { HandlerSimpleAsyncWrapper (self) } } #[ cfg (feature = "hss-handler") ] #[ cfg (feature = "hss-extensions") ] pub struct HandlerSimpleAsyncWrapper (H) where H : HandlerSimpleAsync, ; #[ cfg (feature = "hss-handler") ] #[ cfg (feature = "hss-extensions") ] impl HandlerSimpleAsyncWrapper where H : HandlerSimpleAsync, { pub fn new (_handler : H) -> Self { Self (_handler) } } #[ cfg (feature = "hss-handler") ] #[ cfg (feature = "hss-extensions") ] impl Handler for HandlerSimpleAsyncWrapper where H : HandlerSimpleAsync, H::Future : Unpin, { type Future = HandlerSimpleAsyncWrapperFuture; type ResponseBody = BodyWrapper; type ResponseBodyError = ServerError; fn handle (&self, _request : Request) -> Self::Future { let _future = HandlerSimpleAsync::handle (&self.0, _request); let _future = HandlerSimpleAsyncWrapperFuture (_future); _future } } #[ cfg (feature = "hss-handler") ] #[ cfg (feature = "hss-extensions") ] pub struct HandlerSimpleAsyncWrapperFuture (F) where F : Future>> + Send + 'static + Unpin, ; #[ cfg (feature = "hss-handler") ] #[ cfg (feature = "hss-extensions") ] impl Future for HandlerSimpleAsyncWrapperFuture where F : Future>> + Send + 'static + Unpin, { type Output = ServerResult>>; fn poll (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll { let _self = Pin::into_inner (self); let _delegate = Pin::new (&mut _self.0); let _poll = _delegate.poll (_context); let _poll = _poll.map_ok (|_response| _response.map (BodyWrapper::new)); _poll } } #[ cfg (feature = "hss-handler") ] #[ cfg (feature = "hss-extensions") ] pub trait HandlerSimpleSync where Self : Send + Sync + 'static, { fn handle (&self, _request : &Request, _response : &mut Response) -> ServerResult; fn wrap (self) -> HandlerSimpleSyncWrapper where Self : Sized { HandlerSimpleSyncWrapper (self) } } #[ cfg (feature = "hss-handler") ] #[ cfg (feature = "hss-extensions") ] pub struct HandlerSimpleSyncWrapper (H) where H : HandlerSimpleSync, ; #[ cfg (feature = "hss-handler") ] #[ cfg (feature = "hss-extensions") ] impl HandlerSimpleSyncWrapper where H : HandlerSimpleSync, { pub fn new (_handler : H) -> Self { Self (_handler) } } #[ cfg (feature = "hss-handler") ] #[ cfg (feature = "hss-extensions") ] impl Handler for HandlerSimpleSyncWrapper where H : HandlerSimpleSync, { type Future = future::Ready>>; type ResponseBody = BodyWrapper; type ResponseBodyError = ServerError; fn handle (&self, _request : Request) -> Self::Future { let mut _response = Response::new (Body::empty ()); match HandlerSimpleSync::handle (&self.0, &_request, &mut _response) { Ok (()) => future::ready (Ok (_response.map (BodyWrapper::new))), Err (_error) => future::ready (Err (_error)), } } } #[ cfg (feature = "hss-handler") ] #[ cfg (feature = "hss-extensions") ] pub struct BodyWrapper (B) where B : BodyTrait + Send + 'static + Unpin, B::Error : Error + Send + Sync + 'static, ; #[ cfg (feature = "hss-handler") ] #[ cfg (feature = "hss-extensions") ] impl BodyTrait for BodyWrapper where B : BodyTrait + Send + 'static + Unpin, B::Error : Error + Send + Sync + 'static, { type Data = Bytes; type Error = ServerError; fn poll_data (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll>> { let _future = self.delegate_pin_mut () .poll_data (_context); let _future = _future.map (|_option| _option.map (|_result| _result.map_err (|_error| _error.wrap (0x4e33a117)))); _future } fn poll_trailers (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll>> { let _future = self.delegate_pin_mut () .poll_trailers (_context); let _future = _future.map (|_result| _result.map_err (|_error| _error.wrap (0x3a25b983))); _future } fn is_end_stream (&self) -> bool { self.delegate () .is_end_stream () } fn size_hint (&self) -> BodySizeHint { self.delegate () .size_hint () } } #[ cfg (feature = "hss-handler") ] #[ cfg (feature = "hss-extensions") ] impl BodyWrapper where B : BodyTrait + Send + 'static + Unpin, B::Error : Error + Send + Sync + 'static, { pub fn new (_body : B) -> Self { Self (_body) } fn delegate_pin_mut (self : Pin<&mut Self>) -> Pin<&mut B> { let _self = Pin::into_inner (self); Pin::new (&mut _self.0) } fn delegate (&self) -> Pin<&B> { Pin::new (&self.0) } } #[ cfg (feature = "hss-handler") ] pub trait BodyDyn where Self : Send + 'static, { fn poll_data (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll>>; fn poll_trailers (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll>>; fn is_end_stream (&self) -> bool; fn size_hint (&self) -> BodySizeHint; } #[ cfg (feature = "hss-handler") ] impl BodyDyn for B where B : BodyTrait + Send + 'static, B::Error : Error + Send + Sync + 'static, { fn poll_data (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll>> { let _future = BodyTrait::poll_data (self, _context); let _future = _future.map (|_option| _option.map (|_result| _result.map_err (|_error| _error.wrap (0xd89897d4)))); _future } fn poll_trailers (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll>> { let _future = BodyTrait::poll_trailers (self, _context); let _future = _future.map (|_result| _result.map_err (|_error| _error.wrap (0x8adea6a0))); _future } fn is_end_stream (&self) -> bool { BodyTrait::is_end_stream (self) } fn size_hint (&self) -> BodySizeHint { BodyTrait::size_hint (self) } } #[ cfg (feature = "hss-handler") ] pub struct BodyDynBox (Pin>); #[ cfg (feature = "hss-handler") ] impl BodyTrait for BodyDynBox { type Data = Bytes; type Error = ServerError; fn poll_data (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll>> { self.delegate_pin_mut () .poll_data (_context) } fn poll_trailers (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll>> { self.delegate_pin_mut () .poll_trailers (_context) } fn is_end_stream (&self) -> bool { self.delegate () .is_end_stream () } fn size_hint (&self) -> BodySizeHint { self.delegate () .size_hint () } } #[ cfg (feature = "hss-handler") ] impl BodyDynBox { pub fn new (_body : impl BodyDyn + Sync) -> Self { Self (Box::pin (_body)) } fn delegate_pin_mut (self : Pin<&mut Self>) -> Pin<&mut dyn BodyDyn> { let _self = Pin::into_inner (self); _self.0.as_mut () } fn delegate (&self) -> Pin<&dyn BodyDyn> { self.0.as_ref () } }