web: some stuff
This commit is contained in:
56
packages/web/src/RequestHandler.zig
Normal file
56
packages/web/src/RequestHandler.zig
Normal file
@@ -0,0 +1,56 @@
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user