Skip to content

Lightpanda: the headless browser designed for AI and automation

test: http fragmentation test

Karl Seguinb8f3eba23b7e
diff --git a/src/server/http.zig b/src/server/http.zig
index 97d783b57..b54851b68 100644
--- a/src/server/http.zig
+++ b/src/server/http.zig
@@ -1242,6 +1242,87 @@ test "http: the read buffer grows with the request and gives the space back" {
try testing.expectEqual(INITIAL_BUFFER_SIZE, buffer.buf.len);
}
+test "http: a request can arrive over any number of reads" {
+ const body = "{\"capabilities\":{}}";
+ const head = std.fmt.comptimePrint("POST /session HTTP/1.1\r\nContent-Length: {d}\r\n\r\n", .{body.len});
+ // a head that outgrows the initial buffer, so it's split across doublings too
+ const long_head = "POST /session HTTP/1.1\r\nX-Padding: " ++ repeat("a", INITIAL_BUFFER_SIZE) ++ head["POST /session HTTP/1.1".len..];
+
+ // every two-way split, including within the final CRLF CRLF
+ for (1..head.len + body.len) |at| {
+ try expectSplitRequest(head, body, &.{at});
+ }
+
+ // one byte at a time
+ var one_byte: [head.len + body.len - 1]usize = undefined;
+ for (&one_byte, 1..) |*at, i| {
+ at.* = i;
+ }
+ try expectSplitRequest(head, body, &one_byte);
+
+ // either side of the initial buffer filling, then within the final CRLF
+ // CRLF and the body
+ try expectSplitRequest(long_head, body, &.{
+ INITIAL_BUFFER_SIZE - 1,
+ INITIAL_BUFFER_SIZE,
+ INITIAL_BUFFER_SIZE + 1,
+ long_head.len - 3,
+ long_head.len - 1,
+ long_head.len + 1,
+ });
+}
+
+// Writes head ++ body in pieces ending at each of `splits` (ascending) and
+// expects the request to be incomplete until the last piece arrives.
+fn expectSplitRequest(head: []const u8, body: []const u8, splits: []const usize) !void {
+ var pair: [2]posix.socket_t = undefined;
+ if (std.c.socketpair(posix.AF.LOCAL, posix.SOCK.STREAM, 0, &pair) != 0) {
+ return error.SocketPairFailed;
+ }
+ defer sys_net.close(pair[0]);
+ defer sys_net.close(pair[1]);
+
+ var buffer = try Connection.Buffer.init(testing.allocator, 1024 * 1024);
+ defer buffer.deinit();
+
+ const request = try std.mem.concat(testing.allocator, u8, &.{ head, body });
+ defer testing.allocator.free(request);
+
+ var state: Connection.State = .header;
+ var start: usize = 0;
+ for (splits) |end| {
+ try sys_net.writeAll(pair[1], request[start..end]);
+ start = end;
+ try testing.expectEqual(false, try readWritten(&state, &buffer, pair[0], end));
+ }
+ try sys_net.writeAll(pair[1], request[start..]);
+ try testing.expectEqual(true, try readWritten(&state, &buffer, pair[0], request.len));
+
+ const req = state.request;
+ try testing.expectEqual(.POST, req.method);
+ try testing.expectString("/session", req.path);
+ try testing.expectString(head, req.head);
+ try testing.expectString(body, req.body);
+}
+
+// processHTTP's read/parse step, repeated until the `written` bytes are all
+// in the buffer. True once the request is complete.
+fn readWritten(state: *Connection.State, buffer: *Connection.Buffer, socket: posix.socket_t, written: usize) !bool {
+ while (true) {
+ const data = try buffer.read(socket);
+ switch (try state.parseHeader(testing.allocator, data)) {
+ .complete => {
+ try testing.expectEqual(written, data.len);
+ return true;
+ },
+ .need => |needed| try buffer.ensureCapacity(needed),
+ }
+ if (buffer.len == written) {
+ return false;
+ }
+ }
+}
+
test "http: a declared body is sized upfront" {
var pair: [2]posix.socket_t = undefined;
if (std.c.socketpair(posix.AF.LOCAL, posix.SOCK.STREAM, 0, &pair) != 0) {