def extract_message(cls, raw_bytes): if len(raw_bytes) < 2: return None, raw_bytes if six.byte2int(raw_bytes) != 0x1e: raise FramingError('Start marker is missing: %s' % raw_bytes) if b'\x0a' in raw_bytes: b_msg, rest = raw_bytes.split(b'\x0a', 1) return b_msg[1:], rest else: if b'\x1e' in raw_bytes[1:]: raise FramingError('End marker is missing: %s' % raw_bytes) return None, raw_bytes
def extract_message(self, raw_bytes): rb_len = len(raw_bytes) if rb_len < 4: return None, raw_bytes try: msg_len = unpack('<i', raw_bytes[:4])[0] if msg_len < 5: raise FramingError('Minimum valid message length is 5.') if rb_len < msg_len: return None, raw_bytes else: return raw_bytes[:msg_len], raw_bytes[msg_len:] except Exception as e: raise FramingError(e)
def extract_message(cls, raw_bytes): if len(raw_bytes) < 2: return None, raw_bytes if six.byte2int(raw_bytes) != 123: raise FramingError('Broken state. Expected JSON Object, got: %s' % raw_bytes) stack = [123] uniesc = 0 poppers = {91: [93], 123: [125], 34: [34]} adders = {91: [34, 91, 123], 123: [34, 91, 123], 34: [92], 92: [117]} for idx in range(1, len(raw_bytes)): cbyte = six.indexbytes(raw_bytes, idx) if cbyte in poppers.get(stack[-1], []): stack.pop() elif cbyte in adders.get(stack[-1], []): stack.append(cbyte) elif stack[-1] == 92: stack.pop() elif stack[-1] == 117: uniesc += 1 if uniesc >= 4: stack = stack[:-2] uniesc = 0 if not stack: return raw_bytes[:idx + 1], raw_bytes[idx + 1:] return None, raw_bytes
def extract_message(cls, raw_bytes): if b':' not in raw_bytes: if len(raw_bytes) > 10: raise FramingError('Length information missing: %s' % raw_bytes) return None, raw_bytes msg_len, rest = raw_bytes.split(b':', 1) try: msg_len = int(msg_len) except ValueError: raise FramingError('Invalid length: %s' % raw_bytes) if msg_len < 0: raise FramingError('Negative length: %s' % raw_bytes) if len(rest) < msg_len + 1: return None, raw_bytes else: if six.indexbytes(rest, msg_len) != 44: raise FramingError('Missing correct end marker: %s' % raw_bytes) return rest[:msg_len], rest[(msg_len + 1):]
def into_frame(self, message_bytes): try: return self._framer(message_bytes) except Exception as e: raise FramingError(e)
def extract_message(self, raw_bytes): try: return self._extractor(raw_bytes) except Exception as e: raise FramingError(e)