##// END OF EJS Templates
Avoid float rounding errors when checking http protocol version.
Thomas Arendsen Hein -
r4356:aed9e6dc default
parent child Browse files
Show More
@@ -1,402 +1,403 b''
1 1 # httprepo.py - HTTP repository proxy classes for mercurial
2 2 #
3 3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
4 4 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
5 5 #
6 6 # This software may be used and distributed according to the terms
7 7 # of the GNU General Public License, incorporated herein by reference.
8 8
9 9 from node import *
10 10 from remoterepo import *
11 11 from i18n import gettext as _
12 12 from demandload import *
13 13 demandload(globals(), "hg os urllib urllib2 urlparse zlib util httplib")
14 14 demandload(globals(), "errno keepalive tempfile socket changegroup")
15 15
16 16 class passwordmgr(urllib2.HTTPPasswordMgrWithDefaultRealm):
17 17 def __init__(self, ui):
18 18 urllib2.HTTPPasswordMgrWithDefaultRealm.__init__(self)
19 19 self.ui = ui
20 20
21 21 def find_user_password(self, realm, authuri):
22 22 authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm.find_user_password(
23 23 self, realm, authuri)
24 24 user, passwd = authinfo
25 25 if user and passwd:
26 26 return (user, passwd)
27 27
28 28 if not self.ui.interactive:
29 29 raise util.Abort(_('http authorization required'))
30 30
31 31 self.ui.write(_("http authorization required\n"))
32 32 self.ui.status(_("realm: %s\n") % realm)
33 33 if user:
34 34 self.ui.status(_("user: %s\n") % user)
35 35 else:
36 36 user = self.ui.prompt(_("user:"), default=None)
37 37
38 38 if not passwd:
39 39 passwd = self.ui.getpass()
40 40
41 41 self.add_password(realm, authuri, user, passwd)
42 42 return (user, passwd)
43 43
44 44 def netlocsplit(netloc):
45 45 '''split [user[:passwd]@]host[:port] into 4-tuple.'''
46 46
47 47 a = netloc.find('@')
48 48 if a == -1:
49 49 user, passwd = None, None
50 50 else:
51 51 userpass, netloc = netloc[:a], netloc[a+1:]
52 52 c = userpass.find(':')
53 53 if c == -1:
54 54 user, passwd = urllib.unquote(userpass), None
55 55 else:
56 56 user = urllib.unquote(userpass[:c])
57 57 passwd = urllib.unquote(userpass[c+1:])
58 58 c = netloc.find(':')
59 59 if c == -1:
60 60 host, port = netloc, None
61 61 else:
62 62 host, port = netloc[:c], netloc[c+1:]
63 63 return host, port, user, passwd
64 64
65 65 def netlocunsplit(host, port, user=None, passwd=None):
66 66 '''turn host, port, user, passwd into [user[:passwd]@]host[:port].'''
67 67 if port:
68 68 hostport = host + ':' + port
69 69 else:
70 70 hostport = host
71 71 if user:
72 72 if passwd:
73 73 userpass = urllib.quote(user) + ':' + urllib.quote(passwd)
74 74 else:
75 75 userpass = urllib.quote(user)
76 76 return userpass + '@' + hostport
77 77 return hostport
78 78
79 79 # work around a bug in Python < 2.4.2
80 80 # (it leaves a "\n" at the end of Proxy-authorization headers)
81 81 class request(urllib2.Request):
82 82 def add_header(self, key, val):
83 83 if key.lower() == 'proxy-authorization':
84 84 val = val.strip()
85 85 return urllib2.Request.add_header(self, key, val)
86 86
87 87 class httpsendfile(file):
88 88 def __len__(self):
89 89 return os.fstat(self.fileno()).st_size
90 90
91 91 def _gen_sendfile(connection):
92 92 def _sendfile(self, data):
93 93 # send a file
94 94 if isinstance(data, httpsendfile):
95 95 # if auth required, some data sent twice, so rewind here
96 96 data.seek(0)
97 97 for chunk in util.filechunkiter(data):
98 98 connection.send(self, chunk)
99 99 else:
100 100 connection.send(self, data)
101 101 return _sendfile
102 102
103 103 class httpconnection(keepalive.HTTPConnection):
104 104 # must be able to send big bundle as stream.
105 105 send = _gen_sendfile(keepalive.HTTPConnection)
106 106
107 107 class basehttphandler(keepalive.HTTPHandler):
108 108 def http_open(self, req):
109 109 return self.do_open(httpconnection, req)
110 110
111 111 has_https = hasattr(urllib2, 'HTTPSHandler')
112 112 if has_https:
113 113 class httpsconnection(httplib.HTTPSConnection):
114 114 response_class = keepalive.HTTPResponse
115 115 # must be able to send big bundle as stream.
116 116 send = _gen_sendfile(httplib.HTTPSConnection)
117 117
118 118 class httphandler(basehttphandler, urllib2.HTTPSHandler):
119 119 def https_open(self, req):
120 120 return self.do_open(httpsconnection, req)
121 121 else:
122 122 class httphandler(basehttphandler):
123 123 pass
124 124
125 125 def zgenerator(f):
126 126 zd = zlib.decompressobj()
127 127 try:
128 128 for chunk in util.filechunkiter(f):
129 129 yield zd.decompress(chunk)
130 130 except httplib.HTTPException, inst:
131 131 raise IOError(None, _('connection ended unexpectedly'))
132 132 yield zd.flush()
133 133
134 134 class httprepository(remoterepository):
135 135 def __init__(self, ui, path):
136 136 self.path = path
137 137 self.caps = None
138 138 self.handler = None
139 139 scheme, netloc, urlpath, query, frag = urlparse.urlsplit(path)
140 140 if query or frag:
141 141 raise util.Abort(_('unsupported URL component: "%s"') %
142 142 (query or frag))
143 143 if not urlpath: urlpath = '/'
144 144 host, port, user, passwd = netlocsplit(netloc)
145 145
146 146 # urllib cannot handle URLs with embedded user or passwd
147 147 self._url = urlparse.urlunsplit((scheme, netlocunsplit(host, port),
148 148 urlpath, '', ''))
149 149 self.ui = ui
150 150
151 151 proxyurl = ui.config("http_proxy", "host") or os.getenv('http_proxy')
152 152 # XXX proxyauthinfo = None
153 153 self.handler = httphandler()
154 154 handlers = [self.handler]
155 155
156 156 if proxyurl:
157 157 # proxy can be proper url or host[:port]
158 158 if not (proxyurl.startswith('http:') or
159 159 proxyurl.startswith('https:')):
160 160 proxyurl = 'http://' + proxyurl + '/'
161 161 snpqf = urlparse.urlsplit(proxyurl)
162 162 proxyscheme, proxynetloc, proxypath, proxyquery, proxyfrag = snpqf
163 163 hpup = netlocsplit(proxynetloc)
164 164
165 165 proxyhost, proxyport, proxyuser, proxypasswd = hpup
166 166 if not proxyuser:
167 167 proxyuser = ui.config("http_proxy", "user")
168 168 proxypasswd = ui.config("http_proxy", "passwd")
169 169
170 170 # see if we should use a proxy for this url
171 171 no_list = [ "localhost", "127.0.0.1" ]
172 172 no_list.extend([p.lower() for
173 173 p in ui.configlist("http_proxy", "no")])
174 174 no_list.extend([p.strip().lower() for
175 175 p in os.getenv("no_proxy", '').split(',')
176 176 if p.strip()])
177 177 # "http_proxy.always" config is for running tests on localhost
178 178 if (not ui.configbool("http_proxy", "always") and
179 179 host.lower() in no_list):
180 180 ui.debug(_('disabling proxy for %s\n') % host)
181 181 else:
182 182 proxyurl = urlparse.urlunsplit((
183 183 proxyscheme, netlocunsplit(proxyhost, proxyport,
184 184 proxyuser, proxypasswd or ''),
185 185 proxypath, proxyquery, proxyfrag))
186 186 handlers.append(urllib2.ProxyHandler({scheme: proxyurl}))
187 187 ui.debug(_('proxying through http://%s:%s\n') %
188 188 (proxyhost, proxyport))
189 189
190 190 # urllib2 takes proxy values from the environment and those
191 191 # will take precedence if found, so drop them
192 192 for env in ["HTTP_PROXY", "http_proxy", "no_proxy"]:
193 193 try:
194 194 if os.environ.has_key(env):
195 195 del os.environ[env]
196 196 except OSError:
197 197 pass
198 198
199 199 passmgr = passwordmgr(ui)
200 200 if user:
201 201 ui.debug(_('http auth: user %s, password %s\n') %
202 202 (user, passwd and '*' * len(passwd) or 'not set'))
203 203 passmgr.add_password(None, host, user, passwd or '')
204 204
205 205 handlers.extend((urllib2.HTTPBasicAuthHandler(passmgr),
206 206 urllib2.HTTPDigestAuthHandler(passmgr)))
207 207 opener = urllib2.build_opener(*handlers)
208 208
209 209 # 1.0 here is the _protocol_ version
210 210 opener.addheaders = [('User-agent', 'mercurial/proto-1.0')]
211 211 urllib2.install_opener(opener)
212 212
213 213 def __del__(self):
214 214 if self.handler:
215 215 self.handler.close_all()
216 216 self.handler = None
217 217
218 218 def url(self):
219 219 return self.path
220 220
221 221 # look up capabilities only when needed
222 222
223 223 def get_caps(self):
224 224 if self.caps is None:
225 225 try:
226 226 self.caps = self.do_read('capabilities').split()
227 227 except hg.RepoError:
228 228 self.caps = ()
229 229 self.ui.debug(_('capabilities: %s\n') %
230 230 (' '.join(self.caps or ['none'])))
231 231 return self.caps
232 232
233 233 capabilities = property(get_caps)
234 234
235 235 def lock(self):
236 236 raise util.Abort(_('operation not supported over http'))
237 237
238 238 def do_cmd(self, cmd, **args):
239 239 data = args.pop('data', None)
240 240 headers = args.pop('headers', {})
241 241 self.ui.debug(_("sending %s command\n") % cmd)
242 242 q = {"cmd": cmd}
243 243 q.update(args)
244 244 qs = '?%s' % urllib.urlencode(q)
245 245 cu = "%s%s" % (self._url, qs)
246 246 try:
247 247 if data:
248 248 self.ui.debug(_("sending %s bytes\n") %
249 249 headers.get('content-length', 'X'))
250 250 resp = urllib2.urlopen(request(cu, data, headers))
251 251 except urllib2.HTTPError, inst:
252 252 if inst.code == 401:
253 253 raise util.Abort(_('authorization failed'))
254 254 raise
255 255 except httplib.HTTPException, inst:
256 256 self.ui.debug(_('http error while sending %s command\n') % cmd)
257 257 self.ui.print_exc()
258 258 raise IOError(None, inst)
259 259 except IndexError:
260 260 # this only happens with Python 2.3, later versions raise URLError
261 261 raise util.Abort(_('http error, possibly caused by proxy setting'))
262 262 # record the url we got redirected to
263 263 resp_url = resp.geturl()
264 264 if resp_url.endswith(qs):
265 265 resp_url = resp_url[:-len(qs)]
266 266 if self._url != resp_url:
267 267 self.ui.status(_('real URL is %s\n') % resp_url)
268 268 self._url = resp_url
269 269 try:
270 270 proto = resp.getheader('content-type')
271 271 except AttributeError:
272 272 proto = resp.headers['content-type']
273 273
274 274 # accept old "text/plain" and "application/hg-changegroup" for now
275 275 if not proto.startswith('application/mercurial-') and \
276 276 not proto.startswith('text/plain') and \
277 277 not proto.startswith('application/hg-changegroup'):
278 278 raise hg.RepoError(_("'%s' does not appear to be an hg repository") %
279 279 self._url)
280 280
281 281 if proto.startswith('application/mercurial-'):
282 282 try:
283 version = float(proto[22:])
283 version = proto.split('-', 1)[1]
284 version_info = tuple([int(n) for n in version.split('.')])
284 285 except ValueError:
285 286 raise hg.RepoError(_("'%s' sent a broken Content-type "
286 287 "header (%s)") % (self._url, proto))
287 if version > 0.1:
288 if version_info > (0, 1):
288 289 raise hg.RepoError(_("'%s' uses newer protocol %s") %
289 290 (self._url, version))
290 291
291 292 return resp
292 293
293 294 def do_read(self, cmd, **args):
294 295 fp = self.do_cmd(cmd, **args)
295 296 try:
296 297 return fp.read()
297 298 finally:
298 299 # if using keepalive, allow connection to be reused
299 300 fp.close()
300 301
301 302 def lookup(self, key):
302 303 d = self.do_cmd("lookup", key = key).read()
303 304 success, data = d[:-1].split(' ', 1)
304 305 if int(success):
305 306 return bin(data)
306 307 raise hg.RepoError(data)
307 308
308 309 def heads(self):
309 310 d = self.do_read("heads")
310 311 try:
311 312 return map(bin, d[:-1].split(" "))
312 313 except:
313 314 raise util.UnexpectedOutput(_("unexpected response:"), d)
314 315
315 316 def branches(self, nodes):
316 317 n = " ".join(map(hex, nodes))
317 318 d = self.do_read("branches", nodes=n)
318 319 try:
319 320 br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ]
320 321 return br
321 322 except:
322 323 raise util.UnexpectedOutput(_("unexpected response:"), d)
323 324
324 325 def between(self, pairs):
325 326 n = "\n".join(["-".join(map(hex, p)) for p in pairs])
326 327 d = self.do_read("between", pairs=n)
327 328 try:
328 329 p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ]
329 330 return p
330 331 except:
331 332 raise util.UnexpectedOutput(_("unexpected response:"), d)
332 333
333 334 def changegroup(self, nodes, kind):
334 335 n = " ".join(map(hex, nodes))
335 336 f = self.do_cmd("changegroup", roots=n)
336 337 return util.chunkbuffer(zgenerator(f))
337 338
338 339 def changegroupsubset(self, bases, heads, source):
339 340 baselst = " ".join([hex(n) for n in bases])
340 341 headlst = " ".join([hex(n) for n in heads])
341 342 f = self.do_cmd("changegroupsubset", bases=baselst, heads=headlst)
342 343 return util.chunkbuffer(zgenerator(f))
343 344
344 345 def unbundle(self, cg, heads, source):
345 346 # have to stream bundle to a temp file because we do not have
346 347 # http 1.1 chunked transfer.
347 348
348 349 type = ""
349 350 types = self.capable('unbundle')
350 351 # servers older than d1b16a746db6 will send 'unbundle' as a
351 352 # boolean capability
352 353 try:
353 354 types = types.split(',')
354 355 except AttributeError:
355 356 types = [""]
356 357 if types:
357 358 for x in types:
358 359 if x in changegroup.bundletypes:
359 360 type = x
360 361 break
361 362
362 363 tempname = changegroup.writebundle(cg, None, type)
363 364 fp = httpsendfile(tempname, "rb")
364 365 try:
365 366 try:
366 367 rfp = self.do_cmd(
367 368 'unbundle', data=fp,
368 369 headers={'content-type': 'application/octet-stream'},
369 370 heads=' '.join(map(hex, heads)))
370 371 try:
371 372 ret = int(rfp.readline())
372 373 self.ui.write(rfp.read())
373 374 return ret
374 375 finally:
375 376 rfp.close()
376 377 except socket.error, err:
377 378 if err[0] in (errno.ECONNRESET, errno.EPIPE):
378 379 raise util.Abort(_('push failed: %s') % err[1])
379 380 raise util.Abort(err[1])
380 381 finally:
381 382 fp.close()
382 383 os.unlink(tempname)
383 384
384 385 def stream_out(self):
385 386 return self.do_cmd('stream_out')
386 387
387 388 class httpsrepository(httprepository):
388 389 def __init__(self, ui, path):
389 390 if not has_https:
390 391 raise util.Abort(_('Python support for SSL and HTTPS '
391 392 'is not installed'))
392 393 httprepository.__init__(self, ui, path)
393 394
394 395 def instance(ui, path, create):
395 396 if create:
396 397 raise util.Abort(_('cannot create new http repository'))
397 398 if path.startswith('hg:'):
398 399 ui.warn(_("hg:// syntax is deprecated, please use http:// instead\n"))
399 400 path = 'http:' + path[3:]
400 401 if path.startswith('https:'):
401 402 return httpsrepository(ui, path)
402 403 return httprepository(ui, path)
General Comments 0
You need to be logged in to leave comments. Login now