httprepo.py
202 lines
| 7.1 KiB
| text/x-python
|
PythonLexer
/ mercurial / httprepo.py
|
r1089 | # httprepo.py - HTTP repository proxy classes for mercurial | ||
# | ||||
|
r2859 | # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> | ||
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> | ||||
|
r1089 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r1089 | |||
|
r7211 | from node import bin, hex, nullid | ||
|
r3891 | from i18n import _ | ||
|
r11587 | import repo, changegroup, statichttprepo, error, url, util, wireproto | ||
|
r8312 | import os, urllib, urllib2, urlparse, zlib, httplib | ||
import errno, socket | ||||
|
r9861 | import encoding | ||
|
r4678 | |||
|
r3661 | def zgenerator(f): | ||
zd = zlib.decompressobj() | ||||
try: | ||||
for chunk in util.filechunkiter(f): | ||||
|
r11757 | while chunk: | ||
yield zd.decompress(chunk, 2**18) | ||||
chunk = zd.unconsumed_tail | ||||
|
r7280 | except httplib.HTTPException: | ||
|
r3661 | raise IOError(None, _('connection ended unexpectedly')) | ||
yield zd.flush() | ||||
|
r11587 | class httprepository(wireproto.wirerepository): | ||
|
r1089 | def __init__(self, ui, path): | ||
|
r2673 | self.path = path | ||
|
r2442 | self.caps = None | ||
|
r4132 | self.handler = None | ||
|
r2337 | scheme, netloc, urlpath, query, frag = urlparse.urlsplit(path) | ||
if query or frag: | ||||
raise util.Abort(_('unsupported URL component: "%s"') % | ||||
(query or frag)) | ||||
# urllib cannot handle URLs with embedded user or passwd | ||||
|
r7270 | self._url, authinfo = url.getauthinfo(path) | ||
|
r1089 | self.ui = ui | ||
|
r9467 | self.ui.debug('using %s\n' % self._url) | ||
|
r2337 | |||
|
r7270 | self.urlopener = url.opener(ui, authinfo) | ||
|
r4516 | |||
|
r7752 | def __del__(self): | ||
for h in self.urlopener.handlers: | ||||
h.close() | ||||
if hasattr(h, "close_all"): | ||||
h.close_all() | ||||
|
r2673 | def url(self): | ||
return self.path | ||||
|
r2442 | # look up capabilities only when needed | ||
def get_caps(self): | ||||
if self.caps is None: | ||||
try: | ||||
|
r11589 | self.caps = set(self._call('capabilities').split()) | ||
|
r7637 | except error.RepoError: | ||
|
r8150 | self.caps = set() | ||
|
r9467 | self.ui.debug('capabilities: %s\n' % | ||
|
r2465 | (' '.join(self.caps or ['none']))) | ||
|
r2442 | return self.caps | ||
capabilities = property(get_caps) | ||||
|
r1870 | def lock(self): | ||
raise util.Abort(_('operation not supported over http')) | ||||
|
r11589 | def _callstream(self, cmd, **args): | ||
|
r2465 | data = args.pop('data', None) | ||
headers = args.pop('headers', {}) | ||||
|
r9467 | self.ui.debug("sending %s command\n" % cmd) | ||
|
r1089 | q = {"cmd": cmd} | ||
q.update(args) | ||||
|
r3562 | qs = '?%s' % urllib.urlencode(q) | ||
cu = "%s%s" % (self._url, qs) | ||||
|
r10491 | req = urllib2.Request(cu, data, headers) | ||
if data is not None: | ||||
# len(data) is broken if data doesn't fit into Py_ssize_t | ||||
# add the header ourself to avoid OverflowError | ||||
size = data.__len__() | ||||
self.ui.debug("sending %s bytes\n" % size) | ||||
req.add_unredirected_header('Content-Length', '%d' % size) | ||||
|
r2294 | try: | ||
|
r10491 | resp = self.urlopener.open(req) | ||
|
r2467 | except urllib2.HTTPError, inst: | ||
if inst.code == 401: | ||||
raise util.Abort(_('authorization failed')) | ||||
raise | ||||
|
r2294 | except httplib.HTTPException, inst: | ||
|
r9467 | self.ui.debug('http error while sending %s command\n' % cmd) | ||
|
r8206 | self.ui.traceback() | ||
|
r2336 | raise IOError(None, inst) | ||
|
r3399 | except IndexError: | ||
# this only happens with Python 2.3, later versions raise URLError | ||||
raise util.Abort(_('http error, possibly caused by proxy setting')) | ||||
|
r3562 | # record the url we got redirected to | ||
|
r3570 | resp_url = resp.geturl() | ||
if resp_url.endswith(qs): | ||||
resp_url = resp_url[:-len(qs)] | ||||
|
r9881 | if self._url.rstrip('/') != resp_url.rstrip('/'): | ||
|
r3570 | self.ui.status(_('real URL is %s\n') % resp_url) | ||
|
r10208 | self._url = resp_url | ||
|
r2435 | try: | ||
proto = resp.getheader('content-type') | ||||
except AttributeError: | ||||
proto = resp.headers['content-type'] | ||||
|
r1089 | |||
|
r8053 | safeurl = url.hidepassword(self._url) | ||
|
r1089 | # accept old "text/plain" and "application/hg-changegroup" for now | ||
|
r4633 | if not (proto.startswith('application/mercurial-') or | ||
proto.startswith('text/plain') or | ||||
proto.startswith('application/hg-changegroup')): | ||||
|
r9467 | self.ui.debug("requested URL: '%s'\n" % url.hidepassword(cu)) | ||
|
r10282 | raise error.RepoError( | ||
_("'%s' does not appear to be an hg repository:\n" | ||||
"---%%<--- (%s)\n%s\n---%%<---\n") | ||||
% (safeurl, proto, resp.read())) | ||||
|
r1089 | |||
|
r4012 | if proto.startswith('application/mercurial-'): | ||
try: | ||||
|
r4356 | version = proto.split('-', 1)[1] | ||
version_info = tuple([int(n) for n in version.split('.')]) | ||||
|
r4012 | except ValueError: | ||
|
r7637 | raise error.RepoError(_("'%s' sent a broken Content-Type " | ||
|
r8053 | "header (%s)") % (safeurl, proto)) | ||
|
r4356 | if version_info > (0, 1): | ||
|
r7637 | raise error.RepoError(_("'%s' uses newer protocol %s") % | ||
|
r8053 | (safeurl, version)) | ||
|
r1089 | |||
return resp | ||||
|
r11589 | def _call(self, cmd, **args): | ||
fp = self._callstream(cmd, **args) | ||||
|
r2435 | try: | ||
return fp.read() | ||||
finally: | ||||
# if using keepalive, allow connection to be reused | ||||
fp.close() | ||||
|
r11592 | def _callpush(self, cmd, cg, **args): | ||
|
r2465 | # have to stream bundle to a temp file because we do not have | ||
# http 1.1 chunked transfer. | ||||
|
r3662 | type = "" | ||
types = self.capable('unbundle') | ||||
|
r3703 | # servers older than d1b16a746db6 will send 'unbundle' as a | ||
# boolean capability | ||||
try: | ||||
types = types.split(',') | ||||
except AttributeError: | ||||
types = [""] | ||||
|
r3662 | if types: | ||
|
r3703 | for x in types: | ||
|
r3662 | if x in changegroup.bundletypes: | ||
type = x | ||||
break | ||||
|
r3613 | |||
|
r3662 | tempname = changegroup.writebundle(cg, None, type) | ||
|
r7270 | fp = url.httpsendfile(tempname, "rb") | ||
|
r11592 | headers = {'Content-Type': 'application/mercurial-0.1'} | ||
|
r2465 | try: | ||
try: | ||||
|
r11592 | r = self._call(cmd, data=fp, headers=headers, **args) | ||
return r.split('\n', 1) | ||||
|
r2467 | except socket.error, err: | ||
|
r11567 | if err.args[0] in (errno.ECONNRESET, errno.EPIPE): | ||
raise util.Abort(_('push failed: %s') % err.args[1]) | ||||
raise util.Abort(err.args[1]) | ||||
|
r2465 | finally: | ||
fp.close() | ||||
os.unlink(tempname) | ||||
|
r2439 | |||
|
r11592 | def _abort(self, exception): | ||
raise exception | ||||
|
r2612 | |||
|
r11592 | def _decompress(self, stream): | ||
return util.chunkbuffer(zgenerator(stream)) | ||||
|
r11370 | |||
|
r1089 | class httpsrepository(httprepository): | ||
|
r2569 | def __init__(self, ui, path): | ||
|
r7279 | if not url.has_https: | ||
|
r2569 | raise util.Abort(_('Python support for SSL and HTTPS ' | ||
'is not installed')) | ||||
httprepository.__init__(self, ui, path) | ||||
|
r2740 | |||
def instance(ui, path, create): | ||||
if create: | ||||
raise util.Abort(_('cannot create new http repository')) | ||||
|
r7211 | try: | ||
if path.startswith('https:'): | ||||
inst = httpsrepository(ui, path) | ||||
else: | ||||
inst = httprepository(ui, path) | ||||
inst.between([(nullid, nullid)]) | ||||
return inst | ||||
|
r7637 | except error.RepoError: | ||
|
r7211 | ui.note('(falling back to static-http)\n') | ||
return statichttprepo.instance(ui, "static-" + path, create) | ||||