diff mercurial/httprepo.py @ 2281:7761597b5da3

prompt user for http authentication info in interactive mode, mercurial now asks the user for the username and password when the server requires it. the previous behavior was to fail with an http 401. based on patch from eric jaffe <jaffe.eric@gmail.com>.
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Sun, 14 May 2006 17:37:17 -0700
parents 1a09814a5b1f
children ce67fa312f61
line wrap: on
line diff
--- a/mercurial/httprepo.py	Sat May 13 23:00:46 2006 -0700
+++ b/mercurial/httprepo.py	Sun May 14 17:37:17 2006 -0700
@@ -11,6 +11,25 @@
 from demandload import *
 demandload(globals(), "hg os urllib urllib2 urlparse zlib util httplib")
 
+class passwordmgr(urllib2.HTTPPasswordMgr):
+    def __init__(self, ui):
+        urllib2.HTTPPasswordMgr.__init__(self)
+        self.ui = ui
+
+    def find_user_password(self, realm, authuri):
+        authinfo = urllib2.HTTPPasswordMgr.find_user_password(
+            self, realm, authuri)
+        if authinfo != (None, None):
+            return authinfo
+
+        self.ui.write(_("http authorization required\n"))
+        self.ui.status(_("realm: %s\n") % realm)
+        user = self.ui.prompt(_("user:"), default=None)
+        passwd = self.ui.getpass()
+
+        self.add_password(realm, authuri, user, passwd)
+        return (user, passwd)
+
 class httprepository(remoterepository):
     def __init__(self, ui, path):
         # fix missing / after hostname
@@ -53,13 +72,21 @@
         if host and not no_proxy:
             proxy_handler = urllib2.ProxyHandler({"http" : "http://" + host})
 
-        authinfo = None
+        proxyauthinfo = None
         if user and passwd:
             passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
             passmgr.add_password(None, host, user, passwd)
-            authinfo = urllib2.ProxyBasicAuthHandler(passmgr)
+            proxyauthinfo = urllib2.ProxyBasicAuthHandler(passmgr)
 
-        opener = urllib2.build_opener(proxy_handler, authinfo)
+        if ui.interactive:
+            passmgr = passwordmgr(ui)
+            opener = urllib2.build_opener(
+                proxy_handler, proxyauthinfo,
+                urllib2.HTTPBasicAuthHandler(passmgr),
+                urllib2.HTTPDigestAuthHandler(passmgr))
+        else:
+            opener = urllib2.build_opener(proxy_handler, proxyauthinfo)
+
         # 1.0 here is the _protocol_ version
         opener.addheaders = [('User-agent', 'mercurial/proto-1.0')]
         urllib2.install_opener(opener)