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
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#![forbid(unsafe_code)]
// #![warn(missing_docs)]

pub mod body;
mod redirect;
mod request;
pub mod response;
mod result_ext;
pub mod route;

use std::convert::{Infallible, TryFrom};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

pub use body::Body;
use headers::HeaderMapExt;
pub use http;
pub use hyper;
use hyper::body::HttpBody;
use hyper::service::Service;
pub use redirect::Redirect;
pub use request::{DecodeQueryError, Request, RequestExt};
pub use response::{IntoResponse, Response};
pub use result_ext::ResultExt;

#[derive(Clone)]
pub struct SolarSail<S, H> {
    state: S,
    handler: H,
}

impl<S, H> SolarSail<S, H> {
    pub fn new(state: S, handler: H) -> Self {
        SolarSail { state, handler }
    }

    async fn serve<F>(&self, req: Request) -> http::Response<hyper::Body>
    where
        S: Clone,
        H: Fn(S, Request) -> F,
        F: Future<Output = Response>,
    {
        (self.handler)(self.state.clone(), req)
            .await
            .into_response()
    }

    #[cfg(feature = "server")]
    pub async fn run<F>(self, addr: &std::net::SocketAddr) -> Result<(), hyper::Error>
    where
        S: Clone + Send + Sync + 'static,
        H: Fn(S, Request) -> F + Clone + Send + Sync + 'static,
        F: Future<Output = Response> + Send + 'static,
    {
        let server = hyper::Server::bind(addr).serve(self);
        println!("Listening on http://{}", addr);
        server.await
    }

    #[cfg(feature = "server")]
    pub async fn run_in<F, L, B>(
        self,
        addr: &std::net::SocketAddr,
        layer: L,
    ) -> Result<(), hyper::Error>
    where
        S: Clone + Send + Sync + 'static,
        H: Fn(S, Request) -> F + Clone + Send + Sync + 'static,
        F: Future<Output = Response> + Send + 'static,
        L: tower_layer::Layer<Self>,
        L::Service: Service<http::Request<hyper::Body>, Response = http::Response<B>>
            + Clone
            + Send
            + Sync
            + 'static,
        <L::Service as Service<http::Request<hyper::Body>>>::Future: Send,
        <L::Service as Service<http::Request<hyper::Body>>>::Error: std::error::Error + Send + Sync,
        B: HttpBody + Send + Sync + 'static,
        B::Data: Send,
        B::Error: Into<Box<dyn std::error::Error + Send + Sync>> + Send + Sync,
    {
        let svc = layer.layer(self);
        let svc = hyper::service::make_service_fn(move |_| {
            let svc = svc.clone();
            async move { Ok::<_, std::convert::Infallible>(svc) }
        });
        let server = hyper::Server::bind(addr).serve(svc);
        println!("Listening on http://{}", addr);
        server.await
    }
}

type BoxTrySendFuture<R, E> = Pin<Box<dyn Future<Output = Result<R, E>> + Send>>;

impl<S, H, F> Service<http::Request<hyper::Body>> for SolarSail<S, H>
where
    S: Clone + Send + Sync + 'static,
    H: Fn(S, Request) -> F + Clone + Send + Sync + 'static,
    F: Future<Output = Response> + Send + 'static,
{
    type Response = http::Response<hyper::Body>;
    type Error = Infallible;
    // TODO: get rid of Box?
    type Future = BoxTrySendFuture<Self::Response, Self::Error>;

    fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: http::Request<hyper::Body>) -> Self::Future {
        let (parts, body) = req.into_parts();
        let body = if body.is_end_stream() {
            Body::empty()
        } else {
            Body::new(
                body,
                parts
                    .headers
                    .typed_get()
                    .map(|h: headers::ContentLength| usize::try_from(h.0).unwrap_or(usize::MAX)),
                parts.headers.get(http::header::CONTENT_TYPE).cloned(),
            )
        };

        let svc = self.clone();
        let req = Request::from_parts(parts, body);
        Box::pin(async move { Ok(svc.serve(req).await) })
    }
}

#[cfg(feature = "server")]
impl<S, H> Service<&hyper::server::conn::AddrStream> for SolarSail<S, H>
where
    S: Clone,
    H: Clone,
{
    type Response = SolarSail<S, H>;
    type Error = hyper::Error;
    type Future = std::future::Ready<Result<Self::Response, Self::Error>>;

    fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, _: &hyper::server::conn::AddrStream) -> Self::Future {
        std::future::ready(Ok(self.clone()))
    }
}