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