def server_version(self, client_name=None, protocol_version=None): '''Returns the server version as a string. client_name: a string identifying the client protocol_version: the protocol version spoken by the client ''' if client_name: self.client = str(client_name)[:17] try: self.client_version = tuple( int(part) for part in self.client.split('.')) except Exception: pass # Find the highest common protocol version. Disconnect if # that protocol version in unsupported. ptuple = util.protocol_version(protocol_version, version.PROTOCOL_MIN, version.PROTOCOL_MAX) # From protocol version 1.1, protocol_version cannot be omitted if ptuple is None or (ptuple >= (1, 1) and protocol_version is None): self.log_info('unsupported protocol version request {}'.format( protocol_version)) raise RPCError( 'unsupported protocol version: {}'.format(protocol_version), JSONRPC.FATAL_ERROR) self.set_protocol_handlers(ptuple) # The return value depends on the protocol version if ptuple < (1, 1): return version.VERSION else: return (version.VERSION, self.protocol_version)
def server_version(self, client_name=None, protocol_version=None): '''Returns the server version as a string. client_name: a string identifying the client protocol_version: the protocol version spoken by the client ''' if client_name: if self.env.drop_client is not None and \ self.env.drop_client.match(client_name): self.close_after_send = True raise RPCError(BAD_REQUEST, f'unsupported client: {client_name}') self.client = str(client_name)[:17] try: self.client_version = tuple( int(part) for part in self.client.split('.')) except Exception: pass # Find the highest common protocol version. Disconnect if # that protocol version in unsupported. ptuple = util.protocol_version(protocol_version, version.PROTOCOL_MIN, version.PROTOCOL_MAX) # From protocol version 1.1, protocol_version cannot be omitted if ptuple is None or (ptuple >= (1, 1) and protocol_version is None): self.logger.info('unsupported protocol version request {}'.format( protocol_version)) self.close_after_send = True raise RPCError( BAD_REQUEST, f'unsupported protocol version: {protocol_version}') self.set_protocol_handlers(ptuple) # The return value depends on the protocol version if ptuple < (1, 1): return version.VERSION else: return (version.VERSION, self.protocol_version)
def protocol_tuple(self, client_protocol_str): '''Given a client's protocol version string, return the negotiated protocol version tuple, or None if unsupported. ''' return util.protocol_version(client_protocol_str, self.PROTOCOL_MIN, self.PROTOCOL_MAX)
def test_protocol_version(): assert util.protocol_version(None, "1.0", "1.0") == (1, 0) assert util.protocol_version("0.10", "0.10", "1.1") == (0, 10) assert util.protocol_version("1.0", "1.0", "1.0") == (1, 0) assert util.protocol_version("1.0", "1.0", "1.1") == (1, 0) assert util.protocol_version("1.1", "1.0", "1.1") == (1, 1) assert util.protocol_version("1.2", "1.0", "1.1") is None assert util.protocol_version("0.9", "1.0", "1.1") is None assert util.protocol_version(["0.9", "1.0"], "1.0", "1.1") == (1, 0) assert util.protocol_version(["0.9", "1.1"], "1.0", "1.1") == (1, 1) assert util.protocol_version(["1.1", "0.9"], "1.0", "1.1") is None assert util.protocol_version(["0.8", "0.9"], "1.0", "1.1") is None assert util.protocol_version(["1.1", "1.2"], "1.0", "1.1") == (1, 1) assert util.protocol_version(["1.2", "1.3"], "1.0", "1.1") is None