Skip to content

Static files

Static file helpers live in lib.staticfiles (monorepo) or vow_web_server.staticfiles (vpm). They are not re-exported from the main barrel.

import lib.app
import lib.request
import lib.response
import lib.serve
import lib.staticfiles
var g_fr: FsRead = FsRead { detail: "" };
var g_root: String = "vow-examples/vow-web-server/public";
fn static_route(req: request.Request) -> response.Response {
return staticfiles.files_handler(g_fr, g_root, req);
}
fn main(caps: Caps) -> int {
return match caps.fs_read {
Some(fr) => match caps.net {
Some(cap) => {
g_fr = fr;
let a = app.get(app.get(app.app(), "/", static_route), "/index.html", static_route);
serve.listen(a, cap, 8787)
},
None => 0 - 1,
},
None => 0 - 1,
};
}

Run:

Terminal window
./vow run vow-examples/vow-web-server/static_site.vow -- --grant net: --grant fs_read:
curl http://127.0.0.1:8787/

Example assets: vow-examples/vow-web-server/public/index.html.

Function Role
files_handler(fr, root, req) Map req.path → file under root
file_at(fr, root, url_path) Serve a specific URL path
mime_for(path) Extension → Content-Type
normalize_path(url_path) Collapse //, reject ..
static_middleware(fr, root, req) Alias for files_handler

/ and empty paths serve index.html under the root directory.

Extension Content-Type
.html text/html; charset=utf-8
.css text/css; charset=utf-8
.js application/javascript; charset=utf-8
.json application/json; charset=utf-8
.png image/png
.svg image/svg+xml
.ico image/x-icon
other application/octet-stream

normalize_path rejects paths containing .. and collapses repeated slashes. Traversal attempts return 404 with {"error":"not found"}.

assert len(staticfiles.normalize_path("/../etc/passwd")) == 0;

Reading files requires the fs_read capability passed into main as caps.fs_read:

Terminal window
./vow run vow-examples/vow-web-server/static_site.vow -- --grant net: --grant fs_read:

Grant forms follow the same capability model as Capabilities. Without fs_read, read_file inside file_at fails and the handler returns 404.