use std::marker::PhantomData; use std::pin::Pin; use std::task::{Context, Poll}; use actix_codec::{AsyncRead, AsyncWrite, Framed}; use actix_http::body::BodySize; use actix_http::error::ResponseError; use actix_http::h1::{Codec, Message}; use actix_http::ws::{verify_handshake, HandshakeError}; use actix_http::{Request, Response}; use actix_service::{Service, ServiceFactory}; use futures::future::{err, ok, Either, Ready}; use futures::Future; /// Service that verifies incoming request if it is valid websocket /// upgrade request. In case of error returns `HandshakeError` pub struct VerifyWebSockets { _t: PhantomData<(T, C)>, } impl Default for VerifyWebSockets { fn default() -> Self { VerifyWebSockets { _t: PhantomData } } } impl ServiceFactory for VerifyWebSockets { type Config = C; type Request = (Request, Framed); type Response = (Request, Framed); type Error = (HandshakeError, Framed); type InitError = (); type Service = VerifyWebSockets; type Future = Ready>; fn new_service(&self, _: C) -> Self::Future { ok(VerifyWebSockets { _t: PhantomData }) } } impl Service for VerifyWebSockets { type Request = (Request, Framed); type Response = (Request, Framed); type Error = (HandshakeError, Framed); type Future = Ready>; fn poll_ready(&mut self, _: &mut Context) -> Poll> { Poll::Ready(Ok(())) } fn call(&mut self, (req, framed): (Request, Framed)) -> Self::Future { match verify_handshake(req.head()) { Err(e) => err((e, framed)), Ok(_) => ok((req, framed)), } } } /// Send http/1 error response pub struct SendError(PhantomData<(T, R, E, C)>); impl Default for SendError where T: AsyncRead + AsyncWrite, E: ResponseError, { fn default() -> Self { SendError(PhantomData) } } impl ServiceFactory for SendError where T: AsyncRead + AsyncWrite + Unpin + 'static, R: 'static, E: ResponseError + 'static, { type Config = C; type Request = Result)>; type Response = R; type Error = (E, Framed); type InitError = (); type Service = SendError; type Future = Ready>; fn new_service(&self, _: C) -> Self::Future { ok(SendError(PhantomData)) } } impl Service for SendError where T: AsyncRead + AsyncWrite + Unpin + 'static, R: 'static, E: ResponseError + 'static, { type Request = Result)>; type Response = R; type Error = (E, Framed); type Future = Either)>>, SendErrorFut>; fn poll_ready(&mut self, _: &mut Context) -> Poll> { Poll::Ready(Ok(())) } fn call(&mut self, req: Result)>) -> Self::Future { match req { Ok(r) => Either::Left(ok(r)), Err((e, framed)) => { let res = e.error_response().drop_body(); Either::Right(SendErrorFut { framed: Some(framed), res: Some((res, BodySize::Empty).into()), err: Some(e), _t: PhantomData, }) } } } } #[pin_project::pin_project] pub struct SendErrorFut { res: Option, BodySize)>>, framed: Option>, err: Option, _t: PhantomData, } impl Future for SendErrorFut where E: ResponseError, T: AsyncRead + AsyncWrite + Unpin, { type Output = Result)>; fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll { if let Some(res) = self.res.take() { if self.framed.as_mut().unwrap().write(res).is_err() { return Poll::Ready(Err(( self.err.take().unwrap(), self.framed.take().unwrap(), ))); } } match self.framed.as_mut().unwrap().flush(cx) { Poll::Ready(Ok(_)) => { Poll::Ready(Err((self.err.take().unwrap(), self.framed.take().unwrap()))) } Poll::Ready(Err(_)) => { Poll::Ready(Err((self.err.take().unwrap(), self.framed.take().unwrap()))) } Poll::Pending => Poll::Pending, } } }