1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use super::UpgradeHandle;
use crate::{handshake, Error, Result, WebSocket};
use futures::TryFutureExt;
use hyper::{header, Body, Request, Response, StatusCode};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::sync::mpsc::Sender;
use tower_service::Service;

pub struct WsService {
    tx: Sender<UpgradeHandle>,
}

impl WsService {
    pub fn new(tx: Sender<UpgradeHandle>) -> Self {
        Self { tx }
    }
}

impl Service<Request<Body>> for WsService {
    type Response = Response<Body>;
    type Error = Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response>> + Send + Sync>>;

    fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<()>> {
        Ok(()).into()
    }

    fn call(&mut self, req: Request<Body>) -> Self::Future {
        let mut tx = self.tx.clone();

        Box::pin(async move {
            if let Some(key) = req.headers().get(handshake::SEC_WEBSOCKET_KEY) {
                // TODO don't unwrap
                let accept = handshake::accept(key).await.unwrap();
                let res = Response::builder()
                    .status(StatusCode::SWITCHING_PROTOCOLS)
                    .header(header::CONNECTION, header::UPGRADE)
                    .header(header::UPGRADE, "websocket")
                    .header(handshake::SEC_WEBSOCKET_ACCEPT, accept)
                    .body(Body::empty())
                    .unwrap();

                let handle = tokio::task::spawn(WebSocket::upgrade(req.into_body()));
                tx.send(handle).await;

                Ok(res)
            } else {
                unimplemented!()
            }
        })
    }
}