def get(self, url): """Get an Updater, if any, for this URL. Assumptions: - @url is a string.""" GENERIC_WARNING_MESSAGE = "No updater or interposition for url={url}" DIFFERENT_NETLOC_MESSAGE = "We have an updater for netloc={netloc1} but not for netlocs={netloc2}" HOSTNAME_FOUND_MESSAGE = "Found updater for hostname={hostname}" HOSTNAME_NOT_FOUND_MESSAGE = "No updater for hostname={hostname}" updater = None try: parsed_url = urlparse.urlparse(url) hostname = parsed_url.hostname port = parsed_url.port or 80 netloc = parsed_url.netloc network_location = "{hostname}:{port}".format(hostname=hostname, port=port) # Sometimes parsed_url.netloc does not have a port (e.g. 80), # so we do a double check. network_locations = set((netloc, network_location)) updater = self.__updaters.get(hostname) if updater is None: Logger.warn( HOSTNAME_NOT_FOUND_MESSAGE.format(hostname=hostname)) else: # Ensure that the updater is meant for this (hostname, port). if updater.configuration.network_location in network_locations: Logger.info( HOSTNAME_FOUND_MESSAGE.format(hostname=hostname)) # Raises an exception in case we do not recognize how to # transform this URL for TUF. In that case, there will be no # updater for this URL. target_filepath = updater.get_target_filepath(url) else: # Same hostname, but different (not user-specified) port. Logger.warn( DIFFERENT_NETLOC_MESSAGE.format( netloc1=updater.configuration.network_location, netloc2=network_locations)) updater = None except: Logger.exception(GENERIC_WARNING_MESSAGE.format(url=url)) updater = None finally: if updater is None: Logger.warn(GENERIC_WARNING_MESSAGE.format(url=url)) return updater
def get(self, url): """Get an Updater, if any, for this URL. Assumptions: - @url is a string.""" GENERIC_WARNING_MESSAGE = "No updater or interposition for url={url}" DIFFERENT_NETLOC_MESSAGE = "We have an updater for netloc={netloc1} but not for netlocs={netloc2}" HOSTNAME_FOUND_MESSAGE = "Found updater for hostname={hostname}" HOSTNAME_NOT_FOUND_MESSAGE = "No updater for hostname={hostname}" updater = None try: parsed_url = urlparse.urlparse(url) hostname = parsed_url.hostname port = parsed_url.port or 80 netloc = parsed_url.netloc network_location = "{hostname}:{port}".format(hostname=hostname, port=port) # Sometimes parsed_url.netloc does not have a port (e.g. 80), # so we do a double check. network_locations = set((netloc, network_location)) updater = self.__updaters.get(hostname) if updater is None: Logger.warn(HOSTNAME_NOT_FOUND_MESSAGE.format(hostname=hostname)) else: # Ensure that the updater is meant for this (hostname, port). if updater.configuration.network_location in network_locations: Logger.info(HOSTNAME_FOUND_MESSAGE.format(hostname=hostname)) # Raises an exception in case we do not recognize how to # transform this URL for TUF. In that case, there will be no # updater for this URL. target_filepath = updater.get_target_filepath(url) else: # Same hostname, but different (not user-specified) port. Logger.warn(DIFFERENT_NETLOC_MESSAGE.format( netloc1=updater.configuration.network_location, netloc2=network_locations)) updater = None except: Logger.exception(GENERIC_WARNING_MESSAGE.format(url=url)) updater = None finally: if updater is None: Logger.warn(GENERIC_WARNING_MESSAGE.format(url=url)) return updater
def get(self, url): """ <Purpose> This method is to get the updater if it already exists. It takes the url and parse it. Then it utilizes hostname and port of that url to check if it already exists or not. If the updater exists, then it calls the get_target_filepath() method which returns a target file path to be downloaded. <Arguments> url: URL which TUF is trying to get an updater. Assumption that url is a string. <Exceptions> None <Side Effects> This method logs the messages in a log file if updater is not found or not for the given url. <Returns> The get() method returns the updater with the given configuration. If updater does not exists, it returns None. """ updater = None try: # Parse the given url to access individual parts of it. parsed_url = six.moves.urllib.parse.urlparse(url) hostname = parsed_url.hostname port = parsed_url.port or 80 netloc = parsed_url.netloc # Combine the hostname and port number and assign it to network_location. # The combination of hostname and port is used to identify an updater. network_location = \ "{hostname}:{port}".format(hostname=hostname, port=port) # There can be a case when parsed_url.netloc does not have a port (e.g. # 80). To avoid errors because of this case, tuf.interposition again set # the parameters. network_locations = set((netloc, network_location)) updater = self.__updaters.get(network_location) if updater is None: logger.warning('No updater for ' + repr(hostname)) else: # Ensure that the updater is meant for this (hostname, port). if updater.configuration.network_location in network_locations: logger.info('Found updater for interposed network location: '+ \ repr(network_location)) # Raises an exception in case we do not recognize how to # transform this URL for TUF. In that case, there will be no # updater for this URL. target_filepath = updater.get_target_filepath(url) else: # Same hostname, but different (not user-specified) port. logger.warning('We have an updater for ' + \ repr(updater.configuration.network_location) + \ 'but not for ' + repr(network_locations)) updater = None except: logger.exception('No updater or interposition for ' + repr(url)) updater = None finally: if updater is None: logger.warning('No updater or interposition for ' + repr(url)) return updater