╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
613 .custom => |custom| { 614 if (std.mem.eql(u8, "submit", custom)) { 615 // inside the dashed border the TUI cells draw around the button. 616 const inner_left = r.x + 1; 617 const inner_top = r.y + 1; 618 const inner_width = if (r.size.width > 2) r.size.width - 2 else 0; 619 const inner_height = if (r.size.height > 2) r.size.height - 2 else 0; 620 try out.appendSlice(allocator, "<button type=\"submit\" data-focus-id=\""); 621 var id_buf: [32]u8 = undefined; 622 try out.appendSlice(allocator, try std.fmt.bufPrint(&id_buf, "{d}", .{inner_id})); 623 try out.appendSlice(allocator, "\" style=\"left:"); 624 var pos_buf: [128]u8 = undefined; 625 try out.appendSlice(allocator, try std.fmt.bufPrint(&pos_buf, "{d}ch;top:{d}em;width:{d}ch;height:{d}em", .{ inner_left, inner_top, inner_width, inner_height })); 626 try out.appendSlice(allocator, "\"></button>"); 627 } 628 }, 629 else => {}, 630 } 631 } 632 633 try out.appendSlice(allocator, "</form>"); 634 } 635 636 return try out.toOwnedSlice(allocator); 637 } 638 639 fn colorEql(a: ?Grid.Color, b: ?Grid.Color) bool { 640 if (a) |av| return if (b) |bv| av.eql(bv) else false; 641 return b == null; 642 } 643 644 // builds an HTML ` style="color:#rrggbb;background-color:#rrggbb"` attribute 645 // from a cell's fg/bg into `buf`, omitting whichever color is unset. returns an 646 // empty slice when neither is set. 647 fn styleAttr(buf: []u8, fg: ?Grid.Color, bg: ?Grid.Color) []const u8 { 648 if (fg == null and bg == null) return buf[0..0]; 649 const prefix = " style=\""; 650 @memcpy(buf[0..prefix.len], prefix); 651 var i: usize = prefix.len; 652 if (fg) |c| { 653 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]; 654 i += s.len; 655 } 656 if (bg) |c| { 657 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]; 658 i += s.len; 659 } 660 buf[i] = '"'; 661 i += 1; 662 return buf[0..i]; 663 } 664 665 fn cellFocusId(focus: *Focus, x: usize, y: usize) ?usize { 666 var iter = focus.children.iterator(); 667 while (iter.next()) |entry| { 668 const child = entry.value_ptr.*; 669 if (!child.focus.focusable) continue; 670 const r = child.rect; 671 if (x >= r.x and y >= r.y and x < r.x + r.size.width and y < r.y + r.size.height) { 672 return entry.key_ptr.*; 673 } 674 } 675 return null; 676 } 677 678 fn appendEscapedHtml(allocator: std.mem.Allocator, out: *std.ArrayList(u8), input: []const u8) !void { 679 for (input) |ch| { 680 switch (ch) { 681 '&' => try out.appendSlice(allocator, "&amp;"), 682 '<' => try out.appendSlice(allocator, "&lt;"), 683 '>' => try out.appendSlice(allocator, "&gt;"), 684 '"' => try out.appendSlice(allocator, "&quot;"), 685 '\'' => try out.appendSlice(allocator, "&#39;"), 686 else => try out.append(allocator, ch), 687 } 688 } 689 } 690 691 // --- helpers -------------------------------------------------------------- 692 693 fn getCookieValue(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 694 var iter = request.iterateHeaders(); 695 while (iter.next()) |header| { 696 if (!std.ascii.eqlIgnoreCase(header.name, "cookie")) continue; 697 var pairs = std.mem.splitScalar(u8, header.value, ';'); 698 while (pairs.next()) |pair| { 699 const trimmed = std.mem.trim(u8, pair, " \t"); 700 const eq = std.mem.indexOfScalar(u8, trimmed, '=') orelse continue; 701 if (std.mem.eql(u8, trimmed[0..eq], name)) { 702 return trimmed[eq + 1 ..]; 703 } 704 } 705 } 706 return null; 707 } 708 709 fn parseFormField(allocator: std.mem.Allocator, body: []const u8, key: []const u8) !?[]u8 { 710 var iter = std.mem.splitScalar(u8, body, '&'); 711 while (iter.next()) |pair| { 712 const eq = std.mem.indexOfScalar(u8, pair, '=') orelse continue; 713 if (std.mem.eql(u8, pair[0..eq], key)) { 714 return try decodeFormValue(allocator, pair[eq + 1 ..]); 715 } 716 } 717 return null; 718 } 719 720 fn decodeFormValue(allocator: std.mem.Allocator, encoded: []const u8) ![]u8 { 721 var out: std.ArrayList(u8) = .empty; 722 errdefer out.deinit(allocator); 723 var i: usize = 0; 724 while (i < encoded.len) : (i += 1) { 725 switch (encoded[i]) { 726 '+' => try out.append(allocator, ' '), 727 '%' => { 728 if (i + 2 >= encoded.len) return error.InvalidPercentEncoding; 729 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 730 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 731 try out.append(allocator, hi * 16 + lo); 732 i += 2; 733 }, 734 else => try out.append(allocator, encoded[i]), 735 } 736 } 737 return try out.toOwnedSlice(allocator); 738 } 739 740 // disk-backed web session store. each login mints a random, opaque token 741 // (not the user's id) that is stored as a file named by the token hex, whose 742 // contents are the raw user_id bytes. authenticating a request is a lookup of 743 // the cookie's token; logout deletes the file, revoking the session. there is 744 // deliberately no expiry — a session lives until logout. 745 pub const SessionStore = struct { 746 io: std.Io, 747 dir: std.Io.Dir, 748 749 pub const token_hex_len = evt.event_id_size * 2; 750 751 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 752 try data_dir.createDirPath(io, "sessions"); 753 const dir = try data_dir.openDir(io, "sessions", .{}); 754 return .{ .io = io, .dir = dir }; 755 } 756 757 pub fn deinit(self: SessionStore) void { 758 self.dir.close(self.io); 759 } 760 761 // mint a new session for user_id, returning the token hex to set as the 762 // cookie value. 763 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 764 var token: [evt.event_id_size]u8 = undefined; 765 self.io.random(&token); 766 const token_hex = std.fmt.bytesToHex(token, .lower); 767 const file = try self.dir.createFile(self.io, &token_hex, .{}); 768 defer file.close(self.io); 769 try file.writeStreamingAll(self.io, user_id); 770 return token_hex; 771 } 772 773 // resolve a cookie's token to its user_id. returns false (logged out) for a 774 // missing, malformed, or unknown token. 775 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 776 if (!isToken(token_hex)) return false; 777 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 778 defer file.close(self.io); 779 var storage: [evt.event_id_size]u8 = undefined; 780 var file_reader = file.reader(self.io, &storage); 781 file_reader.interface.readSliceAll(out) catch return false; 782 return true; 783 } 784 785 // revoke a session. a no-op for a missing or malformed token. 786 pub fn remove(self: SessionStore, token_hex: []const u8) void { 787 if (!isToken(token_hex)) return; 788 self.dir.deleteFile(self.io, token_hex) catch {}; 789 } 790 791 // a token is exactly token_hex_len hex chars. validating before using the 792 // value as a path keeps attacker-supplied cookies from escaping the 793 // sessions dir (e.g. via "/" or ".." bytes). 794 fn isToken(token_hex: []const u8) bool { 795 if (token_hex.len != token_hex_len) return false; 796 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 797 return true; 798 } 799 }; 800