def test_mask_noop_long(self): key = six.b("\x00\x00\x00\x00") self.assertEqual(mask(six.b("LongTest"), key), six.b("LongTest"))
def test_mask_noop_long(self): key = "\x00\x00\x00\x00" self.assertEqual(mask("LongTest", key), "LongTest")
def test_mask_noop(self): key = "\x00\x00\x00\x00" self.assertEqual(mask("Test", key), "Test")
def parse_hybi07_frames(buf): """ Parse HyBi-07 frames in a highly compliant manner. """ start = 0 frames = [] while True: # If there's not at least two bytes in the buffer, bail. if len(buf) - start < 2: break # Grab the header. This single byte holds some flags nobody cares # about, and an opcode which nobody cares about. header = buf[start] if six.PY2: header = ord(header) if header & 0x70: # At least one of the reserved flags is set. Pork chop sandwiches! raise WSException("Reserved flag in HyBi-07 frame (%d)" % header) frames.append(("", CLOSE)) return frames, buf # Get the opcode, and translate it to a local enum which we actually # care about. opcode = header & 0xf try: opcode = opcode_types[opcode] except KeyError: raise WSException("Unknown opcode %d in HyBi-07 frame" % opcode) # Get the payload length and determine whether we need to look for an # extra length. length = buf[start + 1] if six.PY2: length = ord(length) masked = length & 0x80 length &= 0x7f # The offset we're gonna be using to walk through the frame. We use # this because the offset is variable depending on the length and # mask. offset = 2 # Extra length fields. if length == 0x7e: if len(buf) - start < 4: break length = buf[start + 2:start + 4] length = unpack(">H", length)[0] offset += 2 elif length == 0x7f: if len(buf) - start < 10: break # Protocol bug: The top bit of this long long *must* be cleared; # that is, it is expected to be interpreted as signed. That's # f*****g stupid, if you don't mind me saying so, and so we're # interpreting it as unsigned anyway. If you wanna send exabytes # of data down the wire, then go ahead! length = buf[start + 2:start + 10] length = unpack(">Q", length)[0] offset += 8 if masked: if len(buf) - (start + offset) < 4: break key = buf[start + offset:start + offset + 4] offset += 4 if len(buf) - (start + offset) < length: break data = buf[start + offset:start + offset + length] if masked: data = mask(data, key) if opcode == CLOSE: if len(data) >= 2: # Gotta unpack the opcode and return usable data here. data = unpack(">H", data[:2])[0], data[2:] else: # No reason given; use generic data. data = 1000, six.b("No reason given") frames.append((opcode, data)) start += offset + length return frames, buf[start:]