Module http::static_files
static file serving for std/http servers.
http_serve_file answers a request with one file; http_serve_dir maps a URL path under a directory root (rejecting anything that would escape it). Both handle what browsers and CDNs expect of a static server:
- Content-Type from the extension (http_mime_type),
- ETag (size + mtime) and Last-Modified validators, answering
If-None-Match / If-Modified-Since with 304,
- single byte ranges (Range: bytes=a-b / a- / -n, with If-Range) as
206 with Content-Range, 416 when unsatisfiable,
- HEAD (the server drops the body), Cache-Control: max-age,
- index.html for directories, an optional SPA fallback.
Only GET and HEAD are served; anything else is 405.
Files are read whole into the response body (a range reads only its part); this is a development/small-site server, not a sendfile() one.
Structs
| StaticOptions |
Functions
| http_mime_type | The media type for a file name's extension (case-insensitive): text/html; charset=utf-8, text/css, text/javascript, application/json, application/wasm, image/png, image/svg+xml, font/woff2, ... and application/octet-stream for anything unknown. |
| http_mime_is_compressible | 1 for text-like types worth compressing (text/*, json, javascript, xml, svg, wasm). |
| http_serve_file | Serves the file at 'file_path' for 'req' (see the file comment). 404 if it doesn't exist or is a directory. 'opts' may be null (defaults()). |
| http_serve_dir | Serves 'url_path' (percent-encoded, as in the request; a query string is ignored) from under the directory 'root'. 403 for a path that tries to leave the root ("..", a backslash, a NUL byte); a directory without a trailing '/' is redirected (301) to one so relative links resolve. |
| http_safe_join | Joins a URL path under 'root' after percent-decoding it and removing empty and "." segments; 0 (and 'out' untouched) if it contains a ".." segment, a backslash, a NUL byte or (unless allow_dotfiles) a segment starting with '.'. |
| http_file_info | Size (bytes), modification time (Unix seconds) and kind of a file: 1 for a regular file, 2 for a directory, 0 if it doesn't exist. |