mercurial/url.py
author Patrick Mezard <pmezard@gmail.com>
Mon, 01 Aug 2011 23:58:50 +0200
branchstable
changeset 15005 4a43e23b8c55
parent 14244 e7525a555a64
child 15008 d0424f39984c
child 15025 0593e8f81c71
permissions -rw-r--r--
hgweb: do not ignore [auth] if url has a username (issue2822) The [auth] section was ignored when handling URLs like: http://user@example.com/foo Instead, we look in [auth] for an entry matching the URL and supplied user name. Entries without username can match URL with a username. Prefix length ties are resolved in favor of entries matching the username. With: foo.prefix = http://example.org foo.username = user foo.password = password bar.prefix = http://example.org/bar and the input URL: http://user@example.org/bar the 'bar' entry will be selected because of prefix length, therefore prompting for a password. This behaviour ensure that entries selection is consistent when looking for credentials or for certificates, and that certificates can be picked even if their entries do no define usernames while the URL does. Additionally, entries without a username matched against a username are returned as if they did have requested username set to avoid prompting again for a username if the password is not set. v2: reparse the URL in readauthforuri() to handle HTTP and HTTPS similarly. v3: allow unset usernames to match URL usernames to pick certificates. Resolve prefix length ties in favor of entries with usernames.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
     1
# url.py - HTTP handling for mercurial
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
     2
#
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
     3
# Copyright 2005, 2006, 2007, 2008 Matt Mackall <mpm@selenic.com>
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
     4
# Copyright 2006, 2007 Alexis S. L. Carvalho <alexis@cecm.usp.br>
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
     5
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
     6
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8208
diff changeset
     7
# 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: 9852
diff changeset
     8
# GNU General Public License version 2 or any later version.
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
     9
14076
924c82157d46 url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents: 14071
diff changeset
    10
import urllib, urllib2, httplib, os, socket, cStringIO
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    11
from i18n import _
14204
5fa21960b2f4 sslutil: extracted ssl methods from httpsconnection in url.py
Augie Fackler <durin42@gmail.com>
parents: 14076
diff changeset
    12
import keepalive, util, sslutil
14244
e7525a555a64 url: use new http support if requested by the user
Augie Fackler <durin42@gmail.com>
parents: 14204
diff changeset
    13
import httpconnection as httpconnectionmod
13371
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
    14
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    15
class passwordmgr(urllib2.HTTPPasswordMgrWithDefaultRealm):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    16
    def __init__(self, ui):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    17
        urllib2.HTTPPasswordMgrWithDefaultRealm.__init__(self)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    18
        self.ui = ui
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    19
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    20
    def find_user_password(self, realm, authuri):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    21
        authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm.find_user_password(
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    22
            self, realm, authuri)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    23
        user, passwd = authinfo
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    24
        if user and passwd:
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
    25
            self._writedebug(user, passwd)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    26
            return (user, passwd)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    27
15005
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 14244
diff changeset
    28
        if not user or not passwd:
14244
e7525a555a64 url: use new http support if requested by the user
Augie Fackler <durin42@gmail.com>
parents: 14204
diff changeset
    29
            res = httpconnectionmod.readauthforuri(self.ui, authuri)
13372
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
    30
            if res:
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
    31
                group, auth = res
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
    32
                user, passwd = auth.get('username'), auth.get('password')
13372
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
    33
                self.ui.debug("using auth.%s.* for authentication\n" % group)
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
    34
        if not user or not passwd:
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
    35
            if not self.ui.interactive():
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
    36
                raise util.Abort(_('http authorization required'))
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    37
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
    38
            self.ui.write(_("http authorization required\n"))
12862
9d6adddc8eea url: show realm/user when asking for username/password
timeless <timeless@gmail.com>
parents: 12770
diff changeset
    39
            self.ui.write(_("realm: %s\n") % realm)
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
    40
            if user:
12862
9d6adddc8eea url: show realm/user when asking for username/password
timeless <timeless@gmail.com>
parents: 12770
diff changeset
    41
                self.ui.write(_("user: %s\n") % user)
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
    42
            else:
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
    43
                user = self.ui.prompt(_("user:"), default=None)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    44
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
    45
            if not passwd:
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
    46
                passwd = self.ui.getpass()
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    47
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    48
        self.add_password(realm, authuri, user, passwd)
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
    49
        self._writedebug(user, passwd)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    50
        return (user, passwd)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    51
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
    52
    def _writedebug(self, user, passwd):
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
    53
        msg = _('http auth: user %s, password %s\n')
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
    54
        self.ui.debug(msg % (user, passwd and '*' * len(passwd) or 'not set'))
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
    55
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    56
class proxyhandler(urllib2.ProxyHandler):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    57
    def __init__(self, ui):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    58
        proxyurl = ui.config("http_proxy", "host") or os.getenv('http_proxy')
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    59
        # XXX proxyauthinfo = None
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    60
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    61
        if proxyurl:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    62
            # proxy can be proper url or host[:port]
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    63
            if not (proxyurl.startswith('http:') or
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    64
                    proxyurl.startswith('https:')):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    65
                proxyurl = 'http://' + proxyurl + '/'
14076
924c82157d46 url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents: 14071
diff changeset
    66
            proxy = util.url(proxyurl)
13820
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
    67
            if not proxy.user:
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
    68
                proxy.user = ui.config("http_proxy", "user")
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
    69
                proxy.passwd = ui.config("http_proxy", "passwd")
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    70
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    71
            # see if we should use a proxy for this url
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    72
            no_list = ["localhost", "127.0.0.1"]
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    73
            no_list.extend([p.lower() for
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    74
                            p in ui.configlist("http_proxy", "no")])
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    75
            no_list.extend([p.strip().lower() for
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    76
                            p in os.getenv("no_proxy", '').split(',')
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    77
                            if p.strip()])
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    78
            # "http_proxy.always" config is for running tests on localhost
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    79
            if ui.configbool("http_proxy", "always"):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    80
                self.no_list = []
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    81
            else:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    82
                self.no_list = no_list
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    83
13820
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
    84
            proxyurl = str(proxy)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    85
            proxies = {'http': proxyurl, 'https': proxyurl}
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 9347
diff changeset
    86
            ui.debug('proxying through http://%s:%s\n' %
13820
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
    87
                      (proxy.host, proxy.port))
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    88
        else:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    89
            proxies = {}
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    90
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    91
        # urllib2 takes proxy values from the environment and those
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    92
        # will take precedence if found, so drop them
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    93
        for env in ["HTTP_PROXY", "http_proxy", "no_proxy"]:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    94
            try:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    95
                if env in os.environ:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    96
                    del os.environ[env]
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    97
            except OSError:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    98
                pass
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    99
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   100
        urllib2.ProxyHandler.__init__(self, proxies)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   101
        self.ui = ui
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   102
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   103
    def proxy_open(self, req, proxy, type_):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   104
        host = req.get_host().split(':')[0]
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   105
        if host in self.no_list:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   106
            return None
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   107
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   108
        # work around a bug in Python < 2.4.2
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   109
        # (it leaves a "\n" at the end of Proxy-authorization headers)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   110
        baseclass = req.__class__
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   111
        class _request(baseclass):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   112
            def add_header(self, key, val):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   113
                if key.lower() == 'proxy-authorization':
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   114
                    val = val.strip()
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   115
                return baseclass.add_header(self, key, val)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   116
        req.__class__ = _request
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   117
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   118
        return urllib2.ProxyHandler.proxy_open(self, req, proxy, type_)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   119
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
   120
def _gen_sendfile(orgsend):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   121
    def _sendfile(self, data):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   122
        # send a file
14244
e7525a555a64 url: use new http support if requested by the user
Augie Fackler <durin42@gmail.com>
parents: 14204
diff changeset
   123
        if isinstance(data, httpconnectionmod.httpsendfile):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   124
            # if auth required, some data sent twice, so rewind here
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   125
            data.seek(0)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   126
            for chunk in util.filechunkiter(data):
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
   127
                orgsend(self, chunk)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   128
        else:
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
   129
            orgsend(self, data)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   130
    return _sendfile
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   131
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   132
has_https = hasattr(urllib2, 'HTTPSHandler')
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   133
if has_https:
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   134
    try:
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   135
        _create_connection = socket.create_connection
10411
af4c42ec19ed ssl: fix compatibility with pre-2.6 Python
Matt Mackall <mpm@selenic.com>
parents: 10409
diff changeset
   136
    except AttributeError:
10482
95265afff99f url: fix python < 2.6 with ssl installed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10415
diff changeset
   137
        _GLOBAL_DEFAULT_TIMEOUT = object()
95265afff99f url: fix python < 2.6 with ssl installed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10415
diff changeset
   138
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   139
        def _create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   140
                               source_address=None):
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   141
            # lifted from Python 2.6
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   142
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   143
            msg = "getaddrinfo returns an empty list"
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   144
            host, port = address
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   145
            for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   146
                af, socktype, proto, canonname, sa = res
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   147
                sock = None
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   148
                try:
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   149
                    sock = socket.socket(af, socktype, proto)
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   150
                    if timeout is not _GLOBAL_DEFAULT_TIMEOUT:
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   151
                        sock.settimeout(timeout)
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   152
                    if source_address:
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   153
                        sock.bind(source_address)
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   154
                    sock.connect(sa)
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   155
                    return sock
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   156
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   157
                except socket.error, msg:
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   158
                    if sock is not None:
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   159
                        sock.close()
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   160
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   161
            raise socket.error, msg
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   162
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   163
class httpconnection(keepalive.HTTPConnection):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   164
    # must be able to send big bundle as stream.
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
   165
    send = _gen_sendfile(keepalive.HTTPConnection.send)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   166
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   167
    def connect(self):
10415
677f15da38c1 url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10411
diff changeset
   168
        if has_https and self.realhostport: # use CONNECT proxy
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   169
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   170
            self.sock.connect((self.host, self.port))
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   171
            if _generic_proxytunnel(self):
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   172
                # we do not support client x509 certificates
14204
5fa21960b2f4 sslutil: extracted ssl methods from httpsconnection in url.py
Augie Fackler <durin42@gmail.com>
parents: 14076
diff changeset
   173
                self.sock = sslutil.ssl_wrap_socket(self.sock, None, None)
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   174
        else:
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   175
            keepalive.HTTPConnection.connect(self)
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   176
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   177
    def getresponse(self):
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   178
        proxyres = getattr(self, 'proxyres', None)
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   179
        if proxyres:
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   180
            if proxyres.will_close:
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   181
                self.close()
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   182
            self.proxyres = None
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   183
            return proxyres
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   184
        return keepalive.HTTPConnection.getresponse(self)
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   185
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   186
# general transaction handler to support different ways to handle
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   187
# HTTPS proxying before and after Python 2.6.3.
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   188
def _generic_start_transaction(handler, h, req):
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   189
    if hasattr(req, '_tunnel_host') and req._tunnel_host:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   190
        tunnel_host = req._tunnel_host
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   191
        if tunnel_host[:7] not in ['http://', 'https:/']:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   192
            tunnel_host = 'https://' + tunnel_host
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   193
        new_tunnel = True
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   194
    else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   195
        tunnel_host = req.get_selector()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   196
        new_tunnel = False
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   197
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   198
    if new_tunnel or tunnel_host == req.get_full_url(): # has proxy
14076
924c82157d46 url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents: 14071
diff changeset
   199
        u = util.url(tunnel_host)
13820
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
   200
        if new_tunnel or u.scheme == 'https': # only use CONNECT for HTTPS
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
   201
            h.realhostport = ':'.join([u.host, (u.port or '443')])
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   202
            h.headers = req.headers.copy()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   203
            h.headers.update(handler.parent.addheaders)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   204
            return
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   205
10415
677f15da38c1 url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10411
diff changeset
   206
    h.realhostport = None
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   207
    h.headers = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   208
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   209
def _generic_proxytunnel(self):
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   210
    proxyheaders = dict(
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   211
            [(x, self.headers[x]) for x in self.headers
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   212
             if x.lower().startswith('proxy-')])
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   213
    self._set_hostport(self.host, self.port)
10415
677f15da38c1 url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10411
diff changeset
   214
    self.send('CONNECT %s HTTP/1.0\r\n' % self.realhostport)
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   215
    for header in proxyheaders.iteritems():
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   216
        self.send('%s: %s\r\n' % header)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   217
    self.send('\r\n')
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   218
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   219
    # majority of the following code is duplicated from
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   220
    # httplib.HTTPConnection as there are no adequate places to
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   221
    # override functions to provide the needed functionality
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   222
    res = self.response_class(self.sock,
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   223
                              strict=self.strict,
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   224
                              method=self._method)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   225
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   226
    while True:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   227
        version, status, reason = res._read_status()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   228
        if status != httplib.CONTINUE:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   229
            break
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   230
        while True:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   231
            skip = res.fp.readline().strip()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   232
            if not skip:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   233
                break
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   234
    res.status = status
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   235
    res.reason = reason.strip()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   236
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   237
    if res.status == 200:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   238
        while True:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   239
            line = res.fp.readline()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   240
            if line == '\r\n':
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   241
                break
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   242
        return True
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   243
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   244
    if version == 'HTTP/1.0':
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   245
        res.version = 10
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   246
    elif version.startswith('HTTP/1.'):
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   247
        res.version = 11
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   248
    elif version == 'HTTP/0.9':
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   249
        res.version = 9
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   250
    else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   251
        raise httplib.UnknownProtocol(version)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   252
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   253
    if res.version == 9:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   254
        res.length = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   255
        res.chunked = 0
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   256
        res.will_close = 1
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   257
        res.msg = httplib.HTTPMessage(cStringIO.StringIO())
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   258
        return False
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   259
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   260
    res.msg = httplib.HTTPMessage(res.fp)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   261
    res.msg.fp = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   262
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   263
    # are we using the chunked-style of transfer encoding?
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   264
    trenc = res.msg.getheader('transfer-encoding')
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   265
    if trenc and trenc.lower() == "chunked":
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   266
        res.chunked = 1
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   267
        res.chunk_left = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   268
    else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   269
        res.chunked = 0
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   270
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   271
    # will the connection close at the end of the response?
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   272
    res.will_close = res._check_close()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   273
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   274
    # do we have a Content-Length?
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   275
    # NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   276
    length = res.msg.getheader('content-length')
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   277
    if length and not res.chunked:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   278
        try:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   279
            res.length = int(length)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   280
        except ValueError:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   281
            res.length = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   282
        else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   283
            if res.length < 0:  # ignore nonsensical negative lengths
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   284
                res.length = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   285
    else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   286
        res.length = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   287
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   288
    # does the body have a fixed length? (of zero)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   289
    if (status == httplib.NO_CONTENT or status == httplib.NOT_MODIFIED or
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   290
        100 <= status < 200 or # 1xx codes
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   291
        res._method == 'HEAD'):
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   292
        res.length = 0
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   293
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   294
    # if the connection remains open, and we aren't using chunked, and
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   295
    # a content-length was not provided, then assume that the connection
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   296
    # WILL close.
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   297
    if (not res.will_close and
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   298
       not res.chunked and
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   299
       res.length is None):
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   300
        res.will_close = 1
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   301
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   302
    self.proxyres = res
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   303
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   304
    return False
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   305
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   306
class httphandler(keepalive.HTTPHandler):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   307
    def http_open(self, req):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   308
        return self.do_open(httpconnection, req)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   309
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   310
    def _start_transaction(self, h, req):
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   311
        _generic_start_transaction(self, h, req)
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   312
        return keepalive.HTTPHandler._start_transaction(self, h, req)
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   313
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   314
if has_https:
13424
08f9c587141f url: merge BetterHTTPS with httpsconnection to get some proxy https validation
Mads Kiilerich <mads@kiilerich.com>
parents: 13422
diff changeset
   315
    class httpsconnection(httplib.HTTPSConnection):
08f9c587141f url: merge BetterHTTPS with httpsconnection to get some proxy https validation
Mads Kiilerich <mads@kiilerich.com>
parents: 13422
diff changeset
   316
        response_class = keepalive.HTTPResponse
08f9c587141f url: merge BetterHTTPS with httpsconnection to get some proxy https validation
Mads Kiilerich <mads@kiilerich.com>
parents: 13422
diff changeset
   317
        # must be able to send big bundle as stream.
08f9c587141f url: merge BetterHTTPS with httpsconnection to get some proxy https validation
Mads Kiilerich <mads@kiilerich.com>
parents: 13422
diff changeset
   318
        send = _gen_sendfile(keepalive.safesend)
08f9c587141f url: merge BetterHTTPS with httpsconnection to get some proxy https validation
Mads Kiilerich <mads@kiilerich.com>
parents: 13422
diff changeset
   319
        getresponse = keepalive.wrapgetresponse(httplib.HTTPSConnection)
9726
430e59ff3437 keepalive: handle broken pipes gracefully during large POSTs
Augie Fackler <durin42@gmail.com>
parents: 9467
diff changeset
   320
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   321
        def connect(self):
13422
ebce5196b9db url: always create BetterHTTPS connections the same way
Mads Kiilerich <mads@kiilerich.com>
parents: 13421
diff changeset
   322
            self.sock = _create_connection((self.host, self.port))
ebce5196b9db url: always create BetterHTTPS connections the same way
Mads Kiilerich <mads@kiilerich.com>
parents: 13421
diff changeset
   323
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   324
            host = self.host
13424
08f9c587141f url: merge BetterHTTPS with httpsconnection to get some proxy https validation
Mads Kiilerich <mads@kiilerich.com>
parents: 13422
diff changeset
   325
            if self.realhostport: # use CONNECT proxy
14064
e4bfb9c337f3 remove unused imports and variables
Alexander Solovyov <alexander@solovyov.net>
parents: 13902
diff changeset
   326
                _generic_proxytunnel(self)
13424
08f9c587141f url: merge BetterHTTPS with httpsconnection to get some proxy https validation
Mads Kiilerich <mads@kiilerich.com>
parents: 13422
diff changeset
   327
                host = self.realhostport.rsplit(':', 1)[0]
14204
5fa21960b2f4 sslutil: extracted ssl methods from httpsconnection in url.py
Augie Fackler <durin42@gmail.com>
parents: 14076
diff changeset
   328
            self.sock = sslutil.ssl_wrap_socket(
5fa21960b2f4 sslutil: extracted ssl methods from httpsconnection in url.py
Augie Fackler <durin42@gmail.com>
parents: 14076
diff changeset
   329
                self.sock, self.key_file, self.cert_file,
5fa21960b2f4 sslutil: extracted ssl methods from httpsconnection in url.py
Augie Fackler <durin42@gmail.com>
parents: 14076
diff changeset
   330
                **sslutil.sslkwargs(self.ui, host))
5fa21960b2f4 sslutil: extracted ssl methods from httpsconnection in url.py
Augie Fackler <durin42@gmail.com>
parents: 14076
diff changeset
   331
            sslutil.validator(self.ui, host)(self.sock)
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   332
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   333
    class httpshandler(keepalive.KeepAliveHandler, urllib2.HTTPSHandler):
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   334
        def __init__(self, ui):
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   335
            keepalive.KeepAliveHandler.__init__(self)
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   336
            urllib2.HTTPSHandler.__init__(self)
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   337
            self.ui = ui
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   338
            self.pwmgr = passwordmgr(self.ui)
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   339
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   340
        def _start_transaction(self, h, req):
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   341
            _generic_start_transaction(self, h, req)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   342
            return keepalive.KeepAliveHandler._start_transaction(self, h, req)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   343
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   344
        def https_open(self, req):
14244
e7525a555a64 url: use new http support if requested by the user
Augie Fackler <durin42@gmail.com>
parents: 14204
diff changeset
   345
            res = httpconnectionmod.readauthforuri(self.ui, req.get_full_url())
13372
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   346
            if res:
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   347
                group, auth = res
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   348
                self.auth = auth
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   349
                self.ui.debug("using auth.%s.* for authentication\n" % group)
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   350
            else:
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   351
                self.auth = None
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   352
            return self.do_open(self._makeconnection, req)
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   353
10408
50fb1fe143ff url: httplib.HTTPSConnection already handles IPv6 and port parsing fine
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
   354
        def _makeconnection(self, host, port=None, *args, **kwargs):
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   355
            keyfile = None
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   356
            certfile = None
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   357
10511
6f61c480f51c url: *args argument is a tuple, not a list (found by pylint)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10484
diff changeset
   358
            if len(args) >= 1: # key_file
6f61c480f51c url: *args argument is a tuple, not a list (found by pylint)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10484
diff changeset
   359
                keyfile = args[0]
6f61c480f51c url: *args argument is a tuple, not a list (found by pylint)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10484
diff changeset
   360
            if len(args) >= 2: # cert_file
6f61c480f51c url: *args argument is a tuple, not a list (found by pylint)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10484
diff changeset
   361
                certfile = args[1]
6f61c480f51c url: *args argument is a tuple, not a list (found by pylint)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10484
diff changeset
   362
            args = args[2:]
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   363
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   364
            # if the user has specified different key/cert files in
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   365
            # hgrc, we prefer these
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   366
            if self.auth and 'key' in self.auth and 'cert' in self.auth:
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   367
                keyfile = self.auth['key']
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   368
                certfile = self.auth['cert']
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   369
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   370
            conn = httpsconnection(host, port, keyfile, certfile, *args, **kwargs)
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   371
            conn.ui = self.ui
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   372
            return conn
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   373
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   374
class httpdigestauthhandler(urllib2.HTTPDigestAuthHandler):
11457
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   375
    def __init__(self, *args, **kwargs):
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   376
        urllib2.HTTPDigestAuthHandler.__init__(self, *args, **kwargs)
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   377
        self.retried_req = None
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   378
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   379
    def reset_retry_count(self):
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   380
        # Python 2.6.5 will call this on 401 or 407 errors and thus loop
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   381
        # forever. We disable reset_retry_count completely and reset in
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   382
        # http_error_auth_reqed instead.
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   383
        pass
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   384
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   385
    def http_error_auth_reqed(self, auth_header, host, req, headers):
11457
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   386
        # Reset the retry counter once for each request.
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   387
        if req is not self.retried_req:
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   388
            self.retried_req = req
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   389
            self.retried = 0
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   390
        # In python < 2.5 AbstractDigestAuthHandler raises a ValueError if
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   391
        # it doesn't know about the auth type requested. This can happen if
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   392
        # somebody is using BasicAuth and types a bad password.
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   393
        try:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   394
            return urllib2.HTTPDigestAuthHandler.http_error_auth_reqed(
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   395
                        self, auth_header, host, req, headers)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   396
        except ValueError, inst:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   397
            arg = inst.args[0]
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   398
            if arg.startswith("AbstractDigestAuthHandler doesn't know "):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   399
                return
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   400
            raise
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   401
11844
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   402
class httpbasicauthhandler(urllib2.HTTPBasicAuthHandler):
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   403
    def __init__(self, *args, **kwargs):
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   404
        urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs)
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   405
        self.retried_req = None
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   406
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   407
    def reset_retry_count(self):
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   408
        # Python 2.6.5 will call this on 401 or 407 errors and thus loop
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   409
        # forever. We disable reset_retry_count completely and reset in
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   410
        # http_error_auth_reqed instead.
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   411
        pass
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   412
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   413
    def http_error_auth_reqed(self, auth_header, host, req, headers):
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   414
        # Reset the retry counter once for each request.
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   415
        if req is not self.retried_req:
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   416
            self.retried_req = req
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   417
            self.retried = 0
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   418
        return urllib2.HTTPBasicAuthHandler.http_error_auth_reqed(
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   419
                        self, auth_header, host, req, headers)
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   420
9347
d0474b184347 url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents: 9122
diff changeset
   421
handlerfuncs = []
d0474b184347 url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents: 9122
diff changeset
   422
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   423
def opener(ui, authinfo=None):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   424
    '''
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   425
    construct an opener suitable for urllib2
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   426
    authinfo will be added to the password manager
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   427
    '''
14244
e7525a555a64 url: use new http support if requested by the user
Augie Fackler <durin42@gmail.com>
parents: 14204
diff changeset
   428
    if ui.configbool('ui', 'usehttp2', False):
e7525a555a64 url: use new http support if requested by the user
Augie Fackler <durin42@gmail.com>
parents: 14204
diff changeset
   429
        handlers = [httpconnectionmod.http2handler(ui, passwordmgr(ui))]
e7525a555a64 url: use new http support if requested by the user
Augie Fackler <durin42@gmail.com>
parents: 14204
diff changeset
   430
    else:
e7525a555a64 url: use new http support if requested by the user
Augie Fackler <durin42@gmail.com>
parents: 14204
diff changeset
   431
        handlers = [httphandler()]
e7525a555a64 url: use new http support if requested by the user
Augie Fackler <durin42@gmail.com>
parents: 14204
diff changeset
   432
        if has_https:
e7525a555a64 url: use new http support if requested by the user
Augie Fackler <durin42@gmail.com>
parents: 14204
diff changeset
   433
            handlers.append(httpshandler(ui))
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   434
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   435
    handlers.append(proxyhandler(ui))
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   436
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   437
    passmgr = passwordmgr(ui)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   438
    if authinfo is not None:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   439
        passmgr.add_password(*authinfo)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   440
        user, passwd = authinfo[2:4]
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 9347
diff changeset
   441
        ui.debug('http auth: user %s, password %s\n' %
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   442
                 (user, passwd and '*' * len(passwd) or 'not set'))
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   443
11844
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   444
    handlers.extend((httpbasicauthhandler(passmgr),
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   445
                     httpdigestauthhandler(passmgr)))
9347
d0474b184347 url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents: 9122
diff changeset
   446
    handlers.extend([h(ui, passmgr) for h in handlerfuncs])
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   447
    opener = urllib2.build_opener(*handlers)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   448
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   449
    # 1.0 here is the _protocol_ version
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   450
    opener.addheaders = [('User-agent', 'mercurial/proto-1.0')]
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   451
    opener.addheaders.append(('Accept', 'application/mercurial-0.1'))
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   452
    return opener
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   453
13818
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   454
def open(ui, url_, data=None):
14076
924c82157d46 url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents: 14071
diff changeset
   455
    u = util.url(url_)
13818
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   456
    if u.scheme:
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   457
        u.scheme = u.scheme.lower()
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   458
        url_, authinfo = u.authinfo()
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   459
    else:
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   460
        path = util.normpath(os.path.abspath(url_))
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   461
        url_ = 'file://' + urllib.pathname2url(path)
7284
ac81ffac0f35 url: fix file:// URL handling
Patrick Mezard <pmezard@gmail.com>
parents: 7270
diff changeset
   462
        authinfo = None
13818
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   463
    return opener(ui, authinfo).open(url_, data)