##// END OF EJS Templates
httpconnection: convert url to bytes in readauthforuri...
Augie Fackler -
r36669:6b1eb4c6 default
parent child Browse files
Show More
@@ -1,107 +1,109 b''
1 1 # httpconnection.py - urllib2 handler for new http support
2 2 #
3 3 # Copyright 2005, 2006, 2007, 2008 Matt Mackall <mpm@selenic.com>
4 4 # Copyright 2006, 2007 Alexis S. L. Carvalho <alexis@cecm.usp.br>
5 5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
6 6 # Copyright 2011 Google, Inc.
7 7 #
8 8 # This software may be used and distributed according to the terms of the
9 9 # GNU General Public License version 2 or any later version.
10 10
11 11 from __future__ import absolute_import
12 12
13 13 import os
14 14
15 15 from .i18n import _
16 16 from . import (
17 pycompat,
17 18 util,
18 19 )
19 20
20 21 urlerr = util.urlerr
21 22 urlreq = util.urlreq
22 23
23 24 # moved here from url.py to avoid a cycle
24 25 class httpsendfile(object):
25 26 """This is a wrapper around the objects returned by python's "open".
26 27
27 28 Its purpose is to send file-like objects via HTTP.
28 29 It do however not define a __len__ attribute because the length
29 30 might be more than Py_ssize_t can handle.
30 31 """
31 32
32 33 def __init__(self, ui, *args, **kwargs):
33 34 self.ui = ui
34 35 self._data = open(*args, **kwargs)
35 36 self.seek = self._data.seek
36 37 self.close = self._data.close
37 38 self.write = self._data.write
38 39 self.length = os.fstat(self._data.fileno()).st_size
39 40 self._pos = 0
40 41 self._total = self.length // 1024 * 2
41 42
42 43 def read(self, *args, **kwargs):
43 44 ret = self._data.read(*args, **kwargs)
44 45 if not ret:
45 46 self.ui.progress(_('sending'), None)
46 47 return ret
47 48 self._pos += len(ret)
48 49 # We pass double the max for total because we currently have
49 50 # to send the bundle twice in the case of a server that
50 51 # requires authentication. Since we can't know until we try
51 52 # once whether authentication will be required, just lie to
52 53 # the user and maybe the push succeeds suddenly at 50%.
53 54 self.ui.progress(_('sending'), self._pos // 1024,
54 55 unit=_('kb'), total=self._total)
55 56 return ret
56 57
57 58 def __enter__(self):
58 59 return self
59 60
60 61 def __exit__(self, exc_type, exc_val, exc_tb):
61 62 self.close()
62 63
63 64 # moved here from url.py to avoid a cycle
64 65 def readauthforuri(ui, uri, user):
66 uri = pycompat.bytesurl(uri)
65 67 # Read configuration
66 68 groups = {}
67 69 for key, val in ui.configitems('auth'):
68 70 if key in ('cookiefile',):
69 71 continue
70 72
71 73 if '.' not in key:
72 74 ui.warn(_("ignoring invalid [auth] key '%s'\n") % key)
73 75 continue
74 76 group, setting = key.rsplit('.', 1)
75 77 gdict = groups.setdefault(group, {})
76 78 if setting in ('username', 'cert', 'key'):
77 79 val = util.expandpath(val)
78 80 gdict[setting] = val
79 81
80 82 # Find the best match
81 83 scheme, hostpath = uri.split('://', 1)
82 84 bestuser = None
83 85 bestlen = 0
84 86 bestauth = None
85 87 for group, auth in groups.iteritems():
86 88 if user and user != auth.get('username', user):
87 89 # If a username was set in the URI, the entry username
88 90 # must either match it or be unset
89 91 continue
90 92 prefix = auth.get('prefix')
91 93 if not prefix:
92 94 continue
93 95 p = prefix.split('://', 1)
94 96 if len(p) > 1:
95 97 schemes, prefix = [p[0]], p[1]
96 98 else:
97 99 schemes = (auth.get('schemes') or 'https').split()
98 100 if (prefix == '*' or hostpath.startswith(prefix)) and \
99 101 (len(prefix) > bestlen or (len(prefix) == bestlen and \
100 102 not bestuser and 'username' in auth)) \
101 103 and scheme in schemes:
102 104 bestlen = len(prefix)
103 105 bestauth = group, auth
104 106 bestuser = auth.get('username')
105 107 if user and not bestuser:
106 108 auth['username'] = user
107 109 return bestauth
General Comments 0
You need to be logged in to leave comments. Login now