atproto pds in zig pds.zat.dev
pds atproto
24

Configure Feed

Select the types of activity you want to include in your feed.

Read request bodies with explicit loops

zzstoatzz (May 22, 2026, 3:47 PM -0500) a42c349c 2e9f048e

+22 -7
+22 -7
src/http/api.zig
··· 55 55 const reader = try request.readerExpectContinue(&reader_buf); 56 56 if (request.head.content_length) |len| { 57 57 if (len > buf.len) return error.BodyTooLarge; 58 - try reader.readSliceAll(buf[0..@intCast(len)]); 59 - return buf[0..@intCast(len)]; 58 + var total: usize = 0; 59 + const expected: usize = @intCast(len); 60 + while (total < expected) { 61 + const n = try reader.readSliceShort(buf[total..expected]); 62 + if (n == 0) return error.UnexpectedEof; 63 + total += n; 64 + } 65 + return buf[0..expected]; 60 66 } 61 67 62 68 var total: usize = 0; ··· 80 86 if (request.head.content_length) |len| { 81 87 if (len > max_len) return error.BodyTooLarge; 82 88 const body = try allocator.alloc(u8, @intCast(len)); 83 - try reader.readSliceAll(body); 89 + var total: usize = 0; 90 + while (total < body.len) { 91 + const n = try reader.readSliceShort(body[total..]); 92 + if (n == 0) return error.UnexpectedEof; 93 + total += n; 94 + } 84 95 return body; 85 96 } 86 97 87 - return reader.allocRemaining(allocator, .limited(max_len)) catch |err| switch (err) { 88 - error.StreamTooLong => error.BodyTooLarge, 89 - else => err, 90 - }; 98 + var body: std.ArrayList(u8) = .empty; 99 + var chunk: [8192]u8 = undefined; 100 + while (true) { 101 + const n = try reader.readSliceShort(&chunk); 102 + if (n == 0) return try body.toOwnedSlice(allocator); 103 + if (body.items.len + n > max_len) return error.BodyTooLarge; 104 + try body.appendSlice(allocator, chunk[0..n]); 105 + } 91 106 } 92 107 93 108 pub fn headerValue(request: *const http.Server.Request, name: []const u8) ?[]const u8 {