Skip to content

Commit 2968e8e

Browse files
committed
std: update logic on payload read
1 parent 12a682d commit 2968e8e

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

lib/std/http/Server.zig

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,8 @@ pub const WebSocket = struct {
792792
if (!payload_head.mask)
793793
return error.MissingMaskBit;
794794

795-
// TODO: Remove check here for op_head.rsv1 once compression is readded.
795+
// TODO: Remove check here for op_head.rsv1 if compression
796+
// is added in the future
796797
if (@bitCast(op_head.rsv1) or @bitCast(op_head.rsv2) or @bitCast(op_head.rsv3))
797798
return error.UnnegociatedReservedBits;
798799

@@ -804,14 +805,15 @@ pub const WebSocket = struct {
804805

805806
const mask: u32 = @bitCast((try in.takeArray(4)).*);
806807
const payload = blk: {
807-
if (total < in.buffered().len)
808-
break :blk try in.take(total);
808+
if (total > in.buffer.len) {
809+
try ws.storage.ensureUnusedCapacity(total);
810+
try in.streamExact(&ws.storage.writer, total);
811+
defer ws.storage.shrinkRetainingCapacity(0);
809812

810-
try ws.storage.ensureUnusedCapacity(total);
811-
try in.streamExact(&ws.storage.writer, total);
812-
defer ws.storage.shrinkRetainingCapacity(0);
813+
break :blk ws.storage.written();
814+
}
813815

814-
break :blk ws.storage.written();
816+
break :blk try in.take(total);
815817
};
816818

817819
// The last item may contain a partial word of unused data.
@@ -827,9 +829,6 @@ pub const WebSocket = struct {
827829
.binary,
828830
=> {
829831
if (!op_head.fin) {
830-
if (ws.fragment.message_type != null)
831-
return error.UnexpectedFragment;
832-
833832
try ws.fragment.writeAll(payload);
834833
ws.fragment.message_type = op_head.opcode;
835834

@@ -852,9 +851,6 @@ pub const WebSocket = struct {
852851
const message_type = ws.fragment.message_type orelse return error.FragmentedControl;
853852

854853
if (!op_head.fin) {
855-
if (ws.fragment.message_type == null)
856-
return error.UnexpectedFragment;
857-
858854
try ws.fragment.writeAll(payload);
859855
continue;
860856
}

0 commit comments

Comments
 (0)