Handle HTTP redirects manually
[zeroinstall.git] / zeroinstall / injector / _download_child.py
blobfa8c927e82cfc85c114170364df168535a9c2378
1 # Copyright (C) 2011, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
4 import sys, os, socket, ssl
6 from zeroinstall import _
7 from zeroinstall.injector import download
9 import urllib2, httplib
11 for ca_bundle in ["/etc/ssl/certs/ca-certificates.crt", # Debian/Ubuntu/Arch Linux
12 "/etc/pki/tls/certs/ca-bundle.crt", # Fedora/RHEL
13 "/etc/ssl/ca-bundle.pem", # openSUSE/SLE (claimed)
14 "/var/lib/ca-certificates/ca-bundle.pem.new"]: # openSUSE (actual)
15 if os.path.exists(ca_bundle):
16 class ValidatingHTTPSConnection(httplib.HTTPSConnection):
17 def connect(self):
18 sock = socket.create_connection((self.host, self.port), self.timeout)
19 if self._tunnel_host:
20 self.sock = sock
21 self._tunnel()
22 self.sock = ssl.wrap_socket(sock, cert_reqs = ssl.CERT_REQUIRED, ca_certs = ca_bundle)
24 class ValidatingHTTPSHandler(urllib2.HTTPSHandler):
25 def https_open(self, req):
26 return self.do_open(self.getConnection, req)
28 def getConnection(self, host, timeout=300):
29 return ValidatingHTTPSConnection(host)
30 MyHTTPSHandler = ValidatingHTTPSHandler
31 break
32 else:
33 from logging import warn
34 warn("No root CA's found; security of HTTPS connections cannot be verified")
35 MyHTTPSHandler = urllib2.HTTPSHandler
37 class Redirect(Exception):
38 def __init__(self, req):
39 Exception.__init__(self, "Redirect")
40 self.req = req
42 class MyRedirectHandler(urllib2.HTTPRedirectHandler):
43 """Throw an exception on redirects instead of continuing. The redirect will be handled in the main thread
44 so it can work with connection pooling."""
45 def redirect_request(self, req, fp, code, msg, headers, newurl):
46 new_req = urllib2.HTTPRedirectHandler.redirect_request(self, req, fp, code, msg, headers, newurl)
47 if new_req:
48 raise Redirect(new_req)
50 # Our handler differs from the Python default in that:
51 # - we don't support file:// URLs
52 # - we don't follow HTTP redirects
53 _my_urlopen = urllib2.OpenerDirector()
54 for klass in [urllib2.ProxyHandler, urllib2.UnknownHandler, urllib2.HTTPHandler,
55 urllib2.HTTPDefaultErrorHandler, MyRedirectHandler,
56 urllib2.FTPHandler, urllib2.HTTPErrorProcessor, MyHTTPSHandler]:
57 _my_urlopen.add_handler(klass())
59 def download_in_thread(url, target_file, if_modified_since, notify_done):
60 try:
61 #print "Child downloading", url
62 if url.startswith('http:') or url.startswith('https:') or url.startswith('ftp:'):
63 req = urllib2.Request(url)
64 if url.startswith('http:') and if_modified_since:
65 req.add_header('If-Modified-Since', if_modified_since)
66 src = _my_urlopen.open(req)
67 else:
68 raise Exception(_('Unsupported URL protocol in: %s') % url)
70 try:
71 sock = src.fp._sock
72 except AttributeError:
73 sock = src.fp.fp._sock # Python 2.5 on FreeBSD
74 while True:
75 data = sock.recv(256)
76 if not data: break
77 target_file.write(data)
78 target_file.flush()
80 notify_done(download.RESULT_OK)
81 except (urllib2.HTTPError, urllib2.URLError, httplib.HTTPException, socket.error) as ex:
82 if isinstance(ex, urllib2.HTTPError) and ex.code == 304: # Not modified
83 notify_done(download.RESULT_NOT_MODIFIED)
84 else:
85 #print >>sys.stderr, "Error downloading '" + url + "': " + (str(ex) or str(ex.__class__.__name__))
86 __, ex, tb = sys.exc_info()
87 notify_done(download.RESULT_FAILED, (download.DownloadError(_('Error downloading {url}: {ex}').format(url = url, ex = ex)), tb))
88 except Redirect as ex:
89 notify_done(download.RESULT_REDIRECT, redirect = ex.req.get_full_url())
90 except Exception as ex:
91 __, ex, tb = sys.exc_info()
92 notify_done(download.RESULT_FAILED, (ex, tb))