mercurial/url.py
author Brodie Rao <brodie@bitheap.org>
Wed, 30 Mar 2011 20:01:35 -0700
changeset 13819 d16894e29f91
parent 13818 bf6156bab41b
child 13820 65b89e80f892
permissions -rw-r--r--
httprepo/sshrepo: use url.url Like the previous patch to getauthinfo(), this also makes username/password parsing more forgiving for SSH URLs. This also opens up the possibility of allowing non-numeric ports, since the URL parser has no problem handling them. Related issues: - issue851: @ in password in http url - issue2055: nonnumeric port bug with https protocol
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
13818
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
    10
import urllib, urllib2, urlparse, httplib, os, socket, cStringIO
11880
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
    11
import __builtin__
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    12
from i18n import _
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    13
import keepalive, util
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    14
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    15
class url(object):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    16
    """Reliable URL parser.
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    17
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    18
    This parses URLs and provides attributes for the following
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    19
    components:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    20
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    21
    <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment>
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    22
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    23
    Missing components are set to None. The only exception is
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    24
    fragment, which is set to '' if present but empty.
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    25
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    26
    If parse_fragment is False, fragment is included in query. If
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    27
    parse_query is False, query is included in path. If both are
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    28
    False, both fragment and query are included in path.
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    29
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    30
    See http://www.ietf.org/rfc/rfc2396.txt for more information.
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    31
13816
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    32
    Note that for backward compatibility reasons, bundle URLs do not
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    33
    take host names. That means 'bundle://../' has a path of '../'.
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    34
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    35
    Examples:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    36
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    37
    >>> url('http://www.ietf.org/rfc/rfc2396.txt')
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    38
    <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'>
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    39
    >>> url('ssh://[::1]:2200//home/joe/repo')
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    40
    <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'>
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    41
    >>> url('file:///home/joe/repo')
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    42
    <url scheme: 'file', path: '/home/joe/repo'>
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    43
    >>> url('bundle:foo')
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    44
    <url scheme: 'bundle', path: 'foo'>
13816
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    45
    >>> url('bundle://../foo')
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    46
    <url scheme: 'bundle', path: '../foo'>
13807
974490c1768f url: deal with drive letters
Matt Mackall <mpm@selenic.com>
parents: 13772
diff changeset
    47
    >>> url('c:\\\\foo\\\\bar')
974490c1768f url: deal with drive letters
Matt Mackall <mpm@selenic.com>
parents: 13772
diff changeset
    48
    <url path: 'c:\\\\foo\\\\bar'>
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    49
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    50
    Authentication credentials:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    51
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    52
    >>> url('ssh://joe:xyz@x/repo')
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    53
    <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'>
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    54
    >>> url('ssh://joe@x/repo')
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    55
    <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'>
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    56
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    57
    Query strings and fragments:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    58
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    59
    >>> url('http://host/a?b#c')
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    60
    <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'>
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    61
    >>> url('http://host/a?b#c', parse_query=False, parse_fragment=False)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    62
    <url scheme: 'http', host: 'host', path: 'a?b#c'>
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    63
    """
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    64
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    65
    _safechars = "!~*'()+"
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    66
    _safepchars = "/!~*'()+"
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    67
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    68
    def __init__(self, path, parse_query=True, parse_fragment=True):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    69
        # We slowly chomp away at path until we have only the path left
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    70
        self.scheme = self.user = self.passwd = self.host = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    71
        self.port = self.path = self.query = self.fragment = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    72
        self._localpath = True
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    73
13807
974490c1768f url: deal with drive letters
Matt Mackall <mpm@selenic.com>
parents: 13772
diff changeset
    74
        # special case for Windows drive letters
13814
03dfe0c85c1a url: move drive letter checking into has_drive_letter() for extensions
Brodie Rao <brodie@bitheap.org>
parents: 13807
diff changeset
    75
        if has_drive_letter(path):
13807
974490c1768f url: deal with drive letters
Matt Mackall <mpm@selenic.com>
parents: 13772
diff changeset
    76
            self.path = path
974490c1768f url: deal with drive letters
Matt Mackall <mpm@selenic.com>
parents: 13772
diff changeset
    77
            return
974490c1768f url: deal with drive letters
Matt Mackall <mpm@selenic.com>
parents: 13772
diff changeset
    78
13816
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    79
        # For compatibility reasons, we can't handle bundle paths as
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    80
        # normal URLS
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    81
        if path.startswith('bundle:'):
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    82
            self.scheme = 'bundle'
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    83
            path = path[7:]
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    84
            if path.startswith('//'):
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    85
                path = path[2:]
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    86
            self.path = path
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    87
            return
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    88
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    89
        if not path.startswith('/') and ':' in path:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    90
            parts = path.split(':', 1)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    91
            if parts[0]:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    92
                self.scheme, path = parts
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    93
                self._localpath = False
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    94
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    95
        if not path:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    96
            path = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    97
            if self._localpath:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    98
                self.path = ''
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    99
                return
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   100
        else:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   101
            if parse_fragment and '#' in path:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   102
                path, self.fragment = path.split('#', 1)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   103
                if not path:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   104
                    path = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   105
            if self._localpath:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   106
                self.path = path
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   107
                return
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   108
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   109
            if parse_query and '?' in path:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   110
                path, self.query = path.split('?', 1)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   111
                if not path:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   112
                    path = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   113
                if not self.query:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   114
                    self.query = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   115
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   116
            # // is required to specify a host/authority
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   117
            if path and path.startswith('//'):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   118
                parts = path[2:].split('/', 1)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   119
                if len(parts) > 1:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   120
                    self.host, path = parts
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   121
                    path = path
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   122
                else:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   123
                    self.host = parts[0]
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   124
                    path = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   125
                if not self.host:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   126
                    self.host = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   127
                    if path:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   128
                        path = '/' + path
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   129
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   130
            if self.host and '@' in self.host:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   131
                self.user, self.host = self.host.rsplit('@', 1)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   132
                if ':' in self.user:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   133
                    self.user, self.passwd = self.user.split(':', 1)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   134
                if not self.host:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   135
                    self.host = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   136
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   137
            # Don't split on colons in IPv6 addresses without ports
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   138
            if (self.host and ':' in self.host and
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   139
                not (self.host.startswith('[') and self.host.endswith(']'))):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   140
                self.host, self.port = self.host.rsplit(':', 1)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   141
                if not self.host:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   142
                    self.host = None
13817
7f18bab2c0b0 url: abort on file:// URLs with non-localhost hosts
Brodie Rao <brodie@bitheap.org>
parents: 13816
diff changeset
   143
7f18bab2c0b0 url: abort on file:// URLs with non-localhost hosts
Brodie Rao <brodie@bitheap.org>
parents: 13816
diff changeset
   144
            if (self.host and self.scheme == 'file' and
7f18bab2c0b0 url: abort on file:// URLs with non-localhost hosts
Brodie Rao <brodie@bitheap.org>
parents: 13816
diff changeset
   145
                self.host not in ('localhost', '127.0.0.1', '[::1]')):
7f18bab2c0b0 url: abort on file:// URLs with non-localhost hosts
Brodie Rao <brodie@bitheap.org>
parents: 13816
diff changeset
   146
                raise util.Abort(_('file:// URLs can only refer to localhost'))
7f18bab2c0b0 url: abort on file:// URLs with non-localhost hosts
Brodie Rao <brodie@bitheap.org>
parents: 13816
diff changeset
   147
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   148
        self.path = path
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   149
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   150
        for a in ('user', 'passwd', 'host', 'port',
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   151
                  'path', 'query', 'fragment'):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   152
            v = getattr(self, a)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   153
            if v is not None:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   154
                setattr(self, a, urllib.unquote(v))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   155
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   156
    def __repr__(self):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   157
        attrs = []
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   158
        for a in ('scheme', 'user', 'passwd', 'host', 'port', 'path',
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   159
                  'query', 'fragment'):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   160
            v = getattr(self, a)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   161
            if v is not None:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   162
                attrs.append('%s: %r' % (a, v))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   163
        return '<url %s>' % ', '.join(attrs)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   164
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   165
    def __str__(self):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   166
        """Join the URL's components back into a URL string.
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   167
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   168
        Examples:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   169
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   170
        >>> str(url('http://user:pw@host:80/?foo#bar'))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   171
        'http://user:pw@host:80/?foo#bar'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   172
        >>> str(url('ssh://user:pw@[::1]:2200//home/joe#'))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   173
        'ssh://user:pw@[::1]:2200//home/joe#'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   174
        >>> str(url('http://localhost:80//'))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   175
        'http://localhost:80//'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   176
        >>> str(url('http://localhost:80/'))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   177
        'http://localhost:80/'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   178
        >>> str(url('http://localhost:80'))
13815
d066d8d652c8 url: add trailing slashes to URLs with hostnames that don't have one
Brodie Rao <brodie@bitheap.org>
parents: 13814
diff changeset
   179
        'http://localhost:80/'
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   180
        >>> str(url('bundle:foo'))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   181
        'bundle:foo'
13816
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
   182
        >>> str(url('bundle://../foo'))
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
   183
        'bundle:../foo'
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   184
        >>> str(url('path'))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   185
        'path'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   186
        """
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   187
        if self._localpath:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   188
            s = self.path
13816
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
   189
            if self.scheme == 'bundle':
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
   190
                s = 'bundle:' + s
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   191
            if self.fragment:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   192
                s += '#' + self.fragment
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   193
            return s
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   194
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   195
        s = self.scheme + ':'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   196
        if (self.user or self.passwd or self.host or
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   197
            self.scheme and not self.path):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   198
            s += '//'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   199
        if self.user:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   200
            s += urllib.quote(self.user, safe=self._safechars)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   201
        if self.passwd:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   202
            s += ':' + urllib.quote(self.passwd, safe=self._safechars)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   203
        if self.user or self.passwd:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   204
            s += '@'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   205
        if self.host:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   206
            if not (self.host.startswith('[') and self.host.endswith(']')):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   207
                s += urllib.quote(self.host)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   208
            else:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   209
                s += self.host
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   210
        if self.port:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   211
            s += ':' + urllib.quote(self.port)
13815
d066d8d652c8 url: add trailing slashes to URLs with hostnames that don't have one
Brodie Rao <brodie@bitheap.org>
parents: 13814
diff changeset
   212
        if self.host:
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   213
            s += '/'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   214
        if self.path:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   215
            s += urllib.quote(self.path, safe=self._safepchars)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   216
        if self.query:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   217
            s += '?' + urllib.quote(self.query, safe=self._safepchars)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   218
        if self.fragment is not None:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   219
            s += '#' + urllib.quote(self.fragment, safe=self._safepchars)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   220
        return s
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   221
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   222
    def authinfo(self):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   223
        user, passwd = self.user, self.passwd
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   224
        try:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   225
            self.user, self.passwd = None, None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   226
            s = str(self)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   227
        finally:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   228
            self.user, self.passwd = user, passwd
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   229
        if not self.user:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   230
            return (s, None)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   231
        return (s, (None, (str(self), self.host),
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   232
                    self.user, self.passwd or ''))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   233
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   234
def has_scheme(path):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   235
    return bool(url(path).scheme)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   236
13814
03dfe0c85c1a url: move drive letter checking into has_drive_letter() for extensions
Brodie Rao <brodie@bitheap.org>
parents: 13807
diff changeset
   237
def has_drive_letter(path):
03dfe0c85c1a url: move drive letter checking into has_drive_letter() for extensions
Brodie Rao <brodie@bitheap.org>
parents: 13807
diff changeset
   238
    return path[1:2] == ':' and path[0:1].isalpha()
03dfe0c85c1a url: move drive letter checking into has_drive_letter() for extensions
Brodie Rao <brodie@bitheap.org>
parents: 13807
diff changeset
   239
13772
463aca32a937 url: use url.url in hidepassword() and removeauth()
Brodie Rao <brodie@bitheap.org>
parents: 13770
diff changeset
   240
def hidepassword(u):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   241
    '''hide user credential in a url string'''
13772
463aca32a937 url: use url.url in hidepassword() and removeauth()
Brodie Rao <brodie@bitheap.org>
parents: 13770
diff changeset
   242
    u = url(u)
463aca32a937 url: use url.url in hidepassword() and removeauth()
Brodie Rao <brodie@bitheap.org>
parents: 13770
diff changeset
   243
    if u.passwd:
463aca32a937 url: use url.url in hidepassword() and removeauth()
Brodie Rao <brodie@bitheap.org>
parents: 13770
diff changeset
   244
        u.passwd = '***'
463aca32a937 url: use url.url in hidepassword() and removeauth()
Brodie Rao <brodie@bitheap.org>
parents: 13770
diff changeset
   245
    return str(u)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   246
13772
463aca32a937 url: use url.url in hidepassword() and removeauth()
Brodie Rao <brodie@bitheap.org>
parents: 13770
diff changeset
   247
def removeauth(u):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   248
    '''remove all authentication information from a url string'''
13772
463aca32a937 url: use url.url in hidepassword() and removeauth()
Brodie Rao <brodie@bitheap.org>
parents: 13770
diff changeset
   249
    u = url(u)
463aca32a937 url: use url.url in hidepassword() and removeauth()
Brodie Rao <brodie@bitheap.org>
parents: 13770
diff changeset
   250
    u.user = u.passwd = None
463aca32a937 url: use url.url in hidepassword() and removeauth()
Brodie Rao <brodie@bitheap.org>
parents: 13770
diff changeset
   251
    return str(u)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   252
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   253
def netlocsplit(netloc):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   254
    '''split [user[:passwd]@]host[:port] into 4-tuple.'''
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   255
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   256
    a = netloc.find('@')
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   257
    if a == -1:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   258
        user, passwd = None, None
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   259
    else:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   260
        userpass, netloc = netloc[:a], netloc[a + 1:]
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   261
        c = userpass.find(':')
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   262
        if c == -1:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   263
            user, passwd = urllib.unquote(userpass), None
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   264
        else:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   265
            user = urllib.unquote(userpass[:c])
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   266
            passwd = urllib.unquote(userpass[c + 1:])
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   267
    c = netloc.find(':')
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   268
    if c == -1:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   269
        host, port = netloc, None
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   270
    else:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   271
        host, port = netloc[:c], netloc[c + 1:]
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   272
    return host, port, user, passwd
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   273
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   274
def netlocunsplit(host, port, user=None, passwd=None):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   275
    '''turn host, port, user, passwd into [user[:passwd]@]host[:port].'''
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   276
    if port:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   277
        hostport = host + ':' + port
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   278
    else:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   279
        hostport = host
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   280
    if user:
10484
cadd7e076b69 url: correctly quote '/' in user and password embedded in urls
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10482
diff changeset
   281
        quote = lambda s: urllib.quote(s, safe='')
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   282
        if passwd:
10484
cadd7e076b69 url: correctly quote '/' in user and password embedded in urls
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10482
diff changeset
   283
            userpass = quote(user) + ':' + quote(passwd)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   284
        else:
10484
cadd7e076b69 url: correctly quote '/' in user and password embedded in urls
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10482
diff changeset
   285
            userpass = quote(user)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   286
        return userpass + '@' + hostport
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   287
    return hostport
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   288
13371
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   289
def readauthforuri(ui, uri):
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   290
    # Read configuration
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   291
    config = dict()
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   292
    for key, val in ui.configitems('auth'):
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   293
        if '.' not in key:
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   294
            ui.warn(_("ignoring invalid [auth] key '%s'\n") % key)
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   295
            continue
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   296
        group, setting = key.rsplit('.', 1)
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   297
        gdict = config.setdefault(group, dict())
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   298
        if setting in ('username', 'cert', 'key'):
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   299
            val = util.expandpath(val)
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   300
        gdict[setting] = val
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   301
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   302
    # Find the best match
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   303
    scheme, hostpath = uri.split('://', 1)
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   304
    bestlen = 0
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   305
    bestauth = None
13372
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   306
    for group, auth in config.iteritems():
13371
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   307
        prefix = auth.get('prefix')
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   308
        if not prefix:
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   309
            continue
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   310
        p = prefix.split('://', 1)
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   311
        if len(p) > 1:
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   312
            schemes, prefix = [p[0]], p[1]
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   313
        else:
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   314
            schemes = (auth.get('schemes') or 'https').split()
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   315
        if (prefix == '*' or hostpath.startswith(prefix)) and \
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   316
            len(prefix) > bestlen and scheme in schemes:
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   317
            bestlen = len(prefix)
13372
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   318
            bestauth = group, auth
13371
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   319
    return bestauth
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   320
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   321
_safe = ('abcdefghijklmnopqrstuvwxyz'
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   322
         'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   323
         '0123456789' '_.-/')
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   324
_safeset = None
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   325
_hex = None
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   326
def quotepath(path):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   327
    '''quote the path part of a URL
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   328
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   329
    This is similar to urllib.quote, but it also tries to avoid
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   330
    quoting things twice (inspired by wget):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   331
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   332
    >>> quotepath('abc def')
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   333
    'abc%20def'
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   334
    >>> quotepath('abc%20def')
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   335
    'abc%20def'
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   336
    >>> quotepath('abc%20 def')
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   337
    'abc%20%20def'
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   338
    >>> quotepath('abc def%20')
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   339
    'abc%20def%20'
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   340
    >>> quotepath('abc def%2')
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   341
    'abc%20def%252'
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   342
    >>> quotepath('abc def%')
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   343
    'abc%20def%25'
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   344
    '''
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   345
    global _safeset, _hex
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   346
    if _safeset is None:
8150
bbc24c0753a0 util: use built-in set and frozenset
Martin Geisler <mg@lazybytes.net>
parents: 7285
diff changeset
   347
        _safeset = set(_safe)
bbc24c0753a0 util: use built-in set and frozenset
Martin Geisler <mg@lazybytes.net>
parents: 7285
diff changeset
   348
        _hex = set('abcdefABCDEF0123456789')
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   349
    l = list(path)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   350
    for i in xrange(len(l)):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   351
        c = l[i]
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   352
        if (c == '%' and i + 2 < len(l) and
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   353
            l[i + 1] in _hex and l[i + 2] in _hex):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   354
            pass
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   355
        elif c not in _safeset:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   356
            l[i] = '%%%02X' % ord(c)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   357
    return ''.join(l)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   358
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   359
class passwordmgr(urllib2.HTTPPasswordMgrWithDefaultRealm):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   360
    def __init__(self, ui):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   361
        urllib2.HTTPPasswordMgrWithDefaultRealm.__init__(self)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   362
        self.ui = ui
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   363
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   364
    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
   365
        authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm.find_user_password(
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   366
            self, realm, authuri)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   367
        user, passwd = authinfo
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   368
        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
   369
            self._writedebug(user, passwd)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   370
            return (user, passwd)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   371
8344
873429914ec5 url: fix bug in passwordmgr related to auth configuration
Sune Foldager <cryo@cyanite.org>
parents: 8333
diff changeset
   372
        if not user:
13372
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   373
            res = readauthforuri(self.ui, authuri)
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   374
            if res:
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   375
                group, auth = res
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   376
                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
   377
                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
   378
        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
   379
            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
   380
                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
   381
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
   382
            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
   383
            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
   384
            if user:
12862
9d6adddc8eea url: show realm/user when asking for username/password
timeless <timeless@gmail.com>
parents: 12770
diff changeset
   385
                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
   386
            else:
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
   387
                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
   388
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
   389
            if not passwd:
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
   390
                passwd = self.ui.getpass()
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   391
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   392
        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
   393
        self._writedebug(user, passwd)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   394
        return (user, passwd)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   395
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
   396
    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
   397
        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
   398
        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
   399
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   400
class proxyhandler(urllib2.ProxyHandler):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   401
    def __init__(self, ui):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   402
        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
   403
        # XXX proxyauthinfo = None
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   404
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   405
        if proxyurl:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   406
            # 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
   407
            if not (proxyurl.startswith('http:') or
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   408
                    proxyurl.startswith('https:')):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   409
                proxyurl = 'http://' + proxyurl + '/'
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   410
            snpqf = urlparse.urlsplit(proxyurl)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   411
            proxyscheme, proxynetloc, proxypath, proxyquery, proxyfrag = snpqf
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   412
            hpup = netlocsplit(proxynetloc)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   413
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   414
            proxyhost, proxyport, proxyuser, proxypasswd = hpup
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   415
            if not proxyuser:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   416
                proxyuser = ui.config("http_proxy", "user")
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   417
                proxypasswd = ui.config("http_proxy", "passwd")
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   418
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   419
            # 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
   420
            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
   421
            no_list.extend([p.lower() for
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   422
                            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
   423
            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
   424
                            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
   425
                            if p.strip()])
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   426
            # "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
   427
            if ui.configbool("http_proxy", "always"):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   428
                self.no_list = []
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   429
            else:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   430
                self.no_list = no_list
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   431
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   432
            proxyurl = urlparse.urlunsplit((
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   433
                proxyscheme, netlocunsplit(proxyhost, proxyport,
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   434
                                                proxyuser, proxypasswd or ''),
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   435
                proxypath, proxyquery, proxyfrag))
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   436
            proxies = {'http': proxyurl, 'https': proxyurl}
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 9347
diff changeset
   437
            ui.debug('proxying through http://%s:%s\n' %
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   438
                      (proxyhost, proxyport))
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   439
        else:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   440
            proxies = {}
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   441
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   442
        # 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
   443
        # 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
   444
        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
   445
            try:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   446
                if env in os.environ:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   447
                    del os.environ[env]
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   448
            except OSError:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   449
                pass
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   450
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   451
        urllib2.ProxyHandler.__init__(self, proxies)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   452
        self.ui = ui
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   453
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   454
    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
   455
        host = req.get_host().split(':')[0]
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   456
        if host in self.no_list:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   457
            return None
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   458
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   459
        # 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
   460
        # (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
   461
        baseclass = req.__class__
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   462
        class _request(baseclass):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   463
            def add_header(self, key, val):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   464
                if key.lower() == 'proxy-authorization':
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   465
                    val = val.strip()
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   466
                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
   467
        req.__class__ = _request
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   468
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   469
        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
   470
11880
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   471
class httpsendfile(object):
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   472
    """This is a wrapper around the objects returned by python's "open".
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   473
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   474
    Its purpose is to send file-like objects via HTTP and, to do so, it
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   475
    defines a __len__ attribute to feed the Content-Length header.
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   476
    """
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   477
13115
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   478
    def __init__(self, ui, *args, **kwargs):
11880
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   479
        # We can't just "self._data = open(*args, **kwargs)" here because there
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   480
        # is an "open" function defined in this module that shadows the global
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   481
        # one
13115
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   482
        self.ui = ui
11880
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   483
        self._data = __builtin__.open(*args, **kwargs)
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   484
        self.seek = self._data.seek
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   485
        self.close = self._data.close
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   486
        self.write = self._data.write
13115
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   487
        self._len = os.fstat(self._data.fileno()).st_size
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   488
        self._pos = 0
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   489
        self._total = len(self) / 1024 * 2
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   490
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   491
    def read(self, *args, **kwargs):
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   492
        try:
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   493
            ret = self._data.read(*args, **kwargs)
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   494
        except EOFError:
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   495
            self.ui.progress(_('sending'), None)
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   496
        self._pos += len(ret)
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   497
        # We pass double the max for total because we currently have
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   498
        # to send the bundle twice in the case of a server that
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   499
        # requires authentication. Since we can't know until we try
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   500
        # once whether authentication will be required, just lie to
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   501
        # the user and maybe the push succeeds suddenly at 50%.
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   502
        self.ui.progress(_('sending'), self._pos / 1024,
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   503
                         unit=_('kb'), total=self._total)
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   504
        return ret
11880
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   505
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   506
    def __len__(self):
13115
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   507
        return self._len
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   508
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
   509
def _gen_sendfile(orgsend):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   510
    def _sendfile(self, data):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   511
        # send a file
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   512
        if isinstance(data, httpsendfile):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   513
            # 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
   514
            data.seek(0)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   515
            for chunk in util.filechunkiter(data):
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
   516
                orgsend(self, chunk)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   517
        else:
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
   518
            orgsend(self, data)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   519
    return _sendfile
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   520
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   521
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
   522
if has_https:
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   523
    try:
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   524
        # avoid using deprecated/broken FakeSocket in python 2.6
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   525
        import ssl
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   526
        _ssl_wrap_socket = ssl.wrap_socket
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   527
        CERT_REQUIRED = ssl.CERT_REQUIRED
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   528
    except ImportError:
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   529
        CERT_REQUIRED = 2
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   530
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   531
        def _ssl_wrap_socket(sock, key_file, cert_file,
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   532
                             cert_reqs=CERT_REQUIRED, ca_certs=None):
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   533
            if ca_certs:
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   534
                raise util.Abort(_(
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   535
                    'certificate checking requires Python 2.6'))
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   536
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   537
            ssl = socket.ssl(sock, key_file, cert_file)
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   538
            return httplib.FakeSocket(sock, ssl)
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   539
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   540
    try:
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   541
        _create_connection = socket.create_connection
10411
af4c42ec19ed ssl: fix compatibility with pre-2.6 Python
Matt Mackall <mpm@selenic.com>
parents: 10409
diff changeset
   542
    except AttributeError:
10482
95265afff99f url: fix python < 2.6 with ssl installed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10415
diff changeset
   543
        _GLOBAL_DEFAULT_TIMEOUT = object()
95265afff99f url: fix python < 2.6 with ssl installed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10415
diff changeset
   544
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   545
        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
   546
                               source_address=None):
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   547
            # 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
   548
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   549
            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
   550
            host, port = address
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   551
            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
   552
                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
   553
                sock = None
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   554
                try:
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   555
                    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
   556
                    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
   557
                        sock.settimeout(timeout)
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   558
                    if source_address:
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   559
                        sock.bind(source_address)
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   560
                    sock.connect(sa)
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   561
                    return sock
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   562
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   563
                except socket.error, msg:
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   564
                    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
   565
                        sock.close()
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   566
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   567
            raise socket.error, msg
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   568
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   569
class httpconnection(keepalive.HTTPConnection):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   570
    # must be able to send big bundle as stream.
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
   571
    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
   572
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   573
    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
   574
        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
   575
            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
   576
            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
   577
            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
   578
                # we do not support client x509 certificates
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   579
                self.sock = _ssl_wrap_socket(self.sock, None, None)
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   580
        else:
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   581
            keepalive.HTTPConnection.connect(self)
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   582
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   583
    def getresponse(self):
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   584
        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
   585
        if proxyres:
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   586
            if proxyres.will_close:
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   587
                self.close()
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   588
            self.proxyres = None
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   589
            return proxyres
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   590
        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
   591
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   592
# 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
   593
# 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
   594
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
   595
    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
   596
        tunnel_host = req._tunnel_host
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   597
        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
   598
            tunnel_host = 'https://' + tunnel_host
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   599
        new_tunnel = True
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   600
    else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   601
        tunnel_host = req.get_selector()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   602
        new_tunnel = False
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   603
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   604
    if new_tunnel or tunnel_host == req.get_full_url(): # has proxy
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   605
        urlparts = urlparse.urlparse(tunnel_host)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   606
        if new_tunnel or urlparts[0] == 'https': # only use CONNECT for HTTPS
10415
677f15da38c1 url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10411
diff changeset
   607
            realhostport = urlparts[1]
677f15da38c1 url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10411
diff changeset
   608
            if realhostport[-1] == ']' or ':' not in realhostport:
677f15da38c1 url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10411
diff changeset
   609
                realhostport += ':443'
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   610
10415
677f15da38c1 url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10411
diff changeset
   611
            h.realhostport = realhostport
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   612
            h.headers = req.headers.copy()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   613
            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
   614
            return
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   615
10415
677f15da38c1 url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10411
diff changeset
   616
    h.realhostport = None
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   617
    h.headers = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   618
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   619
def _generic_proxytunnel(self):
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   620
    proxyheaders = dict(
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   621
            [(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
   622
             if x.lower().startswith('proxy-')])
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   623
    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
   624
    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
   625
    for header in proxyheaders.iteritems():
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   626
        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
   627
    self.send('\r\n')
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   628
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   629
    # 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
   630
    # 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
   631
    # 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
   632
    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
   633
                              strict=self.strict,
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   634
                              method=self._method)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   635
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   636
    while True:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   637
        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
   638
        if status != httplib.CONTINUE:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   639
            break
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   640
        while True:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   641
            skip = res.fp.readline().strip()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   642
            if not skip:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   643
                break
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   644
    res.status = status
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   645
    res.reason = reason.strip()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   646
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   647
    if res.status == 200:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   648
        while True:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   649
            line = res.fp.readline()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   650
            if line == '\r\n':
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   651
                break
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   652
        return True
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   653
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   654
    if version == 'HTTP/1.0':
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   655
        res.version = 10
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   656
    elif version.startswith('HTTP/1.'):
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   657
        res.version = 11
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   658
    elif version == 'HTTP/0.9':
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   659
        res.version = 9
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   660
    else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   661
        raise httplib.UnknownProtocol(version)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   662
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   663
    if res.version == 9:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   664
        res.length = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   665
        res.chunked = 0
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   666
        res.will_close = 1
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   667
        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
   668
        return False
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   669
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   670
    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
   671
    res.msg.fp = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   672
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   673
    # 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
   674
    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
   675
    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
   676
        res.chunked = 1
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   677
        res.chunk_left = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   678
    else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   679
        res.chunked = 0
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   680
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   681
    # 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
   682
    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
   683
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   684
    # 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
   685
    # 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
   686
    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
   687
    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
   688
        try:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   689
            res.length = int(length)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   690
        except ValueError:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   691
            res.length = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   692
        else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   693
            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
   694
                res.length = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   695
    else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   696
        res.length = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   697
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   698
    # 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
   699
    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
   700
        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
   701
        res._method == 'HEAD'):
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   702
        res.length = 0
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   703
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   704
    # 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
   705
    # 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
   706
    # WILL close.
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   707
    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
   708
       not res.chunked and
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   709
       res.length is None):
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   710
        res.will_close = 1
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   711
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   712
    self.proxyres = res
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   713
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   714
    return False
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   715
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   716
class httphandler(keepalive.HTTPHandler):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   717
    def http_open(self, req):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   718
        return self.do_open(httpconnection, req)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   719
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   720
    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
   721
        _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
   722
        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
   723
12592
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   724
def _verifycert(cert, hostname):
12742
6ab4a7d3c179 url: validity (notBefore/notAfter) is checked by OpenSSL (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12607
diff changeset
   725
    '''Verify that cert (in socket.getpeercert() format) matches hostname.
13249
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   726
    CRLs is not handled.
12770
614f0d8724ab check-code: find trailing whitespace
Martin Geisler <mg@lazybytes.net>
parents: 12742
diff changeset
   727
12592
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   728
    Returns error message if any problems are found and None on success.
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   729
    '''
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   730
    if not cert:
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   731
        return _('no certificate received')
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   732
    dnsname = hostname.lower()
13249
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   733
    def matchdnsname(certname):
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   734
        return (certname == dnsname or
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   735
                '.' in dnsname and certname == '*.' + dnsname.split('.', 1)[1])
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   736
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   737
    san = cert.get('subjectAltName', [])
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   738
    if san:
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   739
        certnames = [value.lower() for key, value in san if key == 'DNS']
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   740
        for name in certnames:
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   741
            if matchdnsname(name):
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   742
                return None
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   743
        return _('certificate is for %s') % ', '.join(certnames)
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   744
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   745
    # subject is only checked when subjectAltName is empty
12592
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   746
    for s in cert.get('subject', []):
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   747
        key, value = s[0]
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   748
        if key == 'commonName':
13248
00411a4fa1bb url: fix UnicodeDecodeError on certificate verification error
Yuya Nishihara <yuya@tcha.org>
parents: 13231
diff changeset
   749
            try:
00411a4fa1bb url: fix UnicodeDecodeError on certificate verification error
Yuya Nishihara <yuya@tcha.org>
parents: 13231
diff changeset
   750
                # 'subject' entries are unicode
00411a4fa1bb url: fix UnicodeDecodeError on certificate verification error
Yuya Nishihara <yuya@tcha.org>
parents: 13231
diff changeset
   751
                certname = value.lower().encode('ascii')
00411a4fa1bb url: fix UnicodeDecodeError on certificate verification error
Yuya Nishihara <yuya@tcha.org>
parents: 13231
diff changeset
   752
            except UnicodeEncodeError:
00411a4fa1bb url: fix UnicodeDecodeError on certificate verification error
Yuya Nishihara <yuya@tcha.org>
parents: 13231
diff changeset
   753
                return _('IDN in certificate not supported')
13249
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   754
            if matchdnsname(certname):
12592
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   755
                return None
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   756
            return _('certificate is for %s') % certname
13249
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   757
    return _('no commonName or subjectAltName found in certificate')
12592
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   758
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   759
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
   760
    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
   761
        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
   762
        # 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
   763
        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
   764
        getresponse = keepalive.wrapgetresponse(httplib.HTTPSConnection)
9726
430e59ff3437 keepalive: handle broken pipes gracefully during large POSTs
Augie Fackler <durin42@gmail.com>
parents: 9467
diff changeset
   765
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   766
        def connect(self):
13422
ebce5196b9db url: always create BetterHTTPS connections the same way
Mads Kiilerich <mads@kiilerich.com>
parents: 13421
diff changeset
   767
            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
   768
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   769
            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
   770
            if self.realhostport: # use CONNECT proxy
08f9c587141f url: merge BetterHTTPS with httpsconnection to get some proxy https validation
Mads Kiilerich <mads@kiilerich.com>
parents: 13422
diff changeset
   771
                something = _generic_proxytunnel(self)
08f9c587141f url: merge BetterHTTPS with httpsconnection to get some proxy https validation
Mads Kiilerich <mads@kiilerich.com>
parents: 13422
diff changeset
   772
                host = self.realhostport.rsplit(':', 1)[0]
08f9c587141f url: merge BetterHTTPS with httpsconnection to get some proxy https validation
Mads Kiilerich <mads@kiilerich.com>
parents: 13422
diff changeset
   773
13419
1cc73868c740 url: remove test for self.ui in BetterHTTPS
Mads Kiilerich <mads@kiilerich.com>
parents: 13372
diff changeset
   774
            cacerts = self.ui.config('web', 'cacerts')
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   775
            hostfingerprint = self.ui.config('hostfingerprints', host)
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   776
13314
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   777
            if cacerts and not hostfingerprint:
13544
66d65bccbf06 cacert: improve error report when web.cacert file does not exist
timeless <timeless@gmail.com>
parents: 13424
diff changeset
   778
                cacerts = util.expandpath(cacerts)
66d65bccbf06 cacert: improve error report when web.cacert file does not exist
timeless <timeless@gmail.com>
parents: 13424
diff changeset
   779
                if not os.path.exists(cacerts):
66d65bccbf06 cacert: improve error report when web.cacert file does not exist
timeless <timeless@gmail.com>
parents: 13424
diff changeset
   780
                    raise util.Abort(_('could not find '
66d65bccbf06 cacert: improve error report when web.cacert file does not exist
timeless <timeless@gmail.com>
parents: 13424
diff changeset
   781
                                       'web.cacerts: %s') % cacerts)
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   782
                self.sock = _ssl_wrap_socket(self.sock, self.key_file,
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   783
                    self.cert_file, cert_reqs=CERT_REQUIRED,
13544
66d65bccbf06 cacert: improve error report when web.cacert file does not exist
timeless <timeless@gmail.com>
parents: 13424
diff changeset
   784
                    ca_certs=cacerts)
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   785
                msg = _verifycert(self.sock.getpeercert(), host)
12592
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   786
                if msg:
13328
a939f08fae9c url: add --insecure option to bypass verification of ssl certificates
Yuya Nishihara <yuya@tcha.org>
parents: 13314
diff changeset
   787
                    raise util.Abort(_('%s certificate error: %s '
a939f08fae9c url: add --insecure option to bypass verification of ssl certificates
Yuya Nishihara <yuya@tcha.org>
parents: 13314
diff changeset
   788
                                       '(use --insecure to connect '
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   789
                                       'insecurely)') % (host, msg))
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   790
                self.ui.debug('%s certificate successfully verified\n' % host)
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   791
            else:
13422
ebce5196b9db url: always create BetterHTTPS connections the same way
Mads Kiilerich <mads@kiilerich.com>
parents: 13421
diff changeset
   792
                self.sock = _ssl_wrap_socket(self.sock, self.key_file,
ebce5196b9db url: always create BetterHTTPS connections the same way
Mads Kiilerich <mads@kiilerich.com>
parents: 13421
diff changeset
   793
                    self.cert_file)
13314
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   794
                if hasattr(self.sock, 'getpeercert'):
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   795
                    peercert = self.sock.getpeercert(True)
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   796
                    peerfingerprint = util.sha1(peercert).hexdigest()
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   797
                    nicefingerprint = ":".join([peerfingerprint[x:x + 2]
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   798
                        for x in xrange(0, len(peerfingerprint), 2)])
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   799
                    if hostfingerprint:
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   800
                        if peerfingerprint.lower() != \
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   801
                                hostfingerprint.replace(':', '').lower():
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   802
                            raise util.Abort(_('invalid certificate for %s '
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   803
                                               'with fingerprint %s') %
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   804
                                             (host, nicefingerprint))
13314
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   805
                        self.ui.debug('%s certificate matched fingerprint %s\n' %
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   806
                                      (host, nicefingerprint))
13314
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   807
                    else:
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   808
                        self.ui.warn(_('warning: %s certificate '
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   809
                                       'with fingerprint %s not verified '
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   810
                                       '(check hostfingerprints or web.cacerts '
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   811
                                       'config setting)\n') %
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   812
                                     (host, nicefingerprint))
13314
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   813
                else: # python 2.5 ?
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   814
                    if hostfingerprint:
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   815
                        raise util.Abort(_('no certificate for %s with '
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   816
                                           'configured hostfingerprint') % host)
13314
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   817
                    self.ui.warn(_('warning: %s certificate not verified '
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   818
                                   '(check web.cacerts config setting)\n') %
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   819
                                 host)
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   820
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   821
    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
   822
        def __init__(self, ui):
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   823
            keepalive.KeepAliveHandler.__init__(self)
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   824
            urllib2.HTTPSHandler.__init__(self)
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   825
            self.ui = ui
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   826
            self.pwmgr = passwordmgr(self.ui)
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   827
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   828
        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
   829
            _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
   830
            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
   831
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   832
        def https_open(self, req):
13372
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   833
            res = readauthforuri(self.ui, req.get_full_url())
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   834
            if res:
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   835
                group, auth = res
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   836
                self.auth = auth
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   837
                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
   838
            else:
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   839
                self.auth = None
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   840
            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
   841
10408
50fb1fe143ff url: httplib.HTTPSConnection already handles IPv6 and port parsing fine
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
   842
        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
   843
            keyfile = None
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   844
            certfile = None
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   845
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
   846
            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
   847
                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
   848
            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
   849
                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
   850
            args = args[2:]
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   851
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   852
            # 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
   853
            # hgrc, we prefer these
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   854
            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
   855
                keyfile = self.auth['key']
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   856
                certfile = self.auth['cert']
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   857
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   858
            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
   859
            conn.ui = self.ui
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   860
            return conn
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   861
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   862
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
   863
    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
   864
        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
   865
        self.retried_req = None
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   866
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   867
    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
   868
        # 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
   869
        # 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
   870
        # 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
   871
        pass
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   872
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   873
    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
   874
        # 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
   875
        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
   876
            self.retried_req = req
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   877
            self.retried = 0
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   878
        # 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
   879
        # 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
   880
        # 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
   881
        try:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   882
            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
   883
                        self, auth_header, host, req, headers)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   884
        except ValueError, inst:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   885
            arg = inst.args[0]
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   886
            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
   887
                return
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   888
            raise
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   889
11844
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   890
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
   891
    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
   892
        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
   893
        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
   894
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   895
    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
   896
        # 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
   897
        # 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
   898
        # 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
   899
        pass
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   900
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   901
    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
   902
        # 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
   903
        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
   904
            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
   905
            self.retried = 0
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   906
        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
   907
                        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
   908
9347
d0474b184347 url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents: 9122
diff changeset
   909
handlerfuncs = []
d0474b184347 url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents: 9122
diff changeset
   910
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   911
def opener(ui, authinfo=None):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   912
    '''
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   913
    construct an opener suitable for urllib2
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   914
    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
   915
    '''
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   916
    handlers = [httphandler()]
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   917
    if has_https:
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   918
        handlers.append(httpshandler(ui))
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   919
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   920
    handlers.append(proxyhandler(ui))
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   921
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   922
    passmgr = passwordmgr(ui)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   923
    if authinfo is not None:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   924
        passmgr.add_password(*authinfo)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   925
        user, passwd = authinfo[2:4]
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 9347
diff changeset
   926
        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
   927
                 (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
   928
11844
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   929
    handlers.extend((httpbasicauthhandler(passmgr),
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   930
                     httpdigestauthhandler(passmgr)))
9347
d0474b184347 url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents: 9122
diff changeset
   931
    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
   932
    opener = urllib2.build_opener(*handlers)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   933
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   934
    # 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
   935
    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
   936
    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
   937
    return opener
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   938
13818
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   939
def open(ui, url_, data=None):
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   940
    u = url(url_)
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   941
    if u.scheme:
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   942
        u.scheme = u.scheme.lower()
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   943
        url_, authinfo = u.authinfo()
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   944
    else:
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   945
        path = util.normpath(os.path.abspath(url_))
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   946
        url_ = 'file://' + urllib.pathname2url(path)
7284
ac81ffac0f35 url: fix file:// URL handling
Patrick Mezard <pmezard@gmail.com>
parents: 7270
diff changeset
   947
        authinfo = None
13818
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   948
    return opener(ui, authinfo).open(url_, data)