srctree

Gregory Mullen parent da02ca0d e9d17849
use REMOTE_ADDR when user isn't valid

src/endpoints/repos/diffs.zig added: 83, removed: 42, total 41
@@ -2,6 +2,7 @@ const std = @import("std");
const Allocator = std.mem.Allocator;
const allocPrint = std.fmt.allocPrint;
const bufPrint = std.fmt.bufPrint;
const eql = std.mem.eql;
 
const Commits = @import("commits.zig");
 
@@ -34,7 +35,7 @@ const UriIter = Route.UriIter;
pub const routes = [_]Route.Match{
ROUTE("", list),
ROUTE("new", new),
POST("create", newPost),
POST("create", createDiff),
POST("add-comment", newComment),
};
 
@@ -68,8 +69,12 @@ const DiffCreateChangeReq = struct {
fn new(ctx: *Context) Error!void {
var network: ?S.Network = null;
var patchuri: ?S.PatchUri = .{};
var title: ?[]const u8 = null;
var desc: ?[]const u8 = null;
if (ctx.reqdata.post) |post| {
const udata = post.validate(DiffCreateChangeReq) catch return error.BadData;
title = udata.title;
desc = udata.desc;
 
if (udata.from_network) |_| {
const rd = Repos.RouteData.make(&ctx.uri) orelse return error.Unrouteable;
@@ -112,6 +117,8 @@ fn new(ctx: *Context) Error!void {
.nav_buttons = &try Repos.navButtons(ctx),
.nav_auth = undefined,
} },
.title = title,
.desc = desc,
.network = network,
.patch_uri = patchuri,
});
@@ -135,18 +142,28 @@ const DiffCreateReq = struct {
//},
};
 
fn newPost(ctx: *Context) Error!void {
fn createDiff(ctx: *Context) Error!void {
const rd = Repos.RouteData.make(&ctx.uri) orelse return error.Unrouteable;
if (ctx.reqdata.post) |post| {
const udata = post.validate(DiffCreateReq) catch return error.BadData;
 
if (udata.title.len == 0) return error.BadData;
 
var delta = Delta.new(rd.name) catch unreachable;
//delta.src = src;
delta.title = udata.title;
delta.message = udata.desc;
delta.attach = .{ .diff = 0 };
var remote_addr: []const u8 = "unknown";
for (ctx.request.headers.items) |head| {
if (eql(u8, head.name, "REMOTE_ADDR")) {
remote_addr = head.val;
}
}
 
var delta = Delta.new(
rd.name,
udata.title,
udata.desc,
if (ctx.auth.valid())
(ctx.auth.user(ctx.alloc) catch unreachable).username
else
try allocPrint(ctx.alloc, "REMOTE_ADDR {s}", .{remote_addr}),
) catch unreachable;
delta.commit() catch unreachable;
 
if (inNetwork(udata.patch_uri)) {
@@ -171,8 +188,7 @@ fn newPost(ctx: *Context) Error!void {
return ctx.response.redirect(loc, true) catch unreachable;
}
 
var tmpl = Template.find("diff-new.html");
try ctx.sendTemplate(&tmpl);
return try new(ctx);
}
 
fn newComment(ctx: *Context) Error!void {
 
src/endpoints/repos/issues.zig added: 83, removed: 42, total 41
@@ -53,16 +53,26 @@ fn new(ctx: *Context) Error!void {
try ctx.sendTemplate(&tmpl);
}
 
const IssueCreate = struct {
title: []const u8,
desc: []const u8,
};
 
fn newPost(ctx: *Context) Error!void {
const rd = Repos.RouteData.make(&ctx.uri) orelse return error.Unrouteable;
var buf: [2048]u8 = undefined;
if (ctx.reqdata.post) |post| {
var valid = post.validator();
const title = try valid.require("title");
const msg = try valid.require("desc");
var delta = Delta.new(rd.name) catch unreachable;
delta.title = title.value;
delta.message = msg.value;
const valid = post.validate(IssueCreate) catch return error.BadData;
var delta = Delta.new(
rd.name,
valid.title,
valid.desc,
if (ctx.auth.valid())
(ctx.auth.user(ctx.alloc) catch unreachable).username
else
try allocPrint(ctx.alloc, "remote_address", .{}),
) catch unreachable;
 
delta.attach = .{ .issue = 0 };
delta.commit() catch unreachable;
 
@@ -113,7 +123,6 @@ fn view(ctx: *Context) Error!void {
 
var delta = (Delta.open(ctx.alloc, rd.name, index) catch return error.Unrouteable) orelse return error.Unrouteable;
try ctx.putContext("Repo", .{ .slice = rd.name });
//dom.push(HTML.text(delta.repo));
 
try ctx.putContext(
"Title",
 
src/request.zig added: 83, removed: 42, total 41
@@ -1,5 +1,8 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const indexOf = std.mem.indexOf;
const eql = std.mem.eql;
 
const zWSGIRequest = @import("zwsgi.zig").zWSGIRequest;
const Auth = @import("auth.zig");
 
@@ -7,7 +10,7 @@ pub const Request = @This();
 
pub const RawRequests = union(enum) {
zwsgi: zWSGIRequest,
http: std.http.Server.Request,
http: *std.http.Server.Request,
};
 
const Pair = struct {
@@ -56,7 +59,7 @@ pub fn init(a: Allocator, raw_req: anytype) !Request {
.auth = undefined,
};
for (raw_req.vars) |v| {
try addHeader(&req.headers, v.key, v.val);
try req.addHeader(v.key, v.val);
if (std.mem.eql(u8, v.key, "PATH_INFO")) {
req.uri = v.val;
}
@@ -67,14 +70,22 @@ pub fn init(a: Allocator, raw_req: anytype) !Request {
req.auth = Auth.init(req.headers);
return req;
},
std.http.Server.Request => {
*std.http.Server.Request => {
var req = Request{
.raw_request = .{ .http = raw_req },
.headers = HeaderList.init(a),
.uri = raw_req.head.target,
.method = Methods.GET,
.method = switch (raw_req.head.method) {
.GET => .GET,
.POST => .POST,
else => @panic("not implemented"),
},
.auth = undefined,
};
var itr = raw_req.iterateHeaders();
while (itr.next()) |head| {
try req.addHeader(head.name, head.value);
}
req.auth = Auth.init(req.headers);
return req;
},
@@ -83,8 +94,8 @@ pub fn init(a: Allocator, raw_req: anytype) !Request {
@compileError("unreachable");
}
 
fn addHeader(h: *HeaderList, name: []const u8, val: []const u8) !void {
try h.append(.{ .name = name, .val = val });
pub fn addHeader(self: *Request, name: []const u8, val: []const u8) !void {
try self.headers.append(.{ .name = name, .val = val });
}
 
pub fn getHeader(self: Request, key: []const u8) ?[]const u8 {
 
src/response.zig added: 83, removed: 42, total 41
@@ -56,7 +56,7 @@ pub fn init(a: Allocator, req: *Request) !Response {
.headers = Headers.init(a),
.http_response = switch (req.raw_request) {
.zwsgi => null,
.http => |*h| h.respondStreaming(.{
.http => |h| h.*.respondStreaming(.{
.send_buffer = try a.alloc(u8, 0xffff),
.respond_options = .{
.transfer_encoding = .chunked,
 
src/types/delta.zig added: 83, removed: 42, total 41
@@ -263,7 +263,7 @@ fn openFile(repo: []const u8) !std.fs.File {
return try datad.createFile(filename, .{ .read = true });
}
 
pub fn new(repo: []const u8) !Delta {
pub fn new(repo: []const u8, title: []const u8, msg: []const u8, author: []const u8) !Delta {
// TODO this is probably a bug
const max: usize = currMax(repo) catch 0;
 
@@ -272,9 +272,9 @@ pub fn new(repo: []const u8) !Delta {
.created = std.time.timestamp(),
.updated = std.time.timestamp(),
.repo = repo,
.title = "",
.message = "",
.author = "",
.title = title,
.message = msg,
.author = author,
};
 
var thread = try Thread.new(d);
 
src/zwsgi.zig added: 83, removed: 42, total 41
@@ -160,6 +160,12 @@ fn serveHttp(zwsgi: *ZWSGI) !void {
var hreq = try hsrv.receiveHead();
 
var ctx = try zwsgi.buildContextHttp(a, &hreq);
var ipbuf: [0x20]u8 = undefined;
const ipport = try std.fmt.bufPrint(&ipbuf, "{}", .{conn.address});
if (std.mem.indexOf(u8, ipport, ":")) |i| {
try ctx.request.addHeader("REMOTE_ADDR", ipport[0..i]);
try ctx.request.addHeader("REMOTE_PORT", ipport[i + 1 ..]);
} else unreachable;
 
const callable = zwsgi.routefn(&ctx);
zwsgi.buildfn(&ctx, callable) catch |err| {
@@ -263,8 +269,6 @@ fn findOr(list: []uWSGIVar, search: []const u8) []const u8 {
}
 
fn buildContext(z: ZWSGI, a: Allocator, request: *Request) !Context {
const response = try Response.init(a, request);
 
var post_data: ?RequestData.PostData = null;
var reqdata: RequestData = undefined;
switch (request.raw_request) {
@@ -278,7 +282,7 @@ fn buildContext(z: ZWSGI, a: Allocator, request: *Request) !Context {
post_data = try RequestData.readBody(a, &reader, post_size, h_type);
if (dump_vars) std.log.info(
"post data \"{s}\" {{{any}}}",
.{ post_data.rawdata, post_data.rawdata },
.{ post_data.?.rawpost, post_data.?.rawpost },
);
 
for (post_data.?.items) |itm| {
@@ -296,7 +300,7 @@ fn buildContext(z: ZWSGI, a: Allocator, request: *Request) !Context {
.query = query,
};
},
.http => |*hreq| {
.http => |hreq| {
if (hreq.head.content_length) |h_len| {
if (h_len > 0) {
const h_type = hreq.head.content_type orelse "text/plain";
@@ -304,7 +308,7 @@ fn buildContext(z: ZWSGI, a: Allocator, request: *Request) !Context {
post_data = try RequestData.readBody(a, &reader, h_len, h_type);
if (dump_vars) std.log.info(
"post data \"{s}\" {{{any}}}",
.{ post_data.rawdata, post_data.rawdata },
.{ post_data.?.rawpost, post_data.?.rawpost },
);
 
for (post_data.?.items) |itm| {
@@ -324,6 +328,7 @@ fn buildContext(z: ZWSGI, a: Allocator, request: *Request) !Context {
},
}
 
const response = try Response.init(a, request);
return Context.init(a, z.config, request.*, response, reqdata);
}
 
@@ -336,7 +341,7 @@ fn readHttpHeaders(a: Allocator, req: *std.http.Server.Request) !Request {
if (dump_vars) std.log.info("{}", .{header});
}
 
return try Request.init(a, req.*);
return try Request.init(a, req);
}
 
fn buildContextHttp(z: ZWSGI, a: Allocator, req: *std.http.Server.Request) !Context {
 
templates/diff-new.html added: 83, removed: 42, total 41
@@ -28,7 +28,7 @@
<textarea name="desc" placeholder="Additional information about this patch suggestion"><Desc ORNULL></textarea>
<buttons>
<button name="submit">Submit</button>
<button name="preview">Preview</button>
<button name="preview">Preview (does nothing)</button>
</buttons>
</form>
</content>