def _fileFeatures(filePath): """ From a filepath, construct a dictionary of file features. """ # pylint: disable=W0702 log_debug(3, filePath) if not filePath: raise rhnFault(17, "While looking for file: `%s'" % os.path.basename(filePath)) try: s = os.stat(filePath) except: s = None if not s: l = 0 lastModified = 0 else: l = s[stat.ST_SIZE] lastModified = s[stat.ST_MTIME] del s # Build the result hash result = {} result['name'] = os.path.basename(filePath) result['length'] = l result['path'] = filePath if lastModified: result['lastModified'] = rfc822time(lastModified) else: result['lastModified'] = None return result
def getPackage(self, pkgFilename, *args): """ Get rpm package. """ log_debug(3, pkgFilename) if args: pkg_spec = [pkgFilename] + list(args) else: pkg_spec = pkgFilename redirectsSupported = 0 # If we are talking to a proxy, determine whether it's a version that # supports redirects. proxyVersionString = rhnFlags.get('x-rhn-proxy-version') if proxyVersionString: redirectsSupported = 1 else: # Must be a client. We'll determine the redirect capability via # the x-rhn-transport-capability header instead. transport_cap = rhnFlags.get('x-rhn-transport-capability') if transport_cap: transport_cap_list = transport_cap.split('=') redirectsSupported = transport_cap_list[ 0] == 'follow-redirects' and transport_cap_list[1] >= 2 if redirectsSupported: log_debug(3, "Client supports redirects.") filePath = self.getPackagePath(pkg_spec, 1) else: #older clients just return the hosted url and download the package filePath = self.getPackagePath(pkg_spec) return self._getFile(filePath)
def getPackage(self, pkgFilename, *args): """ Get rpm package. """ log_debug(3, pkgFilename) if args: pkg_spec = [pkgFilename] + list(args) else: pkg_spec = pkgFilename redirectsSupported = 0 # If we are talking to a proxy, determine whether it's a version that # supports redirects. proxyVersionString = rhnFlags.get('x-rhn-proxy-version') if proxyVersionString: redirectsSupported = 1 else: # Must be a client. We'll determine the redirect capability via # the x-rhn-transport-capability header instead. transport_cap = rhnFlags.get('x-rhn-transport-capability') if transport_cap : transport_cap_list = transport_cap.split('=') redirectsSupported = transport_cap_list[0] == 'follow-redirects' and transport_cap_list[1] >= 2 if redirectsSupported: log_debug(3, "Client supports redirects.") filePath = self.getPackagePath(pkg_spec, 1) else: #older clients just return the hosted url and download the package filePath = self.getPackagePath(pkg_spec) return self._getFile(filePath)
def _fileFeatures(filePath): """ From a filepath, construct a dictionary of file features. """ # pylint: disable=W0702 log_debug(3, filePath) if not filePath: raise rhnFault( 17, "While looking for file: `%s'" % os.path.basename(filePath)) try: s = os.stat(filePath) except: s = None if not s: l = 0 lastModified = 0 else: l = s[stat.ST_SIZE] lastModified = s[stat.ST_MTIME] del s # Build the result hash result = {} result['name'] = os.path.basename(filePath) result['length'] = l result['path'] = filePath if lastModified: result['lastModified'] = rfc822time(lastModified) else: result['lastModified'] = None return result
def timer(last): """ a lame timer function for pretty logs """ if not last: return 0 log_debug(2, "Request served in %.2f sec" % (time.time() - last, )) return 0
def cleanupHandler(self, req): """ clean up this session """ log_debug(3) self.lang = "C" self.clientVersion = self.proxyVersion = 0 # clear the global flags rhnFlags.reset() timer(self.start_time) return self._cleanup_request_processor()
def getPackageSource(self, pkgFilename): """ Get srpm packrge. """ log_debug(3, pkgFilename) # Sanity check: l = pkgFilename.split('.') #6/23/05 wregglej 154248, Don't mangle the filename if it's a nosrc package. if l[-2] != "nosrc": l[-2] = 'src' pkgFilename = '.'.join(l) filePath = self.getSourcePackagePath(pkgFilename) return self._getFile(filePath)
def __init__(self, channelName = None): log_debug(2, channelName) RPC_Base.__init__(self) self.channelName = channelName # Default visible functions. self.compress_headers = 1 self.functions = [ 'getPackage', 'getPackageHeader', 'getPackageSource' ]
def cleanupHandler(self, _req): """ clean up this session """ log_debug(3) self.lang = "C" self.clientVersion = self.proxyVersion = 0 # clear the global flags rhnFlags.reset() timer(self.start_time) return self._cleanup_request_processor()
def __init__(self, channelName=None): log_debug(2, channelName) RPC_Base.__init__(self) self.channelName = channelName # Default visible functions. self.compress_headers = 1 self.functions = [ 'getPackage', 'getPackageHeader', 'getPackageSource', 'i18n', ]
def _getFile(self, filePath): """ Returns xmlrpclib file object to any file given a path to it. IN: filePath: path to any file. OUT: XMLed rpm or source rpm, or an xmlrpc file object. """ log_debug(3, filePath) features = self._fileFeatures(filePath) filePath = features['path'] length = features['length'] lastModified = features['lastModified'] self._set_last_modified(lastModified) return rpclib.transports.File(open(filePath, "rb"), length, name=filePath)
def setlang(self, lang, domain): """ An entry point for setting the language for the current sesstion """ self.lang = lang self.domain = domain cat.set(domain=domain) # If the language presented by the client does not exist, the # translation object falls back to printing the original string, which # is pretty much the same as translating to en cat.setlangs(self.lang) log_debug(3, self.lang, self.domain)
def _set_last_modified(self, last_modified, extra_headers={}): log_debug(4, last_modified) if not last_modified: return None # Set a field with the name of the header transport = rhnFlags.get('outputTransportOptions') if last_modified: # Put the last-modified info too if type(last_modified) in (types.IntType, types.FloatType): last_modified = rfc822time(last_modified) transport['Last-Modified'] = last_modified for k, v in extra_headers.items(): transport[str(k)] = str(v) return transport
def headerParserHandler(self, req): """ after a request has been received, first thing we do is to create the input object """ # pylint: disable=R0911 log_setreq(req) log_debug(3) self.start_time = time.time() # Decline if this is a subrequest: if req.main: return apache.DECLINED log_debug(4, req.method, req.path_info, req.headers_in) # Clear the global flags. rhnFlags.reset() # Init the transport options. rhnFlags.set('outputTransportOptions', UserDictCase()) # Init the session token dictionary. rhnFlags.set("AUTH_SESSION_TOKEN", UserDictCase()) ret = self._init_request_processor(req) if ret != apache.OK: return ret ret = self._set_client_info(req) if ret != apache.OK: return ret # Check the protocol version if req.proto_num < 1001: # HTTP protocols prior to 1.1 close the connection rhnFlags.get('outputTransportOptions')["Connection"] = "close" ret = self._set_proxy_info(req) if ret != apache.OK: return ret # Need to run _set_other first, since _set_lang needs RoodDir set ret = self._set_other(req) if ret != apache.OK: return ret ret = self._set_lang(req) if ret != apache.OK: return ret return apache.OK
def _set_last_modified(last_modified, extra_headers=None): log_debug(4, last_modified) if not last_modified: return None # Set a field with the name of the header transport = rhnFlags.get('outputTransportOptions') if last_modified: # Put the last-modified info too if type(last_modified) in (types.IntType, types.FloatType): last_modified = rfc822time(last_modified) transport['Last-Modified'] = last_modified if extra_headers: for k, v in extra_headers.items(): transport[str(k)] = str(v) return transport
def handler(self, req): """ a handler - not doing much for the common case, but called from classes that inherit this one. """ log_debug(3) # Set the lang in the output headers if self.lang != "C": req.headers_out["Content-Language"] = self.getlang() log_debug(4, "URI", req.unparsed_uri) log_debug(4, "CONFIG", req.get_config()) log_debug(4, "OPTIONS", req.get_options()) log_debug(4, "HEADERS", req.headers_in) return apache.OK
def getPackageHeader(self, pkgFilename): """ Get rpm header. XXX: stock 8.0 clients could not compress headers, we need to either change the function name, or version the protocol """ log_debug(3, pkgFilename) pkg = pkgFilename.split('.') # Basic sanity checks: if pkg[-1] not in ["hdr", 'rpm']: raise rhnFault(21, "'%s' not a valid RPM header name" % pkgFilename) pkgFilename = ".".join(pkg[:-1]) + '.rpm' filePath = self.getPackagePath(pkgFilename) data = self._getHeaderFromFile(filePath) # XXX: Interesting. Found that if returned just data, this # function works fine. Investigate later. return rpclib.transports.File(cStringIO.StringIO(data), len(data))
def _setSessionToken(headers): """ Pushes token into rhnFlags. If doesn't exist, returns None. Pull session token out of the headers and into rhnFlags. """ log_debug(3) token = UserDictCase() if headers.has_key('X-RHN-Server-Id'): token['X-RHN-Server-Id'] = headers['X-RHN-Server-Id'] else: # This has to be here, or else we blow-up. return None prefix = "x-rhn-auth" tokenKeys = [ x for x in headers.keys() if x[:len(prefix)].lower() == prefix] for k in tokenKeys: token[k] = headers[k] rhnFlags.set("AUTH_SESSION_TOKEN", token) return token
def _setSessionToken(headers): """ Pushes token into rhnFlags. If doesn't exist, returns None. Pull session token out of the headers and into rhnFlags. """ log_debug(3) token = UserDictCase() if headers.has_key('X-RHN-Server-Id'): token['X-RHN-Server-Id'] = headers['X-RHN-Server-Id'] else: # This has to be here, or else we blow-up. return None prefix = "x-rhn-auth" tokenKeys = [x for x in headers.keys() if x[:len(prefix)].lower() == prefix] for k in tokenKeys: token[k] = headers[k] rhnFlags.set("AUTH_SESSION_TOKEN", token) return token
def _getHeaderFromFile(self, filePath, stat_info=None): """ Utility function to extract a header from an rpm. If stat_info was already passed, don't re-stat the file """ log_debug(3, filePath) if stat_info: s = stat_info else: s = None try: s = os.stat(filePath) except: raise rhnFault( 17, "Unable to read package %s" % os.path.basename(filePath)), None, sys.exc_info()[2] lastModified = s[stat.ST_MTIME] del s # XXX: not neccessary? # Get the package header from the file # since we stat()ed the file, we know it's there already fd = os.open(filePath, os.O_RDONLY) h = rhn_rpm.get_package_header(fd=fd) os.close(fd) if h is None: raise rhnFault(17, "Invalid RPM %s" % os.path.basename(filePath)) stringIO = cStringIO.StringIO() # Put the result in stringIO stringIO.write(h.unload()) del h # XXX: not neccessary? pkgFilename = os.path.basename(filePath) pkg = pkgFilename.split('.') # Replace .rpm with .hdr pkg[-1] = "hdr" pkgFilename = ".".join(pkg) extra_headers = { 'X-RHN-Package-Header': pkgFilename, } self._set_last_modified(lastModified, extra_headers=extra_headers) rhnFlags.set("AlreadyEncoded", 1) return stringIO.getvalue()
def _getHeaderFromFile(self, filePath, stat_info=None): """ Utility function to extract a header from an rpm. If stat_info was already passed, don't re-stat the file """ log_debug(3, filePath) if stat_info: s = stat_info else: s = None try: s = os.stat(filePath) except: raise rhnFault(17, "Unable to read package %s" % os.path.basename(filePath)), None, sys.exc_info()[2] lastModified = s[stat.ST_MTIME] del s # XXX: not neccessary? # Get the package header from the file # since we stat()ed the file, we know it's there already fd = os.open(filePath, os.O_RDONLY) h = rhn_rpm.get_package_header(fd=fd) os.close(fd) if h is None: raise rhnFault(17, "Invalid RPM %s" % os.path.basename(filePath)) stringIO = cStringIO.StringIO() # Put the result in stringIO stringIO.write(h.unload()) del h # XXX: not neccessary? pkgFilename = os.path.basename(filePath) pkg = pkgFilename.split('.') # Replace .rpm with .hdr pkg[-1] = "hdr" pkgFilename = ".".join(pkg) extra_headers = { 'X-RHN-Package-Header' : pkgFilename, } self._set_last_modified(lastModified, extra_headers=extra_headers) rhnFlags.set("AlreadyEncoded", 1) return stringIO.getvalue()
def getPackage(self, pkgFilename, *args): """ Get rpm package. """ log_debug(3, pkgFilename) if args: pkg_spec = [pkgFilename] + list(args) else: pkg_spec = pkgFilename redirectsSupported = 0 # If we are talking to a proxy, determine whether it's a version that # supports redirects. proxyVersionString = rhnFlags.get('x-rhn-proxy-version') if proxyVersionString: # Convert the version string to a number format that we can compare. versionParts = proxyVersionString.split('.') proxyVersionStringBaked = \ "%s.%s" % (versionParts[0], string.join(versionParts[1:], '')) # Check the proxy version. To maintain backward compatibility, we # won't redirect to proxies < v4.1.0. log_debug(3, "Detected proxy version " + proxyVersionStringBaked) if float(proxyVersionStringBaked) >= 4.1: redirectsSupported = 1 else: # Must be a client. We'll determine the redirect capability via # the x-rhn-transport-capability header instead. transport_cap = rhnFlags.get('x-rhn-transport-capability') if transport_cap : transport_cap_list = transport_cap.split('=') redirectsSupported = transport_cap_list[0] == 'follow-redirects' and transport_cap_list[1] >= 2 if redirectsSupported: log_debug(3, "Client supports redirects.") filePath = self.getPackagePath(pkg_spec, 1) else: #older clients just return the hosted url and download the package filePath = self.getPackagePath(pkg_spec) return self._getFile(filePath)
def logHandler(self, req): """ A dummy log function """ log_debug(3) return apache.OK
def logHandler(_req): """ A dummy log function """ log_debug(3) return apache.OK