1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-25 08:52:42 +01:00
actix-web/src/ws/service.rs

51 lines
1.4 KiB
Rust
Raw Normal View History

2018-10-22 18:59:20 +02:00
use std::marker::PhantomData;
use actix_net::codec::Framed;
use actix_net::service::{NewService, Service};
use futures::future::{ok, FutureResult};
use futures::{Async, IntoFuture, Poll};
use h1::Codec;
use request::Request;
use super::{verify_handshake, HandshakeError};
pub struct VerifyWebSockets<T> {
_t: PhantomData<T>,
}
impl<T> Default for VerifyWebSockets<T> {
fn default() -> Self {
VerifyWebSockets { _t: PhantomData }
}
}
2018-11-30 20:57:57 +01:00
impl<T> NewService<(Request, Framed<T, Codec>)> for VerifyWebSockets<T> {
2018-10-22 18:59:20 +02:00
type Response = (Request, Framed<T, Codec>);
type Error = (HandshakeError, Framed<T, Codec>);
type InitError = ();
type Service = VerifyWebSockets<T>;
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future {
ok(VerifyWebSockets { _t: PhantomData })
}
}
2018-11-30 20:57:57 +01:00
impl<T> Service<(Request, Framed<T, Codec>)> for VerifyWebSockets<T> {
2018-10-22 18:59:20 +02:00
type Response = (Request, Framed<T, Codec>);
type Error = (HandshakeError, Framed<T, Codec>);
type Future = FutureResult<Self::Response, Self::Error>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
2018-11-30 20:57:57 +01:00
fn call(&mut self, (req, framed): (Request, Framed<T, Codec>)) -> Self::Future {
2018-10-22 18:59:20 +02:00
match verify_handshake(&req) {
Err(e) => Err((e, framed)).into_future(),
Ok(_) => Ok((req, framed)).into_future(),
}
}
}