(reader: _SocketBuffer)
| 288 | |
| 289 | |
| 290 | def _read_ws_frame(reader: _SocketBuffer) -> tuple[int, bytes]: |
| 291 | first, second = reader.read_exact(2) |
| 292 | opcode = first & 0x0F |
| 293 | payload_length = second & 0x7F |
| 294 | has_mask = bool(second & 0x80) |
| 295 | |
| 296 | if payload_length == 126: |
| 297 | payload_length = struct.unpack("!H", reader.read_exact(2))[0] |
| 298 | elif payload_length == 127: |
| 299 | payload_length = struct.unpack("!Q", reader.read_exact(8))[0] |
| 300 | |
| 301 | mask = reader.read_exact(4) if has_mask else b"" |
| 302 | payload = reader.read_exact(payload_length) if payload_length else b"" |
| 303 | |
| 304 | if has_mask: |
| 305 | payload = bytes( |
| 306 | byte ^ mask[index % 4] for index, byte in enumerate(payload) |
| 307 | ) |
| 308 | |
| 309 | return opcode, payload |
| 310 | |
| 311 | |
| 312 | def _build_websocket_request( |
no test coverage detected