##// END OF EJS Templates
httpconnection: use progress helper...
Martin von Zweigbergk -
r38412:5f9d436c default
parent child Browse files
Show More
@@ -1,109 +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 17 pycompat,
18 18 util,
19 19 )
20 20
21 21 urlerr = util.urlerr
22 22 urlreq = util.urlreq
23 23
24 24 # moved here from url.py to avoid a cycle
25 25 class httpsendfile(object):
26 26 """This is a wrapper around the objects returned by python's "open".
27 27
28 28 Its purpose is to send file-like objects via HTTP.
29 29 It do however not define a __len__ attribute because the length
30 30 might be more than Py_ssize_t can handle.
31 31 """
32 32
33 33 def __init__(self, ui, *args, **kwargs):
34 34 self.ui = ui
35 35 self._data = open(*args, **kwargs)
36 36 self.seek = self._data.seek
37 37 self.close = self._data.close
38 38 self.write = self._data.write
39 39 self.length = os.fstat(self._data.fileno()).st_size
40 40 self._pos = 0
41 self._total = self.length // 1024 * 2
42
43 def read(self, *args, **kwargs):
44 ret = self._data.read(*args, **kwargs)
45 if not ret:
46 self.ui.progress(_('sending'), None)
47 return ret
48 self._pos += len(ret)
49 41 # We pass double the max for total because we currently have
50 42 # to send the bundle twice in the case of a server that
51 43 # requires authentication. Since we can't know until we try
52 44 # once whether authentication will be required, just lie to
53 45 # the user and maybe the push succeeds suddenly at 50%.
54 self.ui.progress(_('sending'), self._pos // 1024,
55 unit=_('kb'), total=self._total)
46 self._progress = ui.makeprogress(_('sending'), unit=_('kb'),
47 total=(self.length // 1024 * 2))
48
49 def read(self, *args, **kwargs):
50 ret = self._data.read(*args, **kwargs)
51 if not ret:
52 self._progress.complete()
53 return ret
54 self._pos += len(ret)
55 self._progress.update(self._pos // 1024)
56 56 return ret
57 57
58 58 def __enter__(self):
59 59 return self
60 60
61 61 def __exit__(self, exc_type, exc_val, exc_tb):
62 62 self.close()
63 63
64 64 # moved here from url.py to avoid a cycle
65 65 def readauthforuri(ui, uri, user):
66 66 uri = pycompat.bytesurl(uri)
67 67 # Read configuration
68 68 groups = {}
69 69 for key, val in ui.configitems('auth'):
70 70 if key in ('cookiefile',):
71 71 continue
72 72
73 73 if '.' not in key:
74 74 ui.warn(_("ignoring invalid [auth] key '%s'\n") % key)
75 75 continue
76 76 group, setting = key.rsplit('.', 1)
77 77 gdict = groups.setdefault(group, {})
78 78 if setting in ('username', 'cert', 'key'):
79 79 val = util.expandpath(val)
80 80 gdict[setting] = val
81 81
82 82 # Find the best match
83 83 scheme, hostpath = uri.split('://', 1)
84 84 bestuser = None
85 85 bestlen = 0
86 86 bestauth = None
87 87 for group, auth in groups.iteritems():
88 88 if user and user != auth.get('username', user):
89 89 # If a username was set in the URI, the entry username
90 90 # must either match it or be unset
91 91 continue
92 92 prefix = auth.get('prefix')
93 93 if not prefix:
94 94 continue
95 95 p = prefix.split('://', 1)
96 96 if len(p) > 1:
97 97 schemes, prefix = [p[0]], p[1]
98 98 else:
99 99 schemes = (auth.get('schemes') or 'https').split()
100 100 if (prefix == '*' or hostpath.startswith(prefix)) and \
101 101 (len(prefix) > bestlen or (len(prefix) == bestlen and \
102 102 not bestuser and 'username' in auth)) \
103 103 and scheme in schemes:
104 104 bestlen = len(prefix)
105 105 bestauth = group, auth
106 106 bestuser = auth.get('username')
107 107 if user and not bestuser:
108 108 auth['username'] = user
109 109 return bestauth
General Comments 0
You need to be logged in to leave comments. Login now