Mercurial > hg
comparison mercurial/url.py @ 13371:c691cfdc6b4d
url: move [auth] parsing out into a utility function
No functionality change, but it makes the [auth] section parsing and
best match detection usable by third party tools
author | Steve Borho <steve@borho.org> |
---|---|
date | Sat, 12 Feb 2011 21:59:43 -0600 |
parents | d13a533a0b11 |
children | 5bced0d28a39 |
comparison
equal
deleted
inserted
replaced
13370:d13a533a0b11 | 13371:c691cfdc6b4d |
---|---|
68 userpass = quote(user) + ':' + quote(passwd) | 68 userpass = quote(user) + ':' + quote(passwd) |
69 else: | 69 else: |
70 userpass = quote(user) | 70 userpass = quote(user) |
71 return userpass + '@' + hostport | 71 return userpass + '@' + hostport |
72 return hostport | 72 return hostport |
73 | |
74 def readauthforuri(ui, uri): | |
75 # Read configuration | |
76 config = dict() | |
77 for key, val in ui.configitems('auth'): | |
78 if '.' not in key: | |
79 ui.warn(_("ignoring invalid [auth] key '%s'\n") % key) | |
80 continue | |
81 group, setting = key.rsplit('.', 1) | |
82 gdict = config.setdefault(group, dict()) | |
83 if setting in ('username', 'cert', 'key'): | |
84 val = util.expandpath(val) | |
85 gdict[setting] = val | |
86 | |
87 # Find the best match | |
88 scheme, hostpath = uri.split('://', 1) | |
89 bestlen = 0 | |
90 bestauth = None | |
91 for auth in config.itervalues(): | |
92 prefix = auth.get('prefix') | |
93 if not prefix: | |
94 continue | |
95 p = prefix.split('://', 1) | |
96 if len(p) > 1: | |
97 schemes, prefix = [p[0]], p[1] | |
98 else: | |
99 schemes = (auth.get('schemes') or 'https').split() | |
100 if (prefix == '*' or hostpath.startswith(prefix)) and \ | |
101 len(prefix) > bestlen and scheme in schemes: | |
102 bestlen = len(prefix) | |
103 bestauth = auth | |
104 return bestauth | |
73 | 105 |
74 _safe = ('abcdefghijklmnopqrstuvwxyz' | 106 _safe = ('abcdefghijklmnopqrstuvwxyz' |
75 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | 107 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
76 '0123456789' '_.-/') | 108 '0123456789' '_.-/') |
77 _safeset = None | 109 _safeset = None |
147 def _writedebug(self, user, passwd): | 179 def _writedebug(self, user, passwd): |
148 msg = _('http auth: user %s, password %s\n') | 180 msg = _('http auth: user %s, password %s\n') |
149 self.ui.debug(msg % (user, passwd and '*' * len(passwd) or 'not set')) | 181 self.ui.debug(msg % (user, passwd and '*' * len(passwd) or 'not set')) |
150 | 182 |
151 def readauthtoken(self, uri): | 183 def readauthtoken(self, uri): |
152 # Read configuration | 184 return readauthforuri(self.ui, uri) |
153 config = dict() | |
154 for key, val in self.ui.configitems('auth'): | |
155 if '.' not in key: | |
156 self.ui.warn(_("ignoring invalid [auth] key '%s'\n") % key) | |
157 continue | |
158 group, setting = key.rsplit('.', 1) | |
159 gdict = config.setdefault(group, dict()) | |
160 if setting in ('username', 'cert', 'key'): | |
161 val = util.expandpath(val) | |
162 gdict[setting] = val | |
163 | |
164 # Find the best match | |
165 scheme, hostpath = uri.split('://', 1) | |
166 bestlen = 0 | |
167 bestauth = None | |
168 for auth in config.itervalues(): | |
169 prefix = auth.get('prefix') | |
170 if not prefix: | |
171 continue | |
172 p = prefix.split('://', 1) | |
173 if len(p) > 1: | |
174 schemes, prefix = [p[0]], p[1] | |
175 else: | |
176 schemes = (auth.get('schemes') or 'https').split() | |
177 if (prefix == '*' or hostpath.startswith(prefix)) and \ | |
178 len(prefix) > bestlen and scheme in schemes: | |
179 bestlen = len(prefix) | |
180 bestauth = auth | |
181 return bestauth | |
182 | 185 |
183 class proxyhandler(urllib2.ProxyHandler): | 186 class proxyhandler(urllib2.ProxyHandler): |
184 def __init__(self, ui): | 187 def __init__(self, ui): |
185 proxyurl = ui.config("http_proxy", "host") or os.getenv('http_proxy') | 188 proxyurl = ui.config("http_proxy", "host") or os.getenv('http_proxy') |
186 # XXX proxyauthinfo = None | 189 # XXX proxyauthinfo = None |