mercurial/httprepo.py
author Vadim Gelfer <vadim.gelfer@gmail.com>
Mon, 31 Jul 2006 07:11:12 -0700
changeset 2740 386f04d6ecb3
parent 2673 109a22f5434a
child 2859 345bac2bc4ec
permissions -rw-r--r--
clean up hg.py: move repo constructor code into each repo module
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1089
142b5d5ec9cc Break apart hg.py
mpm@selenic.com
parents: 1072
diff changeset
     1
# httprepo.py - HTTP repository proxy classes for mercurial
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     2
#
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     3
# Copyright 2005 Matt Mackall <mpm@selenic.com>
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     4
#
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     5
# This software may be used and distributed according to the terms
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     6
# of the GNU General Public License, incorporated herein by reference.
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     7
1089
142b5d5ec9cc Break apart hg.py
mpm@selenic.com
parents: 1072
diff changeset
     8
from node import *
142b5d5ec9cc Break apart hg.py
mpm@selenic.com
parents: 1072
diff changeset
     9
from remoterepo import *
1400
cf9a1233738a i18n first part: make '_' available for files who need it
Benoit Boissinot <benoit.boissinot@ens-lyon.org
parents: 1377
diff changeset
    10
from i18n import gettext as _
1251
84cf8834efb5 Fix lots of exception-related problems.
Bryan O'Sullivan <bos@serpentine.com>
parents: 1089
diff changeset
    11
from demandload import *
2015
1a09814a5b1f Catch HTTPException when reading from remote http repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1870
diff changeset
    12
demandload(globals(), "hg os urllib urllib2 urlparse zlib util httplib")
2467
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
    13
demandload(globals(), "errno keepalive tempfile socket")
926
b765e970c9ff Add a local() method to repository classes
mpm@selenic.com
parents: 923
diff changeset
    14
2447
cd00531ecc16 httprepo: make "http://user:pass@host/" urls work
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2446
diff changeset
    15
class passwordmgr(urllib2.HTTPPasswordMgrWithDefaultRealm):
2281
7761597b5da3 prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2015
diff changeset
    16
    def __init__(self, ui):
2447
cd00531ecc16 httprepo: make "http://user:pass@host/" urls work
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2446
diff changeset
    17
        urllib2.HTTPPasswordMgrWithDefaultRealm.__init__(self)
2281
7761597b5da3 prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2015
diff changeset
    18
        self.ui = ui
7761597b5da3 prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2015
diff changeset
    19
7761597b5da3 prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2015
diff changeset
    20
    def find_user_password(self, realm, authuri):
2447
cd00531ecc16 httprepo: make "http://user:pass@host/" urls work
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2446
diff changeset
    21
        authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm.find_user_password(
2281
7761597b5da3 prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2015
diff changeset
    22
            self, realm, authuri)
2556
f1ebc4311f47 Allow http://user@example.com URLs (i.e. without passwords)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2501
diff changeset
    23
        user, passwd = authinfo
f1ebc4311f47 Allow http://user@example.com URLs (i.e. without passwords)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2501
diff changeset
    24
        if user and passwd:
f1ebc4311f47 Allow http://user@example.com URLs (i.e. without passwords)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2501
diff changeset
    25
            return (user, passwd)
2281
7761597b5da3 prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2015
diff changeset
    26
2446
1b2bbb2b4911 httprepo: fix small bug in authentication.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2442
diff changeset
    27
        if not self.ui.interactive:
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    28
            raise util.Abort(_('http authorization required'))
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    29
2281
7761597b5da3 prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2015
diff changeset
    30
        self.ui.write(_("http authorization required\n"))
7761597b5da3 prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2015
diff changeset
    31
        self.ui.status(_("realm: %s\n") % realm)
2556
f1ebc4311f47 Allow http://user@example.com URLs (i.e. without passwords)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2501
diff changeset
    32
        if user:
f1ebc4311f47 Allow http://user@example.com URLs (i.e. without passwords)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2501
diff changeset
    33
            self.ui.status(_("user: %s\n") % user)
f1ebc4311f47 Allow http://user@example.com URLs (i.e. without passwords)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2501
diff changeset
    34
        else:
f1ebc4311f47 Allow http://user@example.com URLs (i.e. without passwords)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2501
diff changeset
    35
            user = self.ui.prompt(_("user:"), default=None)
f1ebc4311f47 Allow http://user@example.com URLs (i.e. without passwords)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2501
diff changeset
    36
f1ebc4311f47 Allow http://user@example.com URLs (i.e. without passwords)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2501
diff changeset
    37
        if not passwd:
f1ebc4311f47 Allow http://user@example.com URLs (i.e. without passwords)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2501
diff changeset
    38
            passwd = self.ui.getpass()
2281
7761597b5da3 prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2015
diff changeset
    39
7761597b5da3 prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2015
diff changeset
    40
        self.add_password(realm, authuri, user, passwd)
7761597b5da3 prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2015
diff changeset
    41
        return (user, passwd)
7761597b5da3 prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2015
diff changeset
    42
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    43
def netlocsplit(netloc):
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    44
    '''split [user[:passwd]@]host[:port] into 4-tuple.'''
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    45
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    46
    a = netloc.find('@')
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    47
    if a == -1:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    48
        user, passwd = None, None
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    49
    else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    50
        userpass, netloc = netloc[:a], netloc[a+1:]
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    51
        c = userpass.find(':')
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    52
        if c == -1:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    53
            user, passwd = urllib.unquote(userpass), None
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    54
        else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    55
            user = urllib.unquote(userpass[:c])
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    56
            passwd = urllib.unquote(userpass[c+1:])
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    57
    c = netloc.find(':')
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    58
    if c == -1:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    59
        host, port = netloc, None
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    60
    else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    61
        host, port = netloc[:c], netloc[c+1:]
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    62
    return host, port, user, passwd
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    63
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    64
def netlocunsplit(host, port, user=None, passwd=None):
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    65
    '''turn host, port, user, passwd into [user[:passwd]@]host[:port].'''
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    66
    if port:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    67
        hostport = host + ':' + port
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    68
    else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    69
        hostport = host
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    70
    if user:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    71
        if passwd:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    72
            userpass = urllib.quote(user) + ':' + urllib.quote(passwd)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    73
        else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    74
            userpass = urllib.quote(user)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    75
        return userpass + '@' + hostport
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    76
    return hostport
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
    77
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    78
class httpconnection(keepalive.HTTPConnection):
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    79
    # must be able to send big bundle as stream.
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    80
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    81
    def send(self, data):
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    82
        if isinstance(data, str):
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    83
            keepalive.HTTPConnection.send(self, data)
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    84
        else:
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    85
            # if auth required, some data sent twice, so rewind here
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    86
            data.seek(0)
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    87
            for chunk in util.filechunkiter(data):
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    88
                keepalive.HTTPConnection.send(self, chunk)
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    89
2569
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
    90
class basehttphandler(keepalive.HTTPHandler):
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    91
    def http_open(self, req):
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    92
        return self.do_open(httpconnection, req)
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
    93
2569
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
    94
has_https = hasattr(urllib2, 'HTTPSHandler')
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
    95
if has_https:
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
    96
    class httpsconnection(httplib.HTTPSConnection):
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
    97
        response_class = keepalive.HTTPResponse
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
    98
        # must be able to send big bundle as stream.
2557
1727ff712a4e Fix push over https.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2556
diff changeset
    99
2569
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   100
        def send(self, data):
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   101
            if isinstance(data, str):
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   102
                httplib.HTTPSConnection.send(self, data)
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   103
            else:
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   104
                # if auth required, some data sent twice, so rewind here
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   105
                data.seek(0)
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   106
                for chunk in util.filechunkiter(data):
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   107
                    httplib.HTTPSConnection.send(self, chunk)
2557
1727ff712a4e Fix push over https.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2556
diff changeset
   108
2569
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   109
    class httphandler(basehttphandler, urllib2.HTTPSHandler):
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   110
        def https_open(self, req):
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   111
            return self.do_open(httpsconnection, req)
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   112
else:
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   113
    class httphandler(basehttphandler):
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   114
        pass
2557
1727ff712a4e Fix push over https.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2556
diff changeset
   115
926
b765e970c9ff Add a local() method to repository classes
mpm@selenic.com
parents: 923
diff changeset
   116
class httprepository(remoterepository):
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   117
    def __init__(self, ui, path):
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
   118
        self.path = path
2442
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   119
        self.caps = None
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   120
        scheme, netloc, urlpath, query, frag = urlparse.urlsplit(path)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   121
        if query or frag:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   122
            raise util.Abort(_('unsupported URL component: "%s"') %
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   123
                             (query or frag))
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   124
        if not urlpath: urlpath = '/'
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   125
        host, port, user, passwd = netlocsplit(netloc)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   126
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   127
        # urllib cannot handle URLs with embedded user or passwd
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
   128
        self._url = urlparse.urlunsplit((scheme, netlocunsplit(host, port),
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
   129
                                         urlpath, '', ''))
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   130
        self.ui = ui
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   131
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   132
        proxyurl = ui.config("http_proxy", "host") or os.getenv('http_proxy')
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   133
        proxyauthinfo = None
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   134
        handler = httphandler()
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   135
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   136
        if proxyurl:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   137
            # proxy can be proper url or host[:port]
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   138
            if not (proxyurl.startswith('http:') or
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   139
                    proxyurl.startswith('https:')):
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   140
                proxyurl = 'http://' + proxyurl + '/'
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   141
            snpqf = urlparse.urlsplit(proxyurl)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   142
            proxyscheme, proxynetloc, proxypath, proxyquery, proxyfrag = snpqf
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   143
            hpup = netlocsplit(proxynetloc)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   144
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   145
            proxyhost, proxyport, proxyuser, proxypasswd = hpup
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   146
            if not proxyuser:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   147
                proxyuser = ui.config("http_proxy", "user")
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   148
                proxypasswd = ui.config("http_proxy", "passwd")
515
03f27b1381f9 Whitespace cleanups
mpm@selenic.com
parents: 514
diff changeset
   149
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   150
            # see if we should use a proxy for this url
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   151
            no_list = [ "localhost", "127.0.0.1" ]
2501
b73552a00b20 Make "[web] allow_push, deny_push" and "[http_proxy] no" use ui.configlist.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2484
diff changeset
   152
            no_list.extend([p.lower() for
b73552a00b20 Make "[web] allow_push, deny_push" and "[http_proxy] no" use ui.configlist.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2484
diff changeset
   153
                            p in ui.configlist("http_proxy", "no")])
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   154
            no_list.extend([p.strip().lower() for
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   155
                            p in os.getenv("no_proxy", '').split(',')
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   156
                            if p.strip()])
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   157
            # "http_proxy.always" config is for running tests on localhost
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   158
            if (not ui.configbool("http_proxy", "always") and
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   159
                host.lower() in no_list):
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   160
                ui.debug(_('disabling proxy for %s\n') % host)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   161
            else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   162
                proxyurl = urlparse.urlunsplit((
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   163
                    proxyscheme, netlocunsplit(proxyhost, proxyport,
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   164
                                               proxyuser, proxypasswd or ''),
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   165
                    proxypath, proxyquery, proxyfrag))
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   166
                handler = urllib2.ProxyHandler({scheme: proxyurl})
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   167
                ui.debug(_('proxying through %s\n') % proxyurl)
321
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
   168
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   169
        # urllib2 takes proxy values from the environment and those
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   170
        # will take precedence if found, so drop them
424
9294dce4b633 Allow override of HTTP_PROXY, http_proxy and no_proxy; make no_proxy work.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 385
diff changeset
   171
        for env in ["HTTP_PROXY", "http_proxy", "no_proxy"]:
859
6390c377a9e6 Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents: 856
diff changeset
   172
            try:
6390c377a9e6 Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents: 856
diff changeset
   173
                if os.environ.has_key(env):
6390c377a9e6 Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents: 856
diff changeset
   174
                    del os.environ[env]
6390c377a9e6 Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents: 856
diff changeset
   175
            except OSError:
6390c377a9e6 Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents: 856
diff changeset
   176
                pass
321
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
   177
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   178
        passmgr = passwordmgr(ui)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   179
        if user:
2556
f1ebc4311f47 Allow http://user@example.com URLs (i.e. without passwords)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2501
diff changeset
   180
            ui.debug(_('http auth: user %s, password %s\n') %
f1ebc4311f47 Allow http://user@example.com URLs (i.e. without passwords)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2501
diff changeset
   181
                     (user, passwd and '*' * len(passwd) or 'not set'))
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   182
            passmgr.add_password(None, host, user, passwd or '')
321
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
   183
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   184
        opener = urllib2.build_opener(
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   185
            handler,
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   186
            urllib2.HTTPBasicAuthHandler(passmgr),
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2336
diff changeset
   187
            urllib2.HTTPDigestAuthHandler(passmgr))
2281
7761597b5da3 prompt user for http authentication info
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2015
diff changeset
   188
1359
51ac9a79f3e5 Set the user agent for httprepo communication
Matt Mackall <mpm@selenic.com>
parents: 1251
diff changeset
   189
        # 1.0 here is the _protocol_ version
51ac9a79f3e5 Set the user agent for httprepo communication
Matt Mackall <mpm@selenic.com>
parents: 1251
diff changeset
   190
        opener.addheaders = [('User-agent', 'mercurial/proto-1.0')]
321
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
   191
        urllib2.install_opener(opener)
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   192
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
   193
    def url(self):
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
   194
        return self.path
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
   195
2442
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   196
    # look up capabilities only when needed
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   197
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   198
    def get_caps(self):
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   199
        if self.caps is None:
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   200
            try:
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   201
                self.caps = self.do_read('capabilities').split()
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   202
            except hg.RepoError:
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   203
                self.caps = ()
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   204
            self.ui.debug(_('capabilities: %s\n') %
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   205
                          (' '.join(self.caps or ['none'])))
2442
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   206
        return self.caps
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   207
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   208
    capabilities = property(get_caps)
c660691fb45d http: query server for capabilities
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   209
1870
8a8ab47cccde make push over http print good error message.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1736
diff changeset
   210
    def lock(self):
8a8ab47cccde make push over http print good error message.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1736
diff changeset
   211
        raise util.Abort(_('operation not supported over http'))
8a8ab47cccde make push over http print good error message.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1736
diff changeset
   212
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   213
    def do_cmd(self, cmd, **args):
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   214
        data = args.pop('data', None)
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   215
        headers = args.pop('headers', {})
1402
9d2c2e6b32b5 i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1400
diff changeset
   216
        self.ui.debug(_("sending %s command\n") % cmd)
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   217
        q = {"cmd": cmd}
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   218
        q.update(args)
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   219
        qs = urllib.urlencode(q)
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
   220
        cu = "%s?%s" % (self._url, qs)
2294
ce67fa312f61 Catch urllib's HTTPException and give a meaningful error message to the user.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2281
diff changeset
   221
        try:
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   222
            resp = urllib2.urlopen(urllib2.Request(cu, data, headers))
2467
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   223
        except urllib2.HTTPError, inst:
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   224
            if inst.code == 401:
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   225
                raise util.Abort(_('authorization failed'))
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   226
            raise
2294
ce67fa312f61 Catch urllib's HTTPException and give a meaningful error message to the user.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2281
diff changeset
   227
        except httplib.HTTPException, inst:
2336
f77edcffb837 http: print better error if exception happens.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2294
diff changeset
   228
            self.ui.debug(_('http error while sending %s command\n') % cmd)
f77edcffb837 http: print better error if exception happens.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2294
diff changeset
   229
            self.ui.print_exc()
f77edcffb837 http: print better error if exception happens.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2294
diff changeset
   230
            raise IOError(None, inst)
2435
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   231
        try:
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   232
            proto = resp.getheader('content-type')
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   233
        except AttributeError:
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   234
            proto = resp.headers['content-type']
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
   235
753
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
   236
        # accept old "text/plain" and "application/hg-changegroup" for now
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
   237
        if not proto.startswith('application/mercurial') and \
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
   238
               not proto.startswith('text/plain') and \
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
   239
               not proto.startswith('application/hg-changegroup'):
1402
9d2c2e6b32b5 i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1400
diff changeset
   240
            raise hg.RepoError(_("'%s' does not appear to be an hg repository") %
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
   241
                               self._url)
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
   242
753
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
   243
        if proto.startswith('application/mercurial'):
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
   244
            version = proto[22:]
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
   245
            if float(version) > 0.1:
1402
9d2c2e6b32b5 i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1400
diff changeset
   246
                raise hg.RepoError(_("'%s' uses newer protocol %s") %
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
   247
                                   (self._url, version))
753
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
   248
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
   249
        return resp
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   250
2435
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   251
    def do_read(self, cmd, **args):
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   252
        fp = self.do_cmd(cmd, **args)
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   253
        try:
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   254
            return fp.read()
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   255
        finally:
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   256
            # if using keepalive, allow connection to be reused
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   257
            fp.close()
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   258
222
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
   259
    def heads(self):
2435
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   260
        d = self.do_read("heads")
222
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
   261
        try:
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
   262
            return map(bin, d[:-1].split(" "))
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
   263
        except:
1402
9d2c2e6b32b5 i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1400
diff changeset
   264
            self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n")
222
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
   265
            raise
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
   266
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   267
    def branches(self, nodes):
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   268
        n = " ".join(map(hex, nodes))
2435
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   269
        d = self.do_read("branches", nodes=n)
217
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   270
        try:
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   271
            br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ]
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   272
            return br
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   273
        except:
1402
9d2c2e6b32b5 i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1400
diff changeset
   274
            self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n")
217
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   275
            raise
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   276
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   277
    def between(self, pairs):
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   278
        n = "\n".join(["-".join(map(hex, p)) for p in pairs])
2435
ff2bac730b99 http client: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2337
diff changeset
   279
        d = self.do_read("between", pairs=n)
217
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   280
        try:
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   281
            p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ]
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   282
            return p
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   283
        except:
1402
9d2c2e6b32b5 i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1400
diff changeset
   284
            self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n")
217
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   285
            raise
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   286
1736
50de0887bbcd add preoutgoing and outgoing hooks.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 1402
diff changeset
   287
    def changegroup(self, nodes, kind):
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   288
        n = " ".join(map(hex, nodes))
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
   289
        f = self.do_cmd("changegroup", roots=n)
192
5d8553352d2e Changes to network protocol
mpm@selenic.com
parents: 191
diff changeset
   290
        bytes = 0
635
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   291
1376
524ca4a06f70 Fix same performance bug as c3654cfaa77 but for httprepo.py instead.
Eric Hopper <hopper@omnifarious.org>
parents: 1359
diff changeset
   292
        def zgenerator(f):
524ca4a06f70 Fix same performance bug as c3654cfaa77 but for httprepo.py instead.
Eric Hopper <hopper@omnifarious.org>
parents: 1359
diff changeset
   293
            zd = zlib.decompressobj()
2015
1a09814a5b1f Catch HTTPException when reading from remote http repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1870
diff changeset
   294
            try:
1a09814a5b1f Catch HTTPException when reading from remote http repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1870
diff changeset
   295
                for chnk in f:
1a09814a5b1f Catch HTTPException when reading from remote http repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1870
diff changeset
   296
                    yield zd.decompress(chnk)
1a09814a5b1f Catch HTTPException when reading from remote http repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1870
diff changeset
   297
            except httplib.HTTPException, inst:
1a09814a5b1f Catch HTTPException when reading from remote http repository.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1870
diff changeset
   298
                raise IOError(None, _('connection ended unexpectedly'))
1376
524ca4a06f70 Fix same performance bug as c3654cfaa77 but for httprepo.py instead.
Eric Hopper <hopper@omnifarious.org>
parents: 1359
diff changeset
   299
            yield zd.flush()
635
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   300
1376
524ca4a06f70 Fix same performance bug as c3654cfaa77 but for httprepo.py instead.
Eric Hopper <hopper@omnifarious.org>
parents: 1359
diff changeset
   301
        return util.chunkbuffer(zgenerator(util.filechunkiter(f)))
635
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   302
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2435
diff changeset
   303
    def unbundle(self, cg, heads, source):
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   304
        # 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
   305
        # http 1.1 chunked transfer.
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   306
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   307
        fd, tempname = tempfile.mkstemp(prefix='hg-unbundle-')
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   308
        fp = os.fdopen(fd, 'wb+')
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   309
        try:
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   310
            for chunk in util.filechunkiter(cg):
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   311
                fp.write(chunk)
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   312
            length = fp.tell()
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   313
            try:
2467
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   314
                rfp = self.do_cmd(
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   315
                    'unbundle', data=fp,
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   316
                    headers={'content-length': length,
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   317
                             'content-type': 'application/octet-stream'},
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   318
                    heads=' '.join(map(hex, heads)))
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   319
                try:
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   320
                    ret = int(rfp.readline())
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   321
                    self.ui.write(rfp.read())
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   322
                    return ret
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   323
                finally:
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   324
                    rfp.close()
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   325
            except socket.error, err:
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   326
                if err[0] in (errno.ECONNRESET, errno.EPIPE):
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   327
                    raise util.Abort(_('push failed: %s'), err[1])
4e78dc71d946 http client: better work with authorization errors, broken sockets.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2465
diff changeset
   328
                raise util.Abort(err[1])
2465
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   329
        finally:
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   330
            fp.close()
c91118f425d0 push over http: client support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2447
diff changeset
   331
            os.unlink(tempname)
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2435
diff changeset
   332
2612
ffb895f16925 add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2569
diff changeset
   333
    def stream_out(self):
ffb895f16925 add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2569
diff changeset
   334
        return self.do_cmd('stream_out')
ffb895f16925 add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2569
diff changeset
   335
923
c7a3b88505cd Add basic https support for pull
mpm@selenic.com
parents: 919
diff changeset
   336
class httpsrepository(httprepository):
2569
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   337
    def __init__(self, ui, path):
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   338
        if not has_https:
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   339
            raise util.Abort(_('Python support for SSL and HTTPS '
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   340
                               'is not installed'))
52ce0d6bc375 HTTPS: fix python2.3, persistent connections, don't explode if SSL is not available
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2557
diff changeset
   341
        httprepository.__init__(self, ui, path)
2740
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
   342
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
   343
def instance(ui, path, create):
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
   344
    if create:
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
   345
        raise util.Abort(_('cannot create new http repository'))
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
   346
    if path.startswith('hg:'):
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
   347
        ui.warn(_("hg:// syntax is deprecated, please use http:// instead\n"))
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
   348
        path = 'http:' + path[3:]
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
   349
    if path.startswith('https:'):
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
   350
        return httpsrepository(ui, path)
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
   351
    return httprepository(ui, path)