Use the final URL from the finished request instead of the provided URL for filename generation, to properly deal with redirects.

reviewable/pr18780/r1
Tin Tvrtkovic 11 years ago
parent 8036cb0cab
commit b1fa35ac3d

@ -183,11 +183,11 @@ def url_do_get(module, url, dest, use_proxy, last_mod_time):
try: try:
r = urllib2.urlopen(request) r = urllib2.urlopen(request)
info.update(r.info()) info.update(r.info())
info['url'] = r.geturl() # The URL goes in too, because of redirects.
info.update(dict(msg="OK (%s bytes)" % r.headers.get('Content-Length', 'unknown'), status=200)) info.update(dict(msg="OK (%s bytes)" % r.headers.get('Content-Length', 'unknown'), status=200))
except urllib2.HTTPError, e: except urllib2.HTTPError, e:
# Must not fail_json() here so caller can handle HTTP 304 unmodified # Must not fail_json() here so caller can handle HTTP 304 unmodified
info.update(dict(msg=str(e), status=e.code)) info.update(dict(msg=str(e), status=e.code))
return r, info
except urllib2.URLError, e: except urllib2.URLError, e:
code = getattr(e, 'code', -1) code = getattr(e, 'code', -1)
module.fail_json(msg="Request failed: %s" % str(e), status_code=code) module.fail_json(msg="Request failed: %s" % str(e), status_code=code)
@ -287,11 +287,14 @@ def main():
# Now the request has completed, we can finally generate the final # Now the request has completed, we can finally generate the final
# destination file name from the info dict. # destination file name from the info dict.
if dest_is_dir: if dest_is_dir:
filename = extract_filename_from_headers(info) filename = extract_filename_from_headers(info)
if not filename: if not filename:
# Fall back to extracting the filename from the URL. # Fall back to extracting the filename from the URL.
filename = url_filename(url) # Pluck the URL from the info, since a redirect could have changed
# it.
filename = url_filename(info['url'])
dest = os.path.join(dest, filename) dest = os.path.join(dest, filename)
md5sum_src = None md5sum_src = None

Loading…
Cancel
Save