[Python-checkins] CVS: python/dist/src/Lib urllib2.py,1.8,1.9

Moshe Zadka moshez@users.sourceforge.net
Thu, 01 Mar 2001 00:40:44 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv12219/Lib

Modified Files:
	urllib2.py 
Log Message:
Checking in patch 404826 -- urllib2 enhancements and documentations.
(please not that the library reference does *not* include the
urllib2 documnetation -- that will wiat for Fred)


Index: urllib2.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/urllib2.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -r1.8 -r1.9
*** urllib2.py	2001/02/09 10:55:20	1.8
--- urllib2.py	2001/03/01 08:40:42	1.9
***************
*** 58,63 ****
  authinfo.add_password('realm', 'host', 'username', 'password')
  
  # build a new opener that adds authentication and caching FTP handlers
! opener = urllib2.build_opener(authinfo, urllib2.CacheFTPHandler)
  
  # install it
--- 58,65 ----
  authinfo.add_password('realm', 'host', 'username', 'password')
  
+ proxy_support = urllib2.ProxyHandler({"http" : "http://ahad-haam:3128"})
+ 
  # build a new opener that adds authentication and caching FTP handlers
! opener = urllib2.build_opener(proxy_support, authinfo, urllib2.CacheFTPHandler)
  
  # install it
***************
*** 93,97 ****
  import types
  import urlparse
- import os
  import md5
  import mimetypes
--- 95,98 ----
***************
*** 101,104 ****
--- 102,106 ----
  import time
  import gopherlib
+ import posixpath
  
  try:
***************
*** 122,129 ****
  
  # support for FileHandler
! from urllib import localhost, thishost, url2pathname, pathname2url
! 
! # support for GopherHandler
! from urllib import splitgophertype, splitquery
  
  __version__ = "2.0a1"
--- 124,128 ----
  
  # support for FileHandler
! from urllib import localhost, url2pathname
  
  __version__ = "2.0a1"
***************
*** 178,182 ****
--- 177,183 ----
      pass
  
+ 
  class Request:
+ 
      def __init__(self, url, data=None, headers={}):
          # unwrap('<URL:type://host/path>') --> 'type://host/path'
***************
*** 230,242 ****
          return self.__r_host
  
!     def set_proxy(self, proxy):
!         self.__proxy = proxy
!         # XXX this code is based on urllib, but it doesn't seem
!         # correct.  specifically, if the proxy has a port number then
!         # splittype will return the hostname as the type and the port
!         # will be include with everything else
!         self.type, self.__r_type = splittype(self.__proxy)
!         self.host, XXX = splithost(self.__r_type)
!         self.host = unquote(self.host)
          self.__r_host = self.__original
  
--- 231,236 ----
          return self.__r_host
  
!     def set_proxy(self, host, type):
!         self.host, self.type = host, type
          self.__r_host = self.__original
  
***************
*** 330,336 ****
  
      def error(self, proto, *args):
!         if proto == 'http':
!             # XXX http protocol is special cased
!             dict = self.handle_error[proto]
              proto = args[2]  # YUCK!
              meth_name = 'http_error_%d' % proto
--- 324,330 ----
  
      def error(self, proto, *args):
!         if proto in ['http', 'https']:
!             # XXX http[s] protocols are special cased
!             dict = self.handle_error['http'] # https is not different then http
              proto = args[2]  # YUCK!
              meth_name = 'http_error_%d' % proto
***************
*** 398,401 ****
--- 392,397 ----
                         HTTPDefaultErrorHandler, HTTPRedirectHandler,
                         FTPHandler, FileHandler]
+     if hasattr(httplib, 'HTTPS'):
+         default_classes.append(HTTPSHandler)
      skip = []
      for klass in default_classes:
***************
*** 452,456 ****
          new.error_302_dict = {}
          if hasattr(req, 'error_302_dict'):
!             if req.error_302_dict.has_key(newurl):
                  raise HTTPError(req.get_full_url(), code,
                                  self.inf_msg + msg, headers)
--- 448,452 ----
          new.error_302_dict = {}
          if hasattr(req, 'error_302_dict'):
!             if len(error_302_dict)>10 or req.error_302_dict.has_key(newurl):
                  raise HTTPError(req.get_full_url(), code,
                                  self.inf_msg + msg, headers)
***************
*** 478,482 ****
      def proxy_open(self, req, proxy, type):
          orig_type = req.get_type()
!         req.set_proxy(proxy)
          if orig_type == type:
              # let other handlers take care of it
--- 474,485 ----
      def proxy_open(self, req, proxy, type):
          orig_type = req.get_type()
!         type, r_type = splittype(proxy)
!         host, XXX = splithost(r_type)
!         if '@' in host:
!             user_pass, host = host.split('@', 1)
!             user_pass = base64.encode_string(unquote(user_passw)).strip()
!             req.addheader('Proxy-Authorization', user_pass)
!         host = unquote(host)
!         req.set_proxy(host, type)
          if orig_type == type:
              # let other handlers take care of it
***************
*** 570,580 ****
          if base[0] != test[0]:
              return 0
!         common = os.path.commonprefix((base[1], test[1]))
          if len(common) == len(base[1]):
              return 1
          return 0
  
  
- class HTTPBasicAuthHandler(BaseHandler):
      rx = re.compile('[ \t]*([^ \t]+)[ \t]+realm="([^"]*)"')
  
--- 573,593 ----
          if base[0] != test[0]:
              return 0
!         common = posixpath.commonprefix((base[1], test[1]))
          if len(common) == len(base[1]):
              return 1
          return 0
  
+ 
+ class HTTPPasswordMgrWithDefaultRealm(HTTPPasswordMgr):
+ 
+     def find_user_password(self, realm, authuri):
+         user, password = HTTPPasswordMgr.find_user_password(self,realm,authuri)
+         if user is not None:
+             return user, password
+         return HTTPPasswordMgr.find_user_password(self, None, authuri)
+ 
+ 
+ class AbstractBasicAuthHandler:
  
      rx = re.compile('[ \t]*([^ \t]+)[ \t]+realm="([^"]*)"')
  
***************
*** 583,588 ****
      # in parsing them to extract multiple alternatives
  
!     def __init__(self):
!         self.passwd = HTTPPasswordMgr()
          self.add_password = self.passwd.add_password
          self.__current_realm = None
--- 596,603 ----
      # in parsing them to extract multiple alternatives
  
!     def __init__(self, password_mgr=None):
!         if password_mgr is None:
!             password_mgr = HTTPPasswordMgr()
!         self.passwd = password_mgr
          self.add_password = self.passwd.add_password
          self.__current_realm = None
***************
*** 592,606 ****
          # return.
  
!     def http_error_401(self, req, fp, code, msg, headers):
!         # XXX could be mult. headers
!         authreq = headers.get('www-authenticate', None)
          if authreq:
!             mo = HTTPBasicAuthHandler.rx.match(authreq)
              if mo:
                  scheme, realm = mo.groups()
                  if scheme.lower() == 'basic':
!                     return self.retry_http_basic_auth(req, realm)
  
!     def retry_http_basic_auth(self, req, realm):
          if self.__current_realm is None:
              self.__current_realm = realm
--- 607,621 ----
          # return.
  
!     def http_error_auth_reqed(self, authreq, host, req, headers):
!         # XXX could be multiple headers
!         authreq = headers.get(authreq, None)
          if authreq:
!             mo = AbstractBasicAuthHandler.rx.match(authreq)
              if mo:
                  scheme, realm = mo.groups()
                  if scheme.lower() == 'basic':
!                     return self.retry_http_basic_auth(host, req, realm)
  
!     def retry_http_basic_auth(self, host, req, realm):
          if self.__current_realm is None:
              self.__current_realm = realm
***************
*** 608,618 ****
              self.__current_realm = realm
              return None
-         # XXX host isn't really the correct URI?
-         host = req.get_host()
          user,pw = self.passwd.find_user_password(realm, host)
          if pw:
              raw = "%s:%s" % (user, pw)
              auth = base64.encodestring(raw).strip()
!             req.add_header('Authorization', 'Basic %s' % auth)
              resp = self.parent.open(req)
              self.__current_realm = None
--- 623,631 ----
              self.__current_realm = realm
              return None
          user,pw = self.passwd.find_user_password(realm, host)
          if pw:
              raw = "%s:%s" % (user, pw)
              auth = base64.encodestring(raw).strip()
!             req.add_header(self.header, 'Basic %s' % auth)
              resp = self.parent.open(req)
              self.__current_realm = None
***************
*** 622,640 ****
              return None
  
! class HTTPDigestAuthHandler(BaseHandler):
!     """An authentication protocol defined by RFC 2069
  
!     Digest authentication improves on basic authentication because it
!     does not transmit passwords in the clear.
!     """
  
!     def __init__(self):
!         self.passwd = HTTPPasswordMgr()
          self.add_password = self.passwd.add_password
          self.__current_realm = None
  
!     def http_error_401(self, req, fp, code, msg, headers):
!         # XXX could be mult. headers
!         authreq = headers.get('www-authenticate', None)
          if authreq:
              kind = authreq.split()[0]
--- 635,669 ----
              return None
  
! class HTTPBasicAuthHandler(AbstractBasicAuthHandler, BaseHandler):
  
!     header = 'Authorization'
  
!     def http_error_401(self, req, fp, code, msg, headers):
!         host = urlparse.urlparse(req.get_full_url())[1]
!         return self.http_error_auth_reqed('www-authenticate', 
!                                           host, req, headers)
! 
! 
! class ProxyBasicAuthHandler(AbstractBasicAuthHandler, BaseHandler):
! 
!     header = 'Proxy-Authorization'
! 
!     def http_error_407(self, req, fp, code, msg, headers):
!         host = req.get_host()
!         return self.http_error_auth_reqed('proxy-authenticate', 
!                                           host, req, headers)
! 
! 
! class AbstractDigestAuthHandler:
! 
!     def __init__(self, passwd=None):
!         if passwd is None:
!             passwd = HTTPPassowrdMgr()
!         self.passwd = passwd
          self.add_password = self.passwd.add_password
          self.__current_realm = None
  
!     def http_error_auth_reqed(self, authreq, host, req, headers):
!         authreq = headers.get(self.header, None)
          if authreq:
              kind = authreq.split()[0]
***************
*** 647,651 ****
          auth = self.get_authorization(req, chal)
          if auth:
!             req.add_header('Authorization', 'Digest %s' % auth)
              resp = self.parent.open(req)
              self.__current_realm = None
--- 676,680 ----
          auth = self.get_authorization(req, chal)
          if auth:
!             req.add_header(self.header, 'Digest %s' % auth)
              resp = self.parent.open(req)
              self.__current_realm = None
***************
*** 716,719 ****
--- 745,772 ----
          return None
  
+ 
+ class HTTPDigestAuthHandler(BaseHandler, AbstractDigestAuthHandler):
+     """An authentication protocol defined by RFC 2069
+ 
+     Digest authentication improves on basic authentication because it
+     does not transmit passwords in the clear.
+     """
+ 
+     header = 'Authorization'
+ 
+     def http_error_401(self, req, fp, code, msg, headers):
+         host = urlparse.urlparse(req.get_full_url())[1]
+         self.http_error_auth_reqed('www-authenticate', host, req, headers)
+ 
+ 
+ class ProxyDigestAuthHandler(BaseHandler, AbstractDigestAuthHandler):
+ 
+     header = 'Proxy-Authorization'
+ 
+     def http_error_407(self, req, fp, code, msg, headers):
+         host = req.get_host()
+         self.http_error_auth_reqed('proxy-authenticate', host, req, headers)
+ 
+ 
  def encode_digest(digest):
      hexrep = []
***************
*** 726,732 ****
  
  
! class HTTPHandler(BaseHandler):
!     def http_open(self, req):
!         # XXX devise a new mechanism to specify user/password
          host = req.get_host()
          if not host:
--- 779,785 ----
  
  
! class AbstractHTTPHandler(BaseHandler):
! 
!     def do_open(self, http_class, req):
          host = req.get_host()
          if not host:
***************
*** 734,738 ****
  
          try:
!             h = httplib.HTTP(host) # will parse host:port
              if req.has_data():
                  data = req.get_data()
--- 787,791 ----
  
          try:
!             h = http_class(host) # will parse host:port
              if req.has_data():
                  data = req.get_data()
***************
*** 762,765 ****
--- 815,832 ----
          else:
              return self.parent.error('http', req, fp, code, msg, hdrs)
+ 
+ 
+ class HTTPHandler(AbstractHTTPHandler):
+ 
+     def http_open(self, req):
+         return self.do_open(httplib.HTTP, req)
+ 
+ 
+ if hasattr(httplib, 'HTTPS'):
+     class HTTPSHandler(AbstractHTTPHandler):
+ 
+         def https_open(self, req):
+             return self.do_open(httplib.HTTPS, req)
+ 
  
  class UnknownHandler(BaseHandler):