diff options
Diffstat (limited to 'web/httpfs.zig')
| -rw-r--r-- | web/httpfs.zig | 45 |
1 files changed, 29 insertions, 16 deletions
diff --git a/web/httpfs.zig b/web/httpfs.zig index fa783e5..8af5793 100644 --- a/web/httpfs.zig +++ b/web/httpfs.zig @@ -294,16 +294,35 @@ fn FS(comptime M: type) type { fn listDir(self: *Self, st: wire.Stat) !void { const html = self.wantsHtml(); + var name_buf: [256]u8 = undefined; + const name = name_buf[0..@min(st.name.len, name_buf.len)]; + @memcpy(name, st.name[0..name.len]); const leaf = self.leaf_fid.?; const opened = try self.rpcSmall(.{ .open = .{ .fid = leaf, .mode = c9.oread } }); if (opened.msg == .rerror) return self.rerr(opened.msg.rerror.ename); if (opened.msg != .ropen) return error.Upstream; - // JSON/HTML is built into `big`; directory data is read into `small`. - var w: std.Io.Writer = .fixed(self.big); + // Read frames must fit the RPC scratch buffer. Stream the listing + // so large directories neither overflow `big` nor appear empty. + const count: u32 = @intCast(@min(self.chunk(), self.small.len - 11)); + var r: M.Rpc = .{ .buf = self.small }; + var got = try self.m.rpc(.{ .read = .{ .fid = leaf, .offset = 0, .count = count } }, &r); + if (got.msg == .rerror) return self.rerr(got.msg.rerror.ename); + if (got.msg != .rread) return error.Upstream; + self.responded = true; + var bw = try self.request.respondStreaming(self.big, .{ .respond_options = .{ + .status = .ok, + .keep_alive = false, + .extra_headers = &.{ + .{ .name = "content-type", .value = if (html) "text/html; charset=utf-8" else "application/json" }, + .{ .name = "cache-control", .value = "no-store" }, + .{ .name = "x-content-type-options", .value = "nosniff" }, + }, + } }); + const w = &bw.writer; if (html) { w.writeAll("<!doctype html><meta charset=utf-8><title>") catch return error.Upstream; - writeHtml(&w, st.name); + writeHtml(w, name); w.writeAll("</title><ul>") catch return error.Upstream; } else { w.writeByte('[') catch return error.Upstream; @@ -311,37 +330,31 @@ fn FS(comptime M: type) type { var offset: u64 = 0; var first = true; while (true) { - var r: M.Rpc = .{ .buf = self.small }; - const got = try self.m.rpc(.{ .read = .{ .fid = leaf, .offset = offset, .count = self.chunk() } }, &r); - if (got.msg != .rread) break; const data = got.msg.rread.data; if (data.len == 0) break; offset += data.len; var rest: []const u8 = data; while (rest.len >= 2) { const size: usize = @as(usize, std.mem.readInt(u16, rest[0..2], .little)) + 2; - if (size > rest.len) break; - const entry = c9.Stat.decode(rest[0..size]) catch break; + if (size > rest.len) return error.Upstream; + const entry = c9.Stat.decode(rest[0..size]) catch return error.Upstream; rest = rest[size..]; if (html) { w.writeAll("<li>") catch return error.Upstream; - writeHtml(&w, entry.name); + writeHtml(w, entry.name); if (entry.qid.type & c9.qtdir != 0) w.writeByte('/') catch {}; w.writeAll("</li>") catch return error.Upstream; } else { if (!first) w.writeByte(',') catch return error.Upstream; first = false; - writeEntryJson(&w, entry) catch return error.Upstream; + writeEntryJson(w, entry) catch return error.Upstream; } } + got = try self.m.rpc(.{ .read = .{ .fid = leaf, .offset = offset, .count = count } }, &r); + if (got.msg != .rread) return error.Upstream; } if (html) w.writeAll("</ul>") catch return error.Upstream else w.writeByte(']') catch return error.Upstream; - self.responded = true; - self.request.respond(w.buffered(), .{ .status = .ok, .keep_alive = false, .extra_headers = &.{ - .{ .name = "content-type", .value = if (html) "text/html; charset=utf-8" else "application/json" }, - .{ .name = "cache-control", .value = "no-store" }, - .{ .name = "x-content-type-options", .value = "nosniff" }, - } }) catch {}; + try bw.end(); } // -- writes -------------------------------------------------------- |
