mercurial/httppeer.py
author Augie Fackler <augie@google.com>
Sun, 06 Oct 2019 09:45:02 -0400
changeset 43076 2372284d9457
parent 42853 37debb6771f5
child 43077 687b865b95ad
permissions -rw-r--r--
formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17192
1ac628cd7113 peer: introduce real peer classes
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 15246
diff changeset
     1
# httppeer.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
25954
7bbdb78d2842 httppeer: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
     9
from __future__ import absolute_import
7bbdb78d2842 httppeer: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    10
7bbdb78d2842 httppeer: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    11
import errno
33821
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    12
import io
25954
7bbdb78d2842 httppeer: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    13
import os
7bbdb78d2842 httppeer: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    14
import socket
30763
a520aefb96f1 httppeer: advertise and support application/mercurial-0.2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30759
diff changeset
    15
import struct
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
    16
import weakref
25954
7bbdb78d2842 httppeer: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    17
7bbdb78d2842 httppeer: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    18
from .i18n import _
7bbdb78d2842 httppeer: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    19
from . import (
28666
ae53ecc47414 bundle: move writebundle() from changegroup.py to bundle2.py (API)
Martin von Zweigbergk <martinvonz@google.com>
parents: 28530
diff changeset
    20
    bundle2,
25954
7bbdb78d2842 httppeer: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    21
    error,
7bbdb78d2842 httppeer: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    22
    httpconnection,
30924
48dea083f66d py3: convert the mode argument of os.fdopen to unicodes (1 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30763
diff changeset
    23
    pycompat,
25954
7bbdb78d2842 httppeer: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    24
    statichttprepo,
36959
43815d87c6aa httppeer: alias url as urlmod
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36835
diff changeset
    25
    url as urlmod,
25954
7bbdb78d2842 httppeer: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    26
    util,
37483
61e405fb6372 wireproto: crude support for version 2 HTTP peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37411
diff changeset
    27
    wireprotoframing,
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
    28
    wireprototypes,
37614
a81d02ea65db wireproto: move version 1 peer functionality to standalone module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37611
diff changeset
    29
    wireprotov1peer,
37719
a656cba08a04 wireprotov2: move response handling out of httppeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37717
diff changeset
    30
    wireprotov2peer,
37545
93397c4633f6 wireproto: extract HTTP version 2 code to own module
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37544
diff changeset
    31
    wireprotov2server,
25954
7bbdb78d2842 httppeer: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    32
)
42814
2c4f656c8e9f interfaceutil: move to interfaces/
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42813
diff changeset
    33
from .interfaces import (
2c4f656c8e9f interfaceutil: move to interfaces/
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42813
diff changeset
    34
    repository,
2c4f656c8e9f interfaceutil: move to interfaces/
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42813
diff changeset
    35
    util as interfaceutil,
2c4f656c8e9f interfaceutil: move to interfaces/
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42813
diff changeset
    36
)
37810
856f381ad74b interfaceutil: module to stub out zope.interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37738
diff changeset
    37
from .utils import (
39445
cdb56f295b03 httppeer: use our CBOR decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39437
diff changeset
    38
    cborutil,
39436
3fe028b657bf httppeer: log commands for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38783
diff changeset
    39
    stringutil,
37810
856f381ad74b interfaceutil: module to stub out zope.interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37738
diff changeset
    40
)
4678
a814a5b11fff Work around urllib2 digest auth bug with Python < 2.5
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4633
diff changeset
    41
29455
0c741fd6158a py3: conditionalize httplib import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29241
diff changeset
    42
httplib = util.httplib
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28666
diff changeset
    43
urlerr = util.urlerr
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28666
diff changeset
    44
urlreq = util.urlreq
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28666
diff changeset
    45
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
    46
30759
3f5f0c98cd18 httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30564
diff changeset
    47
def encodevalueinheaders(value, header, limit):
3f5f0c98cd18 httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30564
diff changeset
    48
    """Encode a string value into multiple HTTP headers.
3f5f0c98cd18 httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30564
diff changeset
    49
3f5f0c98cd18 httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30564
diff changeset
    50
    ``value`` will be encoded into 1 or more HTTP headers with the names
3f5f0c98cd18 httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30564
diff changeset
    51
    ``header-<N>`` where ``<N>`` is an integer starting at 1. Each header
3f5f0c98cd18 httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30564
diff changeset
    52
    name + value will be at most ``limit`` bytes long.
3f5f0c98cd18 httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30564
diff changeset
    53
34732
67e9678efd98 httppeer: always produce native str header keys and values
Augie Fackler <augie@google.com>
parents: 34725
diff changeset
    54
    Returns an iterable of 2-tuples consisting of header names and
67e9678efd98 httppeer: always produce native str header keys and values
Augie Fackler <augie@google.com>
parents: 34725
diff changeset
    55
    values as native strings.
30759
3f5f0c98cd18 httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30564
diff changeset
    56
    """
34732
67e9678efd98 httppeer: always produce native str header keys and values
Augie Fackler <augie@google.com>
parents: 34725
diff changeset
    57
    # HTTP Headers are ASCII. Python 3 requires them to be unicodes,
67e9678efd98 httppeer: always produce native str header keys and values
Augie Fackler <augie@google.com>
parents: 34725
diff changeset
    58
    # not bytes. This function always takes bytes in as arguments.
67e9678efd98 httppeer: always produce native str header keys and values
Augie Fackler <augie@google.com>
parents: 34725
diff changeset
    59
    fmt = pycompat.strurl(header) + r'-%s'
67e9678efd98 httppeer: always produce native str header keys and values
Augie Fackler <augie@google.com>
parents: 34725
diff changeset
    60
    # Note: it is *NOT* a bug that the last bit here is a bytestring
67e9678efd98 httppeer: always produce native str header keys and values
Augie Fackler <augie@google.com>
parents: 34725
diff changeset
    61
    # and not a unicode: we're just getting the encoded length anyway,
67e9678efd98 httppeer: always produce native str header keys and values
Augie Fackler <augie@google.com>
parents: 34725
diff changeset
    62
    # and using an r-string to make it portable between Python 2 and 3
67e9678efd98 httppeer: always produce native str header keys and values
Augie Fackler <augie@google.com>
parents: 34725
diff changeset
    63
    # doesn't work because then the \r is a literal backslash-r
67e9678efd98 httppeer: always produce native str header keys and values
Augie Fackler <augie@google.com>
parents: 34725
diff changeset
    64
    # instead of a carriage return.
67e9678efd98 httppeer: always produce native str header keys and values
Augie Fackler <augie@google.com>
parents: 34725
diff changeset
    65
    valuelen = limit - len(fmt % r'000') - len(': \r\n')
30759
3f5f0c98cd18 httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30564
diff changeset
    66
    result = []
3f5f0c98cd18 httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30564
diff changeset
    67
3f5f0c98cd18 httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30564
diff changeset
    68
    n = 0
38783
e7aa113b14f7 global: use pycompat.xrange()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38502
diff changeset
    69
    for i in pycompat.xrange(0, len(value), valuelen):
30759
3f5f0c98cd18 httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30564
diff changeset
    70
        n += 1
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
    71
        result.append((fmt % str(n), pycompat.strurl(value[i : i + valuelen])))
30759
3f5f0c98cd18 httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30564
diff changeset
    72
3f5f0c98cd18 httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30564
diff changeset
    73
    return result
3f5f0c98cd18 httppeer: extract code for HTTP header spanning
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30564
diff changeset
    74
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
    75
33821
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    76
class _multifile(object):
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    77
    def __init__(self, *fileobjs):
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    78
        for f in fileobjs:
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    79
            if not util.safehasattr(f, 'length'):
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    80
                raise ValueError(
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    81
                    '_multifile only supports file objects that '
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
    82
                    'have a length but this one does not:',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
    83
                    type(f),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
    84
                    f,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
    85
                )
33821
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    86
        self._fileobjs = fileobjs
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    87
        self._index = 0
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    88
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    89
    @property
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    90
    def length(self):
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    91
        return sum(f.length for f in self._fileobjs)
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    92
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    93
    def read(self, amt=None):
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    94
        if amt <= 0:
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    95
            return ''.join(f.read() for f in self._fileobjs)
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    96
        parts = []
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    97
        while amt and self._index < len(self._fileobjs):
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    98
            parts.append(self._fileobjs[self._index].read(amt))
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
    99
            got = len(parts[-1])
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
   100
            if got < amt:
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
   101
                self._index += 1
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
   102
            amt -= got
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
   103
        return ''.join(parts)
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
   104
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
   105
    def seek(self, offset, whence=os.SEEK_SET):
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
   106
        if whence != os.SEEK_SET:
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
   107
            raise NotImplementedError(
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
   108
                '_multifile does not support anything other'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   109
                ' than os.SEEK_SET for whence on seek()'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   110
            )
33821
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
   111
        if offset != 0:
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
   112
            raise NotImplementedError(
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
   113
                '_multifile only supports seeking to start, but that '
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   114
                'could be fixed if you need it'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   115
            )
33821
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
   116
        for f in self._fileobjs:
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
   117
            f.seek(0)
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
   118
        self._index = 0
3c91cc0c5fde httppeer: add support for httppostargs when we're sending a file
Augie Fackler <augie@google.com>
parents: 33806
diff changeset
   119
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   120
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   121
def makev1commandrequest(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   122
    ui, requestbuilder, caps, capablefn, repobaseurl, cmd, args
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   123
):
37549
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   124
    """Make an HTTP request to run a command for a version 1 client.
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   125
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   126
    ``caps`` is a set of known server capabilities. The value may be
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   127
    None if capabilities are not yet known.
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   128
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   129
    ``capablefn`` is a function to evaluate a capability.
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   130
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   131
    ``cmd``, ``args``, and ``data`` define the command, its arguments, and
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   132
    raw data to pass to it.
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   133
    """
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   134
    if cmd == 'pushkey':
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   135
        args['data'] = ''
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   136
    data = args.pop('data', None)
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   137
    headers = args.pop('headers', {})
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   138
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   139
    ui.debug("sending %s command\n" % cmd)
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   140
    q = [('cmd', cmd)]
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   141
    headersize = 0
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   142
    # Important: don't use self.capable() here or else you end up
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   143
    # with infinite recursion when trying to look up capabilities
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   144
    # for the first time.
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   145
    postargsok = caps is not None and 'httppostargs' in caps
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   146
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   147
    # Send arguments via POST.
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   148
    if postargsok and args:
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   149
        strargs = urlreq.urlencode(sorted(args.items()))
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   150
        if not data:
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   151
            data = strargs
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   152
        else:
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   153
            if isinstance(data, bytes):
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   154
                i = io.BytesIO(data)
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   155
                i.length = len(data)
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   156
                data = i
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   157
            argsio = io.BytesIO(strargs)
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   158
            argsio.length = len(strargs)
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   159
            data = _multifile(argsio, data)
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   160
        headers[r'X-HgArgs-Post'] = len(strargs)
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   161
    elif args:
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   162
        # Calling self.capable() can infinite loop if we are calling
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   163
        # "capabilities". But that command should never accept wire
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   164
        # protocol arguments. So this should never happen.
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   165
        assert cmd != 'capabilities'
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   166
        httpheader = capablefn('httpheader')
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   167
        if httpheader:
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   168
            headersize = int(httpheader.split(',', 1)[0])
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   169
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   170
        # Send arguments via HTTP headers.
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   171
        if headersize > 0:
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   172
            # The headers can typically carry more data than the URL.
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   173
            encargs = urlreq.urlencode(sorted(args.items()))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   174
            for header, value in encodevalueinheaders(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   175
                encargs, 'X-HgArg', headersize
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   176
            ):
37549
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   177
                headers[header] = value
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   178
        # Send arguments via query string (Mercurial <1.9).
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   179
        else:
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   180
            q += sorted(args.items())
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   181
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   182
    qs = '?%s' % urlreq.urlencode(q)
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   183
    cu = "%s%s" % (repobaseurl, qs)
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   184
    size = 0
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   185
    if util.safehasattr(data, 'length'):
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   186
        size = data.length
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   187
    elif data is not None:
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   188
        size = len(data)
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   189
    if data is not None and r'Content-Type' not in headers:
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   190
        headers[r'Content-Type'] = r'application/mercurial-0.1'
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   191
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   192
    # Tell the server we accept application/mercurial-0.2 and multiple
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   193
    # compression formats if the server is capable of emitting those
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   194
    # payloads.
37556
b77aa48ba690 httppeer: only advertise partial-pull if capabilities are known
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37555
diff changeset
   195
    # Note: Keep this set empty by default, as client advertisement of
b77aa48ba690 httppeer: only advertise partial-pull if capabilities are known
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37555
diff changeset
   196
    # protocol parameters should only occur after the handshake.
b77aa48ba690 httppeer: only advertise partial-pull if capabilities are known
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37555
diff changeset
   197
    protoparams = set()
37549
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   198
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   199
    mediatypes = set()
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   200
    if caps is not None:
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   201
        mt = capablefn('httpmediatype')
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   202
        if mt:
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   203
            protoparams.add('0.1')
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   204
            mediatypes = set(mt.split(','))
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   205
37556
b77aa48ba690 httppeer: only advertise partial-pull if capabilities are known
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37555
diff changeset
   206
        protoparams.add('partial-pull')
b77aa48ba690 httppeer: only advertise partial-pull if capabilities are known
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37555
diff changeset
   207
37549
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   208
    if '0.2tx' in mediatypes:
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   209
        protoparams.add('0.2')
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   210
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   211
    if '0.2tx' in mediatypes and capablefn('compression'):
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   212
        # We /could/ compare supported compression formats and prune
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   213
        # non-mutually supported or error if nothing is mutually supported.
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   214
        # For now, send the full list to the server and have it error.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   215
        comps = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   216
            e.wireprotosupport().name
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   217
            for e in util.compengines.supportedwireengines(util.CLIENTROLE)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   218
        ]
37549
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   219
        protoparams.add('comp=%s' % ','.join(comps))
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   220
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   221
    if protoparams:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   222
        protoheaders = encodevalueinheaders(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   223
            ' '.join(sorted(protoparams)), 'X-HgProto', headersize or 1024
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   224
        )
37549
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   225
        for header, value in protoheaders:
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   226
            headers[header] = value
37555
930c433eb311 httppeer: always add x-hg* headers to Vary header
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37554
diff changeset
   227
930c433eb311 httppeer: always add x-hg* headers to Vary header
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37554
diff changeset
   228
    varyheaders = []
930c433eb311 httppeer: always add x-hg* headers to Vary header
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37554
diff changeset
   229
    for header in headers:
930c433eb311 httppeer: always add x-hg* headers to Vary header
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37554
diff changeset
   230
        if header.lower().startswith(r'x-hg'):
37549
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   231
            varyheaders.append(header)
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   232
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   233
    if varyheaders:
37555
930c433eb311 httppeer: always add x-hg* headers to Vary header
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37554
diff changeset
   234
        headers[r'Vary'] = r','.join(sorted(varyheaders))
37549
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   235
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   236
    req = requestbuilder(pycompat.strurl(cu), data, headers)
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   237
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   238
    if data is not None:
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   239
        ui.debug("sending %d bytes\n" % size)
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   240
        req.add_unredirected_header(r'Content-Length', r'%d' % size)
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   241
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   242
    return req, cu, qs
66d1001e1500 httppeer: extract code for creating a request into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37548
diff changeset
   243
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   244
37738
a1f785148097 httppeer: work around API differences on urllib Request objects
Augie Fackler <augie@google.com>
parents: 37737
diff changeset
   245
def _reqdata(req):
a1f785148097 httppeer: work around API differences on urllib Request objects
Augie Fackler <augie@google.com>
parents: 37737
diff changeset
   246
    """Get request data, if any. If no data, returns None."""
a1f785148097 httppeer: work around API differences on urllib Request objects
Augie Fackler <augie@google.com>
parents: 37737
diff changeset
   247
    if pycompat.ispy3:
a1f785148097 httppeer: work around API differences on urllib Request objects
Augie Fackler <augie@google.com>
parents: 37737
diff changeset
   248
        return req.data
a1f785148097 httppeer: work around API differences on urllib Request objects
Augie Fackler <augie@google.com>
parents: 37737
diff changeset
   249
    if not req.has_data():
a1f785148097 httppeer: work around API differences on urllib Request objects
Augie Fackler <augie@google.com>
parents: 37737
diff changeset
   250
        return None
a1f785148097 httppeer: work around API differences on urllib Request objects
Augie Fackler <augie@google.com>
parents: 37737
diff changeset
   251
    return req.get_data()
a1f785148097 httppeer: work around API differences on urllib Request objects
Augie Fackler <augie@google.com>
parents: 37737
diff changeset
   252
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   253
37548
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   254
def sendrequest(ui, opener, req):
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   255
    """Send a prepared HTTP request.
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   256
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   257
    Returns the response object.
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   258
    """
38033
13b2812cff2b httppeer: declare 'dbg' at the function level
Boris Feld <boris.feld@octobus.net>
parents: 38032
diff changeset
   259
    dbg = ui.debug
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   260
    if ui.debugflag and ui.configbool('devel', 'debug.peer-request'):
37548
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   261
        line = 'devel-peer-request: %s\n'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   262
        dbg(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   263
            line
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   264
            % '%s %s'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   265
            % (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   266
                pycompat.bytesurl(req.get_method()),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   267
                pycompat.bytesurl(req.get_full_url()),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   268
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   269
        )
37548
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   270
        hgargssize = None
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   271
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   272
        for header, value in sorted(req.header_items()):
37737
6cb7e3b91883 httppeer: no matter what Python 3 might think, http headers are bytes
Augie Fackler <augie@google.com>
parents: 37736
diff changeset
   273
            header = pycompat.bytesurl(header)
6cb7e3b91883 httppeer: no matter what Python 3 might think, http headers are bytes
Augie Fackler <augie@google.com>
parents: 37736
diff changeset
   274
            value = pycompat.bytesurl(value)
37548
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   275
            if header.startswith('X-hgarg-'):
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   276
                if hgargssize is None:
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   277
                    hgargssize = 0
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   278
                hgargssize += len(value)
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   279
            else:
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   280
                dbg(line % '  %s %s' % (header, value))
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   281
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   282
        if hgargssize is not None:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   283
            dbg(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   284
                line
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   285
                % '  %d bytes of commands arguments in headers'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   286
                % hgargssize
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   287
            )
37738
a1f785148097 httppeer: work around API differences on urllib Request objects
Augie Fackler <augie@google.com>
parents: 37737
diff changeset
   288
        data = _reqdata(req)
a1f785148097 httppeer: work around API differences on urllib Request objects
Augie Fackler <augie@google.com>
parents: 37737
diff changeset
   289
        if data is not None:
37548
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   290
            length = getattr(data, 'length', None)
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   291
            if length is None:
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   292
                length = len(data)
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   293
            dbg(line % '  %d bytes of data' % length)
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   294
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   295
        start = util.timer()
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   296
38502
2279d90eed9a httppeer: fix use of uninitialized variable with devel logging
Martin von Zweigbergk <martinvonz@google.com>
parents: 38239
diff changeset
   297
    res = None
37550
b5862ee01abe httppeer: move error handling and response wrapping into sendrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37549
diff changeset
   298
    try:
b5862ee01abe httppeer: move error handling and response wrapping into sendrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37549
diff changeset
   299
        res = opener.open(req)
b5862ee01abe httppeer: move error handling and response wrapping into sendrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37549
diff changeset
   300
    except urlerr.httperror as inst:
b5862ee01abe httppeer: move error handling and response wrapping into sendrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37549
diff changeset
   301
        if inst.code == 401:
b5862ee01abe httppeer: move error handling and response wrapping into sendrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37549
diff changeset
   302
            raise error.Abort(_('authorization failed'))
b5862ee01abe httppeer: move error handling and response wrapping into sendrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37549
diff changeset
   303
        raise
b5862ee01abe httppeer: move error handling and response wrapping into sendrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37549
diff changeset
   304
    except httplib.HTTPException as inst:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   305
        ui.debug(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   306
            'http error requesting %s\n' % util.hidepassword(req.get_full_url())
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   307
        )
37550
b5862ee01abe httppeer: move error handling and response wrapping into sendrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37549
diff changeset
   308
        ui.traceback()
b5862ee01abe httppeer: move error handling and response wrapping into sendrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37549
diff changeset
   309
        raise IOError(None, inst)
b5862ee01abe httppeer: move error handling and response wrapping into sendrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37549
diff changeset
   310
    finally:
38032
f9dc1d5b676b httppeer: properly gate debug usage behind debug flag check
Boris Feld <boris.feld@octobus.net>
parents: 37832
diff changeset
   311
        if ui.debugflag and ui.configbool('devel', 'debug.peer-request'):
38502
2279d90eed9a httppeer: fix use of uninitialized variable with devel logging
Martin von Zweigbergk <martinvonz@google.com>
parents: 38239
diff changeset
   312
            code = res.code if res else -1
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   313
            dbg(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   314
                line
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   315
                % '  finished in %.4f seconds (%d)'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   316
                % (util.timer() - start, code)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   317
            )
37550
b5862ee01abe httppeer: move error handling and response wrapping into sendrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37549
diff changeset
   318
b5862ee01abe httppeer: move error handling and response wrapping into sendrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37549
diff changeset
   319
    # Insert error handlers for common I/O failures.
40018
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39630
diff changeset
   320
    urlmod.wrapresponse(res)
37548
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   321
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   322
    return res
8e7a4435ab6d httppeer: extract code for performing an HTTP request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37547
diff changeset
   323
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   324
37832
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   325
class RedirectedRepoError(error.RepoError):
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   326
    def __init__(self, msg, respurl):
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   327
        super(RedirectedRepoError, self).__init__(msg)
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   328
        self.respurl = respurl
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   329
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   330
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   331
def parsev1commandresponse(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   332
    ui, baseurl, requrl, qs, resp, compressible, allowcbor=False
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   333
):
37551
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   334
    # record the url we got redirected to
37832
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   335
    redirected = False
37551
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   336
    respurl = pycompat.bytesurl(resp.geturl())
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   337
    if respurl.endswith(qs):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   338
        respurl = respurl[: -len(qs)]
37832
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   339
        qsdropped = False
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   340
    else:
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   341
        qsdropped = True
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   342
37551
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   343
    if baseurl.rstrip('/') != respurl.rstrip('/'):
37832
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   344
        redirected = True
37551
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   345
        if not ui.quiet:
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   346
            ui.warn(_('real URL is %s\n') % respurl)
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   347
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   348
    try:
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   349
        proto = pycompat.bytesurl(resp.getheader(r'content-type', r''))
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   350
    except AttributeError:
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   351
        proto = pycompat.bytesurl(resp.headers.get(r'content-type', r''))
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   352
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   353
    safeurl = util.hidepassword(baseurl)
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   354
    if proto.startswith('application/hg-error'):
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   355
        raise error.OutOfBandError(resp.read())
37554
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   356
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   357
    # Pre 1.0 versions of Mercurial used text/plain and
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   358
    # application/hg-changegroup. We don't support such old servers.
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   359
    if not proto.startswith('application/mercurial-'):
37551
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   360
        ui.debug("requested URL: '%s'\n" % util.hidepassword(requrl))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   361
        msg = _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   362
            "'%s' does not appear to be an hg repository:\n"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   363
            "---%%<--- (%s)\n%s\n---%%<---\n"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   364
        ) % (safeurl, proto or 'no content-type', resp.read(1024))
37832
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   365
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   366
        # Some servers may strip the query string from the redirect. We
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   367
        # raise a special error type so callers can react to this specially.
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   368
        if redirected and qsdropped:
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   369
            raise RedirectedRepoError(msg, respurl)
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   370
        else:
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   371
            raise error.RepoError(msg)
37551
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   372
37554
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   373
    try:
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   374
        subtype = proto.split('-', 1)[1]
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   375
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   376
        # Unless we end up supporting CBOR in the legacy wire protocol,
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   377
        # this should ONLY be encountered for the initial capabilities
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   378
        # request during handshake.
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   379
        if subtype == 'cbor':
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   380
            if allowcbor:
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   381
                return respurl, proto, resp
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   382
            else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   383
                raise error.RepoError(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   384
                    _('unexpected CBOR response from ' 'server')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   385
                )
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   386
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   387
        version_info = tuple([int(n) for n in subtype.split('.')])
37554
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   388
    except ValueError:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   389
        raise error.RepoError(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   390
            _("'%s' sent a broken Content-Type " "header (%s)")
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   391
            % (safeurl, proto)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   392
        )
37551
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   393
37554
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   394
    # TODO consider switching to a decompression reader that uses
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   395
    # generators.
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   396
    if version_info == (0, 1):
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   397
        if compressible:
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   398
            resp = util.compengines['zlib'].decompressorreader(resp)
37551
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   399
37554
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   400
    elif version_info == (0, 2):
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   401
        # application/mercurial-0.2 always identifies the compression
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   402
        # engine in the payload header.
39484
98995b689e03 httppeer: use util.readexactly() to abort on incomplete responses
Anton Shestakov <av6@dwimlabs.net>
parents: 39483
diff changeset
   403
        elen = struct.unpack('B', util.readexactly(resp, 1))[0]
98995b689e03 httppeer: use util.readexactly() to abort on incomplete responses
Anton Shestakov <av6@dwimlabs.net>
parents: 39483
diff changeset
   404
        ename = util.readexactly(resp, elen)
37554
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   405
        engine = util.compengines.forwiretype(ename)
37551
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   406
37554
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   407
        resp = engine.decompressorreader(resp)
301a1d2e8016 httppeer: don't accept very old media types (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37553
diff changeset
   408
    else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   409
        raise error.RepoError(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   410
            _("'%s' uses newer protocol %s") % (safeurl, subtype)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   411
        )
37551
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   412
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   413
    return respurl, proto, resp
37551
946eb204ba67 httppeer: extract common response handling into own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37550
diff changeset
   414
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   415
37614
a81d02ea65db wireproto: move version 1 peer functionality to standalone module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37611
diff changeset
   416
class httppeer(wireprotov1peer.wirepeer):
37552
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
   417
    def __init__(self, ui, path, url, opener, requestbuilder, caps):
37321
e826fe7a08c7 peer: make ui an attribute
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37320
diff changeset
   418
        self.ui = ui
33705
73fd395ee29e httppeer: make several instance attributes internal (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33704
diff changeset
   419
        self._path = path
37006
8e89c2bec1f7 httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36961
diff changeset
   420
        self._url = url
37552
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
   421
        self._caps = caps
42158
69921d02daaf peer: introduce a limitedarguments attributes
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 41315
diff changeset
   422
        self.limitedarguments = caps is not None and 'httppostargs' not in caps
37006
8e89c2bec1f7 httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36961
diff changeset
   423
        self._urlopener = opener
37547
835ccc2a5ef1 httppeer: move requestbuilder defaults into makepeer() argument
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37546
diff changeset
   424
        self._requestbuilder = requestbuilder
4516
96d8a56d4ef9 Removed trailing whitespace and tabs from python files
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4369
diff changeset
   425
7752
998fc8f62539 close sockets on httprepository deletion (issue1487)
Steve Borho <steve@borho.org>
parents: 7641
diff changeset
   426
    def __del__(self):
37006
8e89c2bec1f7 httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36961
diff changeset
   427
        for h in self._urlopener.handlers:
8e89c2bec1f7 httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36961
diff changeset
   428
            h.close()
8e89c2bec1f7 httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36961
diff changeset
   429
            getattr(h, "close_all", lambda: None)()
7752
998fc8f62539 close sockets on httprepository deletion (issue1487)
Steve Borho <steve@borho.org>
parents: 7641
diff changeset
   430
37320
39f7d4ee8bcd repository: port peer interfaces to zope.interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37006
diff changeset
   431
    # Begin of ipeerconnection interface.
33805
f913e90f15a0 httppeer: use peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33705
diff changeset
   432
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
   433
    def url(self):
33705
73fd395ee29e httppeer: make several instance attributes internal (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33704
diff changeset
   434
        return self._path
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
   435
33805
f913e90f15a0 httppeer: use peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33705
diff changeset
   436
    def local(self):
f913e90f15a0 httppeer: use peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33705
diff changeset
   437
        return None
f913e90f15a0 httppeer: use peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33705
diff changeset
   438
f913e90f15a0 httppeer: use peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33705
diff changeset
   439
    def peer(self):
f913e90f15a0 httppeer: use peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33705
diff changeset
   440
        return self
f913e90f15a0 httppeer: use peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33705
diff changeset
   441
f913e90f15a0 httppeer: use peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33705
diff changeset
   442
    def canpush(self):
f913e90f15a0 httppeer: use peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33705
diff changeset
   443
        return True
2442
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   444
33805
f913e90f15a0 httppeer: use peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33705
diff changeset
   445
    def close(self):
40426
588f1e9a4d16 http: work around custom http client classes that refuse extra attrs
Augie Fackler <augie@google.com>
parents: 40171
diff changeset
   446
        try:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   447
            reqs, sent, recv = (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   448
                self._urlopener.requestscount,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   449
                self._urlopener.sentbytescount,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   450
                self._urlopener.receivedbytescount,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   451
            )
40426
588f1e9a4d16 http: work around custom http client classes that refuse extra attrs
Augie Fackler <augie@google.com>
parents: 40171
diff changeset
   452
        except AttributeError:
588f1e9a4d16 http: work around custom http client classes that refuse extra attrs
Augie Fackler <augie@google.com>
parents: 40171
diff changeset
   453
            return
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   454
        self.ui.note(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   455
            _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   456
                '(sent %d HTTP requests and %d bytes; '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   457
                'received %d bytes in responses)\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   458
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   459
            % (reqs, sent, recv)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   460
        )
13603
395a84f78736 httprepo: use caps instead of between for compat check
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 13555
diff changeset
   461
37320
39f7d4ee8bcd repository: port peer interfaces to zope.interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37006
diff changeset
   462
    # End of ipeerconnection interface.
33805
f913e90f15a0 httppeer: use peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33705
diff changeset
   463
37320
39f7d4ee8bcd repository: port peer interfaces to zope.interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37006
diff changeset
   464
    # Begin of ipeercommands interface.
33805
f913e90f15a0 httppeer: use peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33705
diff changeset
   465
f913e90f15a0 httppeer: use peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33705
diff changeset
   466
    def capabilities(self):
33705
73fd395ee29e httppeer: make several instance attributes internal (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33704
diff changeset
   467
        return self._caps
2442
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   468
37320
39f7d4ee8bcd repository: port peer interfaces to zope.interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37006
diff changeset
   469
    # End of ipeercommands interface.
33805
f913e90f15a0 httppeer: use peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33705
diff changeset
   470
30464
e16e234b9ca3 httppeer: do decompression inside _callstream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30241
diff changeset
   471
    def _callstream(self, cmd, _compressible=False, **args):
35359
98bc4c43f570 py3: handle keyword arguments correctly in httppeer.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35197
diff changeset
   472
        args = pycompat.byteskwargs(args)
36218
e4ccd7a69f77 httppeer: change logic around argument handling
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35884
diff changeset
   473
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   474
        req, cu, qs = makev1commandrequest(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   475
            self.ui,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   476
            self._requestbuilder,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   477
            self._caps,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   478
            self.capable,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   479
            self._url,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   480
            cmd,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   481
            args,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   482
        )
30763
a520aefb96f1 httppeer: advertise and support application/mercurial-0.2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30759
diff changeset
   483
37550
b5862ee01abe httppeer: move error handling and response wrapping into sendrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37549
diff changeset
   484
        resp = sendrequest(self.ui, self._urlopener, req)
32002
bf855efe5664 httppeer: wrap HTTPResponse.read() globally
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30924
diff changeset
   485
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   486
        self._url, ct, resp = parsev1commandresponse(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   487
            self.ui, self._url, cu, qs, resp, _compressible
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   488
        )
30464
e16e234b9ca3 httppeer: do decompression inside _callstream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30241
diff changeset
   489
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
   490
        return resp
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   491
11589
e8d22fe2ddab protocol: clean up call-like functions in http and ssh clients
Matt Mackall <mpm@selenic.com>
parents: 11588
diff changeset
   492
    def _call(self, cmd, **args):
e8d22fe2ddab protocol: clean up call-like functions in http and ssh clients
Matt Mackall <mpm@selenic.com>
parents: 11588
diff changeset
   493
        fp = self._callstream(cmd, **args)
2435
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   494
        try:
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   495
            return fp.read()
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   496
        finally:
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   497
            # if using keepalive, allow connection to be reused
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   498
            fp.close()
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   499
11592
26e0782b8380 protocol: unify client unbundle support
Matt Mackall <mpm@selenic.com>
parents: 11591
diff changeset
   500
    def _callpush(self, cmd, cg, **args):
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   501
        # 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
   502
        # http 1.1 chunked transfer.
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   503
3662
f4dc02d7fb71 unduplicate bundle writing code from httprepo
Matt Mackall <mpm@selenic.com>
parents: 3661
diff changeset
   504
        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
   505
        try:
e674cae8efee fix push over HTTP to older servers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3662
diff changeset
   506
            types = types.split(',')
e674cae8efee fix push over HTTP to older servers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3662
diff changeset
   507
        except AttributeError:
14060
aaa9a5989405 bundle: more comments about the different header types, remove useless if
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13819
diff changeset
   508
            # servers older than d1b16a746db6 will send 'unbundle' as a
aaa9a5989405 bundle: more comments about the different header types, remove useless if
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13819
diff changeset
   509
            # boolean capability. They only support headerless/uncompressed
aaa9a5989405 bundle: more comments about the different header types, remove useless if
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13819
diff changeset
   510
            # bundles.
3703
e674cae8efee fix push over HTTP to older servers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3662
diff changeset
   511
            types = [""]
14060
aaa9a5989405 bundle: more comments about the different header types, remove useless if
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13819
diff changeset
   512
        for x in types:
28666
ae53ecc47414 bundle: move writebundle() from changegroup.py to bundle2.py (API)
Martin von Zweigbergk <martinvonz@google.com>
parents: 28530
diff changeset
   513
            if x in bundle2.bundletypes:
14060
aaa9a5989405 bundle: more comments about the different header types, remove useless if
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13819
diff changeset
   514
                type = x
aaa9a5989405 bundle: more comments about the different header types, remove useless if
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13819
diff changeset
   515
                break
3613
cbf352b9a3cd Client support for hgweb unbundle with versions.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3609
diff changeset
   516
28666
ae53ecc47414 bundle: move writebundle() from changegroup.py to bundle2.py (API)
Martin von Zweigbergk <martinvonz@google.com>
parents: 28530
diff changeset
   517
        tempname = bundle2.writebundle(self.ui, cg, None, type)
14244
e7525a555a64 url: use new http support if requested by the user
Augie Fackler <durin42@gmail.com>
parents: 14149
diff changeset
   518
        fp = httpconnection.httpsendfile(self.ui, tempname, "rb")
36297
a59ff82154b8 httppeer: headers are native strings
Augie Fackler <augie@google.com>
parents: 36270
diff changeset
   519
        headers = {r'Content-Type': r'application/mercurial-0.1'}
11592
26e0782b8380 protocol: unify client unbundle support
Matt Mackall <mpm@selenic.com>
parents: 11591
diff changeset
   520
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   521
        try:
25085
e05734cd7902 httppeer: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 23895
diff changeset
   522
            r = self._call(cmd, data=fp, headers=headers, **args)
e05734cd7902 httppeer: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 23895
diff changeset
   523
            vals = r.split('\n', 1)
e05734cd7902 httppeer: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 23895
diff changeset
   524
            if len(vals) < 2:
e05734cd7902 httppeer: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 23895
diff changeset
   525
                raise error.ResponseError(_("unexpected response:"), r)
e05734cd7902 httppeer: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 23895
diff changeset
   526
            return vals
36430
efebfa9b4cab httppeer: explicitly catch urlerr.httperror and re-raise
Augie Fackler <augie@google.com>
parents: 36426
diff changeset
   527
        except urlerr.httperror:
efebfa9b4cab httppeer: explicitly catch urlerr.httperror and re-raise
Augie Fackler <augie@google.com>
parents: 36426
diff changeset
   528
            # Catch and re-raise these so we don't try and treat them
efebfa9b4cab httppeer: explicitly catch urlerr.httperror and re-raise
Augie Fackler <augie@google.com>
parents: 36426
diff changeset
   529
            # like generic socket errors. They lack any values in
efebfa9b4cab httppeer: explicitly catch urlerr.httperror and re-raise
Augie Fackler <augie@google.com>
parents: 36426
diff changeset
   530
            # .args on Python 3 which breaks our socket.error block.
efebfa9b4cab httppeer: explicitly catch urlerr.httperror and re-raise
Augie Fackler <augie@google.com>
parents: 36426
diff changeset
   531
            raise
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25500
diff changeset
   532
        except socket.error as err:
25085
e05734cd7902 httppeer: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 23895
diff changeset
   533
            if err.args[0] in (errno.ECONNRESET, errno.EPIPE):
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25954
diff changeset
   534
                raise error.Abort(_('push failed: %s') % err.args[1])
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25954
diff changeset
   535
            raise error.Abort(err.args[1])
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   536
        finally:
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   537
            fp.close()
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   538
            os.unlink(tempname)
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2435
diff changeset
   539
21074
f8a0d82b0463 httppeer: support for _calltwowaystream
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20905
diff changeset
   540
    def _calltwowaystream(self, cmd, fp, **args):
f8a0d82b0463 httppeer: support for _calltwowaystream
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20905
diff changeset
   541
        filename = None
f8a0d82b0463 httppeer: support for _calltwowaystream
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20905
diff changeset
   542
        try:
f8a0d82b0463 httppeer: support for _calltwowaystream
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20905
diff changeset
   543
            # dump bundle to disk
38164
aac4be30e250 py3: wrap tempfile.mkstemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents: 37832
diff changeset
   544
            fd, filename = pycompat.mkstemp(prefix="hg-bundle-", suffix=".hg")
42852
58f73e9ccfff httppeer: use context manager when writing temporary bundle to send
Martin von Zweigbergk <martinvonz@google.com>
parents: 42814
diff changeset
   545
            with os.fdopen(fd, r"wb") as fh:
21074
f8a0d82b0463 httppeer: support for _calltwowaystream
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20905
diff changeset
   546
                d = fp.read(4096)
42852
58f73e9ccfff httppeer: use context manager when writing temporary bundle to send
Martin von Zweigbergk <martinvonz@google.com>
parents: 42814
diff changeset
   547
                while d:
58f73e9ccfff httppeer: use context manager when writing temporary bundle to send
Martin von Zweigbergk <martinvonz@google.com>
parents: 42814
diff changeset
   548
                    fh.write(d)
58f73e9ccfff httppeer: use context manager when writing temporary bundle to send
Martin von Zweigbergk <martinvonz@google.com>
parents: 42814
diff changeset
   549
                    d = fp.read(4096)
21074
f8a0d82b0463 httppeer: support for _calltwowaystream
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20905
diff changeset
   550
            # start http push
42853
37debb6771f5 httppeer: use context manager when reading temporary bundle to send
Martin von Zweigbergk <martinvonz@google.com>
parents: 42852
diff changeset
   551
            with httpconnection.httpsendfile(self.ui, filename, "rb") as fp_:
37debb6771f5 httppeer: use context manager when reading temporary bundle to send
Martin von Zweigbergk <martinvonz@google.com>
parents: 42852
diff changeset
   552
                headers = {r'Content-Type': r'application/mercurial-0.1'}
37debb6771f5 httppeer: use context manager when reading temporary bundle to send
Martin von Zweigbergk <martinvonz@google.com>
parents: 42852
diff changeset
   553
                return self._callstream(cmd, data=fp_, headers=headers, **args)
21074
f8a0d82b0463 httppeer: support for _calltwowaystream
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20905
diff changeset
   554
        finally:
42852
58f73e9ccfff httppeer: use context manager when writing temporary bundle to send
Martin von Zweigbergk <martinvonz@google.com>
parents: 42814
diff changeset
   555
            if filename is not None:
21074
f8a0d82b0463 httppeer: support for _calltwowaystream
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20905
diff changeset
   556
                os.unlink(filename)
f8a0d82b0463 httppeer: support for _calltwowaystream
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20905
diff changeset
   557
20905
167047ba3cfa wireproto: drop the _decompress method in favor a new call type
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 18742
diff changeset
   558
    def _callcompressable(self, cmd, **args):
30464
e16e234b9ca3 httppeer: do decompression inside _callstream
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30241
diff changeset
   559
        return self._callstream(cmd, _compressible=True, **args)
11370
db3f6f0e4e7d pushkey: add http support
Matt Mackall <mpm@selenic.com>
parents: 11153
diff changeset
   560
21188
d36440d84328 httppeer: reintroduce _abort that accidentally was removed in 167047ba3cfa
Mads Kiilerich <madski@unity3d.com>
parents: 21074
diff changeset
   561
    def _abort(self, exception):
d36440d84328 httppeer: reintroduce _abort that accidentally was removed in 167047ba3cfa
Mads Kiilerich <madski@unity3d.com>
parents: 21074
diff changeset
   562
        raise exception
d36440d84328 httppeer: reintroduce _abort that accidentally was removed in 167047ba3cfa
Mads Kiilerich <madski@unity3d.com>
parents: 21074
diff changeset
   563
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   564
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   565
def sendv2request(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   566
    ui, opener, requestbuilder, apiurl, permission, requests, redirect
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   567
):
40133
762ef19a07e3 wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   568
    wireprotoframing.populatestreamencoders()
762ef19a07e3 wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   569
762ef19a07e3 wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   570
    uiencoders = ui.configlist(b'experimental', b'httppeer.v2-encoder-order')
762ef19a07e3 wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   571
762ef19a07e3 wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   572
    if uiencoders:
762ef19a07e3 wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   573
        encoders = []
762ef19a07e3 wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   574
762ef19a07e3 wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   575
        for encoder in uiencoders:
762ef19a07e3 wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   576
            if encoder not in wireprotoframing.STREAM_ENCODERS:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   577
                ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   578
                    _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   579
                        b'wire protocol version 2 encoder referenced in '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   580
                        b'config (%s) is not known; ignoring\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   581
                    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   582
                    % encoder
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   583
                )
40133
762ef19a07e3 wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   584
            else:
762ef19a07e3 wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   585
                encoders.append(encoder)
762ef19a07e3 wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   586
762ef19a07e3 wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   587
    else:
762ef19a07e3 wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   588
        encoders = wireprotoframing.STREAM_ENCODERS_ORDER
762ef19a07e3 wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40129
diff changeset
   589
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   590
    reactor = wireprotoframing.clientreactor(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   591
        ui,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   592
        hasmultiplesend=False,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   593
        buffersends=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   594
        clientcontentencoders=encoders,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   595
    )
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   596
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   597
    handler = wireprotov2peer.clienthandler(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   598
        ui, reactor, opener=opener, requestbuilder=requestbuilder
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   599
    )
37719
a656cba08a04 wireprotov2: move response handling out of httppeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37717
diff changeset
   600
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   601
    url = '%s/%s' % (apiurl, permission)
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   602
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   603
    if len(requests) > 1:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   604
        url += '/multirequest'
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   605
    else:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   606
        url += '/%s' % requests[0][0]
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   607
39436
3fe028b657bf httppeer: log commands for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38783
diff changeset
   608
    ui.debug('sending %d commands\n' % len(requests))
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   609
    for command, args, f in requests:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   610
        ui.debug(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   611
            'sending command %s: %s\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   612
            % (command, stringutil.pprint(args, indent=2))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   613
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   614
        assert not list(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   615
            handler.callcommand(command, args, f, redirect=redirect)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   616
        )
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   617
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   618
    # TODO stream this.
37719
a656cba08a04 wireprotov2: move response handling out of httppeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37717
diff changeset
   619
    body = b''.join(map(bytes, handler.flushcommands()))
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   620
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   621
    # TODO modify user-agent to reflect v2
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   622
    headers = {
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   623
        r'Accept': wireprotov2server.FRAMINGTYPE,
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   624
        r'Content-Type': wireprotov2server.FRAMINGTYPE,
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   625
    }
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   626
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   627
    req = requestbuilder(pycompat.strurl(url), body, headers)
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   628
    req.add_unredirected_header(r'Content-Length', r'%d' % len(body))
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   629
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   630
    try:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   631
        res = opener.open(req)
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   632
    except urlerr.httperror as e:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   633
        if e.code == 401:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   634
            raise error.Abort(_('authorization failed'))
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   635
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   636
        raise
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   637
    except httplib.HTTPException as e:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   638
        ui.traceback()
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   639
        raise IOError(None, e)
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   640
37719
a656cba08a04 wireprotov2: move response handling out of httppeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37717
diff changeset
   641
    return handler, res
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   642
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   643
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   644
class queuedcommandfuture(pycompat.futures.Future):
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   645
    """Wraps result() on command futures to trigger submission on call."""
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   646
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   647
    def result(self, timeout=None):
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   648
        if self.done():
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   649
            return pycompat.futures.Future.result(self, timeout)
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   650
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   651
        self._peerexecutor.sendcommands()
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   652
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   653
        # sendcommands() will restore the original __class__ and self.result
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   654
        # will resolve to Future.result.
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   655
        return self.result(timeout)
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   656
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   657
37810
856f381ad74b interfaceutil: module to stub out zope.interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37738
diff changeset
   658
@interfaceutil.implementer(repository.ipeercommandexecutor)
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   659
class httpv2executor(object):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   660
    def __init__(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   661
        self, ui, opener, requestbuilder, apiurl, descriptor, redirect
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   662
    ):
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   663
        self._ui = ui
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   664
        self._opener = opener
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   665
        self._requestbuilder = requestbuilder
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   666
        self._apiurl = apiurl
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   667
        self._descriptor = descriptor
40024
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40019
diff changeset
   668
        self._redirect = redirect
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   669
        self._sent = False
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   670
        self._closed = False
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   671
        self._neededpermissions = set()
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   672
        self._calls = []
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   673
        self._futures = weakref.WeakSet()
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   674
        self._responseexecutor = None
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   675
        self._responsef = None
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   676
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   677
    def __enter__(self):
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   678
        return self
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   679
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   680
    def __exit__(self, exctype, excvalue, exctb):
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   681
        self.close()
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   682
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   683
    def callcommand(self, command, args):
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   684
        if self._sent:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   685
            raise error.ProgrammingError(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   686
                'callcommand() cannot be used after ' 'commands are sent'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   687
            )
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   688
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   689
        if self._closed:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   690
            raise error.ProgrammingError(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   691
                'callcommand() cannot be used after ' 'close()'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   692
            )
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   693
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   694
        # The service advertises which commands are available. So if we attempt
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   695
        # to call an unknown command or pass an unknown argument, we can screen
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   696
        # for this.
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   697
        if command not in self._descriptor['commands']:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   698
            raise error.ProgrammingError(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   699
                'wire protocol command %s is not available' % command
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   700
            )
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   701
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   702
        cmdinfo = self._descriptor['commands'][command]
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   703
        unknownargs = set(args.keys()) - set(cmdinfo.get('args', {}))
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   704
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   705
        if unknownargs:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   706
            raise error.ProgrammingError(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   707
                'wire protocol command %s does not accept argument: %s'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   708
                % (command, ', '.join(sorted(unknownargs)))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   709
            )
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   710
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   711
        self._neededpermissions |= set(cmdinfo['permissions'])
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   712
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   713
        # TODO we /could/ also validate types here, since the API descriptor
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   714
        # includes types...
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   715
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   716
        f = pycompat.futures.Future()
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   717
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   718
        # Monkeypatch it so result() triggers sendcommands(), otherwise result()
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   719
        # could deadlock.
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   720
        f.__class__ = queuedcommandfuture
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   721
        f._peerexecutor = self
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   722
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   723
        self._futures.add(f)
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   724
        self._calls.append((command, args, f))
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   725
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   726
        return f
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   727
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   728
    def sendcommands(self):
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   729
        if self._sent:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   730
            return
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   731
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   732
        if not self._calls:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   733
            return
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   734
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   735
        self._sent = True
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   736
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   737
        # Unhack any future types so caller sees a clean type and so we
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   738
        # break reference cycle.
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   739
        for f in self._futures:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   740
            if isinstance(f, queuedcommandfuture):
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   741
                f.__class__ = pycompat.futures.Future
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   742
                f._peerexecutor = None
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   743
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   744
        # Mark the future as running and filter out cancelled futures.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   745
        calls = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   746
            (command, args, f)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   747
            for command, args, f in self._calls
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   748
            if f.set_running_or_notify_cancel()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   749
        ]
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   750
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   751
        # Clear out references, prevent improper object usage.
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   752
        self._calls = None
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   753
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   754
        if not calls:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   755
            return
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   756
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   757
        permissions = set(self._neededpermissions)
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   758
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   759
        if 'push' in permissions and 'pull' in permissions:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   760
            permissions.remove('pull')
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   761
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   762
        if len(permissions) > 1:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   763
            raise error.RepoError(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   764
                _('cannot make request requiring multiple ' 'permissions: %s')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   765
                % _(', ').join(sorted(permissions))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   766
            )
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   767
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   768
        permission = {'push': 'rw', 'pull': 'ro',}[permissions.pop()]
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   769
37719
a656cba08a04 wireprotov2: move response handling out of httppeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37717
diff changeset
   770
        handler, resp = sendv2request(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   771
            self._ui,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   772
            self._opener,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   773
            self._requestbuilder,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   774
            self._apiurl,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   775
            permission,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   776
            calls,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   777
            self._redirect,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   778
        )
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   779
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   780
        # TODO we probably want to validate the HTTP code, media type, etc.
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   781
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   782
        self._responseexecutor = pycompat.futures.ThreadPoolExecutor(1)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   783
        self._responsef = self._responseexecutor.submit(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   784
            self._handleresponse, handler, resp
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   785
        )
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   786
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   787
    def close(self):
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   788
        if self._closed:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   789
            return
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   790
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   791
        self.sendcommands()
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   792
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   793
        self._closed = True
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   794
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   795
        if not self._responsef:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   796
            return
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   797
39437
3c6f7eebc010 httppeer: add TODO about delayed handling of ^C
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39436
diff changeset
   798
        # TODO ^C here may not result in immediate program termination.
3c6f7eebc010 httppeer: add TODO about delayed handling of ^C
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39436
diff changeset
   799
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   800
        try:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   801
            self._responsef.result()
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   802
        finally:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   803
            self._responseexecutor.shutdown(wait=True)
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   804
            self._responsef = None
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   805
            self._responseexecutor = None
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   806
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   807
            # If any of our futures are still in progress, mark them as
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   808
            # errored, otherwise a result() could wait indefinitely.
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   809
            for f in self._futures:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   810
                if not f.done():
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   811
                    f.set_exception(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   812
                        error.ResponseError(_('unfulfilled command response'))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   813
                    )
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   814
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   815
            self._futures = None
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   816
37719
a656cba08a04 wireprotov2: move response handling out of httppeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37717
diff changeset
   817
    def _handleresponse(self, handler, resp):
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   818
        # Called in a thread to read the response.
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   819
40019
f5a05bb48116 wireprotov2: change name and behavior of readframe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40018
diff changeset
   820
        while handler.readdata(resp):
37719
a656cba08a04 wireprotov2: move response handling out of httppeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37717
diff changeset
   821
            pass
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   822
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   823
40171
dac438b7346e httppeer: expose API descriptor on httpv2peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40133
diff changeset
   824
@interfaceutil.implementer(repository.ipeerv2)
37483
61e405fb6372 wireproto: crude support for version 2 HTTP peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37411
diff changeset
   825
class httpv2peer(object):
42158
69921d02daaf peer: introduce a limitedarguments attributes
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 41315
diff changeset
   826
69921d02daaf peer: introduce a limitedarguments attributes
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 41315
diff changeset
   827
    limitedarguments = False
69921d02daaf peer: introduce a limitedarguments attributes
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 41315
diff changeset
   828
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   829
    def __init__(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   830
        self, ui, repourl, apipath, opener, requestbuilder, apidescriptor
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   831
    ):
37483
61e405fb6372 wireproto: crude support for version 2 HTTP peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37411
diff changeset
   832
        self.ui = ui
40171
dac438b7346e httppeer: expose API descriptor on httpv2peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40133
diff changeset
   833
        self.apidescriptor = apidescriptor
37483
61e405fb6372 wireproto: crude support for version 2 HTTP peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37411
diff changeset
   834
61e405fb6372 wireproto: crude support for version 2 HTTP peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37411
diff changeset
   835
        if repourl.endswith('/'):
61e405fb6372 wireproto: crude support for version 2 HTTP peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37411
diff changeset
   836
            repourl = repourl[:-1]
61e405fb6372 wireproto: crude support for version 2 HTTP peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37411
diff changeset
   837
37609
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   838
        self._url = repourl
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   839
        self._apipath = apipath
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   840
        self._apiurl = '%s/%s' % (repourl, apipath)
37483
61e405fb6372 wireproto: crude support for version 2 HTTP peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37411
diff changeset
   841
        self._opener = opener
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   842
        self._requestbuilder = requestbuilder
37483
61e405fb6372 wireproto: crude support for version 2 HTTP peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37411
diff changeset
   843
40024
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40019
diff changeset
   844
        self._redirect = wireprotov2peer.supportedredirects(ui, apidescriptor)
86b22a4cfab1 wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40019
diff changeset
   845
37609
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   846
    # Start of ipeerconnection.
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   847
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   848
    def url(self):
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   849
        return self._url
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   850
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   851
    def local(self):
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   852
        return None
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   853
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   854
    def peer(self):
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   855
        return self
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   856
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   857
    def canpush(self):
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   858
        # TODO change once implemented.
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   859
        return False
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   860
37483
61e405fb6372 wireproto: crude support for version 2 HTTP peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37411
diff changeset
   861
    def close(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   862
        self.ui.note(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   863
            _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   864
                '(sent %d HTTP requests and %d bytes; '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   865
                'received %d bytes in responses)\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   866
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   867
            % (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   868
                self._opener.requestscount,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   869
                self._opener.sentbytescount,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   870
                self._opener.receivedbytescount,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   871
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   872
        )
37483
61e405fb6372 wireproto: crude support for version 2 HTTP peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37411
diff changeset
   873
37609
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   874
    # End of ipeerconnection.
01bfe5ad0c53 httppeer: implement ipeerconnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37558
diff changeset
   875
37611
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   876
    # Start of ipeercapabilities.
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   877
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   878
    def capable(self, name):
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   879
        # The capabilities used internally historically map to capabilities
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   880
        # advertised from the "capabilities" wire protocol command. However,
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   881
        # version 2 of that command works differently.
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   882
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   883
        # Maps to commands that are available.
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   884
        if name in ('branchmap', 'getbundle', 'known', 'lookup', 'pushkey'):
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   885
            return True
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   886
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   887
        # Other concepts.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   888
        if name in 'bundle2':
37611
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   889
            return True
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   890
39628
a5de21c9e370 httppeer: expose capabilities for each command
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39549
diff changeset
   891
        # Alias command-* to presence of command of that name.
a5de21c9e370 httppeer: expose capabilities for each command
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39549
diff changeset
   892
        if name.startswith('command-'):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   893
            return name[len('command-') :] in self.apidescriptor['commands']
39628
a5de21c9e370 httppeer: expose capabilities for each command
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39549
diff changeset
   894
37611
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   895
        return False
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   896
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   897
    def requirecap(self, name, purpose):
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   898
        if self.capable(name):
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   899
            return
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   900
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   901
        raise error.CapabilityError(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   902
            _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   903
                'cannot %s; client or remote repository does not support the '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   904
                '\'%s\' capability'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   905
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   906
            % (purpose, name)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   907
        )
37611
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   908
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   909
    # End of ipeercapabilities.
ae8730877371 httppeer: basic implementation of capabilities interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37609
diff changeset
   910
37483
61e405fb6372 wireproto: crude support for version 2 HTTP peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37411
diff changeset
   911
    def _call(self, name, **args):
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   912
        with self.commandexecutor() as e:
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   913
            return e.callcommand(name, args).result()
37543
01361be9e2dc wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37498
diff changeset
   914
37651
950294e28136 httppeer: implement command executor for version 2 peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37644
diff changeset
   915
    def commandexecutor(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   916
        return httpv2executor(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   917
            self.ui,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   918
            self._opener,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   919
            self._requestbuilder,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   920
            self._apiurl,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   921
            self.apidescriptor,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   922
            self._redirect,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   923
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   924
37483
61e405fb6372 wireproto: crude support for version 2 HTTP peer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37411
diff changeset
   925
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   926
# Registry of API service names to metadata about peers that handle it.
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   927
#
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   928
# The following keys are meaningful:
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   929
#
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   930
# init
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   931
#    Callable receiving (ui, repourl, servicepath, opener, requestbuilder,
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   932
#                        apidescriptor) to create a peer.
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   933
#
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   934
# priority
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   935
#    Integer priority for the service. If we could choose from multiple
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   936
#    services, we choose the one with the highest priority.
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   937
API_PEERS = {
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   938
    wireprototypes.HTTP_WIREPROTO_V2: {'init': httpv2peer, 'priority': 50,},
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   939
}
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   940
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   941
37552
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
   942
def performhandshake(ui, url, opener, requestbuilder):
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
   943
    # The handshake is a request to the capabilities command.
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
   944
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
   945
    caps = None
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   946
37552
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
   947
    def capable(x):
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
   948
        raise error.ProgrammingError('should not be called')
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
   949
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   950
    args = {}
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   951
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   952
    # The client advertises support for newer protocols by adding an
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   953
    # X-HgUpgrade-* header with a list of supported APIs and an
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   954
    # X-HgProto-* header advertising which serializing formats it supports.
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   955
    # We only support the HTTP version 2 transport and CBOR responses for
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   956
    # now.
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   957
    advertisev2 = ui.configbool('experimental', 'httppeer.advertise-v2')
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   958
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   959
    if advertisev2:
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   960
        args['headers'] = {
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   961
            r'X-HgProto-1': r'cbor',
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   962
        }
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   963
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   964
        args['headers'].update(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   965
            encodevalueinheaders(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   966
                ' '.join(sorted(API_PEERS)),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   967
                'X-HgUpgrade',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   968
                # We don't know the header limit this early.
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   969
                # So make it small.
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   970
                1024,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   971
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   972
        )
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
   973
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   974
    req, requrl, qs = makev1commandrequest(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   975
        ui, requestbuilder, caps, capable, url, 'capabilities', args
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   976
    )
37552
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
   977
    resp = sendrequest(ui, opener, req)
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
   978
37832
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   979
    # The server may redirect us to the repo root, stripping the
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   980
    # ?cmd=capabilities query string from the URL. The server would likely
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   981
    # return HTML in this case and ``parsev1commandresponse()`` would raise.
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   982
    # We catch this special case and re-issue the capabilities request against
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   983
    # the new URL.
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   984
    #
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   985
    # We should ideally not do this, as a redirect that drops the query
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   986
    # string from the URL is arguably a server bug. (Garbage in, garbage out).
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   987
    # However,  Mercurial clients for several years appeared to handle this
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   988
    # issue without behavior degradation. And according to issue 5860, it may
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   989
    # be a longstanding bug in some server implementations. So we allow a
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   990
    # redirect that drops the query string to "just work."
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   991
    try:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   992
        respurl, ct, resp = parsev1commandresponse(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   993
            ui, url, requrl, qs, resp, compressible=False, allowcbor=advertisev2
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   994
        )
37832
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   995
    except RedirectedRepoError as e:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   996
        req, requrl, qs = makev1commandrequest(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   997
            ui, requestbuilder, caps, capable, e.respurl, 'capabilities', args
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
   998
        )
37832
6169d95dce3b httppeer: detect redirect to URL without query string (issue5860)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37810
diff changeset
   999
        resp = sendrequest(ui, opener, req)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1000
        respurl, ct, resp = parsev1commandresponse(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1001
            ui, url, requrl, qs, resp, compressible=False, allowcbor=advertisev2
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1002
        )
37552
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
  1003
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
  1004
    try:
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1005
        rawdata = resp.read()
37552
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
  1006
    finally:
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
  1007
        resp.close()
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
  1008
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1009
    if not ct.startswith('application/mercurial-'):
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1010
        raise error.ProgrammingError('unexpected content-type: %s' % ct)
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1011
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1012
    if advertisev2:
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1013
        if ct == 'application/mercurial-cbor':
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1014
            try:
39445
cdb56f295b03 httppeer: use our CBOR decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39437
diff changeset
  1015
                info = cborutil.decodeall(rawdata)[0]
cdb56f295b03 httppeer: use our CBOR decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39437
diff changeset
  1016
            except cborutil.CBORDecodeError:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1017
                raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1018
                    _('error decoding CBOR from remote server'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1019
                    hint=_(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1020
                        'try again and consider contacting '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1021
                        'the server operator'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1022
                    ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1023
                )
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1024
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1025
        # We got a legacy response. That's fine.
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1026
        elif ct in ('application/mercurial-0.1', 'application/mercurial-0.2'):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1027
            info = {'v1capabilities': set(rawdata.split())}
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1028
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1029
        else:
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1030
            raise error.RepoError(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1031
                _('unexpected response type from server: %s') % ct
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1032
            )
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1033
    else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1034
        info = {'v1capabilities': set(rawdata.split())}
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1035
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1036
    return respurl, info
37552
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
  1037
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1038
37553
6b08cf6b900f httppeer: allow opener to be passed to makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37552
diff changeset
  1039
def makepeer(ui, path, opener=None, requestbuilder=urlreq.request):
37547
835ccc2a5ef1 httppeer: move requestbuilder defaults into makepeer() argument
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37546
diff changeset
  1040
    """Construct an appropriate HTTP peer instance.
835ccc2a5ef1 httppeer: move requestbuilder defaults into makepeer() argument
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37546
diff changeset
  1041
37553
6b08cf6b900f httppeer: allow opener to be passed to makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37552
diff changeset
  1042
    ``opener`` is an ``url.opener`` that should be used to establish
6b08cf6b900f httppeer: allow opener to be passed to makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37552
diff changeset
  1043
    connections, perform HTTP requests.
6b08cf6b900f httppeer: allow opener to be passed to makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37552
diff changeset
  1044
37547
835ccc2a5ef1 httppeer: move requestbuilder defaults into makepeer() argument
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37546
diff changeset
  1045
    ``requestbuilder`` is the type used for constructing HTTP requests.
835ccc2a5ef1 httppeer: move requestbuilder defaults into makepeer() argument
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37546
diff changeset
  1046
    It exists as an argument so extensions can override the default.
835ccc2a5ef1 httppeer: move requestbuilder defaults into makepeer() argument
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37546
diff changeset
  1047
    """
37006
8e89c2bec1f7 httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36961
diff changeset
  1048
    u = util.url(path)
8e89c2bec1f7 httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36961
diff changeset
  1049
    if u.query or u.fragment:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1050
        raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1051
            _('unsupported URL component: "%s"') % (u.query or u.fragment)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1052
        )
37006
8e89c2bec1f7 httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36961
diff changeset
  1053
8e89c2bec1f7 httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36961
diff changeset
  1054
    # urllib cannot handle URLs with embedded user or passwd.
8e89c2bec1f7 httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36961
diff changeset
  1055
    url, authinfo = u.authinfo()
8e89c2bec1f7 httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36961
diff changeset
  1056
    ui.debug('using %s\n' % url)
8e89c2bec1f7 httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36961
diff changeset
  1057
37553
6b08cf6b900f httppeer: allow opener to be passed to makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37552
diff changeset
  1058
    opener = opener or urlmod.opener(ui, authinfo)
37006
8e89c2bec1f7 httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36961
diff changeset
  1059
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1060
    respurl, info = performhandshake(ui, url, opener, requestbuilder)
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1061
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1062
    # Given the intersection of APIs that both we and the server support,
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1063
    # sort by their advertised priority and pick the first one.
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1064
    #
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1065
    # TODO consider making this request-based and interface driven. For
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1066
    # example, the caller could say "I want a peer that does X." It's quite
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1067
    # possible that not all peers would do that. Since we know the service
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1068
    # capabilities, we could filter out services not meeting the
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1069
    # requirements. Possibly by consulting the interfaces defined by the
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1070
    # peer type.
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1071
    apipeerchoices = set(info.get('apis', {}).keys()) & set(API_PEERS.keys())
37552
8b8a845c85fc httppeer: perform capabilities request in makepeer()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37551
diff changeset
  1072
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1073
    preferredchoices = sorted(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1074
        apipeerchoices, key=lambda x: API_PEERS[x]['priority'], reverse=True
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1075
    )
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1076
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1077
    for service in preferredchoices:
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1078
        apipath = '%s/%s' % (info['apibase'].rstrip('/'), service)
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1079
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1080
        return API_PEERS[service]['init'](
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1081
            ui, respurl, apipath, opener, requestbuilder, info['apis'][service]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1082
        )
37558
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1083
8a73132214a3 httppeer: support protocol upgrade
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37556
diff changeset
  1084
    # Failed to construct an API peer. Fall back to legacy.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1085
    return httppeer(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1086
        ui, path, respurl, opener, requestbuilder, info['v1capabilities']
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1087
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1088
37006
8e89c2bec1f7 httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36961
diff changeset
  1089
39549
089fc0db0954 hg: allow extra arguments to be passed to repo creation (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39484
diff changeset
  1090
def instance(ui, path, create, intents=None, createopts=None):
2740
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
  1091
    if create:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25954
diff changeset
  1092
        raise error.Abort(_('cannot create new http repository'))
7211
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
  1093
    try:
36959
43815d87c6aa httppeer: alias url as urlmod
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36835
diff changeset
  1094
        if path.startswith('https:') and not urlmod.has_https:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1095
            raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1096
                _('Python support for SSL and HTTPS ' 'is not installed')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1097
            )
35884
197d10e157ce httppeer: remove support for connecting to <0.9.1 servers (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35698
diff changeset
  1098
37006
8e89c2bec1f7 httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36961
diff changeset
  1099
        inst = makepeer(ui, path)
35884
197d10e157ce httppeer: remove support for connecting to <0.9.1 servers (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35698
diff changeset
  1100
7211
25c0dee16ee0 Autodetect static-http
Matt Mackall <mpm@selenic.com>
parents: 7207
diff changeset
  1101
        return inst
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25500
diff changeset
  1102
    except error.RepoError as httpexception:
14148
cc9366a3751b httprepo: use the original exception after falling back to static-http failed
Mads Kiilerich <mads@kiilerich.com>
parents: 14094
diff changeset
  1103
        try:
cc9366a3751b httprepo: use the original exception after falling back to static-http failed
Mads Kiilerich <mads@kiilerich.com>
parents: 14094
diff changeset
  1104
            r = statichttprepo.instance(ui, "static-" + path, create)
29241
269f7ea08983 httppeer: make a message translatable
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28883
diff changeset
  1105
            ui.note(_('(falling back to static-http)\n'))
14148
cc9366a3751b httprepo: use the original exception after falling back to static-http failed
Mads Kiilerich <mads@kiilerich.com>
parents: 14094
diff changeset
  1106
            return r
cc9366a3751b httprepo: use the original exception after falling back to static-http failed
Mads Kiilerich <mads@kiilerich.com>
parents: 14094
diff changeset
  1107
        except error.RepoError:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42853
diff changeset
  1108
            raise httpexception  # use the original http RepoError instead