mercurial/httprepo.py
author Matt Mackall <mpm@selenic.com>
Wed, 12 Nov 2008 19:11:09 -0600
changeset 7364 ad7f736f3214
parent 7342 1dcd2cc6878b
child 7637 1d54e2f6c0b7
permissions -rw-r--r--
help: remove redundant 'hg <command>' from command synopses
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1089
142b5d5ec9cc Break apart hg.py
mpm@selenic.com
parents: 1072
diff changeset
     1
# httprepo.py - HTTP repository proxy classes for mercurial
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     2
#
2859
345bac2bc4ec update copyrights.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2740
diff changeset
     3
# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
345bac2bc4ec update copyrights.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2740
diff changeset
     4
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     5
#
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     6
# This software may be used and distributed according to the terms
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     7
# of the GNU General Public License, incorporated herein by reference.
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     8
7211
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
     9
from node import bin, hex, nullid
3891
6b4127c7d52a Simplify i18n imports
Matt Mackall <mpm@selenic.com>
parents: 3877
diff changeset
    10
from i18n import _
5176
664a1c312972 fix-up references to repo.RepoError
Matt Mackall <mpm@selenic.com>
parents: 5123
diff changeset
    11
import repo, os, urllib, urllib2, urlparse, zlib, util, httplib
7280
810ca383da9c remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7279
diff changeset
    12
import errno, socket, changegroup, statichttprepo
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7269
diff changeset
    13
import url
4678
a814a5b11fff Work around urllib2 digest auth bug with Python < 2.5
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4633
diff changeset
    14
3661
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
    15
def zgenerator(f):
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
    16
    zd = zlib.decompressobj()
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
    17
    try:
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
    18
        for chunk in util.filechunkiter(f):
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
    19
            yield zd.decompress(chunk)
7280
810ca383da9c remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7279
diff changeset
    20
    except httplib.HTTPException:
3661
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
    21
        raise IOError(None, _('connection ended unexpectedly'))
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
    22
    yield zd.flush()
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
    23
6313
c5580db9c3aa remoterepo: no longer needed
Matt Mackall <mpm@selenic.com>
parents: 6294
diff changeset
    24
class httprepository(repo.repository):
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    25
    def __init__(self, ui, path):
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
    26
        self.path = path
2442
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    27
        self.caps = None
4132
0d94e4a3ddb4 Close keepalive connections to fix server traceback
Andrei Vermel <avermel@mail.ru>
parents: 4025
diff changeset
    28
        self.handler = None
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    29
        scheme, netloc, urlpath, query, frag = urlparse.urlsplit(path)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    30
        if query or frag:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    31
            raise util.Abort(_('unsupported URL component: "%s"') %
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    32
                             (query or frag))
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    33
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    34
        # urllib cannot handle URLs with embedded user or passwd
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7269
diff changeset
    35
        self._url, authinfo = url.getauthinfo(path)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7269
diff changeset
    36
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    37
        self.ui = ui
5066
167c422c745f httprepo: quote the path part of the URL
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4739
diff changeset
    38
        self.ui.debug(_('using %s\n') % self._url)
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    39
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7269
diff changeset
    40
        self.urlopener = url.opener(ui, authinfo)
4516
96d8a56d4ef9 Removed trailing whitespace and tabs from python files
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4369
diff changeset
    41
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
    42
    def url(self):
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
    43
        return self.path
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
    44
2442
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    45
    # look up capabilities only when needed
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    46
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    47
    def get_caps(self):
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    48
        if self.caps is None:
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    49
            try:
5258
b534c502bfb3 Turn capabilities into a mutable set, instead of a fixed tuple.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5196
diff changeset
    50
                self.caps = util.set(self.do_read('capabilities').split())
5176
664a1c312972 fix-up references to repo.RepoError
Matt Mackall <mpm@selenic.com>
parents: 5123
diff changeset
    51
            except repo.RepoError:
5258
b534c502bfb3 Turn capabilities into a mutable set, instead of a fixed tuple.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5196
diff changeset
    52
                self.caps = util.set()
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    53
            self.ui.debug(_('capabilities: %s\n') %
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    54
                          (' '.join(self.caps or ['none'])))
2442
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    55
        return self.caps
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    56
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    57
    capabilities = property(get_caps)
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    58
1870
8a8ab47cccde make push over http print good error message.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1736
diff changeset
    59
    def lock(self):
8a8ab47cccde make push over http print good error message.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1736
diff changeset
    60
        raise util.Abort(_('operation not supported over http'))
8a8ab47cccde make push over http print good error message.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1736
diff changeset
    61
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    62
    def do_cmd(self, cmd, **args):
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    63
        data = args.pop('data', None)
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    64
        headers = args.pop('headers', {})
1402
9d2c2e6b32b5 i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1400
diff changeset
    65
        self.ui.debug(_("sending %s command\n") % cmd)
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    66
        q = {"cmd": cmd}
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    67
        q.update(args)
3562
88b4755fa48f httprepo: record the url after a request, makes pull + redirect works
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3445
diff changeset
    68
        qs = '?%s' % urllib.urlencode(q)
88b4755fa48f httprepo: record the url after a request, makes pull + redirect works
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3445
diff changeset
    69
        cu = "%s%s" % (self._url, qs)
2294
ce67fa312f61 Catch urllib's HTTPException and give a meaningful error message to the user.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2281
diff changeset
    70
        try:
3567
3bab1fc0ab75 Turn bundle file into a string for http push, for urllib2 over proxies.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3566
diff changeset
    71
            if data:
5333
6e6a00a7bf75 Push over HTTP: really tell the user the size of the bundle
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5188
diff changeset
    72
                self.ui.debug(_("sending %s bytes\n") % len(data))
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7269
diff changeset
    73
            resp = self.urlopener.open(urllib2.Request(cu, data, headers))
2467
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
    74
        except urllib2.HTTPError, inst:
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
    75
            if inst.code == 401:
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
    76
                raise util.Abort(_('authorization failed'))
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
    77
            raise
2294
ce67fa312f61 Catch urllib's HTTPException and give a meaningful error message to the user.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2281
diff changeset
    78
        except httplib.HTTPException, inst:
2336
f77edcffb837 http: print better error if exception happens.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2294
diff changeset
    79
            self.ui.debug(_('http error while sending %s command\n') % cmd)
f77edcffb837 http: print better error if exception happens.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2294
diff changeset
    80
            self.ui.print_exc()
f77edcffb837 http: print better error if exception happens.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2294
diff changeset
    81
            raise IOError(None, inst)
3399
5dbb3a991bbf Catch python2.3's IndexError with bogus http proxy settings. (issue203)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3223
diff changeset
    82
        except IndexError:
5dbb3a991bbf Catch python2.3's IndexError with bogus http proxy settings. (issue203)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3223
diff changeset
    83
            # this only happens with Python 2.3, later versions raise URLError
5dbb3a991bbf Catch python2.3's IndexError with bogus http proxy settings. (issue203)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3223
diff changeset
    84
            raise util.Abort(_('http error, possibly caused by proxy setting'))
3562
88b4755fa48f httprepo: record the url after a request, makes pull + redirect works
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3445
diff changeset
    85
        # record the url we got redirected to
3570
c141d07198b9 Inform the user about the new URL when being redirected via http.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3569
diff changeset
    86
        resp_url = resp.geturl()
c141d07198b9 Inform the user about the new URL when being redirected via http.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3569
diff changeset
    87
        if resp_url.endswith(qs):
c141d07198b9 Inform the user about the new URL when being redirected via http.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3569
diff changeset
    88
            resp_url = resp_url[:-len(qs)]
c141d07198b9 Inform the user about the new URL when being redirected via http.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3569
diff changeset
    89
        if self._url != resp_url:
c141d07198b9 Inform the user about the new URL when being redirected via http.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3569
diff changeset
    90
            self.ui.status(_('real URL is %s\n') % resp_url)
c141d07198b9 Inform the user about the new URL when being redirected via http.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3569
diff changeset
    91
            self._url = resp_url
2435
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
    92
        try:
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
    93
            proto = resp.getheader('content-type')
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
    94
        except AttributeError:
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
    95
            proto = resp.headers['content-type']
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
    96
753
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
    97
        # accept old "text/plain" and "application/hg-changegroup" for now
4633
ff7253a0d1da Cleanup of whitespace, indentation and line continuation.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4516
diff changeset
    98
        if not (proto.startswith('application/mercurial-') or
ff7253a0d1da Cleanup of whitespace, indentation and line continuation.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4516
diff changeset
    99
                proto.startswith('text/plain') or
ff7253a0d1da Cleanup of whitespace, indentation and line continuation.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4516
diff changeset
   100
                proto.startswith('application/hg-changegroup')):
4739
1da35d1e7ef9 Added full URL to debug output if something doesn't look like an http hg repo.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4678
diff changeset
   101
            self.ui.debug(_("Requested URL: '%s'\n") % cu)
5176
664a1c312972 fix-up references to repo.RepoError
Matt Mackall <mpm@selenic.com>
parents: 5123
diff changeset
   102
            raise repo.RepoError(_("'%s' does not appear to be an hg repository")
4633
ff7253a0d1da Cleanup of whitespace, indentation and line continuation.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4516
diff changeset
   103
                               % self._url)
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
   104
4012
d1e31d7f7d44 fix handling of multiple Content-type headers
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3703
diff changeset
   105
        if proto.startswith('application/mercurial-'):
d1e31d7f7d44 fix handling of multiple Content-type headers
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3703
diff changeset
   106
            try:
4356
aed9e6dceb85 Avoid float rounding errors when checking http protocol version.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4226
diff changeset
   107
                version = proto.split('-', 1)[1]
aed9e6dceb85 Avoid float rounding errors when checking http protocol version.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4226
diff changeset
   108
                version_info = tuple([int(n) for n in version.split('.')])
4012
d1e31d7f7d44 fix handling of multiple Content-type headers
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3703
diff changeset
   109
            except ValueError:
5930
c301f15c965a send conservatively capitalized HTTP headers
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5915
diff changeset
   110
                raise repo.RepoError(_("'%s' sent a broken Content-Type "
4012
d1e31d7f7d44 fix handling of multiple Content-type headers
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3703
diff changeset
   111
                                     "header (%s)") % (self._url, proto))
4356
aed9e6dceb85 Avoid float rounding errors when checking http protocol version.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4226
diff changeset
   112
            if version_info > (0, 1):
5176
664a1c312972 fix-up references to repo.RepoError
Matt Mackall <mpm@selenic.com>
parents: 5123
diff changeset
   113
                raise repo.RepoError(_("'%s' uses newer protocol %s") %
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
   114
                                   (self._url, version))
753
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
   115
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
   116
        return resp
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   117
2435
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   118
    def do_read(self, cmd, **args):
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   119
        fp = self.do_cmd(cmd, **args)
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   120
        try:
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   121
            return fp.read()
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   122
        finally:
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   123
            # if using keepalive, allow connection to be reused
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   124
            fp.close()
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   125
3444
3505fcd5a231 Adding changegroupsubset and lookup to web protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3399
diff changeset
   126
    def lookup(self, key):
5259
65dc707606ed Push capability checking into protocol-level code.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5258
diff changeset
   127
        self.requirecap('lookup', _('look up remote revision'))
3445
233c733e4af5 httprepo: add support for passing lookup exception data
Matt Mackall <mpm@selenic.com>
parents: 3444
diff changeset
   128
        d = self.do_cmd("lookup", key = key).read()
233c733e4af5 httprepo: add support for passing lookup exception data
Matt Mackall <mpm@selenic.com>
parents: 3444
diff changeset
   129
        success, data = d[:-1].split(' ', 1)
233c733e4af5 httprepo: add support for passing lookup exception data
Matt Mackall <mpm@selenic.com>
parents: 3444
diff changeset
   130
        if int(success):
233c733e4af5 httprepo: add support for passing lookup exception data
Matt Mackall <mpm@selenic.com>
parents: 3444
diff changeset
   131
            return bin(data)
5176
664a1c312972 fix-up references to repo.RepoError
Matt Mackall <mpm@selenic.com>
parents: 5123
diff changeset
   132
        raise repo.RepoError(data)
3444
3505fcd5a231 Adding changegroupsubset and lookup to web protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3399
diff changeset
   133
222
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
   134
    def heads(self):
2435
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   135
        d = self.do_read("heads")
222
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
   136
        try:
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
   137
            return map(bin, d[:-1].split(" "))
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
   138
        except:
3565
9073d7366776 Use the new UnexpectedOutput exception in httprepo, too.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3562
diff changeset
   139
            raise util.UnexpectedOutput(_("unexpected response:"), d)
222
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
   140
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   141
    def branches(self, nodes):
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   142
        n = " ".join(map(hex, nodes))
2435
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   143
        d = self.do_read("branches", nodes=n)
217
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   144
        try:
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   145
            br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ]
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   146
            return br
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   147
        except:
3565
9073d7366776 Use the new UnexpectedOutput exception in httprepo, too.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3562
diff changeset
   148
            raise util.UnexpectedOutput(_("unexpected response:"), d)
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   149
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   150
    def between(self, pairs):
7342
1dcd2cc6878b protocol: avoid sending outrageously large between requests
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   151
        batch = 8 # avoid giant requests
1dcd2cc6878b protocol: avoid sending outrageously large between requests
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   152
        r = []
1dcd2cc6878b protocol: avoid sending outrageously large between requests
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   153
        for i in xrange(0, len(pairs), batch):
1dcd2cc6878b protocol: avoid sending outrageously large between requests
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   154
            n = " ".join(["-".join(map(hex, p)) for p in pairs[i:i + batch]])
1dcd2cc6878b protocol: avoid sending outrageously large between requests
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   155
            d = self.do_read("between", pairs=n)
1dcd2cc6878b protocol: avoid sending outrageously large between requests
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   156
            try:
1dcd2cc6878b protocol: avoid sending outrageously large between requests
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   157
                r += [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ]
1dcd2cc6878b protocol: avoid sending outrageously large between requests
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   158
            except:
1dcd2cc6878b protocol: avoid sending outrageously large between requests
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   159
                raise util.UnexpectedOutput(_("unexpected response:"), d)
1dcd2cc6878b protocol: avoid sending outrageously large between requests
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   160
        return r
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   161
1736
50de0887bbcd add preoutgoing and outgoing hooks.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1402
diff changeset
   162
    def changegroup(self, nodes, kind):
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   163
        n = " ".join(map(hex, nodes))
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
   164
        f = self.do_cmd("changegroup", roots=n)
3661
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
   165
        return util.chunkbuffer(zgenerator(f))
3444
3505fcd5a231 Adding changegroupsubset and lookup to web protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3399
diff changeset
   166
3505fcd5a231 Adding changegroupsubset and lookup to web protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3399
diff changeset
   167
    def changegroupsubset(self, bases, heads, source):
5259
65dc707606ed Push capability checking into protocol-level code.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5258
diff changeset
   168
        self.requirecap('changegroupsubset', _('look up remote changes'))
3444
3505fcd5a231 Adding changegroupsubset and lookup to web protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3399
diff changeset
   169
        baselst = " ".join([hex(n) for n in bases])
3505fcd5a231 Adding changegroupsubset and lookup to web protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3399
diff changeset
   170
        headlst = " ".join([hex(n) for n in heads])
3505fcd5a231 Adding changegroupsubset and lookup to web protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3399
diff changeset
   171
        f = self.do_cmd("changegroupsubset", bases=baselst, heads=headlst)
3661
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
   172
        return util.chunkbuffer(zgenerator(f))
635
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   173
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2435
diff changeset
   174
    def unbundle(self, cg, heads, source):
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   175
        # have to stream bundle to a temp file because we do not have
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   176
        # http 1.1 chunked transfer.
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   177
3662
f4dc02d7fb71 unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents: 3661
diff changeset
   178
        type = ""
f4dc02d7fb71 unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents: 3661
diff changeset
   179
        types = self.capable('unbundle')
3703
e674cae8efee fix push over HTTP to older servers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3662
diff changeset
   180
        # servers older than d1b16a746db6 will send 'unbundle' as a
e674cae8efee fix push over HTTP to older servers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3662
diff changeset
   181
        # boolean capability
e674cae8efee fix push over HTTP to older servers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3662
diff changeset
   182
        try:
e674cae8efee fix push over HTTP to older servers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3662
diff changeset
   183
            types = types.split(',')
e674cae8efee fix push over HTTP to older servers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3662
diff changeset
   184
        except AttributeError:
e674cae8efee fix push over HTTP to older servers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3662
diff changeset
   185
            types = [""]
3662
f4dc02d7fb71 unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents: 3661
diff changeset
   186
        if types:
3703
e674cae8efee fix push over HTTP to older servers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3662
diff changeset
   187
            for x in types:
3662
f4dc02d7fb71 unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents: 3661
diff changeset
   188
                if x in changegroup.bundletypes:
f4dc02d7fb71 unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents: 3661
diff changeset
   189
                    type = x
f4dc02d7fb71 unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents: 3661
diff changeset
   190
                    break
3613
cbf352b9a3cd Client support for hgweb unbundle with versions.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3609
diff changeset
   191
3662
f4dc02d7fb71 unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents: 3661
diff changeset
   192
        tempname = changegroup.writebundle(cg, None, type)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7269
diff changeset
   193
        fp = url.httpsendfile(tempname, "rb")
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   194
        try:
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   195
            try:
7010
9141bebefe3e enhance the error output in case of failure during http push
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6787
diff changeset
   196
                resp = self.do_read(
9141bebefe3e enhance the error output in case of failure during http push
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6787
diff changeset
   197
                     'unbundle', data=fp,
9141bebefe3e enhance the error output in case of failure during http push
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6787
diff changeset
   198
                     headers={'Content-Type': 'application/octet-stream'},
9141bebefe3e enhance the error output in case of failure during http push
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6787
diff changeset
   199
                     heads=' '.join(map(hex, heads)))
9141bebefe3e enhance the error output in case of failure during http push
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6787
diff changeset
   200
                resp_code, output = resp.split('\n', 1)
2467
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   201
                try:
7010
9141bebefe3e enhance the error output in case of failure during http push
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6787
diff changeset
   202
                    ret = int(resp_code)
9141bebefe3e enhance the error output in case of failure during http push
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6787
diff changeset
   203
                except ValueError, err:
9141bebefe3e enhance the error output in case of failure during http push
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6787
diff changeset
   204
                    raise util.UnexpectedOutput(
9141bebefe3e enhance the error output in case of failure during http push
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6787
diff changeset
   205
                            _('push failed (unexpected response):'), resp)
9141bebefe3e enhance the error output in case of failure during http push
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6787
diff changeset
   206
                self.ui.write(output)
9141bebefe3e enhance the error output in case of failure during http push
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6787
diff changeset
   207
                return ret
2467
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   208
            except socket.error, err:
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   209
                if err[0] in (errno.ECONNRESET, errno.EPIPE):
3072
bc3fe3b5b785 Never apply string formatting to generated errors with util.Abort.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2859
diff changeset
   210
                    raise util.Abort(_('push failed: %s') % err[1])
2467
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   211
                raise util.Abort(err[1])
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   212
        finally:
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   213
            fp.close()
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   214
            os.unlink(tempname)
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2435
diff changeset
   215
2612
ffb895f16925 add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2569
diff changeset
   216
    def stream_out(self):
ffb895f16925 add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2569
diff changeset
   217
        return self.do_cmd('stream_out')
ffb895f16925 add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2569
diff changeset
   218
923
c7a3b88505cd Add basic https support for pull
mpm@selenic.com
parents: 919
diff changeset
   219
class httpsrepository(httprepository):
2569
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   220
    def __init__(self, ui, path):
7279
1f0f84660dea Fix https availability checking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7270
diff changeset
   221
        if not url.has_https:
2569
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   222
            raise util.Abort(_('Python support for SSL and HTTPS '
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   223
                               'is not installed'))
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   224
        httprepository.__init__(self, ui, path)
2740
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
   225
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
   226
def instance(ui, path, create):
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
   227
    if create:
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
   228
        raise util.Abort(_('cannot create new http repository'))
7211
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   229
    try:
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   230
        if path.startswith('https:'):
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   231
            inst = httpsrepository(ui, path)
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   232
        else:
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   233
            inst = httprepository(ui, path)
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   234
        inst.between([(nullid, nullid)])
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   235
        return inst
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   236
    except repo.RepoError:
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   237
        ui.note('(falling back to static-http)\n')
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   238
        return statichttprepo.instance(ui, "static-" + path, create)