@@ -750,14 +750,19 @@ pub const WebSocket = struct {
750
750
}
751
751
};
752
752
753
+ pub const CloseMessage = struct {
754
+ exit_code : u16 ,
755
+ data : []const u8 = "" ,
756
+ };
757
+
753
758
pub const WebsocketMessage = struct {
754
759
opcode : Opcode ,
755
760
data : []u8 ,
756
761
};
757
762
758
763
/// Sends close frame with the exit code and frees any allocated memory
759
- pub fn close (ws : * WebSocket , exit_code : u16 ) void {
760
- ws .writeCloseFrame (exit_code ) catch {};
764
+ pub fn close (ws : * WebSocket , options : CloseMessage ) void {
765
+ ws .writeCloseFrame (options ) catch {};
761
766
ws .deinit ();
762
767
}
763
768
@@ -787,6 +792,7 @@ pub const WebSocket = struct {
787
792
if (! payload_head .mask )
788
793
return error .MissingMaskBit ;
789
794
795
+ // TODO: Remove check here for op_head.rsv1 once compression is readded.
790
796
if (@bitCast (op_head .rsv1 ) or @bitCast (op_head .rsv2 ) or @bitCast (op_head .rsv3 ))
791
797
return error .UnnegociatedReservedBits ;
792
798
@@ -821,6 +827,9 @@ pub const WebSocket = struct {
821
827
.binary ,
822
828
= > {
823
829
if (! op_head .fin ) {
830
+ if (ws .fragment .message_type != null )
831
+ return error .UnexpectedFragment ;
832
+
824
833
try ws .fragment .writeAll (payload );
825
834
ws .fragment .message_type = op_head .opcode ;
826
835
@@ -843,6 +852,9 @@ pub const WebSocket = struct {
843
852
const message_type = ws .fragment .message_type orelse return error .FragmentedControl ;
844
853
845
854
if (! op_head .fin ) {
855
+ if (ws .fragment .message_type == null )
856
+ return error .UnexpectedFragment ;
857
+
846
858
try ws .fragment .writeAll (payload );
847
859
continue ;
848
860
}
@@ -880,17 +892,21 @@ pub const WebSocket = struct {
880
892
/// Writes to the server a close frame with a provided `exit_code`.
881
893
///
882
894
/// For more details please see: https://www.rfc-editor.org/rfc/rfc6455#section-5.5.1
883
- pub fn writeCloseFrame (ws : * WebSocket , exit_code : u16 ) Writer.Error ! void {
884
- if (exit_code == 0 ) {
895
+ pub fn writeCloseFrame (ws : * WebSocket , options : CloseMessage ) Writer.Error ! void {
896
+ if (options . exit_code == 0 ) {
885
897
@branchHint (.likely );
886
898
887
- return ws .writeFrame ("" , .connection_close );
899
+ return ws .writeFrame (options . data , .connection_close );
888
900
}
889
901
890
902
var buffer : [2 ]u8 = undefined ;
891
- std .mem .writeInt (u16 , buffer [0.. 2], exit_code , .big );
903
+ std .mem .writeInt (u16 , buffer [0.. 2], options .exit_code , .big );
904
+
905
+ var bufs : [2 ][]const u8 = .{ buffer [0.. ], options .data };
906
+ try ws .writeHeaderFrameVecUnflushed (& bufs , .connection_close , true );
907
+ try ws .writeBodyVecUnflushed (& bufs );
892
908
893
- return ws .writeFrame ( buffer [0 .. ], .connection_close );
909
+ return ws .flush ( );
894
910
}
895
911
896
912
/// Writes a websocket frame directly to the socket.
0 commit comments