srctree

Gregory Mullen parent 78452490 223f5939
re-unbreak http server

src/http.zig added: 13, removed: 39, total 0
@@ -35,8 +35,8 @@ pub fn serve(a: Allocator, srv: *Server) !void {
@tagName(http_resp.request.version),
http_resp.request.target,
});
const body = try http_resp.reader().readAllAlloc(alloc, 8192);
defer a.free(body);
//const body = try http_resp.reader().readAllAlloc(alloc, 8192);
//defer a.free(body);
 
try http_resp.headers.append("Server", "Source Tree WebServer");
 
 
src/request.zig added: 13, removed: 39, total 0
@@ -73,15 +73,9 @@ pub fn init(a: Allocator, raw_req: anytype) !Request {
.headers = HeaderList.init(a),
.uri = undefined,
.auth = undefined,
.method = unreachable,
.method = Methods.GET,
};
req.uri = raw_req.request.target;
//for (raw_req.request.headers) |v| {
// try addHeader(&req.headers, v.key, v.val);
// if (std.mem.eql(u8, v.key, "REQUEST_URI")) {
// req.uri = v.val;
// }
//}
//req.auth = Auth.init(&req.headers);
return req;
},
 
src/response.zig added: 13, removed: 39, total 0
@@ -93,6 +93,8 @@ pub fn start(res: *Response) !void {
switch (res.downstream) {
.http => {
res.request.raw_request.http.transfer_encoding = .chunked;
std.debug.print("{}\n", .{res.request.raw_request.http.state});
res.phase = .headers;
return res.request.raw_request.http.do();
},
else => {},
 
src/routes.zig added: 13, removed: 39, total 0
@@ -48,39 +48,17 @@ const root = [_]MatchRouter{
.{ .name = "user", .match = .{ .call = endpoint.commitFlex } },
};
 
fn sendMsg(ctx: *Context, msg: []const u8) !void {
//ctx.response.transfer_encoding = .{ .content_length = msg.len };
try ctx.response.start();
try ctx.response.send(msg);
try ctx.response.finish();
}
 
fn notfound(ctx: *Context) Error!void {
ctx.response.status = .not_found;
const MSG = Template.find("4XX.html").blob;
sendMsg(ctx, MSG) catch |e| {
std.log.err("Unexpected error while responding [{}]\n", .{e});
return Error.AndExit;
};
}
 
fn _respond(ctx: *Context) Error!void {
ctx.response.headersAdd("connection", "keep-alive") catch return Error.ReqResInvalid;
ctx.response.headersAdd("content-type", "text/plain") catch return Error.ReqResInvalid;
const MSG = "Hi, mom!\n";
sendMsg(ctx, MSG) catch |e| {
std.log.err("Unexpected error while responding [{}]\n", .{e});
return Error.AndExit;
};
var tmpl = Template.find("4XX.html");
tmpl.init(ctx.alloc);
ctx.sendTemplate(&tmpl) catch unreachable;
}
 
fn default(ctx: *Context) Error!void {
var tmpl = Template.find("index.html");
tmpl.init(ctx.alloc);
var page = tmpl.buildFor(ctx.alloc, ctx) catch unreachable;
ctx.response.start() catch return Error.Unknown;
ctx.response.send(page) catch return Error.Unknown;
ctx.response.finish() catch return Error.Unknown;
ctx.sendTemplate(&tmpl) catch unreachable;
}
 
fn eql(a: []const u8, b: []const u8) bool {