mercurial/url.py
changeset 20174 af02783dea65
parent 20172 181108726ea5
child 20291 7d589d923b8a
equal deleted inserted replaced
20173:40f79b9a2cc8 20174:af02783dea65
     6 #
     6 #
     7 # This software may be used and distributed according to the terms of the
     7 # This software may be used and distributed according to the terms of the
     8 # GNU General Public License version 2 or any later version.
     8 # GNU General Public License version 2 or any later version.
     9 
     9 
    10 import urllib, urllib2, httplib, os, socket, cStringIO
    10 import urllib, urllib2, httplib, os, socket, cStringIO
    11 import base64
       
    12 from i18n import _
    11 from i18n import _
    13 import keepalive, util, sslutil
    12 import keepalive, util, sslutil
    14 import httpconnection as httpconnectionmod
    13 import httpconnection as httpconnectionmod
    15 
    14 
    16 class passwordmgr(urllib2.HTTPPasswordMgrWithDefaultRealm):
    15 class passwordmgr(urllib2.HTTPPasswordMgrWithDefaultRealm):
   417                 return
   416                 return
   418             raise
   417             raise
   419 
   418 
   420 class httpbasicauthhandler(urllib2.HTTPBasicAuthHandler):
   419 class httpbasicauthhandler(urllib2.HTTPBasicAuthHandler):
   421     def __init__(self, *args, **kwargs):
   420     def __init__(self, *args, **kwargs):
   422         self.auth = None
       
   423         urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs)
   421         urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs)
   424         self.retried_req = None
   422         self.retried_req = None
   425 
       
   426     def http_request(self, request):
       
   427         if self.auth:
       
   428             request.add_unredirected_header(self.auth_header, self.auth)
       
   429 
       
   430         return request
       
   431 
       
   432     def https_request(self, request):
       
   433         if self.auth:
       
   434             request.add_unredirected_header(self.auth_header, self.auth)
       
   435 
       
   436         return request
       
   437 
   423 
   438     def reset_retry_count(self):
   424     def reset_retry_count(self):
   439         # Python 2.6.5 will call this on 401 or 407 errors and thus loop
   425         # Python 2.6.5 will call this on 401 or 407 errors and thus loop
   440         # forever. We disable reset_retry_count completely and reset in
   426         # forever. We disable reset_retry_count completely and reset in
   441         # http_error_auth_reqed instead.
   427         # http_error_auth_reqed instead.
   447             self.retried_req = req
   433             self.retried_req = req
   448             self.retried = 0
   434             self.retried = 0
   449         return urllib2.HTTPBasicAuthHandler.http_error_auth_reqed(
   435         return urllib2.HTTPBasicAuthHandler.http_error_auth_reqed(
   450                         self, auth_header, host, req, headers)
   436                         self, auth_header, host, req, headers)
   451 
   437 
   452     def retry_http_basic_auth(self, host, req, realm):
       
   453         user, pw = self.passwd.find_user_password(realm, host)
       
   454         if pw is not None:
       
   455             raw = "%s:%s" % (user, pw)
       
   456             auth = 'Basic %s' % base64.b64encode(raw).strip()
       
   457             if req.headers.get(self.auth_header, None) == auth:
       
   458                 return None
       
   459             self.auth = auth
       
   460             req.add_unredirected_header(self.auth_header, auth)
       
   461             return self.parent.open(req, timeout=req.timeout)
       
   462         else:
       
   463             return None
       
   464 
       
   465 handlerfuncs = []
   438 handlerfuncs = []
   466 
   439 
   467 def opener(ui, authinfo=None):
   440 def opener(ui, authinfo=None):
   468     '''
   441     '''
   469     construct an opener suitable for urllib2
   442     construct an opener suitable for urllib2