mercurial/httprepo.py
author Wagner Bruna <wbruna@softwareexpress.com.br>
Fri, 10 Sep 2010 21:05:51 -0300
branchstable
changeset 12288 2ca3a845f640
parent 11757 65bd4b8e48bd
child 11759 05deba16c5d5
permissions -rw-r--r--
i18n-pt_BR: synchronized with 0f6164806283
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
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8206
diff changeset
     6
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9878
diff changeset
     7
# GNU General Public License version 2 or any later version.
0
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 _
11370
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
    11
import repo, changegroup, statichttprepo, error, url, util, pushkey
8312
b87a50b7125c separate import lines from mercurial and general python modules
Simon Heimberg <simohe@besonet.ch>
parents: 8225
diff changeset
    12
import os, urllib, urllib2, urlparse, zlib, httplib
b87a50b7125c separate import lines from mercurial and general python modules
Simon Heimberg <simohe@besonet.ch>
parents: 8225
diff changeset
    13
import errno, socket
9861
0262bb59016f support encoding fallback in branchmap to maintain compatibility with 1.3.x
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 9748
diff changeset
    14
import encoding
4678
a814a5b11fff Work around urllib2 digest auth bug with Python < 2.5
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4633
diff changeset
    15
3661
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
    16
def zgenerator(f):
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
    17
    zd = zlib.decompressobj()
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
    18
    try:
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
    19
        for chunk in util.filechunkiter(f):
11757
65bd4b8e48bd httprepo: decompress stream incrementally to reduce memory usage
Matt Mackall <mpm@selenic.com>
parents: 11370
diff changeset
    20
            while chunk:
65bd4b8e48bd httprepo: decompress stream incrementally to reduce memory usage
Matt Mackall <mpm@selenic.com>
parents: 11370
diff changeset
    21
                yield zd.decompress(chunk, 2**18)
65bd4b8e48bd httprepo: decompress stream incrementally to reduce memory usage
Matt Mackall <mpm@selenic.com>
parents: 11370
diff changeset
    22
                chunk = zd.unconsumed_tail
7280
810ca383da9c remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7279
diff changeset
    23
    except httplib.HTTPException:
3661
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
    24
        raise IOError(None, _('connection ended unexpectedly'))
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
    25
    yield zd.flush()
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
    26
6313
c5580db9c3aa remoterepo: no longer needed
Matt Mackall <mpm@selenic.com>
parents: 6294
diff changeset
    27
class httprepository(repo.repository):
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    28
    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
    29
        self.path = path
2442
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    30
        self.caps = None
4132
0d94e4a3ddb4 Close keepalive connections to fix server traceback
Andrei Vermel <avermel@mail.ru>
parents: 4025
diff changeset
    31
        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
    32
        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
    33
        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
    34
            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
    35
                             (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
    36
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    37
        # 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
    38
        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
    39
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    40
        self.ui = ui
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 8563
diff changeset
    41
        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
    42
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7269
diff changeset
    43
        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
    44
7752
998fc8f62539 close sockets on httprepository deletion (issue1487)
Steve Borho <steve@borho.org>
parents: 7641
diff changeset
    45
    def __del__(self):
998fc8f62539 close sockets on httprepository deletion (issue1487)
Steve Borho <steve@borho.org>
parents: 7641
diff changeset
    46
        for h in self.urlopener.handlers:
998fc8f62539 close sockets on httprepository deletion (issue1487)
Steve Borho <steve@borho.org>
parents: 7641
diff changeset
    47
            h.close()
998fc8f62539 close sockets on httprepository deletion (issue1487)
Steve Borho <steve@borho.org>
parents: 7641
diff changeset
    48
            if hasattr(h, "close_all"):
998fc8f62539 close sockets on httprepository deletion (issue1487)
Steve Borho <steve@borho.org>
parents: 7641
diff changeset
    49
                h.close_all()
998fc8f62539 close sockets on httprepository deletion (issue1487)
Steve Borho <steve@borho.org>
parents: 7641
diff changeset
    50
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
    51
    def url(self):
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
    52
        return self.path
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
    53
2442
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    54
    # look up capabilities only when needed
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    55
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    56
    def get_caps(self):
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    57
        if self.caps is None:
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    58
            try:
8150
bbc24c0753a0 util: use built-in set and frozenset
Martin Geisler <mg@lazybytes.net>
parents: 8055
diff changeset
    59
                self.caps = set(self.do_read('capabilities').split())
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7342
diff changeset
    60
            except error.RepoError:
8150
bbc24c0753a0 util: use built-in set and frozenset
Martin Geisler <mg@lazybytes.net>
parents: 8055
diff changeset
    61
                self.caps = set()
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 8563
diff changeset
    62
            self.ui.debug('capabilities: %s\n' %
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    63
                          (' '.join(self.caps or ['none'])))
2442
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    64
        return self.caps
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    65
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    66
    capabilities = property(get_caps)
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    67
1870
8a8ab47cccde make push over http print good error message.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1736
diff changeset
    68
    def lock(self):
8a8ab47cccde make push over http print good error message.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1736
diff changeset
    69
        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
    70
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    71
    def do_cmd(self, cmd, **args):
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    72
        data = args.pop('data', None)
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    73
        headers = args.pop('headers', {})
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 8563
diff changeset
    74
        self.ui.debug("sending %s command\n" % cmd)
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    75
        q = {"cmd": cmd}
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    76
        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
    77
        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
    78
        cu = "%s%s" % (self._url, qs)
10491
d7e582cab6b6 http: len(x) fails if it doesn't fit into an int, use __len__() instead
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    79
        req = urllib2.Request(cu, data, headers)
d7e582cab6b6 http: len(x) fails if it doesn't fit into an int, use __len__() instead
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    80
        if data is not None:
d7e582cab6b6 http: len(x) fails if it doesn't fit into an int, use __len__() instead
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    81
            # len(data) is broken if data doesn't fit into Py_ssize_t
d7e582cab6b6 http: len(x) fails if it doesn't fit into an int, use __len__() instead
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    82
            # add the header ourself to avoid OverflowError
d7e582cab6b6 http: len(x) fails if it doesn't fit into an int, use __len__() instead
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    83
            size = data.__len__()
d7e582cab6b6 http: len(x) fails if it doesn't fit into an int, use __len__() instead
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    84
            self.ui.debug("sending %s bytes\n" % size)
d7e582cab6b6 http: len(x) fails if it doesn't fit into an int, use __len__() instead
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    85
            req.add_unredirected_header('Content-Length', '%d' % size)
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
    86
        try:
10491
d7e582cab6b6 http: len(x) fails if it doesn't fit into an int, use __len__() instead
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    87
            resp = self.urlopener.open(req)
2467
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
    88
        except urllib2.HTTPError, inst:
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
    89
            if inst.code == 401:
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
    90
                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
    91
            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
    92
        except httplib.HTTPException, inst:
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 8563
diff changeset
    93
            self.ui.debug('http error while sending %s command\n' % cmd)
8206
cce63ef1045b ui: print_exc() -> traceback()
Matt Mackall <mpm@selenic.com>
parents: 8150
diff changeset
    94
            self.ui.traceback()
2336
f77edcffb837 http: print better error if exception happens.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2294
diff changeset
    95
            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
    96
        except IndexError:
5dbb3a991bbf Catch python2.3's IndexError with bogus http proxy settings. (issue203)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3223
diff changeset
    97
            # 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
    98
            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
    99
        # 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
   100
        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
   101
        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
   102
            resp_url = resp_url[:-len(qs)]
9881
54b518fc6671 httprepo: suppress the `real URL is...' message in safe, common cases.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9878
diff changeset
   103
        if self._url.rstrip('/') != resp_url.rstrip('/'):
3570
c141d07198b9 Inform the user about the new URL when being redirected via http.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3569
diff changeset
   104
            self.ui.status(_('real URL is %s\n') % resp_url)
10208
37c4ce51a12d httprepo: always store the response url (issue1968)
Steve Borho <steve@borho.org>
parents: 9881
diff changeset
   105
        self._url = resp_url
2435
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   106
        try:
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   107
            proto = resp.getheader('content-type')
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   108
        except AttributeError:
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   109
            proto = resp.headers['content-type']
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
   110
8053
976170068286 hide passwords in httprepo error messages
Steve Borho <steve@borho.org>
parents: 7752
diff changeset
   111
        safeurl = url.hidepassword(self._url)
753
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
   112
        # 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
   113
        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
   114
                proto.startswith('text/plain') or
ff7253a0d1da Cleanup of whitespace, indentation and line continuation.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4516
diff changeset
   115
                proto.startswith('application/hg-changegroup')):
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 8563
diff changeset
   116
            self.ui.debug("requested URL: '%s'\n" % url.hidepassword(cu))
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   117
            raise error.RepoError(
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   118
                _("'%s' does not appear to be an hg repository:\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   119
                  "---%%<--- (%s)\n%s\n---%%<---\n")
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   120
                % (safeurl, proto, resp.read()))
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
   121
4012
d1e31d7f7d44 fix handling of multiple Content-type headers
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3703
diff changeset
   122
        if proto.startswith('application/mercurial-'):
d1e31d7f7d44 fix handling of multiple Content-type headers
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3703
diff changeset
   123
            try:
4356
aed9e6dceb85 Avoid float rounding errors when checking http protocol version.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4226
diff changeset
   124
                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
   125
                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
   126
            except ValueError:
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7342
diff changeset
   127
                raise error.RepoError(_("'%s' sent a broken Content-Type "
8053
976170068286 hide passwords in httprepo error messages
Steve Borho <steve@borho.org>
parents: 7752
diff changeset
   128
                                        "header (%s)") % (safeurl, proto))
4356
aed9e6dceb85 Avoid float rounding errors when checking http protocol version.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4226
diff changeset
   129
            if version_info > (0, 1):
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7342
diff changeset
   130
                raise error.RepoError(_("'%s' uses newer protocol %s") %
8053
976170068286 hide passwords in httprepo error messages
Steve Borho <steve@borho.org>
parents: 7752
diff changeset
   131
                                      (safeurl, version))
753
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
   132
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
   133
        return resp
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   134
2435
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   135
    def do_read(self, cmd, **args):
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   136
        fp = self.do_cmd(cmd, **args)
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   137
        try:
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   138
            return fp.read()
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   139
        finally:
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   140
            # if using keepalive, allow connection to be reused
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   141
            fp.close()
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   142
3444
3505fcd5a231 Adding changegroupsubset and lookup to web protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3399
diff changeset
   143
    def lookup(self, key):
5259
65dc707606ed Push capability checking into protocol-level code.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5258
diff changeset
   144
        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
   145
        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
   146
        success, data = d[:-1].split(' ', 1)
233c733e4af5 httprepo: add support for passing lookup exception data
Matt Mackall <mpm@selenic.com>
parents: 3444
diff changeset
   147
        if int(success):
233c733e4af5 httprepo: add support for passing lookup exception data
Matt Mackall <mpm@selenic.com>
parents: 3444
diff changeset
   148
            return bin(data)
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7342
diff changeset
   149
        raise error.RepoError(data)
3444
3505fcd5a231 Adding changegroupsubset and lookup to web protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3399
diff changeset
   150
222
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
   151
    def heads(self):
2435
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   152
        d = self.do_read("heads")
222
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
   153
        try:
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
   154
            return map(bin, d[:-1].split(" "))
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
   155
        except:
7641
d2f753830f80 error: move UnexpectedOutput (now ResponseError)
Matt Mackall <mpm@selenic.com>
parents: 7637
diff changeset
   156
            raise error.ResponseError(_("unexpected response:"), d)
222
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
   157
8563
f8ff65a83169 named branches: client branchmap wire protocol support (issue736)
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8312
diff changeset
   158
    def branchmap(self):
f8ff65a83169 named branches: client branchmap wire protocol support (issue736)
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8312
diff changeset
   159
        d = self.do_read("branchmap")
f8ff65a83169 named branches: client branchmap wire protocol support (issue736)
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8312
diff changeset
   160
        try:
f8ff65a83169 named branches: client branchmap wire protocol support (issue736)
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8312
diff changeset
   161
            branchmap = {}
f8ff65a83169 named branches: client branchmap wire protocol support (issue736)
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8312
diff changeset
   162
            for branchpart in d.splitlines():
f8ff65a83169 named branches: client branchmap wire protocol support (issue736)
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8312
diff changeset
   163
                branchheads = branchpart.split(' ')
f8ff65a83169 named branches: client branchmap wire protocol support (issue736)
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8312
diff changeset
   164
                branchname = urllib.unquote(branchheads[0])
9878
7e7d56fe4833 branchmap: fix defective fallback fix 0262bb59016f
Sune Foldager <cryo@cyanite.org>
parents: 9861
diff changeset
   165
                # Earlier servers (1.3.x) send branch names in (their) local
7e7d56fe4833 branchmap: fix defective fallback fix 0262bb59016f
Sune Foldager <cryo@cyanite.org>
parents: 9861
diff changeset
   166
                # charset. The best we can do is assume it's identical to our
7e7d56fe4833 branchmap: fix defective fallback fix 0262bb59016f
Sune Foldager <cryo@cyanite.org>
parents: 9861
diff changeset
   167
                # own local charset, in case it's not utf-8.
9861
0262bb59016f support encoding fallback in branchmap to maintain compatibility with 1.3.x
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 9748
diff changeset
   168
                try:
9878
7e7d56fe4833 branchmap: fix defective fallback fix 0262bb59016f
Sune Foldager <cryo@cyanite.org>
parents: 9861
diff changeset
   169
                    branchname.decode('utf-8')
9861
0262bb59016f support encoding fallback in branchmap to maintain compatibility with 1.3.x
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 9748
diff changeset
   170
                except UnicodeDecodeError:
9878
7e7d56fe4833 branchmap: fix defective fallback fix 0262bb59016f
Sune Foldager <cryo@cyanite.org>
parents: 9861
diff changeset
   171
                    branchname = encoding.fromlocal(branchname)
8563
f8ff65a83169 named branches: client branchmap wire protocol support (issue736)
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8312
diff changeset
   172
                branchheads = [bin(x) for x in branchheads[1:]]
f8ff65a83169 named branches: client branchmap wire protocol support (issue736)
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8312
diff changeset
   173
                branchmap[branchname] = branchheads
f8ff65a83169 named branches: client branchmap wire protocol support (issue736)
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8312
diff changeset
   174
            return branchmap
f8ff65a83169 named branches: client branchmap wire protocol support (issue736)
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8312
diff changeset
   175
        except:
f8ff65a83169 named branches: client branchmap wire protocol support (issue736)
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8312
diff changeset
   176
            raise error.ResponseError(_("unexpected response:"), d)
f8ff65a83169 named branches: client branchmap wire protocol support (issue736)
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8312
diff changeset
   177
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   178
    def branches(self, nodes):
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   179
        n = " ".join(map(hex, nodes))
2435
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   180
        d = self.do_read("branches", nodes=n)
217
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   181
        try:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   182
            br = [tuple(map(bin, b.split(" "))) for b in d.splitlines()]
217
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   183
            return br
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   184
        except:
7641
d2f753830f80 error: move UnexpectedOutput (now ResponseError)
Matt Mackall <mpm@selenic.com>
parents: 7637
diff changeset
   185
            raise error.ResponseError(_("unexpected response:"), d)
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   186
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   187
    def between(self, pairs):
7342
1dcd2cc6878b protocol: avoid sending outrageously large between requests
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   188
        batch = 8 # avoid giant requests
1dcd2cc6878b protocol: avoid sending outrageously large between requests
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   189
        r = []
1dcd2cc6878b protocol: avoid sending outrageously large between requests
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   190
        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
   191
            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
   192
            d = self.do_read("between", pairs=n)
1dcd2cc6878b protocol: avoid sending outrageously large between requests
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   193
            try:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   194
                r += [l and map(bin, l.split(" ")) or []
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   195
                      for l in d.splitlines()]
7342
1dcd2cc6878b protocol: avoid sending outrageously large between requests
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   196
            except:
7641
d2f753830f80 error: move UnexpectedOutput (now ResponseError)
Matt Mackall <mpm@selenic.com>
parents: 7637
diff changeset
   197
                raise error.ResponseError(_("unexpected response:"), d)
7342
1dcd2cc6878b protocol: avoid sending outrageously large between requests
Matt Mackall <mpm@selenic.com>
parents: 7280
diff changeset
   198
        return r
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   199
1736
50de0887bbcd add preoutgoing and outgoing hooks.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1402
diff changeset
   200
    def changegroup(self, nodes, kind):
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   201
        n = " ".join(map(hex, nodes))
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
   202
        f = self.do_cmd("changegroup", roots=n)
3661
e99ba8726bda remove duplicate zgenerator in httprepo
Matt Mackall <mpm@selenic.com>
parents: 3614
diff changeset
   203
        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
   204
3505fcd5a231 Adding changegroupsubset and lookup to web protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3399
diff changeset
   205
    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
   206
        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
   207
        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
   208
        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
   209
        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
   210
        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
   211
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2435
diff changeset
   212
    def unbundle(self, cg, heads, source):
11153
9936ed1d04f4 push: document return values between various repo methods.
Greg Ward <greg-hg@gerg.ca>
parents: 10544
diff changeset
   213
        '''Send cg (a readable file-like object representing the
9936ed1d04f4 push: document return values between various repo methods.
Greg Ward <greg-hg@gerg.ca>
parents: 10544
diff changeset
   214
        changegroup to push, typically a chunkbuffer object) to the
9936ed1d04f4 push: document return values between various repo methods.
Greg Ward <greg-hg@gerg.ca>
parents: 10544
diff changeset
   215
        remote server as a bundle. Return an integer response code:
9936ed1d04f4 push: document return values between various repo methods.
Greg Ward <greg-hg@gerg.ca>
parents: 10544
diff changeset
   216
        non-zero indicates a successful push (see
9936ed1d04f4 push: document return values between various repo methods.
Greg Ward <greg-hg@gerg.ca>
parents: 10544
diff changeset
   217
        localrepository.addchangegroup()), and zero indicates either
9936ed1d04f4 push: document return values between various repo methods.
Greg Ward <greg-hg@gerg.ca>
parents: 10544
diff changeset
   218
        error or nothing to push.'''
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   219
        # 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
   220
        # http 1.1 chunked transfer.
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   221
3662
f4dc02d7fb71 unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents: 3661
diff changeset
   222
        type = ""
f4dc02d7fb71 unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents: 3661
diff changeset
   223
        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
   224
        # 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
   225
        # boolean capability
e674cae8efee fix push over HTTP to older servers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3662
diff changeset
   226
        try:
e674cae8efee fix push over HTTP to older servers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3662
diff changeset
   227
            types = types.split(',')
e674cae8efee fix push over HTTP to older servers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3662
diff changeset
   228
        except AttributeError:
e674cae8efee fix push over HTTP to older servers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3662
diff changeset
   229
            types = [""]
3662
f4dc02d7fb71 unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents: 3661
diff changeset
   230
        if types:
3703
e674cae8efee fix push over HTTP to older servers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3662
diff changeset
   231
            for x in types:
3662
f4dc02d7fb71 unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents: 3661
diff changeset
   232
                if x in changegroup.bundletypes:
f4dc02d7fb71 unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents: 3661
diff changeset
   233
                    type = x
f4dc02d7fb71 unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents: 3661
diff changeset
   234
                    break
3613
cbf352b9a3cd Client support for hgweb unbundle with versions.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3609
diff changeset
   235
3662
f4dc02d7fb71 unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents: 3661
diff changeset
   236
        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
   237
        fp = url.httpsendfile(tempname, "rb")
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   238
        try:
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   239
            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
   240
                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
   241
                     'unbundle', data=fp,
10526
72d3a02c62e6 httprepo: send Content-Type instead of content-type
Sune Foldager <cryo@cyanite.org>
parents: 10525
diff changeset
   242
                     headers={'Content-Type': 'application/mercurial-0.1'},
7010
9141bebefe3e enhance the error output in case of failure during http push
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6787
diff changeset
   243
                     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
   244
                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
   245
                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
   246
                    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
   247
                except ValueError, err:
7641
d2f753830f80 error: move UnexpectedOutput (now ResponseError)
Matt Mackall <mpm@selenic.com>
parents: 7637
diff changeset
   248
                    raise error.ResponseError(
7010
9141bebefe3e enhance the error output in case of failure during http push
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6787
diff changeset
   249
                            _('push failed (unexpected response):'), resp)
10544
2e1a9b811d13 httprepo: normalize output from unbundle with ssh
Sune Foldager <cryo@cyanite.org>
parents: 10526
diff changeset
   250
                for l in output.splitlines(True):
2e1a9b811d13 httprepo: normalize output from unbundle with ssh
Sune Foldager <cryo@cyanite.org>
parents: 10526
diff changeset
   251
                    self.ui.status(_('remote: '), l)
7010
9141bebefe3e enhance the error output in case of failure during http push
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6787
diff changeset
   252
                return ret
2467
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   253
            except socket.error, err:
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   254
                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
   255
                    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
   256
                raise util.Abort(err[1])
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   257
        finally:
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   258
            fp.close()
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   259
            os.unlink(tempname)
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2435
diff changeset
   260
2612
ffb895f16925 add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2569
diff changeset
   261
    def stream_out(self):
ffb895f16925 add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2569
diff changeset
   262
        return self.do_cmd('stream_out')
ffb895f16925 add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2569
diff changeset
   263
11370
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   264
    def pushkey(self, namespace, key, old, new):
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   265
        if not self.capable('pushkey'):
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   266
            return False
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   267
        d = self.do_cmd("pushkey", data="", # force a POST
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   268
                        namespace=namespace, key=key, old=old, new=new).read()
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   269
        code, output = d.split('\n', 1)
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   270
        try:
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   271
            ret = bool(int(code))
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   272
        except ValueError, err:
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   273
            raise error.ResponseError(
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   274
                _('push failed (unexpected response):'), d)
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   275
        for l in output.splitlines(True):
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   276
            self.ui.status(_('remote: '), l)
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   277
        return ret
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   278
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   279
    def listkeys(self, namespace):
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   280
        if not self.capable('pushkey'):
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   281
            return {}
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   282
        d = self.do_cmd("listkeys", namespace=namespace).read()
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   283
        r = {}
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   284
        for l in d.splitlines():
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   285
            k, v = l.split('\t')
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   286
            r[k.decode('string-escape')] = v.decode('string-escape')
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   287
        return r
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   288
923
c7a3b88505cd Add basic https support for pull
mpm@selenic.com
parents: 919
diff changeset
   289
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
   290
    def __init__(self, ui, path):
7279
1f0f84660dea Fix https availability checking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7270
diff changeset
   291
        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
   292
            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
   293
                               '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
   294
        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
   295
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
   296
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
   297
    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
   298
        raise util.Abort(_('cannot create new http repository'))
7211
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   299
    try:
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   300
        if path.startswith('https:'):
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   301
            inst = httpsrepository(ui, path)
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   302
        else:
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   303
            inst = httprepository(ui, path)
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   304
        inst.between([(nullid, nullid)])
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   305
        return inst
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7342
diff changeset
   306
    except error.RepoError:
7211
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   307
        ui.note('(falling back to static-http)\n')
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
   308
        return statichttprepo.instance(ui, "static-" + path, create)