def __init__(self, address, sock, protocol_version, error_handler, **config): self.address = address self.socket = sock self.protocol_version = protocol_version self.error_handler = error_handler self.server = ServerInfo(SocketAddress.from_socket(sock)) self.input_buffer = ChunkedInputBuffer() self.output_buffer = ChunkedOutputBuffer() self.packer = Packer(self.output_buffer) self.unpacker = Unpacker() self.responses = deque() self._max_connection_lifetime = config.get("max_connection_lifetime", default_config["max_connection_lifetime"]) self._creation_timestamp = perf_counter() # Determine the user agent and ensure it is a Unicode value user_agent = config.get("user_agent", default_config["user_agent"]) if isinstance(user_agent, bytes): user_agent = user_agent.decode("UTF-8") self.user_agent = user_agent # Determine auth details auth = config.get("auth") if not auth: self.auth_dict = {} elif isinstance(auth, tuple) and 2 <= len(auth) <= 3: from neo4j.v1 import basic_auth self.auth_dict = vars(basic_auth(*auth)) else: try: self.auth_dict = vars(auth) except (KeyError, TypeError): raise TypeError("Cannot determine auth details from %r" % auth) # Pick up the server certificate, if any self.der_encoded_server_certificate = config.get("der_encoded_server_certificate")
def _yield_messages(self, sock): try: buffer = UnpackableBuffer() chunk_loader = self._load_chunks(sock, buffer) unpacker = Unpacker(buffer) details = [] while True: unpacker.reset() details[:] = () chunk_size = -1 while chunk_size != 0: chunk_size = next(chunk_loader) summary_signature = None summary_metadata = None size, signature = unpacker.unpack_structure_header() if size > 1: raise ProtocolError("Expected one field") if signature == b"\x71": data = unpacker.unpack() details.append(data) else: summary_signature = signature summary_metadata = unpacker.unpack_map() yield details, summary_signature, summary_metadata except OSError as error: self.on_error(error)
def pop_message(self): data = bytearray() while True: chunk = self.pop_chunk() print("CHUNK %r" % chunk) if chunk: data.extend(chunk) elif data: break # end of message else: continue # NOOP header = data[0] n_fields = header % 0x10 tag = data[1] buffer = UnpackableBuffer(data[2:]) unpacker = Unpacker(buffer) fields = [unpacker.unpack() for _ in range(n_fields)] return tag, fields
def _yield_messages(self, sock): try: buffer = UnpackableBuffer() unpacker = Unpacker(buffer) chunk_size = 0 while True: while chunk_size == 0: # Determine the chunk size and skip noop buffer.receive(sock, 2) chunk_size = buffer.pop_u16() if chunk_size == 0: log.debug("[#%04X] S: <NOOP>", sock.getsockname()[1]) buffer.receive(sock, chunk_size + 2) chunk_size = buffer.pop_u16() if chunk_size == 0: # chunk_size was the end marker for the message size, tag = unpacker.unpack_structure_header() fields = [unpacker.unpack() for _ in range(size)] yield tag, fields # Reset for new message unpacker.reset() except OSError as error: self.on_error(error)
def __init__(self, unresolved_address, sock, max_connection_lifetime, *, auth=None, user_agent=None, routing_context=None): self.unresolved_address = unresolved_address self.socket = sock self.server_info = ServerInfo(Address(sock.getpeername()), self.PROTOCOL_VERSION) self.outbox = Outbox() self.inbox = Inbox(self.socket, on_error=self._set_defunct) self.packer = Packer(self.outbox) self.unpacker = Unpacker(self.inbox) self.responses = deque() self._max_connection_lifetime = max_connection_lifetime self._creation_timestamp = perf_counter() self.supports_multiple_results = False self.supports_multiple_databases = False self._is_reset = True self.routing_context = routing_context # Determine the user agent if user_agent: self.user_agent = user_agent else: self.user_agent = get_user_agent() # Determine auth details if not auth: self.auth_dict = {} elif isinstance(auth, tuple) and 2 <= len(auth) <= 3: from neo4j import Auth self.auth_dict = vars(Auth("basic", *auth)) else: try: self.auth_dict = vars(auth) except (KeyError, TypeError): raise AuthError("Cannot determine auth details from %r" % auth) # Check for missing password try: credentials = self.auth_dict["credentials"] except KeyError: pass else: if credentials is None: raise AuthError("Password cannot be None")
def assert_packable(cls, value, packed_value): stream_out = BytesIO() packer = Packer(stream_out) packer.pack(value) packed = stream_out.getvalue() try: assert packed == packed_value except AssertionError: raise AssertionError("Packed value %r is %r instead of expected %r" % (value, packed, packed_value)) unpacked = Unpacker(UnpackableBuffer(packed)).unpack() try: assert unpacked == value except AssertionError: raise AssertionError("Unpacked value %r is not equal to original %r" % (unpacked, value))
def __init__(self, unresolved_address, sock, *, auth=None, protocol_version=None, **config): self.config = PoolConfig.consume(config) self.protocol_version = protocol_version self.unresolved_address = unresolved_address self.socket = sock self.server = ServerInfo(Address(sock.getpeername()), protocol_version) self.outbox = Outbox() self.inbox = Inbox(BufferedSocket(self.socket, 32768), on_error=self._set_defunct) self.packer = Packer(self.outbox) self.unpacker = Unpacker(self.inbox) self.responses = deque() self._max_connection_lifetime = self.config.max_age self._creation_timestamp = perf_counter() # Determine the user agent user_agent = self.config.user_agent if user_agent: self.user_agent = user_agent else: self.user_agent = get_user_agent() # Determine auth details if not auth: self.auth_dict = {} elif isinstance(auth, tuple) and 2 <= len(auth) <= 3: from neo4j import Auth self.auth_dict = vars(Auth("basic", *auth)) else: try: self.auth_dict = vars(auth) except (KeyError, TypeError): raise AuthError("Cannot determine auth details from %r" % auth) # Check for missing password try: credentials = self.auth_dict["credentials"] except KeyError: pass else: if credentials is None: raise AuthError("Password cannot be None")
def _yield_messages(self, sock): try: buffer = UnpackableBuffer() chunk_loader = self._load_chunks(sock, buffer) unpacker = Unpacker(buffer) while True: unpacker.reset() chunk_size = -1 while chunk_size != 0: chunk_size = next(chunk_loader) size, tag = unpacker.unpack_structure_header() fields = [unpacker.unpack() for _ in range(size)] yield tag, fields except OSError as error: self.on_error(error)
def test_list_stream(self): packed_value = b"\xD7\x01\x02\x03\xDF" unpacked_value = [1, 2, 3] stream_out = BytesIO() packer = Packer(stream_out) packer.pack_list_stream_header() packer.pack(1) packer.pack(2) packer.pack(3) packer.pack_end_of_stream() packed = stream_out.getvalue() try: assert packed == packed_value except AssertionError: raise AssertionError("Packed value is %r instead of expected %r" % (packed, packed_value)) unpacked = Unpacker(UnpackableBuffer(packed)).unpack() try: assert unpacked == unpacked_value except AssertionError: raise AssertionError("Unpacked value %r is not equal to expected %r" % (unpacked, unpacked_value))
def __init__(self, sock, **config): self.socket = sock self.server = ServerInfo(SocketAddress.from_socket(sock)) self.input_buffer = ChunkedInputBuffer() self.output_buffer = ChunkedOutputBuffer() self.packer = Packer(self.output_buffer) self.unpacker = Unpacker() self.responses = deque() # Determine the user agent and ensure it is a Unicode value user_agent = config.get("user_agent", DEFAULT_USER_AGENT) if isinstance(user_agent, bytes): user_agent = user_agent.decode("UTF-8") self.user_agent = user_agent # Determine auth details auth = config.get("auth") if not auth: self.auth_dict = {} elif isinstance(auth, tuple) and 2 <= len(auth) <= 3: from neo4j.v1 import basic_auth self.auth_dict = vars(basic_auth(*auth)) else: try: self.auth_dict = vars(auth) except (KeyError, TypeError): raise TypeError("Cannot determine auth details from %r" % auth) # Pick up the server certificate, if any self.der_encoded_server_certificate = config.get( "der_encoded_server_certificate") response = InitResponse(self) self.append(INIT, (self.user_agent, self.auth_dict), response=response) self.sync() self._supports_statement_reuse = self.server.supports_statement_reuse() self.packer.supports_bytes = self.server.supports_bytes()
def test_map_stream(self): packed_value = b"\xDB\x81A\x01\x81B\x02\xDF" unpacked_value = {u"A": 1, u"B": 2} stream_out = BytesIO() packer = Packer(stream_out) packer.pack_map_stream_header() packer.pack(u"A") packer.pack(1) packer.pack(u"B") packer.pack(2) packer.pack_end_of_stream() packed = stream_out.getvalue() try: assert packed == packed_value except AssertionError: raise AssertionError("Packed value is %r instead of expected %r" % (packed, packed_value)) unpacked = Unpacker(UnpackableBuffer(packed)).unpack() try: assert unpacked == unpacked_value except AssertionError: raise AssertionError("Unpacked value %r is not equal to expected %r" % (unpacked, unpacked_value))