srctree

Gregory Mullen parent e76c4a6f f1f973b8
undo the mistake that was content injection

src/template.zig added: 36, removed: 62, total 0
@@ -1,5 +1,6 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const eql = std.mem.eql;
const allocPrint = std.fmt.allocPrint;
const log = std.log.scoped(.Verse);
 
@@ -40,17 +41,18 @@ fn tailPath(path: []const u8) []const u8 {
return path[0..0];
}
 
pub const builtin: [compiled.data.len]Template = blk: {
@setEvalBranchQuota(5000);
var t: [compiled.data.len]Template = undefined;
for (compiled.data, &t) |filedata, *dst| {
dst.* = Template{
pub const builtin: []const Template = constructTemplates();
 
fn constructTemplates() []const Template {
var t: []const Template = &[0]Template{};
for (compiled.data) |filedata| {
t = t ++ [_]Template{.{
.name = tailPath(filedata.path),
.blob = filedata.blob,
};
}};
}
break :blk t;
};
return t;
}
 
pub var dynamic: []const Template = undefined;
 
@@ -112,7 +114,7 @@ pub fn load(a: Allocator, comptime name: []const u8) Template {
 
pub fn findTemplate(comptime name: []const u8) Template {
inline for (builtin) |bi| {
if (comptime std.mem.eql(u8, bi.name, name)) {
if (comptime eql(u8, bi.name, name)) {
return bi;
}
}
@@ -120,6 +122,10 @@ pub fn findTemplate(comptime name: []const u8) Template {
}
 
pub fn PageData(comptime name: []const u8) type {
//const n = std.fmt.comptimePrint("search for {s}", .{"templates/" ++ name});
//const data = @embedFile(name);
//@compileLog(n);
//@compileLog(data.len);
const template = findTemplate(name);
const page_data = comptime findPageType(name);
return Page(template, page_data);
 
src/template/directive.zig added: 36, removed: 62, total 0
@@ -426,7 +426,7 @@ pub fn format(d: Directive, comptime _: []const u8, _: std.fmt.FormatOptions, ou
unreachable;
}
 
pub fn formatTyped(d: Directive, comptime T: type, ctx: T, inj: ?Pages.Injector, out: anytype) !void {
pub fn formatTyped(d: Directive, comptime T: type, ctx: T, out: anytype) !void {
switch (d.verb) {
.variable => {
if (d.known_type) |_| return d.doTyped(T, ctx, out);
@@ -455,7 +455,7 @@ pub fn formatTyped(d: Directive, comptime T: type, ctx: T, inj: ?Pages.Injector,
if (std.mem.eql(u8, field.name, realname)) {
if (@field(ctx, field.name)) |subdata| {
var subpage = template.pageOf(otype.child, subdata);
try subpage.format2("{}", inj, out);
try subpage.format("{}", .{}, out);
} else std.debug.print(
"sub template data was null for {s}\n",
.{field.name},
@@ -466,7 +466,7 @@ pub fn formatTyped(d: Directive, comptime T: type, ctx: T, inj: ?Pages.Injector,
if (std.mem.eql(u8, field.name, noun)) {
const subdata = @field(ctx, field.name);
var subpage = template.pageOf(@TypeOf(subdata), subdata);
try subpage.format2("{}", inj, out);
try subpage.format("{}", .{}, out);
}
},
else => {}, //@compileLog(field.type),
 
src/template/page.zig added: 36, removed: 62, total 0
@@ -2,11 +2,6 @@ const Templates = @import("../template.zig");
const Template = Templates.Template;
const Directive = Templates.Directive;
 
pub const Injector = struct {
ctx: *anyopaque,
func: *const fn (*anyopaque, []const u8) ?[]const u8,
};
 
pub fn PageRuntime(comptime PageDataType: type) type {
return struct {
pub const Self = @This();
@@ -21,11 +16,7 @@ pub fn PageRuntime(comptime PageDataType: type) type {
};
}
 
pub fn format(self: Self, comptime f: []const u8, _: std.fmt.FormatOptions, out: anytype) !void {
return try self.format2(f, null, out);
}
 
pub fn format2(self: Self, comptime _: []const u8, injt: ?Injector, out: anytype) !void {
pub fn format(self: Self, comptime _: []const u8, _: std.fmt.FormatOptions, out: anytype) !void {
//var ctx = self.data;
var blob = self.template.blob;
while (blob.len > 0) {
@@ -34,20 +25,11 @@ pub fn PageRuntime(comptime PageDataType: type) type {
blob = blob[offset..];
if (Directive.init(blob)) |drct| {
const end = drct.tag_block.len;
drct.formatTyped(PageDataType, self.data, injt, out) catch |err| switch (err) {
drct.formatTyped(PageDataType, self.data, out) catch |err| switch (err) {
error.IgnoreDirective => try out.writeAll(blob[0..end]),
error.VariableMissing => {
if (injt) |inj| {
if (inj.func(inj.ctx, blob[0..end])) |str| {
try out.writeAll(str);
} else {
if (!is_test) log.err("Template Error, variable missing {{{s}}} Injection failed", .{blob[0..end]});
try out.writeAll(blob[0..end]);
}
} else {
if (!is_test) log.err("Template Error, variable missing {{{s}}}", .{blob[0..end]});
try out.writeAll(blob[0..end]);
}
if (!is_test) log.err("Template Error, variable missing {{{s}}}", .{blob[0..end]});
try out.writeAll(blob[0..end]);
},
else => return err,
};
@@ -80,11 +62,7 @@ pub fn Page(comptime template: Template, comptime PageDataType: type) type {
return .{ .data = d };
}
 
pub fn format(self: Self, comptime f: []const u8, _: std.fmt.FormatOptions, out: anytype) !void {
return try self.format2(f, null, out);
}
 
pub fn format2(self: Self, comptime _: []const u8, injt: ?Injector, out: anytype) !void {
pub fn format(self: Self, comptime _: []const u8, _: std.fmt.FormatOptions, out: anytype) !void {
var blob = Self.PageTemplate.blob;
while (blob.len > 0) {
if (indexOfScalar(u8, blob, '<')) |offset| {
@@ -93,20 +71,11 @@ pub fn Page(comptime template: Template, comptime PageDataType: type) type {
 
if (Directive.init(blob)) |drct| {
const end = drct.tag_block.len;
drct.formatTyped(PageDataType, self.data, injt, out) catch |err| switch (err) {
drct.formatTyped(PageDataType, self.data, out) catch |err| switch (err) {
error.IgnoreDirective => try out.writeAll(blob[0..end]),
error.VariableMissing => {
if (injt) |inj| {
if (inj.func(inj.ctx, blob[0..end])) |str| {
try out.writeAll(str);
} else {
if (!is_test) log.err("Template Error, variable missing {{{s}}} Injection failed", .{blob[0..end]});
try out.writeAll(blob[0..end]);
}
} else {
if (!is_test) log.err("Template Error, variable missing {{{s}}}", .{blob[0..end]});
try out.writeAll(blob[0..end]);
}
if (!is_test) log.err("Template Error, variable missing {{{s}}}", .{blob[0..end]});
try out.writeAll(blob[0..end]);
},
else => return err,
};
 
src/verse.zig added: 36, removed: 62, total 0
@@ -26,8 +26,7 @@ uri: UriIter,
 
// TODO fix this unstable API
auth: Auth,
route_ctx: ?*const anyopaque = null,
page_injector: ?*const fn (*anyopaque, []const u8) ?[]const u8 = null,
endpoint_ctx: ?*const anyopaque = null,
 
// Raw move from response.zig
headers: Headers,
@@ -56,6 +55,8 @@ const VarPair = struct {
[]const u8,
};
 
pub const EndpointWrapper = struct {};
 
pub fn init(a: Allocator, req: *const Request, reqdata: RequestData) !Verse {
std.debug.assert(req.uri[0] == '/');
return .{
@@ -228,12 +229,10 @@ pub fn redirect(vrs: *Verse, loc: []const u8, see_other: bool) !void {
pub fn sendPage(vrs: *Verse, page: anytype) NetworkError!void {
try vrs.quickStart();
 
const inj: ?Template.Pages.Injector = if (vrs.page_injector) |pj| .{ .ctx = vrs, .func = pj } else null;
 
switch (vrs.downstream) {
.http, .zwsgi => |stream| {
const w = stream.writer();
page.format2("{}", inj, w) catch |err| switch (err) {
page.format("{}", .{}, w) catch |err| switch (err) {
else => log.err("Page Build Error {}", .{err}),
};
},
 
src/zwsgi.zig added: 36, removed: 62, total 0
@@ -105,7 +105,7 @@ pub fn serve(z: *zWSGI) !void {
const callable = z.router.routerfn(&verse, z.router.routefn);
z.router.builderfn(&verse, callable);
}
log.debug("closing, and cleaning up", .{});
log.warn("closing, and cleaning up", .{});
}
 
export fn sig_cb(sig: c_int, _: *const siginfo_t, _: ?*const anyopaque) callconv(.C) void {