web.zig
All projectsSmall Zig libraries for server-rendered applications. Pick the HTTP and HTML pieces you need. The application owns its routes, data and rendering decisions.
Explore the primitives
Match a path to a method
The consumer test matches GET /items/42 to a named parameter. The same path with POST is rejected by the router.
const Route = struct { method: std.http.Method, pattern: []const u8 };
const routes = [_]Route{.{ .method = .GET, .pattern = "/items/:id" }};
const matched = web_router.match(Route, &routes, .GET, "/items/42").matched;
if (!std.mem.eql(u8, matched.params.get("id").?, "42")) return error.RouteParameterLost;
if (web_router.match(Route, &routes, .POST, "/items/42") != .method_not_allowed) return error.MethodNotRejected;
Write text into a document
The consumer writes a complete document and checks that HTML-significant characters in its text are escaped.
var storage: [256]u8 = undefined;
var writer: std.Io.Writer = .fixed(&storage);
try web_html.documentStart(&writer, .{ .title = "Consumer" });
try writer.writeAll("<main>");
try web_html.text(&writer, "<unsafe & text>");
try writer.writeAll("</main>");
try web_html.documentEnd(&writer);
if (std.mem.indexOf(u8, writer.buffered(), "<unsafe & text>") == null) return error.EscapingFailed;
Send a typed response
The HTML helper sets the response type and delegates the status and extra headers to the shared response writer.
pub fn html(request: *std.http.Server.Request, body: []const u8, status: std.http.Status, extra: []const std.http.Header) !void {
try respond(request, body, .{
.status = status,
.content_type = "text/html; charset=utf-8",
.extra_headers = extra,
});
}
Choose a cache policy
The cache policy maps a small set of Zig values to HTTP headers. The application chooses the appropriate policy for each response.
pub fn headerValue(policy: Policy) []const u8 {
return switch (policy) {
.no_store => "private, no-store",
.private_revalidate => "private, no-cache",
.public_revalidate => "public, no-cache",
.immutable => "public, max-age=31536000, immutable",
};
}
Check the production policy
The header test checks the content type boundary, content security policy and HTTPS behavior of a production response.
test "production HTML policy emits explicit browser boundaries" {
const headers = build(.{
.content_security_policy = "default-src 'self'",
.secure_transport = true,
.noindex = true,
});
try std.testing.expectEqualStrings("nosniff", headers.get("x-content-type-options").?);
try std.testing.expectEqualStrings("default-src 'self'", headers.get("content-security-policy").?);
try std.testing.expect(headers.get("strict-transport-security") != null);
try std.testing.expect(headers.get("x-robots-tag") != null);
Where the application takes over
- RequestParse the request and route it.
- ApplicationAuthorize, load state and build a typed view.
- ResponseRender the complete HTML view through a writer.
The renderer consumes its view model without reading a database or changing application state. HTMX can enhance navigation after the initial page. Rendering contract