@@ -21,17 +21,14 @@ async fn[R : @io.Reader] read_frame(reader : R) -> Frame {
2121 let byte1 = header [1 ]
2222 let fin = (byte0 .to_int () & 0x80 ) != 0
2323 let opcode_byte = byte0 & b '\x0F '
24- let opcode = match OpCode ::from_byte (opcode_byte ) {
25- Some (op ) => op
26- None => raise FrameError ("Invalid opcode: \{ opcode_byte } " )
27- }
24+ let opcode = OpCode ::from_byte (opcode_byte )
2825 let masked = (byte1 .to_int () & 0x80 ) != 0
2926 let mut payload_len = (byte1 .to_int () & 0x7F ).to_int64 ()
3027
31- // Validate payload length according to RFC 6455
28+ // Validate payload length according to RFC 6455 Section 5.2
3229 if payload_len == 126L {
3330 let len_bytes = reader .read_exactly (2 )
34- guard len_bytes is [u16be (len ), .. ]
31+ guard len_bytes is [u16be (len )]
3532 payload_len = len .to_int64 ()
3633 if payload_len < 126L {
3734 raise FrameError (
@@ -40,7 +37,7 @@ async fn[R : @io.Reader] read_frame(reader : R) -> Frame {
4037 }
4138 } else if payload_len == 127L {
4239 let len_bytes = reader .read_exactly (8 )
43- guard len_bytes is [u64be (len ), .. ]
40+ guard len_bytes is [u64be (len )]
4441 payload_len = len .reinterpret_as_int64 ()
4542 if payload_len < 65536L {
4643 raise FrameError (
@@ -49,14 +46,12 @@ async fn[R : @io.Reader] read_frame(reader : R) -> Frame {
4946 }
5047 if payload_len < 0L {
5148 raise FrameError (
52- "Payload length too large (negative when interpreted as signed) " ,
49+ "Invalid payload length: MSB must be 0 for 64-bit length " ,
5350 )
5451 }
5552 }
56-
57- // Check for reasonable payload size limit (1MB for now)
58- if payload_len > 1048576L {
59- raise FrameError ("Payload too large: \{ payload_len } bytes (max 1MB)" )
53+ if payload_len > @int .max_value.to_int64 () {
54+ raise FrameError ("Payload too large: \{ payload_len } bytes" )
6055 }
6156
6257 // Read masking key if present
@@ -89,13 +84,6 @@ async fn[W : @io.Writer] write_frame(
8984 masked : Bool ,
9085) -> Unit {
9186 let payload_len = payload .length ().to_int64 ()
92-
93- // Validate payload size
94- if payload_len > 1048576L {
95- raise FrameError (
96- "Payload too large for sending: \{ payload_len } bytes (max 1MB)" ,
97- )
98- }
9987 let mut header_len = 2
10088
10189 // Calculate extended length size
@@ -105,45 +93,37 @@ async fn[W : @io.Writer] write_frame(
10593 header_len + = 8
10694 }
10795
108- // Add mask size if needed
109- if masked {
110- header_len + = 4
111- }
112-
11396 // Build header
114- let header = FixedArray ::make (header_len , b '\x00 ' )
115- let mut offset = 0
97+ let header = if masked {
98+ FixedArray ::make (header_len + 4 , b '\x00 ' )
99+ } else {
100+ FixedArray ::make (header_len , b '\x00 ' )
101+ }
116102
117103 // First byte: FIN + opcode
118- header [offset ] = if fin {
104+ header [0 ] = if fin {
119105 (0x80 | opcode .to_byte ().to_int ()).to_byte ()
120106 } else {
121107 opcode .to_byte ()
122108 }
123- offset + = 1
124109
125110 // Second byte: MASK + payload length
126111 let mask_bit = if masked { 0x80 } else { 0 }
127112 if payload_len < 126L {
128- header [offset ] = (mask_bit | payload_len .to_int ()).to_byte ()
129- offset + = 1
113+ header [1 ] = (mask_bit | payload_len .to_int ()).to_byte ()
130114 } else if payload_len <= 65535L {
131- header [offset ] = (mask_bit | 126 ).to_byte ()
132- offset + = 1
133- header .unsafe_write_uint16_be (offset , payload_len .to_uint16 ())
134- offset + = 2
115+ header [1 ] = (mask_bit | 126 ).to_byte ()
116+ header .unsafe_write_uint16_be (2 , payload_len .to_uint16 ())
135117 } else {
136- header [offset ] = (mask_bit | 127 ).to_byte ()
137- offset + = 1
138- header .unsafe_write_uint64_be (offset , payload_len .reinterpret_as_uint64 ())
139- offset + = 8
118+ header [1 ] = (mask_bit | 127 ).to_byte ()
119+ header .unsafe_write_uint64_be (2 , payload_len .reinterpret_as_uint64 ())
140120 }
141121
142122 // Add masking key and mask payload if needed
143123 let final_payload = if masked {
144124 let mask = generate_mask ()
145125 for i = 0 ; i < 4 ; i = i + 1 {
146- header [offset + i ] = mask [i ]
126+ header [header_len + i ] = mask [i ]
147127 }
148128 let payload_arr = payload .to_fixedarray ()
149129 mask_payload (payload_arr , mask )
0 commit comments