Mercurial > hg
comparison mercurial/httppeer.py @ 17192:1ac628cd7113
peer: introduce real peer classes
This change separates peer implementations from the repository implementation.
localpeer currently is a simple pass-through to localrepository, except for
legacy calls, which have already been removed from localpeer. This ensures that
the local client code only uses the most modern peer API when talking to local
repos.
Peers have a .local() method which returns either None or the underlying
localrepository (or descendant thereof). Repos have a .peer() method to return
a freshly constructed localpeer. The latter is used by hg.peer(), and also to
allow folks to pass either a peer or a repo to some generic helper methods.
We might want to get rid of .peer() eventually.
The only user of locallegacypeer is debugdiscovery, which uses it to pose as a
pre-setdiscovery client. But we decided to leave the old API defined in
locallegacypeer for clarity and maybe for other uses in the future.
It might be nice to actually define the peer API directly in peer.py as stub
methods. One problem there is, however, that localpeer implements
lock/addchangegroup, whereas the true remote peers implement unbundle.
It might be desireable to get rid of this distinction eventually.
author | Peter Arrenbrecht <peter.arrenbrecht@gmail.com> |
---|---|
date | Fri, 13 Jul 2012 21:47:06 +0200 |
parents | mercurial/httprepo.py@7b15dd9125b3 |
children | 988974c2a4bf |
comparison
equal
deleted
inserted
replaced
17191:5884812686f7 | 17192:1ac628cd7113 |
---|---|
1 # httppeer.py - HTTP repository proxy classes for mercurial | |
2 # | |
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> | |
4 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> | |
5 # | |
6 # This software may be used and distributed according to the terms of the | |
7 # GNU General Public License version 2 or any later version. | |
8 | |
9 from node import nullid | |
10 from i18n import _ | |
11 import changegroup, statichttprepo, error, httpconnection, url, util, wireproto | |
12 import os, urllib, urllib2, zlib, httplib | |
13 import errno, socket | |
14 | |
15 def zgenerator(f): | |
16 zd = zlib.decompressobj() | |
17 try: | |
18 for chunk in util.filechunkiter(f): | |
19 while chunk: | |
20 yield zd.decompress(chunk, 2**18) | |
21 chunk = zd.unconsumed_tail | |
22 except httplib.HTTPException: | |
23 raise IOError(None, _('connection ended unexpectedly')) | |
24 yield zd.flush() | |
25 | |
26 class httppeer(wireproto.wirepeer): | |
27 def __init__(self, ui, path): | |
28 self.path = path | |
29 self.caps = None | |
30 self.handler = None | |
31 self.urlopener = None | |
32 u = util.url(path) | |
33 if u.query or u.fragment: | |
34 raise util.Abort(_('unsupported URL component: "%s"') % | |
35 (u.query or u.fragment)) | |
36 | |
37 # urllib cannot handle URLs with embedded user or passwd | |
38 self._url, authinfo = u.authinfo() | |
39 | |
40 self.ui = ui | |
41 self.ui.debug('using %s\n' % self._url) | |
42 | |
43 self.urlopener = url.opener(ui, authinfo) | |
44 | |
45 def __del__(self): | |
46 if self.urlopener: | |
47 for h in self.urlopener.handlers: | |
48 h.close() | |
49 getattr(h, "close_all", lambda : None)() | |
50 | |
51 def url(self): | |
52 return self.path | |
53 | |
54 # look up capabilities only when needed | |
55 | |
56 def _fetchcaps(self): | |
57 self.caps = set(self._call('capabilities').split()) | |
58 | |
59 def _capabilities(self): | |
60 if self.caps is None: | |
61 try: | |
62 self._fetchcaps() | |
63 except error.RepoError: | |
64 self.caps = set() | |
65 self.ui.debug('capabilities: %s\n' % | |
66 (' '.join(self.caps or ['none']))) | |
67 return self.caps | |
68 | |
69 def lock(self): | |
70 raise util.Abort(_('operation not supported over http')) | |
71 | |
72 def _callstream(self, cmd, **args): | |
73 if cmd == 'pushkey': | |
74 args['data'] = '' | |
75 data = args.pop('data', None) | |
76 size = 0 | |
77 if util.safehasattr(data, 'length'): | |
78 size = data.length | |
79 elif data is not None: | |
80 size = len(data) | |
81 headers = args.pop('headers', {}) | |
82 | |
83 if size and self.ui.configbool('ui', 'usehttp2', False): | |
84 headers['Expect'] = '100-Continue' | |
85 headers['X-HgHttp2'] = '1' | |
86 | |
87 self.ui.debug("sending %s command\n" % cmd) | |
88 q = [('cmd', cmd)] | |
89 headersize = 0 | |
90 if len(args) > 0: | |
91 httpheader = self.capable('httpheader') | |
92 if httpheader: | |
93 headersize = int(httpheader.split(',')[0]) | |
94 if headersize > 0: | |
95 # The headers can typically carry more data than the URL. | |
96 encargs = urllib.urlencode(sorted(args.items())) | |
97 headerfmt = 'X-HgArg-%s' | |
98 contentlen = headersize - len(headerfmt % '000' + ': \r\n') | |
99 headernum = 0 | |
100 for i in xrange(0, len(encargs), contentlen): | |
101 headernum += 1 | |
102 header = headerfmt % str(headernum) | |
103 headers[header] = encargs[i:i + contentlen] | |
104 varyheaders = [headerfmt % str(h) for h in range(1, headernum + 1)] | |
105 headers['Vary'] = ','.join(varyheaders) | |
106 else: | |
107 q += sorted(args.items()) | |
108 qs = '?%s' % urllib.urlencode(q) | |
109 cu = "%s%s" % (self._url, qs) | |
110 req = urllib2.Request(cu, data, headers) | |
111 if data is not None: | |
112 self.ui.debug("sending %s bytes\n" % size) | |
113 req.add_unredirected_header('Content-Length', '%d' % size) | |
114 try: | |
115 resp = self.urlopener.open(req) | |
116 except urllib2.HTTPError, inst: | |
117 if inst.code == 401: | |
118 raise util.Abort(_('authorization failed')) | |
119 raise | |
120 except httplib.HTTPException, inst: | |
121 self.ui.debug('http error while sending %s command\n' % cmd) | |
122 self.ui.traceback() | |
123 raise IOError(None, inst) | |
124 except IndexError: | |
125 # this only happens with Python 2.3, later versions raise URLError | |
126 raise util.Abort(_('http error, possibly caused by proxy setting')) | |
127 # record the url we got redirected to | |
128 resp_url = resp.geturl() | |
129 if resp_url.endswith(qs): | |
130 resp_url = resp_url[:-len(qs)] | |
131 if self._url.rstrip('/') != resp_url.rstrip('/'): | |
132 if not self.ui.quiet: | |
133 self.ui.warn(_('real URL is %s\n') % resp_url) | |
134 self._url = resp_url | |
135 try: | |
136 proto = resp.getheader('content-type') | |
137 except AttributeError: | |
138 proto = resp.headers.get('content-type', '') | |
139 | |
140 safeurl = util.hidepassword(self._url) | |
141 if proto.startswith('application/hg-error'): | |
142 raise error.OutOfBandError(resp.read()) | |
143 # accept old "text/plain" and "application/hg-changegroup" for now | |
144 if not (proto.startswith('application/mercurial-') or | |
145 proto.startswith('text/plain') or | |
146 proto.startswith('application/hg-changegroup')): | |
147 self.ui.debug("requested URL: '%s'\n" % util.hidepassword(cu)) | |
148 raise error.RepoError( | |
149 _("'%s' does not appear to be an hg repository:\n" | |
150 "---%%<--- (%s)\n%s\n---%%<---\n") | |
151 % (safeurl, proto or 'no content-type', resp.read())) | |
152 | |
153 if proto.startswith('application/mercurial-'): | |
154 try: | |
155 version = proto.split('-', 1)[1] | |
156 version_info = tuple([int(n) for n in version.split('.')]) | |
157 except ValueError: | |
158 raise error.RepoError(_("'%s' sent a broken Content-Type " | |
159 "header (%s)") % (safeurl, proto)) | |
160 if version_info > (0, 1): | |
161 raise error.RepoError(_("'%s' uses newer protocol %s") % | |
162 (safeurl, version)) | |
163 | |
164 return resp | |
165 | |
166 def _call(self, cmd, **args): | |
167 fp = self._callstream(cmd, **args) | |
168 try: | |
169 return fp.read() | |
170 finally: | |
171 # if using keepalive, allow connection to be reused | |
172 fp.close() | |
173 | |
174 def _callpush(self, cmd, cg, **args): | |
175 # have to stream bundle to a temp file because we do not have | |
176 # http 1.1 chunked transfer. | |
177 | |
178 types = self.capable('unbundle') | |
179 try: | |
180 types = types.split(',') | |
181 except AttributeError: | |
182 # servers older than d1b16a746db6 will send 'unbundle' as a | |
183 # boolean capability. They only support headerless/uncompressed | |
184 # bundles. | |
185 types = [""] | |
186 for x in types: | |
187 if x in changegroup.bundletypes: | |
188 type = x | |
189 break | |
190 | |
191 tempname = changegroup.writebundle(cg, None, type) | |
192 fp = httpconnection.httpsendfile(self.ui, tempname, "rb") | |
193 headers = {'Content-Type': 'application/mercurial-0.1'} | |
194 | |
195 try: | |
196 try: | |
197 r = self._call(cmd, data=fp, headers=headers, **args) | |
198 vals = r.split('\n', 1) | |
199 if len(vals) < 2: | |
200 raise error.ResponseError(_("unexpected response:"), r) | |
201 return vals | |
202 except socket.error, err: | |
203 if err.args[0] in (errno.ECONNRESET, errno.EPIPE): | |
204 raise util.Abort(_('push failed: %s') % err.args[1]) | |
205 raise util.Abort(err.args[1]) | |
206 finally: | |
207 fp.close() | |
208 os.unlink(tempname) | |
209 | |
210 def _abort(self, exception): | |
211 raise exception | |
212 | |
213 def _decompress(self, stream): | |
214 return util.chunkbuffer(zgenerator(stream)) | |
215 | |
216 class httpspeer(httppeer): | |
217 def __init__(self, ui, path): | |
218 if not url.has_https: | |
219 raise util.Abort(_('Python support for SSL and HTTPS ' | |
220 'is not installed')) | |
221 httppeer.__init__(self, ui, path) | |
222 | |
223 def instance(ui, path, create): | |
224 if create: | |
225 raise util.Abort(_('cannot create new http repository')) | |
226 try: | |
227 if path.startswith('https:'): | |
228 inst = httpspeer(ui, path) | |
229 else: | |
230 inst = httppeer(ui, path) | |
231 try: | |
232 # Try to do useful work when checking compatibility. | |
233 # Usually saves a roundtrip since we want the caps anyway. | |
234 inst._fetchcaps() | |
235 except error.RepoError: | |
236 # No luck, try older compatibility check. | |
237 inst.between([(nullid, nullid)]) | |
238 return inst | |
239 except error.RepoError, httpexception: | |
240 try: | |
241 r = statichttprepo.instance(ui, "static-" + path, create) | |
242 ui.note('(falling back to static-http)\n') | |
243 return r | |
244 except error.RepoError: | |
245 raise httpexception # use the original http RepoError instead |