mercurial/url.py
changeset 20964 a939eeb94833
parent 20291 7d589d923b8a
child 21543 21b3513d43e4
equal deleted inserted replaced
20963:ffddabb8aa5d 20964:a939eeb94833
     5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
     5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
     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, base64
    11 from i18n import _
    11 from i18n import _
    12 import keepalive, util, sslutil
    12 import keepalive, util, sslutil
    13 import httpconnection as httpconnectionmod
    13 import httpconnection as httpconnectionmod
    14 
    14 
    15 class passwordmgr(urllib2.HTTPPasswordMgrWithDefaultRealm):
    15 class passwordmgr(urllib2.HTTPPasswordMgrWithDefaultRealm):
   420                 return
   420                 return
   421             raise
   421             raise
   422 
   422 
   423 class httpbasicauthhandler(urllib2.HTTPBasicAuthHandler):
   423 class httpbasicauthhandler(urllib2.HTTPBasicAuthHandler):
   424     def __init__(self, *args, **kwargs):
   424     def __init__(self, *args, **kwargs):
       
   425         self.auth = None
   425         urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs)
   426         urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs)
   426         self.retried_req = None
   427         self.retried_req = None
       
   428 
       
   429     def http_request(self, request):
       
   430         if self.auth:
       
   431             request.add_unredirected_header(self.auth_header, self.auth)
       
   432 
       
   433         return request
       
   434 
       
   435     def https_request(self, request):
       
   436         if self.auth:
       
   437             request.add_unredirected_header(self.auth_header, self.auth)
       
   438 
       
   439         return request
   427 
   440 
   428     def reset_retry_count(self):
   441     def reset_retry_count(self):
   429         # Python 2.6.5 will call this on 401 or 407 errors and thus loop
   442         # Python 2.6.5 will call this on 401 or 407 errors and thus loop
   430         # forever. We disable reset_retry_count completely and reset in
   443         # forever. We disable reset_retry_count completely and reset in
   431         # http_error_auth_reqed instead.
   444         # http_error_auth_reqed instead.
   436         if req is not self.retried_req:
   449         if req is not self.retried_req:
   437             self.retried_req = req
   450             self.retried_req = req
   438             self.retried = 0
   451             self.retried = 0
   439         return urllib2.HTTPBasicAuthHandler.http_error_auth_reqed(
   452         return urllib2.HTTPBasicAuthHandler.http_error_auth_reqed(
   440                         self, auth_header, host, req, headers)
   453                         self, auth_header, host, req, headers)
       
   454 
       
   455     def retry_http_basic_auth(self, host, req, realm):
       
   456         user, pw = self.passwd.find_user_password(realm, req.get_full_url())
       
   457         if pw is not None:
       
   458             raw = "%s:%s" % (user, pw)
       
   459             auth = 'Basic %s' % base64.b64encode(raw).strip()
       
   460             if req.headers.get(self.auth_header, None) == auth:
       
   461                 return None
       
   462             self.auth = auth
       
   463             req.add_unredirected_header(self.auth_header, auth)
       
   464             return self.parent.open(req)
       
   465         else:
       
   466             return None
   441 
   467 
   442 handlerfuncs = []
   468 handlerfuncs = []
   443 
   469 
   444 def opener(ui, authinfo=None):
   470 def opener(ui, authinfo=None):
   445     '''
   471     '''