[Python-checkins] CVS: python/dist/src/Lib urllib.py,1.88,1.89

Guido van Rossum guido@cnri.reston.va.us
Tue, 7 Dec 1999 16:37:20 -0500 (EST)


Update of /projects/cvsroot/python/dist/src/Lib
In directory eric:/projects/python/develop/guido/src/Lib

Modified Files:
	urllib.py 
Log Message:
OpenSSL support.  This is based on patches for a version of SSLeay by
Brian E Gallew, which were improved and adapted to OpenSSL 0.9.4 by
Laszlo Kovacs of HP.  Both have kindly given permission to include
the patches in the Python distribution.  Final formatting by GvR.


Index: urllib.py
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Lib/urllib.py,v
retrieving revision 1.88
retrieving revision 1.89
diff -C2 -r1.88 -r1.89
*** urllib.py	1999/08/18 17:40:33	1.88
--- urllib.py	1999/12/07 21:37:17	1.89
***************
*** 28,32 ****
  
  
! __version__ = '1.11'    # XXX This version is not always updated :-(
  
  MAXFTPCACHE = 10        # Trim the ftp cache beyond this size
--- 28,32 ----
  
  
! __version__ = '1.12'    # XXX This version is not always updated :-(
  
  MAXFTPCACHE = 10        # Trim the ftp cache beyond this size
***************
*** 82,90 ****
  
      # Constructor
!     def __init__(self, proxies=None):
          if proxies is None:
              proxies = getproxies()
          assert hasattr(proxies, 'has_key'), "proxies must be a mapping"
          self.proxies = proxies
          server_version = "Python-urllib/%s" % __version__
          self.addheaders = [('User-agent', server_version)]
--- 82,92 ----
  
      # Constructor
!     def __init__(self, proxies=None, **x509):
          if proxies is None:
              proxies = getproxies()
          assert hasattr(proxies, 'has_key'), "proxies must be a mapping"
          self.proxies = proxies
+         self.key_file = x509.get('key_file')
+         self.cert_file = x509.get('cert_file')
          server_version = "Python-urllib/%s" % __version__
          self.addheaders = [('User-agent', server_version)]
***************
*** 145,148 ****
--- 147,151 ----
              url = (host, fullurl) # Signal special case to open_*()
          name = 'open_' + type
+         self.type = type
          if '-' in name:
              # replace - with _
***************
*** 295,298 ****
--- 298,337 ----
          raise IOError, ('http error', errcode, errmsg, headers)
  
+     # Use HTTPS protocol
+     if hasattr(socket, "ssl"):
+         def open_https(self, url):
+             import httplib
+             if type(url) is type(""):
+                 host, selector = splithost(url)
+                 user_passwd, host = splituser(host)
+             else:
+                 host, selector = url
+                 urltype, rest = splittype(selector)
+                 if string.lower(urltype) == 'https':
+                     realhost, rest = splithost(rest)
+                     user_passwd, realhost = splituser(realhost)
+                     if user_passwd:
+                         selector = "%s://%s%s" % (urltype, realhost, rest)
+                 print "proxy via https:", host, selector
+             if not host: raise IOError, ('https error', 'no host given')
+             if user_passwd:
+                 import base64
+                 auth = string.strip(base64.encodestring(user_passwd))
+             else:
+                 auth = None
+             h = httplib.HTTPS(host, 0,
+                               key_file=self.key_file,
+                               cert_file=self.cert_file)
+             h.putrequest('GET', selector)
+             if auth: h.putheader('Authorization: Basic %s' % auth)
+             for args in self.addheaders: apply(h.putheader, args)
+             h.endheaders()
+             errcode, errmsg, headers = h.getreply()
+             fp = h.getfile()
+             if errcode == 200:
+                 return addinfourl(fp, headers, url)
+             else:
+                 return self.http_error(url, fp, errcode, errmsg, headers)
+   
      # Use Gopher protocol
      def open_gopher(self, url):
***************
*** 478,482 ****
                  scheme, realm = match.groups()
                  if string.lower(scheme) == 'basic':
!                     return self.retry_http_basic_auth(url, realm, data)
  
      def retry_http_basic_auth(self, url, realm, data):
--- 517,522 ----
                  scheme, realm = match.groups()
                  if string.lower(scheme) == 'basic':
!                    name = 'retry_' + self.type + '_basic_auth'
!                    return getattr(self,name)(url, realm)
  
      def retry_http_basic_auth(self, url, realm, data):
***************
*** 489,492 ****
--- 529,542 ----
          newurl = 'http://' + host + selector
          return self.open(newurl, data)
+    
+     def retry_https_basic_auth(self, url, realm):
+             host, selector = splithost(url)
+             i = string.find(host, '@') + 1
+             host = host[i:]
+             user, passwd = self.get_user_passwd(host, realm, i)
+             if not (user or passwd): return None
+             host = user + ':' + passwd + '@' + host
+             newurl = '//' + host + selector
+             return self.open_https(newurl)
  
      def get_user_passwd(self, host, realm, clear_cache = 0):
***************
*** 631,636 ****
          self.read = self.fp.read
          self.readline = self.fp.readline
!         self.readlines = self.fp.readlines
!         self.fileno = self.fp.fileno
      def __repr__(self):
          return '<%s at %s whose fp = %s>' % (self.__class__.__name__,
--- 681,686 ----
          self.read = self.fp.read
          self.readline = self.fp.readline
!         if hasattr(self.fp, "readlines"): self.readlines = self.fp.readlines
!         if hasattr(self.fp, "fileno"): self.fileno = self.fp.fileno
      def __repr__(self):
          return '<%s at %s whose fp = %s>' % (self.__class__.__name__,
***************
*** 1016,1019 ****
--- 1066,1071 ----
              'http://www.python.org/index.html',
              ]
+         if hasattr(URLopener, "open_https"):
+             args.append('https://synergy.as.cmu.edu/~geek/')
      try:
          for url in args: