╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2095 } else if (cell.rune) |rune| { 2096 var encoded: [4]u8 = undefined; 2097 const encoded_len = try std.unicode.utf8Encode(rune, &encoded); 2098 const rune_text = encoded[0..encoded_len]; 2099 // a double-width rune is pinned inside a 2ch span so the 2100 // fallback glyph's advance width can't shift the rest of the row 2101 const wide = x + 1 < grid.size.width and (try grid.cell(x + 1, y)).continuation; 2102 if (wide) { 2103 try output.appendSlice(allocator, "<span class=\"w2\">"); 2104 try appendEscapedHtml(allocator, output, rune_text); 2105 try output.appendSlice(allocator, "</span>"); 2106 } else { 2107 try appendEscapedHtml(allocator, output, rune_text); 2108 } 2109 } else { 2110 try appendEscapedHtml(allocator, output, " "); 2111 } 2112 } 2113 if (open_tag) |t| try output.appendSlice(allocator, t.closeTag()); 2114 try output.append(allocator, '\n'); 2115 } 2116 try output.appendSlice(allocator, "</div>"); 2117 2118 // native controls live in their panel, beside the replaceable cells. their 2119 // stable keys let JS update layout without recreating their DOM nodes, and 2120 // being actual descendants of a scroll gives them native movement/clipping. 2121 try renderPanelControls(allocator, output, focus, session, root_focus, &excluded); 2122 2123 // each child scroll becomes a natively-scrollable div positioned over its 2124 // viewport, holding its full content rendered recursively. 2125 for (direct.items) |id| { 2126 const child = focus.children.get(id) orelse continue; 2127 const info = child.focus.scroll orelse continue; 2128 const r = child.rect; 2129 // overflow only on the axes the widget scrolls; `auto` shows a bar only 2130 // when that axis actually overflows. the other axis is clipped (hidden), 2131 // matching how the terminal Scroll handles its non-scrolling axis. 2132 const overflow = switch (info.direction) { 2133 .vert => "overflow-x:hidden;overflow-y:auto", 2134 .horiz => "overflow-x:auto;overflow-y:hidden", 2135 .both => "overflow:auto", 2136 }; 2137 // the id carries a content version so JS preserves the native scroll 2138 // position across re-renders of the same content but resets it when the 2139 // content is replaced — e.g. selecting a different commit. the widget's 2140 // scroll offset (in cells) rides along so JS can apply it when it has no 2141 // preserved position, letting a wasm-side scrollToRect (the files list 2142 // scrolling to its selected row on page load) reach the browser. 2143 var buf: [256]u8 = undefined; 2144 try output.appendSlice(allocator, try std.fmt.bufPrint(&buf, "<div class=\"scroll\" data-key=\"scroll:{d}\" 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, id, child.focus.version, @tagName(info.direction), info.offset_x, info.offset_y, r.x, r.y, r.size.width, r.size.height, overflow })); 2145 try renderPanel(allocator, output, child.focus, info.content, session, root_focus); 2146 try output.appendSlice(allocator, "</div>"); 2147 } 2148 } 2149 2150 const form_prefix = "form:"; 2151 const form_id_prefix = "haxy-form-"; 2152 2153 fn formAction(focus: *const Focus) ?[]const u8 { 2154 return switch (focus.kind) { 2155 .custom => |custom| if (std.mem.startsWith(u8, custom, form_prefix)) custom[form_prefix.len..] else null, 2156 else => null, 2157 }; 2158 } 2159 2160 // find the form ancestor of a flattened focus-tree child. html's `form` 2161 // attribute then associates the control with that form without requiring the 2162 // form and control to share a DOM parent. 2163 fn formOwner(root_focus: *Focus, focus_id: usize) ?usize { 2164 var id = focus_id; 2165 while (root_focus.children.get(id)) |child| { 2166 const parent_id = child.parent_id; 2167 if (parent_id == root_focus.id) return if (formAction(root_focus) != null) root_focus.id else null; 2168 const parent = root_focus.children.get(parent_id) orelse return null; 2169 if (formAction(parent.focus) != null) return parent_id; 2170 id = parent_id; 2171 } 2172 return null; 2173 } 2174 2175 fn appendFormAttribute(allocator: std.mem.Allocator, output: *std.ArrayList(u8), root_focus: *Focus, focus_id: usize) !void { 2176 const form_id = formOwner(root_focus, focus_id) orelse return; 2177 var buf: [64]u8 = undefined; 2178 try output.appendSlice(allocator, try std.fmt.bufPrint(&buf, " form=\"" ++ form_id_prefix ++ "{d}\"", .{form_id})); 2179 } 2180 2181 fn renderPanelControls( 2182 allocator: std.mem.Allocator, 2183 output: *std.ArrayList(u8), 2184 focus: *Focus, 2185 session: *ui.Session, 2186 root_focus: *Focus, 2187 excluded: *const std.AutoHashMapUnmanaged(usize, void), 2188 ) !void { 2189 var iter = focus.children.iterator(); 2190 while (iter.next()) |entry| { 2191 const id = entry.key_ptr.*; 2192 if (excluded.contains(id)) continue; 2193 const child = entry.value_ptr.*; 2194 const r = child.rect; 2195 2196 if (r.size.width == 0 or r.size.height == 0) continue; // not laid out, rather than merely off-screen 2197 2198 switch (child.focus.kind) { 2199 .text_input, .text_input_password, .text_area => { 2200 const ti = session.text_inputs.get(id) orelse continue; 2201 2202 const inner_left = r.x + 1; 2203 const inner_top = r.y + 1; 2204 const inner_width = if (r.size.width > 2) r.size.width - 2 else 0; 2205 const preserve = !ti.options.read_only; 2206 2207 if (child.focus.kind == .text_area) { 2208 const value = try ti.text(allocator); 2209 defer allocator.free(value); 2210 var id_buf: [128]u8 = undefined; 2211 try output.appendSlice(allocator, try std.fmt.bufPrint(&id_buf, "<textarea data-key=\"control:{d}\" data-focus-id=\"{d}\"", .{ id, id })); 2212 if (preserve) try output.appendSlice(allocator, " data-preserve-value") else try output.appendSlice(allocator, " readonly"); 2213 try appendFormAttribute(allocator, output, root_focus, id); 2214 if (ti.options.name.len > 0) { 2215 try output.appendSlice(allocator, " name=\""); 2216 try appendEscapedHtml(allocator, output, ti.options.name); 2217 try output.append(allocator, '"'); 2218 } 2219 const inner_height = if (r.size.height > 2) r.size.height - 2 else 0; 2220 var pos_buf: [128]u8 = undefined; 2221 try output.appendSlice(allocator, try std.fmt.bufPrint(&pos_buf, " style=\"left:{d}ch;top:{d}em;width:{d}ch;height:{d}em\">\n", .{ inner_left, inner_top, inner_width, inner_height })); 2222 try appendEscapedHtml(allocator, output, value); 2223 try output.appendSlice(allocator, "</textarea>"); 2224 continue; 2225 } 2226 2227 var id_buf: [160]u8 = undefined; 2228 try output.appendSlice(allocator, try std.fmt.bufPrint(&id_buf, "<input type=\"{s}\" data-key=\"control:{d}\" data-focus-id=\"{d}\"", .{ if (child.focus.kind == .text_input_password) "password" else "text", id, id })); 2229 if (preserve) try output.appendSlice(allocator, " data-preserve-value") else try output.appendSlice(allocator, " readonly"); 2230 try appendFormAttribute(allocator, output, root_focus, id); 2231 if (ti.options.name.len > 0) { 2232 try output.appendSlice(allocator, " name=\""); 2233 try appendEscapedHtml(allocator, output, ti.options.name); 2234 try output.append(allocator, '"'); 2235 } 2236 if (child.focus.kind != .text_input_password) { 2237 const value = try ti.text(allocator); 2238 defer allocator.free(value); 2239 try output.appendSlice(allocator, " value=\""); 2240 try appendEscapedHtml(allocator, output, value); 2241 try output.append(allocator, '"'); 2242 } 2243 var pos_buf: [96]u8 = undefined; 2244 try output.appendSlice(allocator, try std.fmt.bufPrint(&pos_buf, " style=\"left:{d}ch;top:{d}em;width:{d}ch;height:1em\">", .{ inner_left, inner_top, inner_width })); 2245 }, 2246 .custom => |custom| { 2247 if (std.mem.eql(u8, custom, "submit") or std.mem.startsWith(u8, custom, ui.submit_action_prefix)) { 2248 if (formOwner(root_focus, id) == null) continue; 2249 var id_buf: [128]u8 = undefined; 2250 try output.appendSlice(allocator, try std.fmt.bufPrint(&id_buf, "<button type=\"submit\" data-key=\"control:{d}\" data-focus-id=\"{d}\"", .{ id, id })); 2251 try appendFormAttribute(allocator, output, root_focus, id); 2252 if (std.mem.startsWith(u8, custom, ui.submit_action_prefix)) { 2253 try output.appendSlice(allocator, " formaction=\""); 2254 try appendEscapedHtml(allocator, output, custom[ui.submit_action_prefix.len..]); 2255 try output.append(allocator, '"'); 2256 } 2257 var pos_buf: [128]u8 = undefined; 2258 try output.appendSlice(allocator, try std.fmt.bufPrint(&pos_buf, " style=\"left:{d}ch;top:{d}em;width:{d}ch;height:{d}em\"></button>", .{ r.x, r.y, r.size.width, r.size.height })); 2259 } else if (std.mem.startsWith(u8, custom, ui.file_input_prefix)) { 2260 try output.appendSlice(allocator, "<input type=\"file\" data-preserve-value data-action=\""); 2261 try appendEscapedHtml(allocator, output, custom[ui.file_input_prefix.len..]); 2262 var id_buf: [128]u8 = undefined; 2263 try output.appendSlice(allocator, try std.fmt.bufPrint(&id_buf, "\" data-key=\"control:{d}\" data-focus-id=\"{d}\" style=\"opacity:0;left:{d}ch;top:{d}em;width:{d}ch;height:{d}em\">", .{ id, id, r.x, r.y, r.size.width, r.size.height })); 2264 } 2265 }, 2266 else => {}, 2267 } 2268 } 2269 } 2270 2271 // emit hidden form owners beside the rendered panels. their controls point 2272 // back here with the standard HTML `form` attribute. 2273 fn renderForms(allocator: std.mem.Allocator, output: *std.ArrayList(u8), root_focus: *Focus) !void { 2274 // the focus child map preserves insertion order, which matches the order 2275 // widgets were added in code 2276 var iter = root_focus.children.iterator(); 2277 while (iter.next()) |entry| { 2278 const child = entry.value_ptr.*; 2279 const action_url = formAction(child.focus) orelse continue; 2280 2281 try output.appendSlice(allocator, "<form hidden data-key=\"form:"); 2282 var id_buf: [96]u8 = undefined; 2283 try output.appendSlice(allocator, try std.fmt.bufPrint(&id_buf, "{d}\" id=\"" ++ form_id_prefix ++ "{d}\" action=\"", .{ entry.key_ptr.*, entry.key_ptr.* })); 2284 try appendEscapedHtml(allocator, output, action_url); 2285 try output.appendSlice(allocator, "\" method=\"post\"></form>"); 2286 } 2287 } 2288 2289 fn colorEql(a: ?Grid.Color, b: ?Grid.Color) bool { 2290 if (a) |av| return if (b) |bv| av.eql(bv) else false; 2291 return b == null; 2292 } 2293 2294 // builds an HTML ` style="color:#rrggbb;background-color:#rrggbb"` attribute 2295 // from a cell's fg/bg into `buf`, omitting whichever color is unset. returns an 2296 // empty slice when neither is set. 2297 fn styleAttr(buf: []u8, fg: ?Grid.Color, bg: ?Grid.Color) []const u8 { 2298 if (fg == null and bg == null) return buf[0..0]; 2299 const prefix = " style=\""; 2300 @memcpy(buf[0..prefix.len], prefix); 2301 var i: usize = prefix.len; 2302 if (fg) |c| { 2303 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]; 2304 i += s.len; 2305 } 2306 if (bg) |c| { 2307 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]; 2308 i += s.len; 2309 } 2310 buf[i] = '"'; 2311 i += 1; 2312 return buf[0..i]; 2313 } 2314 2315 fn appendEscapedHtml(allocator: std.mem.Allocator, out: *std.ArrayList(u8), input: []const u8) !void { 2316 for (input) |ch| { 2317 switch (ch) { 2318 '&' => try out.appendSlice(allocator, "&amp;"), 2319 '<' => try out.appendSlice(allocator, "&lt;"), 2320 '>' => try out.appendSlice(allocator, "&gt;"), 2321 '"' => try out.appendSlice(allocator, "&quot;"), 2322 '\'' => try out.appendSlice(allocator, "&#39;"), 2323 else => try out.append(allocator, ch), 2324 } 2325 } 2326 } 2327 2328 // --- helpers -------------------------------------------------------------- 2329 2330 fn getCookieValue(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 2331 var iter = request.iterateHeaders(); 2332 while (iter.next()) |header| { 2333 if (!std.ascii.eqlIgnoreCase(header.name, "cookie")) continue; 2334 var pairs = std.mem.splitScalar(u8, header.value, ';'); 2335 while (pairs.next()) |pair| { 2336 const trimmed = std.mem.trim(u8, pair, " \t"); 2337 const eq = std.mem.indexOfScalar(u8, trimmed, '=') orelse continue; 2338 if (std.mem.eql(u8, trimmed[0..eq], name)) { 2339 return trimmed[eq + 1 ..]; 2340 } 2341 } 2342 } 2343 return null; 2344 } 2345 2346 fn parseFormField(allocator: std.mem.Allocator, body: []const u8, key: []const u8) !?[]u8 { 2347 var iter = std.mem.splitScalar(u8, body, '&'); 2348 while (iter.next()) |pair| { 2349 const eq = std.mem.indexOfScalar(u8, pair, '=') orelse continue; 2350 if (std.mem.eql(u8, pair[0..eq], key)) { 2351 return try decodeFormValue(allocator, pair[eq + 1 ..]); 2352 } 2353 } 2354 return null; 2355 } 2356 2357 fn readFormBody(request: *std.http.Server.Request, allocator: std.mem.Allocator) ![]u8 { 2358 var buffer: [256]u8 = undefined; 2359 return request.readerExpectNone(&buffer).allocRemaining(allocator, .limited(65536)); 2360 } 2361 2362 fn decodeFormValue(allocator: std.mem.Allocator, encoded: []const u8) ![]u8 { 2363 var out: std.ArrayList(u8) = .empty; 2364 errdefer out.deinit(allocator); 2365 var i: usize = 0; 2366 while (i < encoded.len) : (i += 1) { 2367 switch (encoded[i]) { 2368 '+' => try out.append(allocator, ' '), 2369 '%' => { 2370 if (i + 2 >= encoded.len) return error.InvalidPercentEncoding; 2371 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 2372 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 2373 try out.append(allocator, hi * 16 + lo); 2374 i += 2; 2375 }, 2376 else => try out.append(allocator, encoded[i]), 2377 } 2378 } 2379 return try out.toOwnedSlice(allocator); 2380 } 2381 2382 // disk-backed web session store. each login mints a random, opaque token 2383 // (not the user's id) that is stored as a file named by the token hex, whose 2384 // contents are the raw user_id bytes. authenticating a request is a lookup of 2385 // the cookie's token; logout deletes the file, revoking the session. there is 2386 // deliberately no expiry — a session lives until logout. form failures use 2387 // files in the flash subdirectory that the redirected GET consumes. the 2388 // directory is cleared whenever the server starts. 2389 pub const SessionStore = struct { 2390 io: std.Io, 2391 dir: std.Io.Dir, 2392 flash_dir: std.Io.Dir, 2393 auto_login: ?[token_hex_len]u8, 2394 2395 pub const token_hex_len = evt.event_id_size * 2; 2396 2397 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 2398 try data_dir.createDirPath(io, "sessions"); 2399 const dir = try data_dir.openDir(io, "sessions", .{}); 2400 errdefer dir.close(io); 2401 try dir.deleteTree(io, "flash"); 2402 try dir.createDirPath(io, "flash"); 2403 const flash_dir = try dir.openDir(io, "flash", .{}); 2404 errdefer flash_dir.close(io); 2405 const auto_login = blk: { 2406 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 2407 defer file.close(io); 2408 var token: [token_hex_len]u8 = undefined; 2409 var storage: [token_hex_len]u8 = undefined; 2410 var reader = file.reader(io, &storage); 2411 reader.interface.readSliceAll(&token) catch break :blk null; 2412 break :blk token; 2413 }; 2414 return .{ .io = io, .dir = dir, .flash_dir = flash_dir, .auto_login = auto_login }; 2415 } 2416 2417 pub fn deinit(self: SessionStore) void { 2418 self.flash_dir.close(self.io); 2419 self.dir.close(self.io); 2420 } 2421 2422 // mint a new session for user_id, returning the token hex to set as the 2423 // cookie value. 2424 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2425 var token: [evt.event_id_size]u8 = undefined; 2426 self.io.random(&token); 2427 const token_hex = std.fmt.bytesToHex(token, .lower); 2428 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2429 defer file.close(self.io); 2430 try file.writeStreamingAll(self.io, user_id); 2431 return token_hex; 2432 } 2433 2434 fn createFlash(self: SessionStore, allocator: std.mem.Allocator, value: ui.Session.FormFeedback) ![token_hex_len]u8 { 2435 var token: [evt.event_id_size]u8 = undefined; 2436 self.io.random(&token); 2437 const token_hex = std.fmt.bytesToHex(token, .lower); 2438 const file = try self.flash_dir.createFile(self.io, &token_hex, .{}); 2439 errdefer self.flash_dir.deleteFile(self.io, &token_hex) catch {}; 2440 defer file.close(self.io); 2441 2442 var json: std.Io.Writer.Allocating = .init(allocator); 2443 defer json.deinit(); 2444 try std.json.Stringify.value(value, .{}, &json.writer); 2445 try file.writeStreamingAll(self.io, json.written()); 2446 return token_hex; 2447 } 2448 2449 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 2450 if (!isToken(token_hex)) return null; 2451 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 2452 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 2453 defer file.close(self.io); 2454 2455 var storage: [4096]u8 = undefined; 2456 var reader = file.reader(self.io, &storage); 2457 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 2458 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 2459 } 2460 2461 // resolve a cookie's token to its user_id. returns false (logged out) for a 2462 // missing, malformed, or unknown token. 2463 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2464 if (!isToken(token_hex)) return false; 2465 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2466 defer file.close(self.io); 2467 var storage: [evt.event_id_size]u8 = undefined; 2468 var file_reader = file.reader(self.io, &storage); 2469 file_reader.interface.readSliceAll(out) catch return false; 2470 return true; 2471 } 2472 2473 // revoke a session. a no-op for a missing or malformed token. 2474 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2475 if (!isToken(token_hex)) return; 2476 self.dir.deleteFile(self.io, token_hex) catch {}; 2477 } 2478 2479 // leave `token_hex`'s session for visitors arriving without one. only the 2480 // `try` fixture writes one. the name can't collide with a session token. 2481 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2482 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2483 defer file.close(self.io); 2484 try file.writeStreamingAll(self.io, token_hex); 2485 } 2486 2487 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2488 out.* = self.auto_login orelse return false; 2489 return true; 2490 } 2491 2492 const auto_login_name = "auto-login"; 2493 2494 // a token is exactly token_hex_len hex chars. validating before using the 2495 // value as a path keeps attacker-supplied cookies from escaping the 2496 // sessions dir (e.g. via "/" or ".." bytes). 2497 fn isToken(token_hex: []const u8) bool { 2498 if (token_hex.len != token_hex_len) return false; 2499 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2500 return true; 2501 } 2502 }; 2503