╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
939 @memcpy(buf[0..prefix.len], prefix); 940 var i: usize = prefix.len; 941 if (fg) |c| { 942 const s = std.fmt.bufPrint(buf[i..], "color:#{x:0>2}{x:0>2}{x:0>2};", .{ c.r, c.g, c.b }) catch return buf[0..0]; 943 i += s.len; 944 } 945 if (bg) |c| { 946 const s = std.fmt.bufPrint(buf[i..], "background-color:#{x:0>2}{x:0>2}{x:0>2};", .{ c.r, c.g, c.b }) catch return buf[0..0]; 947 i += s.len; 948 } 949 buf[i] = '"'; 950 i += 1; 951 return buf[0..i]; 952 } 953 954 fn appendEscapedHtml(allocator: std.mem.Allocator, out: *std.ArrayList(u8), input: []const u8) !void { 955 for (input) |ch| { 956 switch (ch) { 957 '&' => try out.appendSlice(allocator, "&amp;"), 958 '<' => try out.appendSlice(allocator, "&lt;"), 959 '>' => try out.appendSlice(allocator, "&gt;"), 960 '"' => try out.appendSlice(allocator, "&quot;"), 961 '\'' => try out.appendSlice(allocator, "&#39;"), 962 else => try out.append(allocator, ch), 963 } 964 } 965 } 966 967 // --- helpers -------------------------------------------------------------- 968 969 fn getCookieValue(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 970 var iter = request.iterateHeaders(); 971 while (iter.next()) |header| { 972 if (!std.ascii.eqlIgnoreCase(header.name, "cookie")) continue; 973 var pairs = std.mem.splitScalar(u8, header.value, ';'); 974 while (pairs.next()) |pair| { 975 const trimmed = std.mem.trim(u8, pair, " \t"); 976 const eq = std.mem.indexOfScalar(u8, trimmed, '=') orelse continue; 977 if (std.mem.eql(u8, trimmed[0..eq], name)) { 978 return trimmed[eq + 1 ..]; 979 } 980 } 981 } 982 return null; 983 } 984 985 fn parseFormField(allocator: std.mem.Allocator, body: []const u8, key: []const u8) !?[]u8 { 986 var iter = std.mem.splitScalar(u8, body, '&'); 987 while (iter.next()) |pair| { 988 const eq = std.mem.indexOfScalar(u8, pair, '=') orelse continue; 989 if (std.mem.eql(u8, pair[0..eq], key)) { 990 return try decodeFormValue(allocator, pair[eq + 1 ..]); 991 } 992 } 993 return null; 994 } 995 996 fn decodeFormValue(allocator: std.mem.Allocator, encoded: []const u8) ![]u8 { 997 var out: std.ArrayList(u8) = .empty; 998 errdefer out.deinit(allocator); 999 var i: usize = 0; 1000 while (i < encoded.len) : (i += 1) { 1001 switch (encoded[i]) { 1002 '+' => try out.append(allocator, ' '), 1003 '%' => { 1004 if (i + 2 >= encoded.len) return error.InvalidPercentEncoding; 1005 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 1006 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 1007 try out.append(allocator, hi * 16 + lo); 1008 i += 2; 1009 }, 1010 else => try out.append(allocator, encoded[i]), 1011 } 1012 } 1013 return try out.toOwnedSlice(allocator); 1014 } 1015 1016 // disk-backed web session store. each login mints a random, opaque token 1017 // (not the user's id) that is stored as a file named by the token hex, whose 1018 // contents are the raw user_id bytes. authenticating a request is a lookup of 1019 // the cookie's token; logout deletes the file, revoking the session. there is 1020 // deliberately no expiry — a session lives until logout. 1021 pub const SessionStore = struct { 1022 io: std.Io, 1023 dir: std.Io.Dir, 1024 1025 pub const token_hex_len = evt.event_id_size * 2; 1026 1027 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 1028 try data_dir.createDirPath(io, "sessions"); 1029 const dir = try data_dir.openDir(io, "sessions", .{}); 1030 return .{ .io = io, .dir = dir }; 1031 } 1032 1033 pub fn deinit(self: SessionStore) void { 1034 self.dir.close(self.io); 1035 } 1036 1037 // mint a new session for user_id, returning the token hex to set as the 1038 // cookie value. 1039 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 1040 var token: [evt.event_id_size]u8 = undefined; 1041 self.io.random(&token); 1042 const token_hex = std.fmt.bytesToHex(token, .lower); 1043 const file = try self.dir.createFile(self.io, &token_hex, .{}); 1044 defer file.close(self.io); 1045 try file.writeStreamingAll(self.io, user_id); 1046 return token_hex; 1047 } 1048 1049 // resolve a cookie's token to its user_id. returns false (logged out) for a 1050 // missing, malformed, or unknown token. 1051 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 1052 if (!isToken(token_hex)) return false; 1053 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 1054 defer file.close(self.io); 1055 var storage: [evt.event_id_size]u8 = undefined; 1056 var file_reader = file.reader(self.io, &storage); 1057 file_reader.interface.readSliceAll(out) catch return false; 1058 return true; 1059 } 1060 1061 // revoke a session. a no-op for a missing or malformed token. 1062 pub fn remove(self: SessionStore, token_hex: []const u8) void { 1063 if (!isToken(token_hex)) return; 1064 self.dir.deleteFile(self.io, token_hex) catch {}; 1065 } 1066 1067 // a token is exactly token_hex_len hex chars. validating before using the 1068 // value as a path keeps attacker-supplied cookies from escaping the 1069 // sessions dir (e.g. via "/" or ".." bytes). 1070 fn isToken(token_hex: []const u8) bool { 1071 if (token_hex.len != token_hex_len) return false; 1072 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 1073 return true; 1074 } 1075 }; 1076