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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#![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;
use http_body::Body as _;
pub use http_body_util::{BodyExt, Full};
pub use hyper::body::Bytes;
use hyper::body::Incoming;
use hyper::service::Service;
use hyper_util::rt::TokioExecutor;
use hyper_util::server::conn::auto;
use hyper_util::service::TowerToHyperService;
pub use redirect::Redirect;
pub use request::{DecodeQueryError, Request, RequestExt};
pub use response::{IntoResponse, Response};
pub use result_ext::ResultExt;
pub use {http, hyper};

#[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) -> Response
    where
        S: Clone,
        H: Fn(S, Request) -> F,
        F: Future<Output = Response>,
    {
        (self.handler)(self.state.clone(), req)
            .await
            .into_response()
    }

    pub async fn run<F>(
        self,
        addr: &std::net::SocketAddr,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
    where
        S: Clone + Send + Sync + 'static,
        H: Fn(S, Request) -> F + Clone + Send + Sync + 'static,
        F: Future<Output = Response> + Send + 'static,
    {
        use hyper_util::rt::TokioIo;
        use tokio::net::TcpListener;

        let listener = TcpListener::bind(addr).await?;
        println!("Listening on http://{}", addr);
        loop {
            let (stream, _) = listener.accept().await?;
            let service = self.clone();
            tokio::task::spawn(async move {
                if let Err(err) = auto::Builder::new(TokioExecutor::new())
                    .serve_connection(TokioIo::new(stream), service)
                    .await
                {
                    // Ignore certain network errors
                    if let Some(err) = err.downcast_ref::<hyper::Error>() {
                        if err.is_incomplete_message() {
                            return;
                        }
                    }

                    #[cfg(not(feature = "tracing"))]
                    let _ = err;

                    #[cfg(feature = "tracing")]
                    tracing::error!(%err, "error serving connection");
                }
            });
        }
    }

    pub async fn run_in<F, L, B>(
        self,
        addr: &std::net::SocketAddr,
        layer: L,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
    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: tower_service::Service<http::Request<Incoming>, Response = http::Response<B>>
            + Clone
            + Send
            + Sync
            + 'static,
        <L::Service as tower_service::Service<http::Request<Incoming>>>::Future: Send,
        <L::Service as tower_service::Service<http::Request<Incoming>>>::Error:
            std::error::Error + Send + Sync,
        B: http_body::Body + Send + 'static,
        B::Data: Send,
        B::Error: Into<Box<dyn std::error::Error + Send + Sync>> + Send + Sync,
    {
        use hyper_util::rt::TokioIo;
        use tokio::net::TcpListener;

        let svc = TowerToHyperService::new(layer.layer(self));
        let listener = TcpListener::bind(addr).await?;
        println!("Listening on http://{}", addr);
        loop {
            let (stream, _) = listener.accept().await?;
            let svc = svc.clone();
            tokio::task::spawn(async move {
                if let Err(err) = auto::Builder::new(TokioExecutor::new())
                    .serve_connection(TokioIo::new(stream), svc)
                    .await
                {
                    // Ignore certain network errors
                    if let Some(err) = err.downcast_ref::<hyper::Error>() {
                        if err.is_incomplete_message() {
                            return;
                        }
                    }

                    #[cfg(not(feature = "tracing"))]
                    let _ = err;

                    #[cfg(feature = "tracing")]
                    tracing::error!(%err, "error serving connection");
                }
            });
        }
    }
}

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

impl<S, H, F> Service<http::Request<Incoming>> 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 = Response;
    type Error = Infallible;
    // TODO: get rid of Box?
    type Future = BoxTrySendFuture<Self::Response, Self::Error>;

    fn call(&self, req: http::Request<Incoming>) -> Self::Future {
        let (parts, body) = req.into_parts();
        let body = if body.is_end_stream() {
            body::Body::empty()
        } else {
            body::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) })
    }
}

impl<S, H, F> tower_service::Service<http::Request<Incoming>> 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 = Response;
    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<Incoming>) -> Self::Future {
        Service::call(&*self, req)
    }
}