╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1707 1708 if (first or cell_id != cur_id or !colorEql(fg, cur_fg) or !colorEql(bg, cur_bg)) { 1709 if (open_tag) |t| try output.appendSlice(allocator, t.closeTag()); 1710 1711 var new_tag: ?CellTag = null; 1712 if (cell_id) |id| { 1713 if (focus.children.get(id)) |child| { 1714 new_tag = switch (child.focus.kind) { 1715 .custom => |custom| CellTag.init(custom), 1716 else => .span, 1717 }; 1718 } 1719 } 1720 if (new_tag == null and (fg != null or bg != null)) new_tag = .plain; 1721 1722 if (new_tag) |t| { 1723 var style_buf: [64]u8 = undefined; 1724 try t.writeOpenTag(allocator, output, cell_id orelse 0, styleAttr(&style_buf, fg, bg)); 1725 } 1726 1727 open_tag = new_tag; 1728 cur_id = cell_id; 1729 cur_fg = fg; 1730 cur_bg = bg; 1731 first = false; 1732 } 1733 1734 if (covered_by_scroll) { 1735 try appendEscapedHtml(allocator, output, " "); 1736 } else if (cell.continuation) { 1737 // the wide rune to the left was emitted 2ch wide, covering this column 1738 } else if (cell.rune) |rune| { 1739 // a double-width rune is pinned inside a 2ch span so the 1740 // fallback glyph's advance width can't shift the rest of the row 1741 const wide = x + 1 < grid.size.width and grid.cells.items[try grid.cells.at(.{ y, x + 1 })].continuation; 1742 if (wide) { 1743 try output.appendSlice(allocator, "<span class=\"w2\">"); 1744 try appendEscapedHtml(allocator, output, rune); 1745 try output.appendSlice(allocator, "</span>"); 1746 } else { 1747 try appendEscapedHtml(allocator, output, rune); 1748 } 1749 } else { 1750 try appendEscapedHtml(allocator, output, " "); 1751 } 1752 } 1753 if (open_tag) |t| try output.appendSlice(allocator, t.closeTag()); 1754 try output.append(allocator, '\n'); 1755 } 1756 1757 // read-only inputs have no live form state to preserve, so emit them in 1758 // their panel and let a native scroll move and clip them with the grid. 1759 var input_iter = focus.children.iterator(); 1760 while (input_iter.next()) |input_entry| { 1761 const input_id = input_entry.key_ptr.*; 1762 if (excluded.contains(input_id)) continue; 1763 const child = input_entry.value_ptr.*; 1764 switch (child.focus.kind) { 1765 .text_input => {}, 1766 else => continue, 1767 } 1768 const text_input = session.text_inputs.get(input_id) orelse continue; 1769 if (!text_input.options.read_only) continue; 1770 const value = session.input_values.get(input_id) orelse continue; 1771 const r = child.rect; 1772 const inner_width = if (r.size.width > 2) r.size.width - 2 else 0; 1773 1774 try output.appendSlice(allocator, "<input type=\"text\" readonly data-focus-id=\""); 1775 var id_buf: [32]u8 = undefined; 1776 try output.appendSlice(allocator, try std.fmt.bufPrint(&id_buf, "{d}", .{input_id})); 1777 try output.appendSlice(allocator, "\" value=\""); 1778 try appendEscapedHtml(allocator, output, value); 1779 try output.appendSlice(allocator, "\" style=\"left:"); 1780 var pos_buf: [64]u8 = undefined; 1781 try output.appendSlice(allocator, try std.fmt.bufPrint(&pos_buf, "{d}ch;top:{d}em;width:{d}ch;height:1em\">", .{ r.x + 1, r.y + 1, inner_width })); 1782 } 1783 1784 // each child scroll becomes a natively-scrollable div positioned over its 1785 // viewport, holding its full content rendered recursively. 1786 for (direct.items) |id| { 1787 const child = focus.children.get(id) orelse continue; 1788 const info = child.focus.scroll orelse continue; 1789 const r = child.rect; 1790 // overflow only on the axes the widget scrolls; `auto` shows a bar only 1791 // when that axis actually overflows. the other axis is clipped (hidden), 1792 // matching how the terminal Scroll handles its non-scrolling axis. 1793 const overflow = switch (info.direction) { 1794 .vert => "overflow-x:hidden;overflow-y:auto", 1795 .horiz => "overflow-x:auto;overflow-y:hidden", 1796 .both => "overflow:auto", 1797 }; 1798 // the id carries a content version so JS preserves the native scroll 1799 // position across re-renders of the same content but resets it when the 1800 // content is replaced — e.g. selecting a different commit. the widget's 1801 // scroll offset (in cells) rides along so JS can apply it when it has no 1802 // preserved position, letting a wasm-side scrollToRect (the files list 1803 // scrolling to its selected row on page load) reach the browser. 1804 var buf: [256]u8 = undefined; 1805 try output.appendSlice(allocator, try std.fmt.bufPrint(&buf, "<div class=\"scroll\" data-scroll-id=\"{d}-{d}\" data-scroll-direction=\"{s}\" data-scroll-x=\"{d}\" data-scroll-y=\"{d}\" style=\"left:{d}ch;top:{d}em;width:{d}ch;height:{d}em;{s}\">", .{ id, child.focus.version, @tagName(info.direction), info.offset_x, info.offset_y, r.x, r.y, r.size.width, r.size.height, overflow })); 1806 try renderPanel(allocator, output, child.focus, info.content, session); 1807 try output.appendSlice(allocator, "</div>"); 1808 } 1809 } 1810 1811 // emits the form overlay — one <form> per "form:<url>" focus subtree in the 1812 // widget tree, each wrapping the text inputs and submit button inside it, 1813 // positioned absolutely over the matching grid cells. 1814 pub fn generateOverlay(allocator: std.mem.Allocator, root: *ui.Widget, session: *ui.Session) ![]const u8 { 1815 const root_focus = root.getFocus(); 1816 1817 var out: std.ArrayList(u8) = .empty; 1818 errdefer out.deinit(allocator); 1819 1820 const form_prefix = "form:"; 1821 1822 // Focus.children is an AutoArrayHashMap that preserves insertion order, 1823 // which matches the order widgets were added in code 1824 var iter = root_focus.children.iterator(); 1825 while (iter.next()) |entry| { 1826 const child = entry.value_ptr.*; 1827 const action_url = switch (child.focus.kind) { 1828 .custom => |custom| if (std.mem.startsWith(u8, custom, form_prefix)) 1829 custom[form_prefix.len..] 1830 else 1831 continue, 1832 else => continue, 1833 }; 1834 1835 try out.appendSlice(allocator, "<form action=\""); 1836 try appendEscapedHtml(allocator, &out, action_url); 1837 try out.appendSlice(allocator, "\" method=\"post\""); 1838 try out.appendSlice(allocator, ">"); 1839 1840 // form.focus.children was populated by addChild, which flattens the 1841 // whole subtree into one map — so descendants (inputs + the submit 1842 // button) all appear here. positions on form.focus are relative to 1843 // the form, so we look up absolute rects via root_focus for layout. 1844 var inner_iter = child.focus.children.iterator(); 1845 while (inner_iter.next()) |inner_entry| { 1846 const inner_id = inner_entry.key_ptr.*; 1847 const root_child = root_focus.children.get(inner_id) orelse continue; 1848 const r = root_child.rect; 1849 switch (root_child.focus.kind) { 1850 .text_input, .text_input_password => { 1851 const ti = session.text_inputs.get(inner_id) orelse continue; 1852 if (ti.options.read_only) continue; // rendered with the grid 1853 const inner_left = r.x + 1; 1854 const inner_top = r.y + 1; 1855 const inner_width = if (r.size.width > 2) r.size.width - 2 else 0; 1856 1857 try out.appendSlice(allocator, "<input type=\""); 1858 try out.appendSlice(allocator, if (root_child.focus.kind == .text_input_password) "password" else "text"); 1859 try out.appendSlice(allocator, "\" data-focus-id=\""); 1860 var id_buf: [32]u8 = undefined; 1861 try out.appendSlice(allocator, try std.fmt.bufPrint(&id_buf, "{d}", .{inner_id})); 1862 if (ti.options.name.len > 0) { 1863 try out.appendSlice(allocator, "\" name=\""); 1864 try appendEscapedHtml(allocator, &out, ti.options.name); 1865 } 1866 // never the live content as the `value`: the browser tracks user 1867 // input natively, and including the wasm-side value here would make 1868 // the overlay HTML differ on every keystroke — that would trip the 1869 // diff in _setOverlay and rebuild the <input>, eating the user's 1870 // caret. a page-constant initial value is safe. 1871 if (session.input_values.get(inner_id)) |value| { 1872 try out.appendSlice(allocator, "\" value=\""); 1873 try appendEscapedHtml(allocator, &out, value); 1874 } 1875 try out.appendSlice(allocator, "\" style=\"left:"); 1876 var pos_buf: [64]u8 = undefined; 1877 try out.appendSlice(allocator, try std.fmt.bufPrint(&pos_buf, "{d}ch;top:{d}em;width:{d}ch;height:1em", .{ inner_left, inner_top, inner_width })); 1878 try out.appendSlice(allocator, "\">"); 1879 }, 1880 .text_area => { 1881 const ti = session.text_inputs.get(inner_id) orelse continue; 1882 if (ti.options.read_only) continue; // rendered with the grid 1883 const inner_left = r.x + 1; 1884 const inner_top = r.y + 1; 1885 const inner_width = if (r.size.width > 2) r.size.width - 2 else 0; 1886 const inner_height = if (r.size.height > 2) r.size.height - 2 else 0; 1887 1888 try out.appendSlice(allocator, "<textarea data-focus-id=\""); 1889 var id_buf: [32]u8 = undefined; 1890 try out.appendSlice(allocator, try std.fmt.bufPrint(&id_buf, "{d}", .{inner_id})); 1891 if (ti.options.name.len > 0) { 1892 try out.appendSlice(allocator, "\" name=\""); 1893 try appendEscapedHtml(allocator, &out, ti.options.name); 1894 } 1895 // like the input above, only a page-constant initial value 1896 // inside: the browser tracks the live value and the 1897 // overlay diff must stay stable across keystrokes 1898 try out.appendSlice(allocator, "\" style=\"left:"); 1899 var pos_buf: [128]u8 = undefined; 1900 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 })); 1901 // textarea discards the first newline immediately following 1902 // its start tag, so add one of our own so text that begins 1903 // with a newline doesn't lose it 1904 try out.appendSlice(allocator, "\">\n"); 1905 if (session.input_values.get(inner_id)) |value| try appendEscapedHtml(allocator, &out, value); 1906 try out.appendSlice(allocator, "</textarea>"); 1907 }, 1908 .custom => |custom| { 1909 if (std.mem.eql(u8, "submit", custom) or std.mem.startsWith(u8, custom, ui.submit_action_prefix)) { 1910 try out.appendSlice(allocator, "<button type=\"submit\" data-focus-id=\""); 1911 var id_buf: [32]u8 = undefined; 1912 try out.appendSlice(allocator, try std.fmt.bufPrint(&id_buf, "{d}", .{inner_id})); 1913 if (std.mem.startsWith(u8, custom, ui.submit_action_prefix)) { 1914 try out.appendSlice(allocator, "\" formaction=\""); 1915 try appendEscapedHtml(allocator, &out, custom[ui.submit_action_prefix.len..]); 1916 } 1917 try out.appendSlice(allocator, "\" style=\"left:"); 1918 var pos_buf: [128]u8 = undefined; 1919 try out.appendSlice(allocator, try std.fmt.bufPrint(&pos_buf, "{d}ch;top:{d}em;width:{d}ch;height:{d}em", .{ r.x, r.y, r.size.width, r.size.height })); 1920 try out.appendSlice(allocator, "\"></button>"); 1921 } 1922 }, 1923 else => {}, 1924 } 1925 } 1926 1927 try out.appendSlice(allocator, "</form>"); 1928 } 1929 1930 // a file picker isn't part of a form: it posts its file straight to the url 1931 // in its marker. it sits transparent over the button the tui drew, so 1932 // clicking that button opens the browser's own dialog. 1933 var file_iter = root_focus.children.iterator(); 1934 while (file_iter.next()) |entry| { 1935 const child = entry.value_ptr.*; 1936 const action_url = switch (child.focus.kind) { 1937 .custom => |custom| if (std.mem.startsWith(u8, custom, ui.file_input_prefix)) 1938 custom[ui.file_input_prefix.len..] 1939 else 1940 continue, 1941 else => continue, 1942 }; 1943 const r = child.rect; 1944 1945 try out.appendSlice(allocator, "<input type=\"file\" data-action=\""); 1946 try appendEscapedHtml(allocator, &out, action_url); 1947 try out.appendSlice(allocator, "\" data-focus-id=\""); 1948 var id_buf: [32]u8 = undefined; 1949 try out.appendSlice(allocator, try std.fmt.bufPrint(&id_buf, "{d}", .{entry.key_ptr.*})); 1950 try out.appendSlice(allocator, "\" style=\"opacity:0;left:"); 1951 var pos_buf: [128]u8 = undefined; 1952 try out.appendSlice(allocator, try std.fmt.bufPrint(&pos_buf, "{d}ch;top:{d}em;width:{d}ch;height:{d}em", .{ r.x, r.y, r.size.width, r.size.height })); 1953 try out.appendSlice(allocator, "\">"); 1954 } 1955 1956 return try out.toOwnedSlice(allocator); 1957 } 1958 1959 fn colorEql(a: ?Grid.Color, b: ?Grid.Color) bool { 1960 if (a) |av| return if (b) |bv| av.eql(bv) else false; 1961 return b == null; 1962 } 1963 1964 // builds an HTML ` style="color:#rrggbb;background-color:#rrggbb"` attribute 1965 // from a cell's fg/bg into `buf`, omitting whichever color is unset. returns an 1966 // empty slice when neither is set. 1967 fn styleAttr(buf: []u8, fg: ?Grid.Color, bg: ?Grid.Color) []const u8 { 1968 if (fg == null and bg == null) return buf[0..0]; 1969 const prefix = " style=\""; 1970 @memcpy(buf[0..prefix.len], prefix); 1971 var i: usize = prefix.len; 1972 if (fg) |c| { 1973 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]; 1974 i += s.len; 1975 } 1976 if (bg) |c| { 1977 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]; 1978 i += s.len; 1979 } 1980 buf[i] = '"'; 1981 i += 1; 1982 return buf[0..i]; 1983 } 1984 1985 fn appendEscapedHtml(allocator: std.mem.Allocator, out: *std.ArrayList(u8), input: []const u8) !void { 1986 for (input) |ch| { 1987 switch (ch) { 1988 '&' => try out.appendSlice(allocator, "&amp;"), 1989 '<' => try out.appendSlice(allocator, "&lt;"), 1990 '>' => try out.appendSlice(allocator, "&gt;"), 1991 '"' => try out.appendSlice(allocator, "&quot;"), 1992 '\'' => try out.appendSlice(allocator, "&#39;"), 1993 else => try out.append(allocator, ch), 1994 } 1995 } 1996 } 1997 1998 // --- helpers -------------------------------------------------------------- 1999 2000 fn getCookieValue(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 2001 var iter = request.iterateHeaders(); 2002 while (iter.next()) |header| { 2003 if (!std.ascii.eqlIgnoreCase(header.name, "cookie")) continue; 2004 var pairs = std.mem.splitScalar(u8, header.value, ';'); 2005 while (pairs.next()) |pair| { 2006 const trimmed = std.mem.trim(u8, pair, " \t"); 2007 const eq = std.mem.indexOfScalar(u8, trimmed, '=') orelse continue; 2008 if (std.mem.eql(u8, trimmed[0..eq], name)) { 2009 return trimmed[eq + 1 ..]; 2010 } 2011 } 2012 } 2013 return null; 2014 } 2015 2016 fn parseFormField(allocator: std.mem.Allocator, body: []const u8, key: []const u8) !?[]u8 { 2017 var iter = std.mem.splitScalar(u8, body, '&'); 2018 while (iter.next()) |pair| { 2019 const eq = std.mem.indexOfScalar(u8, pair, '=') orelse continue; 2020 if (std.mem.eql(u8, pair[0..eq], key)) { 2021 return try decodeFormValue(allocator, pair[eq + 1 ..]); 2022 } 2023 } 2024 return null; 2025 } 2026 2027 fn readFormBody(request: *std.http.Server.Request, allocator: std.mem.Allocator) ![]u8 { 2028 var buffer: [256]u8 = undefined; 2029 return request.readerExpectNone(&buffer).allocRemaining(allocator, .limited(65536)); 2030 } 2031 2032 fn decodeFormValue(allocator: std.mem.Allocator, encoded: []const u8) ![]u8 { 2033 var out: std.ArrayList(u8) = .empty; 2034 errdefer out.deinit(allocator); 2035 var i: usize = 0; 2036 while (i < encoded.len) : (i += 1) { 2037 switch (encoded[i]) { 2038 '+' => try out.append(allocator, ' '), 2039 '%' => { 2040 if (i + 2 >= encoded.len) return error.InvalidPercentEncoding; 2041 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 2042 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 2043 try out.append(allocator, hi * 16 + lo); 2044 i += 2; 2045 }, 2046 else => try out.append(allocator, encoded[i]), 2047 } 2048 } 2049 return try out.toOwnedSlice(allocator); 2050 } 2051 2052 // disk-backed web session store. each login mints a random, opaque token 2053 // (not the user's id) that is stored as a file named by the token hex, whose 2054 // contents are the raw user_id bytes. authenticating a request is a lookup of 2055 // the cookie's token; logout deletes the file, revoking the session. there is 2056 // deliberately no expiry — a session lives until logout. 2057 pub const SessionStore = struct { 2058 io: std.Io, 2059 dir: std.Io.Dir, 2060 auto_login: ?[token_hex_len]u8, 2061 2062 pub const token_hex_len = evt.event_id_size * 2; 2063 2064 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 2065 try data_dir.createDirPath(io, "sessions"); 2066 const dir = try data_dir.openDir(io, "sessions", .{}); 2067 const auto_login = blk: { 2068 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 2069 defer file.close(io); 2070 var token: [token_hex_len]u8 = undefined; 2071 var storage: [token_hex_len]u8 = undefined; 2072 var reader = file.reader(io, &storage); 2073 reader.interface.readSliceAll(&token) catch break :blk null; 2074 break :blk token; 2075 }; 2076 return .{ .io = io, .dir = dir, .auto_login = auto_login }; 2077 } 2078 2079 pub fn deinit(self: SessionStore) void { 2080 self.dir.close(self.io); 2081 } 2082 2083 // mint a new session for user_id, returning the token hex to set as the 2084 // cookie value. 2085 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2086 var token: [evt.event_id_size]u8 = undefined; 2087 self.io.random(&token); 2088 const token_hex = std.fmt.bytesToHex(token, .lower); 2089 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2090 defer file.close(self.io); 2091 try file.writeStreamingAll(self.io, user_id); 2092 return token_hex; 2093 } 2094 2095 // resolve a cookie's token to its user_id. returns false (logged out) for a 2096 // missing, malformed, or unknown token. 2097 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2098 if (!isToken(token_hex)) return false; 2099 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2100 defer file.close(self.io); 2101 var storage: [evt.event_id_size]u8 = undefined; 2102 var file_reader = file.reader(self.io, &storage); 2103 file_reader.interface.readSliceAll(out) catch return false; 2104 return true; 2105 } 2106 2107 // revoke a session. a no-op for a missing or malformed token. 2108 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2109 if (!isToken(token_hex)) return; 2110 self.dir.deleteFile(self.io, token_hex) catch {}; 2111 } 2112 2113 // leave `token_hex`'s session for visitors arriving without one. only the 2114 // `try` fixture writes one. the name can't collide with a session token. 2115 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2116 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2117 defer file.close(self.io); 2118 try file.writeStreamingAll(self.io, token_hex); 2119 } 2120 2121 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2122 out.* = self.auto_login orelse return false; 2123 return true; 2124 } 2125 2126 const auto_login_name = "auto-login"; 2127 2128 // a token is exactly token_hex_len hex chars. validating before using the 2129 // value as a path keeps attacker-supplied cookies from escaping the 2130 // sessions dir (e.g. via "/" or ".." bytes). 2131 fn isToken(token_hex: []const u8) bool { 2132 if (token_hex.len != token_hex_len) return false; 2133 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2134 return true; 2135 } 2136 }; 2137