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};
21#[cfg(feature = "http2")]
22use hyper_util::rt::TokioExecutor;
23use hyper_util::rt::{TokioIo, TokioTimer};
24#[cfg(feature = "http2")]
25use hyper_util::server::conn::auto;
26use tokio::net::TcpListener;
27
28use crate::app::App;
29use crate::request::{BoxError, Request};
30use crate::response::{Body, Response};
31
32pub type HyperBody = UnsyncBoxBody<Bytes, BoxError>;
33
34impl App {
35    /// Bind to the configured address and serve until shutdown.
36    pub async fn serve(self) -> std::io::Result<()> {
37        let addr = self.config().socket_addr_string();
38        let listener = TcpListener::bind(&addr).await.map_err(|e| {
39            std::io::Error::new(
40                e.kind(),
41                format!("could not bind {addr}: {e} (is another server running? set PORT to change the port)"),
42            )
43        })?;
44        self.serve_listener(listener).await
45    }
46
47    /// Serve on an existing listener (tests, socket activation).
48    pub async fn serve_listener(self, listener: TcpListener) -> std::io::Result<()> {
49        let local = listener.local_addr()?;
50        let config = self.inner.config.clone();
51        let banner = crate::banner::enabled(self.environment());
52        if banner {
53            crate::banner::started(local, self.inner.routes.pages.len(), self.inner.routes.apis.len());
54        } else {
55            crate::log::info(&format!(
56                "ready on http://{}:{} ({}, {} routes)",
57                if local.ip().is_unspecified() { "localhost".to_string() } else { local.ip().to_string() },
58                local.port(),
59                self.environment().as_str(),
60                self.inner.routes.pages.len() + self.inner.routes.apis.len()
61            ));
62        }
63
64        let header_timeout = Duration::from_secs(config.server.header_timeout.max(1));
65        #[cfg(feature = "http2")]
66        let builder = {
67            let mut builder = auto::Builder::new(TokioExecutor::new());
68            builder.http1().timer(TokioTimer::new()).header_read_timeout(header_timeout).keep_alive(true);
69            if config.server.http2 {
70                builder.http2().timer(TokioTimer::new()).keep_alive_interval(Duration::from_secs(20));
71            } else {
72                builder = builder.http1_only();
73            }
74            builder
75        };
76        #[cfg(not(feature = "http2"))]
77        let builder = {
78            if config.server.http2 {
79                crate::log::warn("[server] http2 = true needs the `http2` feature of next-rust; serving HTTP/1.1");
80            }
81            let mut builder = hyper::server::conn::http1::Builder::new();
82            builder.timer(TokioTimer::new()).header_read_timeout(header_timeout).keep_alive(true);
83            builder
84        };
85        // Every connection task holds a receiver: sending tells them to finish
86        // gracefully, and `closed()` resolves once all of them are done.
87        let (graceful, _) = tokio::sync::watch::channel(());
88        let shutdown = shutdown_signal();
89        tokio::pin!(shutdown);
90
91        loop {
92            tokio::select! {
93                accepted = listener.accept() => {
94                    let (stream, remote) = match accepted {
95                        Ok(v) => v,
96                        Err(e) => {
97                            crate::log::warn(&format!("accept failed: {e}"));
98                            tokio::time::sleep(Duration::from_millis(50)).await;
99                            continue;
100                        }
101                    };
102                    let _ = stream.set_nodelay(true);
103                    let app = self.clone();
104                    let service = hyper::service::service_fn(move |req: hyper::Request<Incoming>| {
105                        let app = app.clone();
106                        async move { Ok::<_, Infallible>(app.handle_hyper(req, Some(remote)).await) }
107                    });
108                    #[cfg(feature = "http2")]
109                    let conn = builder.serve_connection_with_upgrades(TokioIo::new(stream), service).into_owned();
110                    #[cfg(not(feature = "http2"))]
111                    let conn = builder.serve_connection(TokioIo::new(stream), service).with_upgrades();
112                    let mut stop = graceful.subscribe();
113                    tokio::spawn(async move {
114                        let mut conn = std::pin::pin!(conn);
115                        let result = tokio::select! {
116                            result = conn.as_mut() => result,
117                            _ = stop.changed() => {
118                                conn.as_mut().graceful_shutdown();
119                                conn.await
120                            }
121                        };
122                        if let Err(e) = result {
123                            crate::log::debug(&format!("connection error: {e}"));
124                        }
125                        drop(stop);
126                    });
127                }
128                _ = &mut shutdown => {
129                    if banner {
130                        crate::banner::stopping();
131                    }
132                    crate::log::info("shutting down gracefully");
133                    break;
134                }
135            }
136        }
137        let wait = Duration::from_secs(config.server.shutdown_timeout);
138        tokio::select! {
139            _ = async {
140                let _ = graceful.send(());
141                graceful.closed().await;
142            } => {}
143            _ = tokio::time::sleep(wait) => crate::log::warn("shutdown timeout: closing remaining connections"),
144        }
145        if banner {
146            crate::banner::stopped();
147        }
148        Ok(())
149    }
150
151    /// Handle a hyper request (used by the built-in server).
152    pub async fn handle_hyper(
153        &self,
154        req: hyper::Request<Incoming>,
155        remote: Option<SocketAddr>,
156    ) -> hyper::Response<HyperBody> {
157        let start = Instant::now();
158        let config = &self.inner.config;
159        let (parts, body) = req.into_parts();
160        let method = parts.method.clone();
161        let path = parts.uri.path().to_owned();
162        let accept_encoding = parts.headers.get("accept-encoding").and_then(|v| v.to_str().ok()).map(str::to_owned);
163        let body = body.map_err(|e| Box::new(e) as BoxError).boxed_unsync();
164        let request = Request::from_parts(parts, body, remote, config.server.body_limit, !self.environment().is_dev());
165
166        let timeout = config.server.request_timeout;
167        let res = if timeout > 0 {
168            match tokio::time::timeout(Duration::from_secs(timeout), self.handle(request)).await {
169                Ok(r) => r,
170                Err(_) => Response::text("Gateway Timeout").with_status(504),
171            }
172        } else {
173            self.handle(request).await
174        };
175
176        if config.logging.requests(self.environment()) && !path.starts_with("/_next-rust/dev/") {
177            let id = res.header("x-request-id").map(str::to_owned);
178            // Action URLs carry per-visitor tokens: keep them out of logs.
179            let logged = if path.starts_with("/_next-rust/action/") { "/_next-rust/action/…" } else { path.as_str() };
180            crate::log::request(
181                method.as_str(),
182                logged,
183                res.status.as_u16(),
184                start.elapsed().as_secs_f64() * 1000.0,
185                id.as_deref(),
186            );
187        }
188        let res = if config.server.compression { compress(res, accept_encoding.as_deref()) } else { res };
189        into_hyper(res)
190    }
191}
192
193/// Convert a framework response into a hyper response.
194pub fn into_hyper(res: Response) -> hyper::Response<HyperBody> {
195    let body: HyperBody = match res.body {
196        Body::Empty => Empty::new().map_err(|never| match never {}).boxed_unsync(),
197        Body::Bytes(b) => Full::new(b).map_err(|never| match never {}).boxed_unsync(),
198        Body::Stream(s) => StreamBody::new(s.map(|r| r.map(Frame::data))).boxed_unsync(),
199    };
200    let mut out = hyper::Response::new(body);
201    *out.status_mut() = res.status;
202    *out.headers_mut() = res.headers;
203    out
204}
205
206async fn shutdown_signal() {
207    let ctrl_c = async {
208        let _ = tokio::signal::ctrl_c().await;
209    };
210    #[cfg(unix)]
211    let terminate = async {
212        match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
213            Ok(mut s) => {
214                s.recv().await;
215            }
216            Err(_) => std::future::pending::<()>().await,
217        }
218    };
219    #[cfg(not(unix))]
220    let terminate = std::future::pending::<()>();
221    tokio::select! {
222        _ = ctrl_c => {}
223        _ = terminate => {}
224    }
225}
226
227#[cfg(feature = "compression")]
228/// Whether `Accept-Encoding` allows `coding`.
229pub(crate) fn accepts(header: Option<&str>, coding: &str) -> bool {
230    header.is_some_and(|h| {
231        h.split(',').any(|part| {
232            let mut it = part.trim().split(';');
233            let name = it.next().unwrap_or("").trim();
234            let q = it.find_map(|p| p.trim().strip_prefix("q=")).and_then(|q| q.parse::<f32>().ok()).unwrap_or(1.0);
235            (name.eq_ignore_ascii_case(coding) || name == "*") && q > 0.0
236        })
237    })
238}
239
240/// A streaming encoder: Brotli when the client accepts it, gzip otherwise.
241#[cfg(feature = "compression")]
242enum Encoder {
243    Br(Box<brotli::CompressorWriter<Vec<u8>>>),
244    Gz(flate2::write::GzEncoder<Vec<u8>>),
245}
246
247#[cfg(feature = "compression")]
248impl Encoder {
249    /// Fast settings: pages are compressed per request.
250    fn new(br: bool) -> Self {
251        if br {
252            Encoder::Br(Box::new(brotli::CompressorWriter::new(Vec::with_capacity(8192), 4096, 4, 20)))
253        } else {
254            Encoder::Gz(flate2::write::GzEncoder::new(Vec::with_capacity(8192), flate2::Compression::fast()))
255        }
256    }
257
258    fn coding(&self) -> &'static str {
259        match self {
260            Encoder::Br(_) => "br",
261            Encoder::Gz(_) => "gzip",
262        }
263    }
264
265    fn write(&mut self, data: &[u8]) -> std::io::Result<()> {
266        use std::io::Write;
267        match self {
268            Encoder::Br(w) => w.write_all(data),
269            Encoder::Gz(w) => w.write_all(data),
270        }
271    }
272
273    /// Everything encoded so far, flushed so the browser can use it.
274    fn take(&mut self) -> std::io::Result<Vec<u8>> {
275        use std::io::Write;
276        match self {
277            Encoder::Br(w) => {
278                w.flush()?;
279                Ok(std::mem::take(w.get_mut()))
280            }
281            Encoder::Gz(w) => {
282                w.flush()?;
283                Ok(std::mem::take(w.get_mut()))
284            }
285        }
286    }
287
288    fn finish(self) -> std::io::Result<Vec<u8>> {
289        match self {
290            Encoder::Br(w) => Ok(w.into_inner()),
291            Encoder::Gz(w) => w.finish(),
292        }
293    }
294}
295
296#[cfg(feature = "compression")]
297pub(crate) fn compress(mut res: Response, accept_encoding: Option<&str>) -> Response {
298    let ct = res.header("content-type").unwrap_or("");
299    let br = accepts(accept_encoding, "br");
300    let eligible = (br || accepts(accept_encoding, "gzip"))
301        && !res.headers.contains_key("content-encoding")
302        && res.status != http::StatusCode::PARTIAL_CONTENT
303        && res.status != http::StatusCode::NOT_MODIFIED
304        && !ct.starts_with("text/event-stream")
305        && next_rust_assets::mime::is_compressible(ct);
306    if !eligible {
307        return res;
308    }
309    let coding = Encoder::new(br).coding();
310    match std::mem::replace(&mut res.body, Body::Empty) {
311        Body::Bytes(b) if b.len() >= 1024 => {
312            let mut enc = Encoder::new(br);
313            match enc.write(&b).and_then(|()| enc.finish()) {
314                Ok(out) => {
315                    res.body = Body::Bytes(Bytes::from(out));
316                    res.headers.remove("content-length");
317                    res.set_header("content-encoding", coding);
318                    res.append_header("vary", "Accept-Encoding");
319                }
320                Err(_) => res.body = Body::Bytes(b),
321            }
322        }
323        Body::Stream(stream) => {
324            // Each chunk is compressed and flushed so streamed HTML still
325            // reaches the browser progressively.
326            let state = (stream, Some(Encoder::new(br)));
327            let compressed = futures_util::stream::unfold(state, |(mut stream, enc)| async move {
328                let mut enc = enc?;
329                match stream.next().await {
330                    Some(Ok(chunk)) => match enc.write(&chunk).and_then(|()| enc.take()) {
331                        Ok(out) => Some((Ok(Bytes::from(out)), (stream, Some(enc)))),
332                        Err(e) => Some((Err::<Bytes, BoxError>(e.into()), (stream, None))),
333                    },
334                    Some(Err(e)) => Some((Err(e), (stream, None))),
335                    None => match enc.finish() {
336                        Ok(tail) => Some((Ok(Bytes::from(tail)), (stream, None))),
337                        Err(e) => Some((Err(e.into()), (stream, None))),
338                    },
339                }
340            });
341            res.body = Body::Stream(Box::pin(compressed));
342            res.headers.remove("content-length");
343            res.set_header("content-encoding", coding);
344            res.append_header("vary", "Accept-Encoding");
345        }
346        other => res.body = other,
347    }
348    res
349}
350
351#[cfg(not(feature = "compression"))]
352pub(crate) fn compress(res: Response, _accept_encoding: Option<&str>) -> Response {
353    res
354}
355
356#[cfg(all(test, feature = "compression"))]
357mod compression_tests {
358    use std::io::Read;
359
360    use bytes::Bytes;
361    use futures_util::StreamExt;
362
363    use super::compress;
364    use crate::response::{Body, Response};
365
366    fn page() -> String {
367        "<p>Hello, compressed world.</p>".repeat(100)
368    }
369
370    fn inflate(coding: &str, data: &[u8]) -> String {
371        let mut out = String::new();
372        match coding {
373            "br" => brotli::Decompressor::new(data, 4096).read_to_string(&mut out).unwrap(),
374            _ => flate2::read::GzDecoder::new(data).read_to_string(&mut out).unwrap(),
375        };
376        out
377    }
378
379    #[tokio::test]
380    async fn brotli_is_preferred_and_gzip_still_works() {
381        for (accept, coding) in [("gzip, deflate, br", "br"), ("gzip", "gzip"), ("br;q=0, gzip", "gzip")] {
382            let res = compress(Response::html(page()), Some(accept));
383            assert_eq!(res.header("content-encoding"), Some(coding), "{accept}");
384            assert_eq!(res.header("vary"), Some("Accept-Encoding"));
385            let body = res.into_bytes().await.unwrap();
386            assert_eq!(inflate(coding, &body), page());
387        }
388        let plain = compress(Response::html(page()), Some("identity"));
389        assert_eq!(plain.header("content-encoding"), None);
390        assert_eq!(compress(Response::html("tiny"), Some("br")).header("content-encoding"), None, "small bodies stay");
391    }
392
393    #[tokio::test]
394    async fn streamed_bodies_are_flushed_per_chunk() {
395        let chunks = vec![Ok::<_, crate::request::BoxError>(Bytes::from(page())), Ok(Bytes::from("<!-- end -->"))];
396        let stream: crate::response::ByteStream = Box::pin(futures_util::stream::iter(chunks));
397        let res = compress(
398            Response::new(http::StatusCode::OK, Body::Stream(stream)).with_content_type("text/html"),
399            Some("br"),
400        );
401        assert_eq!(res.header("content-encoding"), Some("br"));
402        let Body::Stream(mut s) = res.body else { panic!("stream") };
403        let mut parts = Vec::new();
404        while let Some(chunk) = s.next().await {
405            parts.push(chunk.unwrap());
406        }
407        assert!(parts.len() >= 2, "one compressed chunk per source chunk: {}", parts.len());
408        assert_eq!(inflate("br", &parts.concat()), page() + "<!-- end -->");
409    }
410}