def __init__(self, protocol_version, address, sock, **config): self.protocol_version = protocol_version self.address = address self.socket = sock self.error_handler = config.get("error_handler", ConnectionErrorHandler()) self.server = ServerInfo(SocketAddress.from_socket(sock), protocol_version) 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_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_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: self.auth_dict = vars(AuthToken("basic", *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 __init__(self, protocol_version, unresolved_address, sock, **config): self.protocol_version = protocol_version self.unresolved_address = unresolved_address self.socket = sock self.server = ServerInfo(SocketAddress.from_socket(sock), 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 = config.get( "max_connection_lifetime", DEFAULT_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", get_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: self.auth_dict = vars(AuthToken("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") # Pick up the server certificate, if any self.der_encoded_server_certificate = config.get( "der_encoded_server_certificate")