def GetCookies(host, path, cookie_paths=None): """Returns cookies that should be set on a request. Used by CreateHttpConn for any requests that do not already specify a Cookie header. All requests made by this library are HTTPS. Args: host: The hostname of the Gerrit service. path: The path on the Gerrit service, already including /a/ if applicable. cookie_paths: Files to look in for cookies. Defaults to looking in the standard places where GoB places cookies. Returns: A dict of cookie name to value, with no URL encoding applied. """ cookies = {} if cookie_paths is None: cookie_paths = (constants.GOB_COOKIE_PATH, constants.GITCOOKIES_PATH) for cookie_path in cookie_paths: if os.path.isfile(cookie_path): with open(cookie_path) as f: for line in f: fields = line.strip().split('\t') if line.strip().startswith('#') or len(fields) != 7: continue domain, xpath, key, value = fields[0], fields[2], fields[5], fields[6] if cookielib.domain_match(host, domain) and path.startswith(xpath): cookies[key] = value return cookies
def domain_match(self, a, b): if http_cookiejar.domain_match(a, b): return True elif http_cookiejar.domain_match(a, b.strip(".")): return True return False