Files
castle/packages/web/src/RequestHandler.zig
2026-03-07 21:08:22 +01:00

57 lines
1.9 KiB
Zig

const std = @import("std");
const RequestHandler = @This();
const Header = @import("http/Header.zig");
const Response = @import("Response.zig");
const Route = @import("Route.zig");
const Worker = @import("Worker.zig");
ptr: *anyopaque,
vtable: *const VTable,
pub const VTable = struct {
/// Called multiple times (could be zero) for each header in the request.
header: *const fn (self: *anyopaque, response: *Response, header: Header) anyerror!void,
/// Called exactly once after the whole request is received. When there is
/// no body, then `body.len == 0`.
body: *const fn (self: *anyopaque, response: *Response, body: []const u8) anyerror!void,
/// Called when the request parsing has halted. Possible reasons are:
///
/// 1. One of the calls to this object returned an error.
/// 2. The request was malformed and the HTTP parser returned an error.
/// 3. The whole request was received.
///
/// When no errors occurs (the third case), this method will be call after
/// `body`. This method should only be used to clean up internal resources,
/// if necessary.
finalize: *const fn (self: *anyopaque) void,
};
pub fn noHeader(self: *anyopaque, response: *Response, header: Header) anyerror!void {
_ = self;
_ = response;
_ = header;
}
pub fn noBody(self: *anyopaque, response: *Response, body: []const u8) anyerror!void {
_ = self;
_ = response;
_ = body;
}
pub fn noFinalize(self: *anyopaque) void {
_ = self;
}
pub inline fn rawHeader(rh: RequestHandler, response: *Response, header: Header) anyerror!void {
return rh.vtable.header(rh.ptr, response, header);
}
pub inline fn rawBody(rh: RequestHandler, response: *Response, body: []const u8) anyerror!void {
return rh.vtable.body(rh.ptr, response, body);
}
pub inline fn rawFinalize(rh: RequestHandler) void {
rh.vtable.finalize(rh.ptr);
}