Skip to main content

typst_kit/
server.rs

1//! A minimal hot-reloading HTTP server.
2
3#![cfg(feature = "http-server")]
4
5use std::io::{self, Write};
6use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener};
7use std::sync::Arc;
8
9use ecow::eco_format;
10use parking_lot::{Condvar, Mutex, MutexGuard};
11use percent_encoding::percent_decode_str;
12use tiny_http::{Header, Request, Response, StatusCode};
13use typst_library::diag::{StrResult, bail};
14use typst_library::foundations::Bytes;
15
16type Router = Box<dyn Fn(&str) -> Option<HttpBody> + Send + Sync>;
17type RouterBucket = Bucket<Router>;
18
19/// Serves HTML with live reload.
20pub struct HttpServer {
21    addr: SocketAddr,
22    bucket: Arc<RouterBucket>,
23}
24
25impl HttpServer {
26    /// Create a new HTTP server that serves live HTML.
27    pub fn new(title: &str, port: Option<u16>, live: bool) -> StrResult<Self> {
28        let (addr, server) = start_server(port)?;
29
30        let placeholder = PLACEHOLDER_HTML.replace("{INPUT}", title);
31        let bucket = Arc::new(Bucket::new(html_single_fs(placeholder)));
32        let bucket2 = bucket.clone();
33
34        std::thread::spawn(move || {
35            for req in server.incoming_requests() {
36                let _ = handle(req, live, &bucket2);
37            }
38        });
39
40        Ok(Self { addr, bucket })
41    }
42
43    /// The address that we serve the HTML on.
44    pub fn addr(&self) -> SocketAddr {
45        self.addr
46    }
47
48    /// Updates the served contents to a page of HTML served on `/`, triggering
49    /// a reload in all connected browsers.
50    pub fn set_html(&self, html: String) {
51        self.bucket.put(html_single_fs(html));
52    }
53
54    /// Updates the served contents to a bundle.
55    #[cfg(feature = "bundle")]
56    pub fn set_bundle(&self, bundle: typst_bundle::Bundle, fs: typst_bundle::VirtualFs) {
57        self.set_router(move |route| {
58            let path = typst_syntax::VirtualPath::new(route).ok()?;
59            let with_index = path.join("index.html").unwrap();
60            for path in [path, with_index] {
61                let Some(data) = fs.get(&path) else { continue };
62                let body = if matches!(
63                    bundle.files.get(&path),
64                    Some(typst_bundle::BundleFile::Document(
65                        typst_bundle::BundleDocument::Html(_)
66                    ))
67                ) && let Ok(string) = data.as_str()
68                {
69                    HttpBody::Html(string.to_owned())
70                } else {
71                    HttpBody::Raw(data.clone())
72                };
73                return Some(body);
74            }
75
76            None
77        });
78    }
79
80    /// Updates the content handler, triggering a reload in all connected browsers.
81    pub fn set_router<R>(&self, router: R)
82    where
83        R: Fn(&str) -> Option<HttpBody> + Send + Sync + 'static,
84    {
85        self.bucket.put(Box::new(router));
86    }
87}
88
89/// Creates a handler that serves just one HTML page at `/`.
90fn html_single_fs(html: String) -> Router {
91    Box::new(move |route| (route == "/").then(|| HttpBody::Html(html.clone())))
92}
93
94/// Something that can be served by the [`HttpServer`].
95pub enum HttpBody {
96    /// An HTML page.
97    ///
98    /// The string must contain valid HTML. If live reload is enabled, a script
99    /// will be injected into the HTML.
100    Html(String),
101    /// A raw body that does not support live reload.
102    Raw(Bytes),
103}
104
105/// Starts a local HTTP server.
106///
107/// Uses the specified port or tries to find a free port in the range
108/// `3000..=3005`.
109fn start_server(port: Option<u16>) -> StrResult<(SocketAddr, tiny_http::Server)> {
110    const BASE_PORT: u16 = 3000;
111
112    let mut addr;
113    let mut retries = 0;
114
115    let listener = loop {
116        addr = SocketAddr::new(
117            IpAddr::V4(Ipv4Addr::LOCALHOST),
118            port.unwrap_or(BASE_PORT + retries),
119        );
120
121        match TcpListener::bind(addr) {
122            Ok(listener) => break listener,
123            Err(err) if err.kind() == io::ErrorKind::AddrInUse => {
124                if let Some(port) = port {
125                    bail!("port {port} is already in use")
126                } else if retries < 5 {
127                    // If the port is in use, try the next one.
128                    retries += 1;
129                } else {
130                    bail!("could not find free port for HTTP server");
131                }
132            }
133            Err(err) => bail!("failed to start TCP server: {err}"),
134        }
135    };
136
137    let server = tiny_http::Server::from_listener(listener, None)
138        .map_err(|err| eco_format!("failed to start HTTP server: {err}"))?;
139
140    Ok((addr, server))
141}
142
143/// Handles a request.
144fn handle(req: Request, reload: bool, bucket: &Arc<RouterBucket>) -> io::Result<()> {
145    let base = url::Url::parse("http://localhost").unwrap();
146    let Ok(url) = base.join(req.url()) else {
147        return req.respond(Response::empty(StatusCode(400)));
148    };
149
150    let path = url.path();
151    let Ok(path) = percent_decode_str(path).decode_utf8() else {
152        return req.respond(Response::empty(StatusCode(400)));
153    };
154
155    if path == "/__events" {
156        return handle_events(req, bucket.clone());
157    }
158
159    let fs = bucket.get();
160    let Some(body) = fs(path.as_ref()) else {
161        return req.respond(Response::empty(StatusCode(404)));
162    };
163
164    handle_body(req, reload, body)
165}
166
167/// Handles for the `/` route. Serves the compiled HTML.
168fn handle_body(req: Request, reload: bool, mut body: HttpBody) -> io::Result<()> {
169    let (data, mime) = match &mut body {
170        HttpBody::Html(html) => {
171            if reload {
172                inject_live_reload_script(html);
173            }
174            (html.as_bytes(), Some("text/html"))
175        }
176        HttpBody::Raw(data) => (data.as_slice(), select_mime_type(req.url(), data)),
177    };
178
179    let mut headers = Vec::new();
180    if let Some(mime) = mime {
181        headers.push(Header::from_bytes("Content-Type", mime).unwrap());
182    }
183
184    req.respond(Response::new(StatusCode(200), headers, data, Some(data.len()), None))
185}
186
187/// Handler for the `/__events` route.
188fn handle_events(req: Request, bucket: Arc<RouterBucket>) -> io::Result<()> {
189    std::thread::spawn(move || {
190        // When this returns an error, the client is disconnected and we can
191        // terminate the thread.
192        let _ = handle_events_blocking(req, &bucket);
193    });
194    Ok(())
195}
196
197/// Event stream for the `/events` route.
198fn handle_events_blocking(req: Request, bucket: &RouterBucket) -> io::Result<()> {
199    let mut writer = req.into_writer();
200    let writer: &mut dyn Write = &mut *writer;
201
202    // We need to write the header manually because `tiny-http` defaults to
203    // `Transfer-Encoding: chunked` when no `Content-Length` is provided, which
204    // Chrome & Safari dislike for `Content-Type: text/event-stream`.
205    write!(writer, "HTTP/1.1 200 OK\r\n")?;
206    write!(writer, "Content-Type: text/event-stream\r\n")?;
207    write!(writer, "Cache-Control: no-cache\r\n")?;
208    write!(writer, "\r\n")?;
209    writer.flush()?;
210
211    // If the user closes the browser tab, this loop will terminate once it
212    // tries to write to the dead socket for the first time.
213    loop {
214        bucket.wait();
215        // Trigger a server-sent event. The browser is listening to it via
216        // an `EventSource` listener` (see `inject_script`).
217        write!(writer, "event: reload\ndata:\n\n")?;
218        writer.flush()?;
219    }
220}
221
222/// Injects the live reload script into a string of HTML.
223fn inject_live_reload_script(html: &mut String) {
224    let pos = html.rfind("</body>").unwrap_or(html.len());
225    html.insert_str(pos, LIVE_RELOAD_SCRIPT);
226}
227
228/// Selects a MIME type for a request based on path and data.
229fn select_mime_type(path: &str, buf: &[u8]) -> Option<&'static str> {
230    match path.rsplit_once('.').map(|(_, r)| r) {
231        Some("html") => Some("text/html"),
232        Some("pdf") => Some("application/pdf"),
233        Some("png") => Some("image/png"),
234        Some("svg") => Some("image/svg+xml"),
235        Some("css") => Some("text/css"),
236        Some("js") => Some("text/javascript"),
237        _ => infer::get(buf).map(|ty| ty.mime_type()),
238    }
239}
240
241/// Holds data and notifies consumers when it's updated.
242struct Bucket<T> {
243    mutex: Mutex<T>,
244    condvar: Condvar,
245}
246
247impl<T> Bucket<T> {
248    /// Creates a new bucket with initial data.
249    fn new(init: T) -> Self {
250        Self { mutex: Mutex::new(init), condvar: Condvar::new() }
251    }
252
253    /// Retrieves the current data in the bucket.
254    fn get(&self) -> MutexGuard<'_, T> {
255        self.mutex.lock()
256    }
257
258    /// Puts new data into the bucket and notifies everyone who's currently
259    /// [waiting](Self::wait).
260    fn put(&self, data: T) {
261        *self.mutex.lock() = data;
262        self.condvar.notify_all();
263    }
264
265    /// Waits for new data in the bucket.
266    fn wait(&self) {
267        self.condvar.wait(&mut self.mutex.lock());
268    }
269}
270
271/// The initial HTML before compilation is finished.
272const PLACEHOLDER_HTML: &str = "\
273<!DOCTYPE html>
274<html>
275  <head>
276    <meta charset=\"utf-8\">
277    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
278    <title>Waiting for {INPUT}</title>
279    <style>
280      body {
281        display: flex;
282        justify-content: center;
283        align-items: center;
284        color: #565565;
285        background: #eff0f3;
286      }
287
288      body > main > div {
289        margin-block: 16px;
290        text-align: center;
291      }
292    </style>
293  </head>
294  <body>
295    <main>
296      <div>Waiting for output…</div>
297      <div><code>typst watch {INPUT}</code></div>
298    </main>
299  </body>
300</html>
301";
302
303/// Reloads the page whenever it receives a "reload" server-sent event
304/// on the `/__events` route.
305const LIVE_RELOAD_SCRIPT: &str = "\
306<script>\
307  new EventSource(\"/__events\")\
308    .addEventListener(\"reload\", () => location.reload())\
309</script>\
310";