Merged changes from master
[zeroinstall/solver.git] / zeroinstall / injector / _download_child.py
blobdc2befa9f9b942f7c52ae00b2350b09cfead16c3
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
8 from zeroinstall.support import ssl_match_hostname
10 if sys.version_info[0] > 2:
11 from urllib import request as urllib2
12 from http.client import HTTPSConnection, HTTPException
13 else:
14 import urllib2
15 from httplib import HTTPSConnection, HTTPException
17 _fallback_ca_bundle = [
18 os.path.join(os.path.dirname(__file__), "cacert.pem"),
19 os.path.join(os.path.dirname(sys.executable), "cacert.pem"),
22 # Note: on MacOS X at least, it will also look in the system keychain provided that you supply *some* CAs.
23 # (if you don't specify any trusted CAs, Python trusts everything!)
24 # So, the "fallback" option doesn't necessarily mean that other sites won't work.
25 for ca_bundle in [
26 "/etc/ssl/certs/ca-certificates.crt", # Debian/Ubuntu/Arch Linux
27 "/etc/pki/tls/certs/ca-bundle.crt", # Fedora/RHEL
28 "/etc/ssl/ca-bundle.pem", # openSUSE/SLE (claimed)
29 "/var/lib/ca-certificates/ca-bundle.pem.new", # openSUSE (actual)
30 ] + _fallback_ca_bundle:
31 if os.path.exists(ca_bundle):
32 class ValidatingHTTPSConnection(HTTPSConnection):
33 def connect(self):
34 sock = socket.create_connection((self.host, self.port), self.timeout)
35 if hasattr(self, '_tunnel_host') and self._tunnel_host:
36 self.sock = sock
37 self._tunnel()
38 sock = ssl.wrap_socket(sock, cert_reqs = ssl.CERT_REQUIRED, ca_certs = ca_bundle)
39 ssl_match_hostname.match_hostname(sock.getpeercert(), self.host)
40 self.sock = sock
42 class ValidatingHTTPSHandler(urllib2.HTTPSHandler):
43 def https_open(self, req):
44 return self.do_open(self.getConnection, req)
46 def getConnection(self, host, timeout=300):
47 return ValidatingHTTPSConnection(host)
48 MyHTTPSHandler = ValidatingHTTPSHandler
49 break
50 else:
51 raise Exception("No root CA's found (not even the built-in one!); security of HTTPS connections cannot be verified")
53 class Redirect(Exception):
54 def __init__(self, req):
55 Exception.__init__(self, "Redirect")
56 self.req = req
58 class MyRedirectHandler(urllib2.HTTPRedirectHandler):
59 """Throw an exception on redirects instead of continuing. The redirect will be handled in the main thread
60 so it can work with connection pooling."""
61 def redirect_request(self, req, fp, code, msg, headers, newurl):
62 new_req = urllib2.HTTPRedirectHandler.redirect_request(self, req, fp, code, msg, headers, newurl)
63 if new_req:
64 raise Redirect(new_req)
66 # Our handler differs from the Python default in that:
67 # - we don't support file:// URLs
68 # - we don't follow HTTP redirects
69 _my_urlopen = urllib2.OpenerDirector()
70 for klass in [urllib2.ProxyHandler, urllib2.UnknownHandler, urllib2.HTTPHandler,
71 urllib2.HTTPDefaultErrorHandler, MyRedirectHandler,
72 urllib2.FTPHandler, urllib2.HTTPErrorProcessor, MyHTTPSHandler]:
73 _my_urlopen.add_handler(klass())
75 def download_in_thread(url, target_file, if_modified_since, notify_done):
76 try:
77 #print "Child downloading", url
78 if url.startswith('http:') or url.startswith('https:') or url.startswith('ftp:'):
79 req = urllib2.Request(url)
80 if url.startswith('http:') and if_modified_since:
81 req.add_header('If-Modified-Since', if_modified_since)
82 src = _my_urlopen.open(req)
83 else:
84 raise Exception(_('Unsupported URL protocol in: %s') % url)
86 if sys.version_info[0] > 2:
87 sock_recv = src.fp.read1 # Python 3
88 else:
89 try:
90 sock_recv = src.fp._sock.recv # Python 2
91 except AttributeError:
92 sock_recv = src.fp.fp._sock.recv # Python 2.5 on FreeBSD
93 while True:
94 data = sock_recv(256)
95 if not data: break
96 target_file.write(data)
97 target_file.flush()
99 notify_done(download.RESULT_OK)
100 except (urllib2.HTTPError, urllib2.URLError, HTTPException, socket.error) as ex:
101 if isinstance(ex, urllib2.HTTPError) and ex.code == 304: # Not modified
102 notify_done(download.RESULT_NOT_MODIFIED)
103 else:
104 #print >>sys.stderr, "Error downloading '" + url + "': " + (str(ex) or str(ex.__class__.__name__))
105 __, ex, tb = sys.exc_info()
106 notify_done(download.RESULT_FAILED, (download.DownloadError(_('Error downloading {url}: {ex}').format(url = url, ex = ex)), tb))
107 except Redirect as ex:
108 notify_done(download.RESULT_REDIRECT, redirect = ex.req.get_full_url())
109 except Exception as ex:
110 __, ex, tb = sys.exc_info()
111 notify_done(download.RESULT_FAILED, (ex, tb))