Skip to main content

next_rust_server/
server.rs

1//! Production HTTP server built on hyper.
2//!
3//! * HTTP/1.1 with keep-alive and HTTP/2 (h2c, and via TLS-terminating
4//!   proxies) using hyper's automatic protocol detection.
5//! * Header read timeout (slowloris protection) and request timeouts.
6//! * Graceful shutdown on SIGINT/SIGTERM.
7//! * gzip compression for compressible responses, including streams.
8//!
9//! TLS and HTTP/3 are expected to be terminated by a reverse proxy or load
10//! balancer (see the "Deployment" docs page).
11
12use std::convert::Infallible;
13use std::net::SocketAddr;
14use std::time::{Duration, Instant};
15
16use bytes::Bytes;
17use futures_util::StreamExt;
18use http_body_util::combinators::UnsyncBoxBody;
19use http_body_util::{BodyExt, Empty, Full, StreamBody};
20use hyper::body::{Frame, Incoming};
21use hyper_util::rt::{TokioExecutor, TokioIo, TokioTimer};
22use hyper_util::server::conn::auto;
23use hyper_util::server::graceful::GracefulShutdown;
24use tokio::net::TcpListener;
25
26use crate::app::App;
27use crate::request::{BoxError, Request};
28use crate::response::{Body, Response};
29
30pub type HyperBody = UnsyncBoxBody<Bytes, BoxError>;
31
32impl App {
33    /// Bind to the configured address and serve until shutdown.
34    pub async fn serve(self) -> std::io::Result<()> {
35        let addr = self.config().socket_addr_string();
36        let listener = TcpListener::bind(&addr).await.map_err(|e| {
37            std::io::Error::new(
38                e.kind(),
39                format!("could not bind {addr}: {e} (is another server running? set PORT to change the port)"),
40            )
41        })?;
42        self.serve_listener(listener).await
43    }
44
45    /// Serve on an existing listener (tests, socket activation).
46    pub async fn serve_listener(self, listener: TcpListener) -> std::io::Result<()> {
47        let local = listener.local_addr()?;
48        let config = self.inner.config.clone();
49        crate::log::info(&format!(
50            "ready on http://{}:{} ({}, {} routes)",
51            if local.ip().is_unspecified() { "localhost".to_string() } else { local.ip().to_string() },
52            local.port(),
53            self.environment().as_str(),
54            self.inner.routes.pages.len() + self.inner.routes.apis.len()
55        ));
56
57        let mut builder = auto::Builder::new(TokioExecutor::new());
58        builder
59            .http1()
60            .timer(TokioTimer::new())
61            .header_read_timeout(Duration::from_secs(config.server.header_timeout.max(1)))
62            .keep_alive(true);
63        if config.server.http2 {
64            builder.http2().timer(TokioTimer::new()).keep_alive_interval(Duration::from_secs(20));
65        }
66        let graceful = GracefulShutdown::new();
67        let shutdown = shutdown_signal();
68        tokio::pin!(shutdown);
69
70        loop {
71            tokio::select! {
72                accepted = listener.accept() => {
73                    let (stream, remote) = match accepted {
74                        Ok(v) => v,
75                        Err(e) => {
76                            crate::log::warn(&format!("accept failed: {e}"));
77                            tokio::time::sleep(Duration::from_millis(50)).await;
78                            continue;
79                        }
80                    };
81                    let _ = stream.set_nodelay(true);
82                    let app = self.clone();
83                    let service = hyper::service::service_fn(move |req: hyper::Request<Incoming>| {
84                        let app = app.clone();
85                        async move { Ok::<_, Infallible>(app.handle_hyper(req, Some(remote)).await) }
86                    });
87                    let conn = builder.serve_connection_with_upgrades(TokioIo::new(stream), service);
88                    let conn = graceful.watch(conn.into_owned());
89                    tokio::spawn(async move {
90                        if let Err(e) = conn.await {
91                            crate::log::debug(&format!("connection error: {e}"));
92                        }
93                    });
94                }
95                _ = &mut shutdown => {
96                    crate::log::info("shutting down gracefully");
97                    break;
98                }
99            }
100        }
101        let wait = Duration::from_secs(config.server.shutdown_timeout);
102        tokio::select! {
103            _ = graceful.shutdown() => {}
104            _ = tokio::time::sleep(wait) => crate::log::warn("shutdown timeout: closing remaining connections"),
105        }
106        Ok(())
107    }
108
109    /// Handle a hyper request (used by the built-in server).
110    pub async fn handle_hyper(
111        &self,
112        req: hyper::Request<Incoming>,
113        remote: Option<SocketAddr>,
114    ) -> hyper::Response<HyperBody> {
115        let start = Instant::now();
116        let config = &self.inner.config;
117        let (parts, body) = req.into_parts();
118        let method = parts.method.clone();
119        let path = parts.uri.path().to_owned();
120        let accept_encoding = parts.headers.get("accept-encoding").and_then(|v| v.to_str().ok()).map(str::to_owned);
121        let body = body.map_err(|e| Box::new(e) as BoxError).boxed_unsync();
122        let request = Request::from_parts(parts, body, remote, config.server.body_limit, !self.environment().is_dev());
123
124        let timeout = config.server.request_timeout;
125        let res = if timeout > 0 {
126            match tokio::time::timeout(Duration::from_secs(timeout), self.handle(request)).await {
127                Ok(r) => r,
128                Err(_) => Response::text("Gateway Timeout").with_status(504),
129            }
130        } else {
131            self.handle(request).await
132        };
133
134        if config.logging.requests(self.environment()) && !path.starts_with("/_nr/dev/") {
135            let id = res.header("x-request-id").map(str::to_owned);
136            crate::log::request(
137                method.as_str(),
138                &path,
139                res.status.as_u16(),
140                start.elapsed().as_secs_f64() * 1000.0,
141                id.as_deref(),
142            );
143        }
144        let res = if config.server.compression { compress(res, accept_encoding.as_deref()) } else { res };
145        into_hyper(res)
146    }
147}
148
149/// Convert a framework response into a hyper response.
150pub fn into_hyper(res: Response) -> hyper::Response<HyperBody> {
151    let body: HyperBody = match res.body {
152        Body::Empty => Empty::new().map_err(|never| match never {}).boxed_unsync(),
153        Body::Bytes(b) => Full::new(b).map_err(|never| match never {}).boxed_unsync(),
154        Body::Stream(s) => StreamBody::new(s.map(|r| r.map(Frame::data))).boxed_unsync(),
155    };
156    let mut out = hyper::Response::new(body);
157    *out.status_mut() = res.status;
158    *out.headers_mut() = res.headers;
159    out
160}
161
162async fn shutdown_signal() {
163    let ctrl_c = async {
164        let _ = tokio::signal::ctrl_c().await;
165    };
166    #[cfg(unix)]
167    let terminate = async {
168        match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
169            Ok(mut s) => {
170                s.recv().await;
171            }
172            Err(_) => std::future::pending::<()>().await,
173        }
174    };
175    #[cfg(not(unix))]
176    let terminate = std::future::pending::<()>();
177    tokio::select! {
178        _ = ctrl_c => {}
179        _ = terminate => {}
180    }
181}
182
183#[cfg(feature = "compression")]
184fn accepts_gzip(header: Option<&str>) -> bool {
185    header.is_some_and(|h| {
186        h.split(',').any(|part| {
187            let mut it = part.trim().split(';');
188            let coding = it.next().unwrap_or("").trim();
189            let q = it.find_map(|p| p.trim().strip_prefix("q=")).and_then(|q| q.parse::<f32>().ok()).unwrap_or(1.0);
190            (coding.eq_ignore_ascii_case("gzip") || coding == "*") && q > 0.0
191        })
192    })
193}
194
195#[cfg(feature = "compression")]
196pub(crate) fn compress(mut res: Response, accept_encoding: Option<&str>) -> Response {
197    use std::io::Write;
198
199    use flate2::Compression;
200    use flate2::write::GzEncoder;
201
202    let ct = res.header("content-type").unwrap_or("");
203    let eligible = accepts_gzip(accept_encoding)
204        && !res.headers.contains_key("content-encoding")
205        && res.status != http::StatusCode::PARTIAL_CONTENT
206        && res.status != http::StatusCode::NOT_MODIFIED
207        && !ct.starts_with("text/event-stream")
208        && next_rust_assets::mime::is_compressible(ct);
209    if !eligible {
210        return res;
211    }
212    match std::mem::replace(&mut res.body, Body::Empty) {
213        Body::Bytes(b) if b.len() >= 1024 => {
214            let mut enc = GzEncoder::new(Vec::with_capacity(b.len() / 3), Compression::fast());
215            if enc.write_all(&b).is_err() {
216                res.body = Body::Bytes(b);
217                return res;
218            }
219            match enc.finish() {
220                Ok(out) => {
221                    res.body = Body::Bytes(Bytes::from(out));
222                    res.headers.remove("content-length");
223                    res.set_header("content-encoding", "gzip");
224                    res.append_header("vary", "Accept-Encoding");
225                }
226                Err(_) => res.body = Body::Bytes(b),
227            }
228        }
229        Body::Stream(stream) => {
230            // Each chunk is compressed and sync-flushed so streamed HTML still
231            // reaches the browser progressively.
232            let state = (stream, Some(GzEncoder::new(Vec::new(), Compression::fast())));
233            let compressed = futures_util::stream::unfold(state, |(mut stream, enc)| async move {
234                let mut enc = enc?;
235                match stream.next().await {
236                    Some(Ok(chunk)) => {
237                        if enc.write_all(&chunk).is_err() || enc.flush().is_err() {
238                            return Some((Err::<Bytes, BoxError>("gzip failure".into()), (stream, None)));
239                        }
240                        let out = std::mem::take(enc.get_mut());
241                        Some((Ok(Bytes::from(out)), (stream, Some(enc))))
242                    }
243                    Some(Err(e)) => Some((Err(e), (stream, None))),
244                    None => match enc.finish() {
245                        Ok(tail) => Some((Ok(Bytes::from(tail)), (stream, None))),
246                        Err(e) => Some((Err(e.into()), (stream, None))),
247                    },
248                }
249            });
250            res.body = Body::Stream(Box::pin(compressed));
251            res.headers.remove("content-length");
252            res.set_header("content-encoding", "gzip");
253            res.append_header("vary", "Accept-Encoding");
254        }
255        other => res.body = other,
256    }
257    res
258}
259
260#[cfg(not(feature = "compression"))]
261pub(crate) fn compress(res: Response, _accept_encoding: Option<&str>) -> Response {
262    res
263}