def _follow_redirects(uri_list, http_timeout):
""" Follow HTTP redirects from servers. Needed so that we can create
RewriteRules for all repository URLs that pkg clients may encounter.
We return a sorted list of URIs that were found having followed all
redirects in 'uri_list'. We also return a boolean, True if we timed out
when following any of the URIs.
"""
ret_uris = set(uri_list)
timed_out = False
class SysrepoRedirectHandler(HTTPRedirectHandler):
""" A HTTPRedirectHandler that saves URIs we've been
redirected to along the path to our eventual destination."""
def __init__(self):
self.redirects = set()
def redirect_request(self, req, fp, code, msg, hdrs, newurl):
self.redirects.add(newurl)
return HTTPRedirectHandler.redirect_request(
self, req, fp, code, msg, hdrs, newurl)
for uri in uri_list:
handler = SysrepoRedirectHandler()
opener = build_opener(handler)
if not uri.startswith("http:"):
ret_uris.update([uri])
continue
# otherwise, open a known url to check for redirects
try:
opener.open("{0}/versions/0".format(uri), None,
http_timeout)
ret_uris.update(set(
[item.replace("/versions/0", "").rstrip("/")
for item in handler.redirects]))
except URLError as err:
# We need to log this, and carry on - the url
# could become available at a later date.
msg(_("WARNING: unable to access {uri} when checking "
"for redirects: {err}").format(**locals()))
timed_out = True
return sorted(list(ret_uris)), timed_out
评论列表
文章目录