mercurial/httprepo.py
author mpm@selenic.com
Sat, 27 Aug 2005 14:21:25 -0700
changeset 1089 142b5d5ec9cc
parent 1072 mercurial/hg.py@05dc7aba22eb
child 1251 84cf8834efb5
permissions -rw-r--r--
Break apart hg.py - move the various parts of hg.py into their own files - create node.py to store node manipulation functions
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
import urllib, urllib2, urlparse, os, zlib
142b5d5ec9cc Break apart hg.py
mpm@selenic.com
parents: 1072
diff changeset
     9
from node import *
142b5d5ec9cc Break apart hg.py
mpm@selenic.com
parents: 1072
diff changeset
    10
from remoterepo import *
926
b765e970c9ff Add a local() method to repository classes
mpm@selenic.com
parents: 923
diff changeset
    11
b765e970c9ff Add a local() method to repository classes
mpm@selenic.com
parents: 923
diff changeset
    12
class httprepository(remoterepository):
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    13
    def __init__(self, ui, path):
765
1e31d97c3d70 Hack to fix missing '/' problem in URLs
mpm@selenic.com
parents: 764
diff changeset
    14
        # fix missing / after hostname
1e31d97c3d70 Hack to fix missing '/' problem in URLs
mpm@selenic.com
parents: 764
diff changeset
    15
        s = urlparse.urlsplit(path)
1e31d97c3d70 Hack to fix missing '/' problem in URLs
mpm@selenic.com
parents: 764
diff changeset
    16
        partial = s[2]
1e31d97c3d70 Hack to fix missing '/' problem in URLs
mpm@selenic.com
parents: 764
diff changeset
    17
        if not partial: partial = "/"
1e31d97c3d70 Hack to fix missing '/' problem in URLs
mpm@selenic.com
parents: 764
diff changeset
    18
        self.url = urlparse.urlunsplit((s[0], s[1], partial, '', ''))
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    19
        self.ui = ui
321
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    20
        no_list = [ "localhost", "127.0.0.1" ]
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    21
        host = ui.config("http_proxy", "host")
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
    22
        if host is None:
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
    23
            host = os.environ.get("http_proxy")
426
8c90ab5644c9 Allow hgrc's proxy host and $http_proxy env var to start with http://
Thomas Arendsen Hein <thomas@intevation.de>
parents: 424
diff changeset
    24
        if host and host.startswith('http://'):
8c90ab5644c9 Allow hgrc's proxy host and $http_proxy env var to start with http://
Thomas Arendsen Hein <thomas@intevation.de>
parents: 424
diff changeset
    25
            host = host[7:]
321
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    26
        user = ui.config("http_proxy", "user")
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    27
        passwd = ui.config("http_proxy", "passwd")
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    28
        no = ui.config("http_proxy", "no")
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
    29
        if no is None:
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
    30
            no = os.environ.get("no_proxy")
321
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    31
        if no:
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    32
            no_list = no_list + no.split(",")
515
03f27b1381f9 Whitespace cleanups
mpm@selenic.com
parents: 514
diff changeset
    33
321
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    34
        no_proxy = 0
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    35
        for h in no_list:
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    36
            if (path.startswith("http://" + h + "/") or
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    37
                path.startswith("http://" + h + ":") or
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    38
                path == "http://" + h):
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    39
                no_proxy = 1
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    40
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    41
        # Note: urllib2 takes proxy values from the environment and those will
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    42
        # take precedence
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
    43
        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
    44
            try:
6390c377a9e6 Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents: 856
diff changeset
    45
                if os.environ.has_key(env):
6390c377a9e6 Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents: 856
diff changeset
    46
                    del os.environ[env]
6390c377a9e6 Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents: 856
diff changeset
    47
            except OSError:
6390c377a9e6 Trap OSError when deleting env vars
Edouard Gomez <ed.gomez@free.fr>
parents: 856
diff changeset
    48
                pass
321
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    49
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    50
        proxy_handler = urllib2.BaseHandler()
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    51
        if host and not no_proxy:
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    52
            proxy_handler = urllib2.ProxyHandler({"http" : "http://" + host})
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    53
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    54
        authinfo = None
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    55
        if user and passwd:
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    56
            passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    57
            passmgr.add_password(None, host, user, passwd)
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    58
            authinfo = urllib2.ProxyBasicAuthHandler(passmgr)
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    59
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    60
        opener = urllib2.build_opener(proxy_handler, authinfo)
73b8a8a059ec Transparent proxy support
mpm@selenic.com
parents: 317
diff changeset
    61
        urllib2.install_opener(opener)
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    62
634
da5378d39269 Add a repo method to report repo device
Matt Mackall <mpm@selenic.com>
parents: 627
diff changeset
    63
    def dev(self):
da5378d39269 Add a repo method to report repo device
Matt Mackall <mpm@selenic.com>
parents: 627
diff changeset
    64
        return -1
da5378d39269 Add a repo method to report repo device
Matt Mackall <mpm@selenic.com>
parents: 627
diff changeset
    65
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    66
    def do_cmd(self, cmd, **args):
83
9fd5b35cfc45 Add -q quiet option
mpm@selenic.com
parents: 79
diff changeset
    67
        self.ui.debug("sending %s command\n" % cmd)
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    68
        q = {"cmd": cmd}
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    69
        q.update(args)
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    70
        qs = urllib.urlencode(q)
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    71
        cu = "%s?%s" % (self.url, qs)
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
    72
        resp = urllib2.urlopen(cu)
753
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
    73
        proto = resp.headers['content-type']
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
    74
753
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
    75
        # accept old "text/plain" and "application/hg-changegroup" for now
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
    76
        if not proto.startswith('application/mercurial') and \
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
    77
               not proto.startswith('text/plain') and \
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
    78
               not proto.startswith('application/hg-changegroup'):
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
    79
            raise RepoError("'%s' does not appear to be an hg repository"
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
    80
                            % self.url)
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
    81
753
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
    82
        if proto.startswith('application/mercurial'):
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
    83
            version = proto[22:]
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
    84
            if float(version) > 0.1:
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
    85
                raise RepoError("'%s' uses newer protocol %s" %
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
    86
                                (self.url, version))
8760d0c83b9b Check protocol versions
mpm@selenic.com
parents: 752
diff changeset
    87
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
    88
        return resp
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    89
222
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
    90
    def heads(self):
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
    91
        d = self.do_cmd("heads").read()
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
    92
        try:
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
    93
            return map(bin, d[:-1].split(" "))
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
    94
        except:
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
    95
            self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n")
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
    96
            raise
87484f627422 make pull work for multiple heads
mpm@selenic.com
parents: 220
diff changeset
    97
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    98
    def branches(self, nodes):
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
    99
        n = " ".join(map(hex, nodes))
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
   100
        d = self.do_cmd("branches", nodes=n).read()
217
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   101
        try:
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   102
            br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ]
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   103
            return br
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   104
        except:
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   105
            self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n")
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   106
            raise
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   107
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   108
    def between(self, pairs):
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   109
        n = "\n".join(["-".join(map(hex, p)) for p in pairs])
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
   110
        d = self.do_cmd("between", pairs=n).read()
217
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   111
        try:
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   112
            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
   113
            return p
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   114
        except:
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   115
            self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n")
e6d6497a6331 merge: catch unexpected responses
mpm@selenic.com
parents: 216
diff changeset
   116
            raise
60
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   117
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   118
    def changegroup(self, nodes):
e32fdbd97839 Add hg:// protocol
mpm@selenic.com
parents: 56
diff changeset
   119
        n = " ".join(map(hex, nodes))
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
   120
        f = self.do_cmd("changegroup", roots=n)
192
5d8553352d2e Changes to network protocol
mpm@selenic.com
parents: 191
diff changeset
   121
        bytes = 0
635
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   122
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   123
        class zread:
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   124
            def __init__(self, f):
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   125
                self.zd = zlib.decompressobj()
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   126
                self.f = f
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   127
                self.buf = ""
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   128
            def read(self, l):
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   129
                while l > len(self.buf):
751
0b245edec124 When pulling from a non hg repository URL (e.g. http://www.kernel.org/hg)
Muli Ben-Yehuda <mulix@mulix.org>
parents: 741
diff changeset
   130
                    r = self.f.read(4096)
635
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   131
                    if r:
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   132
                        self.buf += self.zd.decompress(r)
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   133
                    else:
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   134
                        self.buf += self.zd.flush()
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   135
                        break
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   136
                d, self.buf = self.buf[:l], self.buf[l:]
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   137
                return d
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   138
752
c693eafd5967 Simplify content type checking
mpm@selenic.com
parents: 751
diff changeset
   139
        return zread(f)
635
85e2209d401c Protocol switch from using generators to stream-like objects.
Matt Mackall <mpm@selenic.com>
parents: 634
diff changeset
   140
923
c7a3b88505cd Add basic https support for pull
mpm@selenic.com
parents: 919
diff changeset
   141
class httpsrepository(httprepository):
c7a3b88505cd Add basic https support for pull
mpm@selenic.com
parents: 919
diff changeset
   142
    pass