mercurial/url.py
author Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
Sat, 30 Apr 2011 17:21:37 +0200
changeset 14073 72c84f24b420
parent 14071 b23a8dd36a21
child 14076 924c82157d46
permissions -rw-r--r--
discovery: drop findoutgoing and simplify findcommonincoming's api This is a long desired cleanup and paves the way for new discovery. To specify subsets for bundling changes, all code should use the heads of the desired subset ("heads") and the heads of the common subset ("common") to be excluded from the bundled set. These can be used revlog.findmissing instead of revlog.nodesbetween. This fixes an actual bug exposed by the change in test-bundle-r.t where we try to bundle a changeset while specifying that said changeset is to be assumed already present in the target. This used to still bundle the changeset. It no longer does. This is similar to the bugs fixed by the recent switch to heads/common for incoming/pull.
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
13848
b2798c1defff url: be stricter about detecting schemes
Brodie Rao <brodie@bitheap.org>
parents: 13827
diff changeset
    10
import urllib, urllib2, httplib, os, socket, cStringIO, re
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
13827
f1823b9f073b url: nuke some newly-introduced underbars in identifiers
Matt Mackall <mpm@selenic.com>
parents: 13826
diff changeset
    26
    If parsefragment is False, fragment is included in query. If
f1823b9f073b url: nuke some newly-introduced underbars in identifiers
Matt Mackall <mpm@selenic.com>
parents: 13826
diff changeset
    27
    parsequery is False, query is included in path. If both are
13770
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'>
13827
f1823b9f073b url: nuke some newly-introduced underbars in identifiers
Matt Mackall <mpm@selenic.com>
parents: 13826
diff changeset
    61
    >>> url('http://host/a?b#c', parsequery=False, parsefragment=False)
13770
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 = "/!~*'()+"
13848
b2798c1defff url: be stricter about detecting schemes
Brodie Rao <brodie@bitheap.org>
parents: 13827
diff changeset
    67
    _matchscheme = re.compile(r'^[a-zA-Z0-9+.\-]+:').match
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    68
13827
f1823b9f073b url: nuke some newly-introduced underbars in identifiers
Matt Mackall <mpm@selenic.com>
parents: 13826
diff changeset
    69
    def __init__(self, path, parsequery=True, parsefragment=True):
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    70
        # 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
    71
        self.scheme = self.user = self.passwd = self.host = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    72
        self.port = self.path = self.query = self.fragment = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    73
        self._localpath = True
13826
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
    74
        self._hostport = ''
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
    75
        self._origpath = path
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    76
13807
974490c1768f url: deal with drive letters
Matt Mackall <mpm@selenic.com>
parents: 13772
diff changeset
    77
        # special case for Windows drive letters
13827
f1823b9f073b url: nuke some newly-introduced underbars in identifiers
Matt Mackall <mpm@selenic.com>
parents: 13826
diff changeset
    78
        if hasdriveletter(path):
13807
974490c1768f url: deal with drive letters
Matt Mackall <mpm@selenic.com>
parents: 13772
diff changeset
    79
            self.path = path
974490c1768f url: deal with drive letters
Matt Mackall <mpm@selenic.com>
parents: 13772
diff changeset
    80
            return
974490c1768f url: deal with drive letters
Matt Mackall <mpm@selenic.com>
parents: 13772
diff changeset
    81
13816
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    82
        # 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
    83
        # normal URLS
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    84
        if path.startswith('bundle:'):
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    85
            self.scheme = 'bundle'
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    86
            path = path[7:]
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    87
            if path.startswith('//'):
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    88
                path = path[2:]
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    89
            self.path = path
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    90
            return
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
    91
13848
b2798c1defff url: be stricter about detecting schemes
Brodie Rao <brodie@bitheap.org>
parents: 13827
diff changeset
    92
        if self._matchscheme(path):
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    93
            parts = path.split(':', 1)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    94
            if parts[0]:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    95
                self.scheme, path = parts
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    96
                self._localpath = False
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    97
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    98
        if not path:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
    99
            path = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   100
            if self._localpath:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   101
                self.path = ''
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   102
                return
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   103
        else:
13827
f1823b9f073b url: nuke some newly-introduced underbars in identifiers
Matt Mackall <mpm@selenic.com>
parents: 13826
diff changeset
   104
            if parsefragment and '#' in path:
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   105
                path, self.fragment = path.split('#', 1)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   106
                if not path:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   107
                    path = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   108
            if self._localpath:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   109
                self.path = path
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   110
                return
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   111
13827
f1823b9f073b url: nuke some newly-introduced underbars in identifiers
Matt Mackall <mpm@selenic.com>
parents: 13826
diff changeset
   112
            if parsequery and '?' in path:
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   113
                path, self.query = path.split('?', 1)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   114
                if not path:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   115
                    path = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   116
                if not self.query:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   117
                    self.query = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   118
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   119
            # // is required to specify a host/authority
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   120
            if path and path.startswith('//'):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   121
                parts = path[2:].split('/', 1)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   122
                if len(parts) > 1:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   123
                    self.host, path = parts
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   124
                    path = path
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   125
                else:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   126
                    self.host = parts[0]
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   127
                    path = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   128
                if not self.host:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   129
                    self.host = None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   130
                    if path:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   131
                        path = '/' + path
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   132
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   133
            if self.host and '@' in self.host:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   134
                self.user, self.host = self.host.rsplit('@', 1)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   135
                if ':' in self.user:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   136
                    self.user, self.passwd = self.user.split(':', 1)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   137
                if not self.host:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   138
                    self.host = None
11035
e4f911ce21de schemes: fix // breakage with Python 2.6.5 (issue2111)
Michael Glassford <glassfordmjg@gmail.com>
parents: 10794
diff changeset
   139
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   140
            # Don't split on colons in IPv6 addresses without ports
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   141
            if (self.host and ':' in self.host and
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   142
                not (self.host.startswith('[') and self.host.endswith(']'))):
13826
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   143
                self._hostport = self.host
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   144
                self.host, self.port = self.host.rsplit(':', 1)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   145
                if not self.host:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   146
                    self.host = None
13817
7f18bab2c0b0 url: abort on file:// URLs with non-localhost hosts
Brodie Rao <brodie@bitheap.org>
parents: 13816
diff changeset
   147
7f18bab2c0b0 url: abort on file:// URLs with non-localhost hosts
Brodie Rao <brodie@bitheap.org>
parents: 13816
diff changeset
   148
            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
   149
                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
   150
                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
   151
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   152
        self.path = path
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   153
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   154
        for a in ('user', 'passwd', 'host', 'port',
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   155
                  'path', 'query', 'fragment'):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   156
            v = getattr(self, a)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   157
            if v is not None:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   158
                setattr(self, a, urllib.unquote(v))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   159
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   160
    def __repr__(self):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   161
        attrs = []
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   162
        for a in ('scheme', 'user', 'passwd', 'host', 'port', 'path',
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   163
                  'query', 'fragment'):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   164
            v = getattr(self, a)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   165
            if v is not None:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   166
                attrs.append('%s: %r' % (a, v))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   167
        return '<url %s>' % ', '.join(attrs)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   168
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   169
    def __str__(self):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   170
        """Join the URL's components back into a URL string.
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   171
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   172
        Examples:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   173
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   174
        >>> str(url('http://user:pw@host:80/?foo#bar'))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   175
        'http://user:pw@host:80/?foo#bar'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   176
        >>> str(url('ssh://user:pw@[::1]:2200//home/joe#'))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   177
        'ssh://user:pw@[::1]:2200//home/joe#'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   178
        >>> str(url('http://localhost:80//'))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   179
        'http://localhost:80//'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   180
        >>> str(url('http://localhost:80/'))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   181
        'http://localhost:80/'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   182
        >>> 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
   183
        'http://localhost:80/'
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   184
        >>> str(url('bundle:foo'))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   185
        'bundle:foo'
13816
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
   186
        >>> str(url('bundle://../foo'))
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
   187
        'bundle:../foo'
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   188
        >>> str(url('path'))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   189
        'path'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   190
        """
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   191
        if self._localpath:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   192
            s = self.path
13816
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
   193
            if self.scheme == 'bundle':
2540f8087e02 url: special case bundle URL parsing to preserve backwards compatibility
Brodie Rao <brodie@bitheap.org>
parents: 13815
diff changeset
   194
                s = 'bundle:' + s
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   195
            if self.fragment:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   196
                s += '#' + self.fragment
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   197
            return s
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   198
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   199
        s = self.scheme + ':'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   200
        if (self.user or self.passwd or self.host or
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   201
            self.scheme and not self.path):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   202
            s += '//'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   203
        if self.user:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   204
            s += urllib.quote(self.user, safe=self._safechars)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   205
        if self.passwd:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   206
            s += ':' + urllib.quote(self.passwd, safe=self._safechars)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   207
        if self.user or self.passwd:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   208
            s += '@'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   209
        if self.host:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   210
            if not (self.host.startswith('[') and self.host.endswith(']')):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   211
                s += urllib.quote(self.host)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   212
            else:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   213
                s += self.host
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   214
        if self.port:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   215
            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
   216
        if self.host:
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   217
            s += '/'
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   218
        if self.path:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   219
            s += urllib.quote(self.path, safe=self._safepchars)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   220
        if self.query:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   221
            s += '?' + urllib.quote(self.query, safe=self._safepchars)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   222
        if self.fragment is not None:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   223
            s += '#' + urllib.quote(self.fragment, safe=self._safepchars)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   224
        return s
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   225
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   226
    def authinfo(self):
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   227
        user, passwd = self.user, self.passwd
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   228
        try:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   229
            self.user, self.passwd = None, None
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   230
            s = str(self)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   231
        finally:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   232
            self.user, self.passwd = user, passwd
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   233
        if not self.user:
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   234
            return (s, None)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   235
        return (s, (None, (str(self), self.host),
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   236
                    self.user, self.passwd or ''))
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   237
13826
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   238
    def localpath(self):
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   239
        if self.scheme == 'file' or self.scheme == 'bundle':
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   240
            path = self.path or '/'
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   241
            # For Windows, we need to promote hosts containing drive
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   242
            # letters to paths with drive letters.
13827
f1823b9f073b url: nuke some newly-introduced underbars in identifiers
Matt Mackall <mpm@selenic.com>
parents: 13826
diff changeset
   243
            if hasdriveletter(self._hostport):
13826
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   244
                path = self._hostport + '/' + self.path
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   245
            elif self.host is not None and self.path:
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   246
                path = '/' + path
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   247
            # We also need to handle the case of file:///C:/, which
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   248
            # should return C:/, not /C:/.
13827
f1823b9f073b url: nuke some newly-introduced underbars in identifiers
Matt Mackall <mpm@selenic.com>
parents: 13826
diff changeset
   249
            elif hasdriveletter(path):
13826
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   250
                # Strip leading slash from paths with drive names
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   251
                return path[1:]
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   252
            return path
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   253
        return self._origpath
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   254
13827
f1823b9f073b url: nuke some newly-introduced underbars in identifiers
Matt Mackall <mpm@selenic.com>
parents: 13826
diff changeset
   255
def hasscheme(path):
13770
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   256
    return bool(url(path).scheme)
4e8f2310f310 url: provide url object
Brodie Rao <brodie@bitheap.org>
parents: 13544
diff changeset
   257
13827
f1823b9f073b url: nuke some newly-introduced underbars in identifiers
Matt Mackall <mpm@selenic.com>
parents: 13826
diff changeset
   258
def hasdriveletter(path):
13814
03dfe0c85c1a url: move drive letter checking into has_drive_letter() for extensions
Brodie Rao <brodie@bitheap.org>
parents: 13807
diff changeset
   259
    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
   260
13826
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   261
def localpath(path):
13827
f1823b9f073b url: nuke some newly-introduced underbars in identifiers
Matt Mackall <mpm@selenic.com>
parents: 13826
diff changeset
   262
    return url(path, parsequery=False, parsefragment=False).localpath()
13826
e574207e3bcd url: refactor util.drop_scheme() and hg.localpath() into url.localpath()
Brodie Rao <brodie@bitheap.org>
parents: 13820
diff changeset
   263
13772
463aca32a937 url: use url.url in hidepassword() and removeauth()
Brodie Rao <brodie@bitheap.org>
parents: 13770
diff changeset
   264
def hidepassword(u):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   265
    '''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
   266
    u = url(u)
463aca32a937 url: use url.url in hidepassword() and removeauth()
Brodie Rao <brodie@bitheap.org>
parents: 13770
diff changeset
   267
    if u.passwd:
463aca32a937 url: use url.url in hidepassword() and removeauth()
Brodie Rao <brodie@bitheap.org>
parents: 13770
diff changeset
   268
        u.passwd = '***'
463aca32a937 url: use url.url in hidepassword() and removeauth()
Brodie Rao <brodie@bitheap.org>
parents: 13770
diff changeset
   269
    return str(u)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   270
13772
463aca32a937 url: use url.url in hidepassword() and removeauth()
Brodie Rao <brodie@bitheap.org>
parents: 13770
diff changeset
   271
def removeauth(u):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   272
    '''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
   273
    u = url(u)
463aca32a937 url: use url.url in hidepassword() and removeauth()
Brodie Rao <brodie@bitheap.org>
parents: 13770
diff changeset
   274
    u.user = u.passwd = None
463aca32a937 url: use url.url in hidepassword() and removeauth()
Brodie Rao <brodie@bitheap.org>
parents: 13770
diff changeset
   275
    return str(u)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   276
13371
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   277
def readauthforuri(ui, uri):
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   278
    # Read configuration
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   279
    config = dict()
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   280
    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
   281
        if '.' not in key:
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   282
            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
   283
            continue
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   284
        group, setting = key.rsplit('.', 1)
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   285
        gdict = config.setdefault(group, dict())
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   286
        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
   287
            val = util.expandpath(val)
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   288
        gdict[setting] = val
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   289
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   290
    # Find the best match
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   291
    scheme, hostpath = uri.split('://', 1)
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   292
    bestlen = 0
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   293
    bestauth = None
13372
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   294
    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
   295
        prefix = auth.get('prefix')
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   296
        if not prefix:
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   297
            continue
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   298
        p = prefix.split('://', 1)
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   299
        if len(p) > 1:
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   300
            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
   301
        else:
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   302
            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
   303
        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
   304
            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
   305
            bestlen = len(prefix)
13372
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   306
            bestauth = group, auth
13371
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   307
    return bestauth
c691cfdc6b4d url: move [auth] parsing out into a utility function
Steve Borho <steve@borho.org>
parents: 13370
diff changeset
   308
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   309
class passwordmgr(urllib2.HTTPPasswordMgrWithDefaultRealm):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   310
    def __init__(self, ui):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   311
        urllib2.HTTPPasswordMgrWithDefaultRealm.__init__(self)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   312
        self.ui = ui
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   313
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   314
    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
   315
        authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm.find_user_password(
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   316
            self, realm, authuri)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   317
        user, passwd = authinfo
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   318
        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
   319
            self._writedebug(user, passwd)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   320
            return (user, passwd)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   321
8344
873429914ec5 url: fix bug in passwordmgr related to auth configuration
Sune Foldager <cryo@cyanite.org>
parents: 8333
diff changeset
   322
        if not user:
13372
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   323
            res = readauthforuri(self.ui, authuri)
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   324
            if res:
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   325
                group, auth = res
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   326
                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
   327
                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
   328
        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
   329
            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
   330
                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
   331
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
   332
            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
   333
            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
   334
            if user:
12862
9d6adddc8eea url: show realm/user when asking for username/password
timeless <timeless@gmail.com>
parents: 12770
diff changeset
   335
                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
   336
            else:
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
   337
                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
   338
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
   339
            if not passwd:
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
   340
                passwd = self.ui.getpass()
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   341
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   342
        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
   343
        self._writedebug(user, passwd)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   344
        return (user, passwd)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   345
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
   346
    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
   347
        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
   348
        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
   349
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   350
class proxyhandler(urllib2.ProxyHandler):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   351
    def __init__(self, ui):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   352
        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
   353
        # XXX proxyauthinfo = None
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   354
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   355
        if proxyurl:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   356
            # 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
   357
            if not (proxyurl.startswith('http:') or
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   358
                    proxyurl.startswith('https:')):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   359
                proxyurl = 'http://' + proxyurl + '/'
13820
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
   360
            proxy = url(proxyurl)
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
   361
            if not proxy.user:
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
   362
                proxy.user = ui.config("http_proxy", "user")
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
   363
                proxy.passwd = ui.config("http_proxy", "passwd")
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   364
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   365
            # 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
   366
            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
   367
            no_list.extend([p.lower() for
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   368
                            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
   369
            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
   370
                            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
   371
                            if p.strip()])
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   372
            # "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
   373
            if ui.configbool("http_proxy", "always"):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   374
                self.no_list = []
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   375
            else:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   376
                self.no_list = no_list
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   377
13820
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
   378
            proxyurl = str(proxy)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   379
            proxies = {'http': proxyurl, 'https': proxyurl}
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 9347
diff changeset
   380
            ui.debug('proxying through http://%s:%s\n' %
13820
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
   381
                      (proxy.host, proxy.port))
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   382
        else:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   383
            proxies = {}
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   384
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   385
        # 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
   386
        # 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
   387
        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
   388
            try:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   389
                if env in os.environ:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   390
                    del os.environ[env]
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   391
            except OSError:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   392
                pass
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   393
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   394
        urllib2.ProxyHandler.__init__(self, proxies)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   395
        self.ui = ui
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   396
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   397
    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
   398
        host = req.get_host().split(':')[0]
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   399
        if host in self.no_list:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   400
            return None
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   401
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   402
        # 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
   403
        # (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
   404
        baseclass = req.__class__
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   405
        class _request(baseclass):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   406
            def add_header(self, key, val):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   407
                if key.lower() == 'proxy-authorization':
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   408
                    val = val.strip()
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   409
                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
   410
        req.__class__ = _request
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   411
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   412
        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
   413
11880
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   414
class httpsendfile(object):
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   415
    """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
   416
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   417
    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
   418
    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
   419
    """
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   420
13115
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   421
    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
   422
        # 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
   423
        # 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
   424
        # one
13115
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   425
        self.ui = ui
11880
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   426
        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
   427
        self.seek = self._data.seek
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   428
        self.close = self._data.close
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   429
        self.write = self._data.write
13115
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   430
        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
   431
        self._pos = 0
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   432
        self._total = len(self) / 1024 * 2
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   433
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   434
    def read(self, *args, **kwargs):
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   435
        try:
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   436
            ret = self._data.read(*args, **kwargs)
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   437
        except EOFError:
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   438
            self.ui.progress(_('sending'), None)
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   439
        self._pos += len(ret)
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   440
        # 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
   441
        # 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
   442
        # 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
   443
        # 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
   444
        # 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
   445
        self.ui.progress(_('sending'), self._pos / 1024,
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   446
                         unit=_('kb'), total=self._total)
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   447
        return ret
11880
e3526634d5a3 url.py: removed 'file' inheritance in the httpsendfile class
Renato Cunha <renatoc@gmail.com>
parents: 11876
diff changeset
   448
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   449
    def __len__(self):
13115
bda5f35fbf67 httpsendfile: record progress information during read()
Augie Fackler <durin42@gmail.com>
parents: 12906
diff changeset
   450
        return self._len
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   451
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
   452
def _gen_sendfile(orgsend):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   453
    def _sendfile(self, data):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   454
        # send a file
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   455
        if isinstance(data, httpsendfile):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   456
            # 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
   457
            data.seek(0)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   458
            for chunk in util.filechunkiter(data):
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
   459
                orgsend(self, chunk)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   460
        else:
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
   461
            orgsend(self, data)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   462
    return _sendfile
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   463
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   464
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
   465
if has_https:
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   466
    try:
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   467
        # 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
   468
        import ssl
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   469
        _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
   470
        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
   471
    except ImportError:
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   472
        CERT_REQUIRED = 2
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   473
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   474
        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
   475
                             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
   476
            if ca_certs:
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   477
                raise util.Abort(_(
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   478
                    '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
   479
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   480
            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
   481
            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
   482
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   483
    try:
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   484
        _create_connection = socket.create_connection
10411
af4c42ec19ed ssl: fix compatibility with pre-2.6 Python
Matt Mackall <mpm@selenic.com>
parents: 10409
diff changeset
   485
    except AttributeError:
10482
95265afff99f url: fix python < 2.6 with ssl installed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10415
diff changeset
   486
        _GLOBAL_DEFAULT_TIMEOUT = object()
95265afff99f url: fix python < 2.6 with ssl installed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10415
diff changeset
   487
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   488
        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
   489
                               source_address=None):
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   490
            # 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
   491
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   492
            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
   493
            host, port = address
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   494
            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
   495
                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
   496
                sock = None
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   497
                try:
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   498
                    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
   499
                    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
   500
                        sock.settimeout(timeout)
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   501
                    if source_address:
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   502
                        sock.bind(source_address)
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   503
                    sock.connect(sa)
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   504
                    return sock
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   505
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   506
                except socket.error, msg:
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   507
                    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
   508
                        sock.close()
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   509
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   510
            raise socket.error, msg
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   511
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   512
class httpconnection(keepalive.HTTPConnection):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   513
    # must be able to send big bundle as stream.
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
   514
    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
   515
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   516
    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
   517
        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
   518
            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
   519
            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
   520
            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
   521
                # 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
   522
                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
   523
        else:
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   524
            keepalive.HTTPConnection.connect(self)
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   525
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   526
    def getresponse(self):
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   527
        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
   528
        if proxyres:
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   529
            if proxyres.will_close:
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   530
                self.close()
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   531
            self.proxyres = None
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   532
            return proxyres
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   533
        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
   534
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   535
# 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
   536
# 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
   537
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
   538
    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
   539
        tunnel_host = req._tunnel_host
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   540
        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
   541
            tunnel_host = 'https://' + tunnel_host
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   542
        new_tunnel = True
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   543
    else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   544
        tunnel_host = req.get_selector()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   545
        new_tunnel = False
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   546
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   547
    if new_tunnel or tunnel_host == req.get_full_url(): # has proxy
13820
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
   548
        u = url(tunnel_host)
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
   549
        if new_tunnel or u.scheme == 'https': # only use CONNECT for HTTPS
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
   550
            h.realhostport = ':'.join([u.host, (u.port or '443')])
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   551
            h.headers = req.headers.copy()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   552
            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
   553
            return
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   554
10415
677f15da38c1 url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10411
diff changeset
   555
    h.realhostport = None
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   556
    h.headers = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   557
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   558
def _generic_proxytunnel(self):
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   559
    proxyheaders = dict(
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   560
            [(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
   561
             if x.lower().startswith('proxy-')])
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   562
    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
   563
    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
   564
    for header in proxyheaders.iteritems():
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   565
        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
   566
    self.send('\r\n')
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   567
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   568
    # 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
   569
    # 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
   570
    # 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
   571
    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
   572
                              strict=self.strict,
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   573
                              method=self._method)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   574
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   575
    while True:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   576
        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
   577
        if status != httplib.CONTINUE:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   578
            break
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   579
        while True:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   580
            skip = res.fp.readline().strip()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   581
            if not skip:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   582
                break
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   583
    res.status = status
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   584
    res.reason = reason.strip()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   585
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   586
    if res.status == 200:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   587
        while True:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   588
            line = res.fp.readline()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   589
            if line == '\r\n':
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   590
                break
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   591
        return True
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   592
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   593
    if version == 'HTTP/1.0':
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   594
        res.version = 10
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   595
    elif version.startswith('HTTP/1.'):
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   596
        res.version = 11
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   597
    elif version == 'HTTP/0.9':
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   598
        res.version = 9
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   599
    else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   600
        raise httplib.UnknownProtocol(version)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   601
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   602
    if res.version == 9:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   603
        res.length = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   604
        res.chunked = 0
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   605
        res.will_close = 1
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   606
        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
   607
        return False
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   608
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   609
    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
   610
    res.msg.fp = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   611
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   612
    # 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
   613
    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
   614
    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
   615
        res.chunked = 1
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   616
        res.chunk_left = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   617
    else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   618
        res.chunked = 0
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   619
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   620
    # 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
   621
    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
   622
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   623
    # 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
   624
    # 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
   625
    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
   626
    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
   627
        try:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   628
            res.length = int(length)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   629
        except ValueError:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   630
            res.length = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   631
        else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   632
            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
   633
                res.length = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   634
    else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   635
        res.length = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   636
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   637
    # 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
   638
    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
   639
        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
   640
        res._method == 'HEAD'):
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   641
        res.length = 0
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   642
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   643
    # 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
   644
    # 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
   645
    # WILL close.
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   646
    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
   647
       not res.chunked and
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   648
       res.length is None):
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   649
        res.will_close = 1
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   650
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   651
    self.proxyres = res
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   652
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   653
    return False
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   654
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   655
class httphandler(keepalive.HTTPHandler):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   656
    def http_open(self, req):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   657
        return self.do_open(httpconnection, req)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   658
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
   659
    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
   660
        _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
   661
        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
   662
12592
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   663
def _verifycert(cert, hostname):
12742
6ab4a7d3c179 url: validity (notBefore/notAfter) is checked by OpenSSL (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12607
diff changeset
   664
    '''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
   665
    CRLs is not handled.
12770
614f0d8724ab check-code: find trailing whitespace
Martin Geisler <mg@lazybytes.net>
parents: 12742
diff changeset
   666
12592
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   667
    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
   668
    '''
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   669
    if not cert:
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   670
        return _('no certificate received')
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   671
    dnsname = hostname.lower()
13249
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   672
    def matchdnsname(certname):
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   673
        return (certname == dnsname or
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   674
                '.' 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
   675
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   676
    san = cert.get('subjectAltName', [])
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   677
    if san:
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   678
        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
   679
        for name in certnames:
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   680
            if matchdnsname(name):
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   681
                return None
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   682
        return _('certificate is for %s') % ', '.join(certnames)
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   683
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   684
    # 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
   685
    for s in cert.get('subject', []):
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   686
        key, value = s[0]
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   687
        if key == 'commonName':
13248
00411a4fa1bb url: fix UnicodeDecodeError on certificate verification error
Yuya Nishihara <yuya@tcha.org>
parents: 13231
diff changeset
   688
            try:
00411a4fa1bb url: fix UnicodeDecodeError on certificate verification error
Yuya Nishihara <yuya@tcha.org>
parents: 13231
diff changeset
   689
                # 'subject' entries are unicode
00411a4fa1bb url: fix UnicodeDecodeError on certificate verification error
Yuya Nishihara <yuya@tcha.org>
parents: 13231
diff changeset
   690
                certname = value.lower().encode('ascii')
00411a4fa1bb url: fix UnicodeDecodeError on certificate verification error
Yuya Nishihara <yuya@tcha.org>
parents: 13231
diff changeset
   691
            except UnicodeEncodeError:
00411a4fa1bb url: fix UnicodeDecodeError on certificate verification error
Yuya Nishihara <yuya@tcha.org>
parents: 13231
diff changeset
   692
                return _('IDN in certificate not supported')
13249
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   693
            if matchdnsname(certname):
12592
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   694
                return None
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents: 12391
diff changeset
   695
            return _('certificate is for %s') % certname
13249
75d0c38a0bca url: check subjectAltName when verifying ssl certificate
Yuya Nishihara <yuya@tcha.org>
parents: 13248
diff changeset
   696
    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
   697
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   698
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
   699
    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
   700
        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
   701
        # 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
   702
        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
   703
        getresponse = keepalive.wrapgetresponse(httplib.HTTPSConnection)
9726
430e59ff3437 keepalive: handle broken pipes gracefully during large POSTs
Augie Fackler <durin42@gmail.com>
parents: 9467
diff changeset
   704
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   705
        def connect(self):
13422
ebce5196b9db url: always create BetterHTTPS connections the same way
Mads Kiilerich <mads@kiilerich.com>
parents: 13421
diff changeset
   706
            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
   707
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   708
            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
   709
            if self.realhostport: # use CONNECT proxy
14064
e4bfb9c337f3 remove unused imports and variables
Alexander Solovyov <alexander@solovyov.net>
parents: 13902
diff changeset
   710
                _generic_proxytunnel(self)
13424
08f9c587141f url: merge BetterHTTPS with httpsconnection to get some proxy https validation
Mads Kiilerich <mads@kiilerich.com>
parents: 13422
diff changeset
   711
                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
   712
13419
1cc73868c740 url: remove test for self.ui in BetterHTTPS
Mads Kiilerich <mads@kiilerich.com>
parents: 13372
diff changeset
   713
            cacerts = self.ui.config('web', 'cacerts')
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   714
            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
   715
13314
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   716
            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
   717
                cacerts = util.expandpath(cacerts)
66d65bccbf06 cacert: improve error report when web.cacert file does not exist
timeless <timeless@gmail.com>
parents: 13424
diff changeset
   718
                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
   719
                    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
   720
                                       'web.cacerts: %s') % cacerts)
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   721
                self.sock = _ssl_wrap_socket(self.sock, self.key_file,
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   722
                    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
   723
                    ca_certs=cacerts)
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   724
                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
   725
                if msg:
13328
a939f08fae9c url: add --insecure option to bypass verification of ssl certificates
Yuya Nishihara <yuya@tcha.org>
parents: 13314
diff changeset
   726
                    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
   727
                                       '(use --insecure to connect '
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   728
                                       'insecurely)') % (host, msg))
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   729
                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
   730
            else:
13422
ebce5196b9db url: always create BetterHTTPS connections the same way
Mads Kiilerich <mads@kiilerich.com>
parents: 13421
diff changeset
   731
                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
   732
                    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
   733
                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
   734
                    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
   735
                    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
   736
                    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
   737
                        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
   738
                    if hostfingerprint:
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   739
                        if peerfingerprint.lower() != \
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   740
                                hostfingerprint.replace(':', '').lower():
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   741
                            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
   742
                                               'with fingerprint %s') %
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   743
                                             (host, nicefingerprint))
13314
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   744
                        self.ui.debug('%s certificate matched fingerprint %s\n' %
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   745
                                      (host, nicefingerprint))
13314
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   746
                    else:
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   747
                        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
   748
                                       '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
   749
                                       '(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
   750
                                       'config setting)\n') %
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   751
                                     (host, nicefingerprint))
13314
8dc488dfcdb4 url: 'ssh known host'-like checking of fingerprints of HTTPS certificates
Mads Kiilerich <mads@kiilerich.com>
parents: 13249
diff changeset
   752
                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
   753
                    if hostfingerprint:
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   754
                        raise util.Abort(_('no certificate for %s with '
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   755
                                           '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
   756
                    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
   757
                                   '(check web.cacerts config setting)\n') %
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
   758
                                 host)
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   759
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   760
    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
   761
        def __init__(self, ui):
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   762
            keepalive.KeepAliveHandler.__init__(self)
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   763
            urllib2.HTTPSHandler.__init__(self)
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   764
            self.ui = ui
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   765
            self.pwmgr = passwordmgr(self.ui)
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   766
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
   767
        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
   768
            _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
   769
            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
   770
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   771
        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
   772
            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
   773
            if res:
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   774
                group, auth = res
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   775
                self.auth = auth
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   776
                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
   777
            else:
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
   778
                self.auth = None
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   779
            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
   780
10408
50fb1fe143ff url: httplib.HTTPSConnection already handles IPv6 and port parsing fine
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
   781
        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
   782
            keyfile = None
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   783
            certfile = None
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   784
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
   785
            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
   786
                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
   787
            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
   788
                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
   789
            args = args[2:]
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   790
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   791
            # 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
   792
            # hgrc, we prefer these
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   793
            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
   794
                keyfile = self.auth['key']
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   795
                certfile = self.auth['cert']
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   796
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   797
            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
   798
            conn.ui = self.ui
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
   799
            return conn
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   800
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   801
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
   802
    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
   803
        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
   804
        self.retried_req = None
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   805
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   806
    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
   807
        # 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
   808
        # 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
   809
        # 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
   810
        pass
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   811
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   812
    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
   813
        # 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
   814
        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
   815
            self.retried_req = req
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   816
            self.retried = 0
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
   817
        # 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
   818
        # 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
   819
        # 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
   820
        try:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   821
            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
   822
                        self, auth_header, host, req, headers)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   823
        except ValueError, inst:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   824
            arg = inst.args[0]
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   825
            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
   826
                return
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   827
            raise
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   828
11844
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   829
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
   830
    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
   831
        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
   832
        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
   833
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   834
    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
   835
        # 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
   836
        # 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
   837
        # 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
   838
        pass
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   839
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   840
    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
   841
        # 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
   842
        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
   843
            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
   844
            self.retried = 0
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   845
        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
   846
                        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
   847
9347
d0474b184347 url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents: 9122
diff changeset
   848
handlerfuncs = []
d0474b184347 url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents: 9122
diff changeset
   849
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   850
def opener(ui, authinfo=None):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   851
    '''
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   852
    construct an opener suitable for urllib2
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   853
    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
   854
    '''
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   855
    handlers = [httphandler()]
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   856
    if has_https:
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
   857
        handlers.append(httpshandler(ui))
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   858
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   859
    handlers.append(proxyhandler(ui))
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   860
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   861
    passmgr = passwordmgr(ui)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   862
    if authinfo is not None:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   863
        passmgr.add_password(*authinfo)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   864
        user, passwd = authinfo[2:4]
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 9347
diff changeset
   865
        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
   866
                 (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
   867
11844
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
   868
    handlers.extend((httpbasicauthhandler(passmgr),
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   869
                     httpdigestauthhandler(passmgr)))
9347
d0474b184347 url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents: 9122
diff changeset
   870
    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
   871
    opener = urllib2.build_opener(*handlers)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   872
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   873
    # 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
   874
    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
   875
    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
   876
    return opener
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   877
13818
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   878
def open(ui, url_, data=None):
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   879
    u = url(url_)
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   880
    if u.scheme:
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   881
        u.scheme = u.scheme.lower()
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   882
        url_, authinfo = u.authinfo()
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   883
    else:
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   884
        path = util.normpath(os.path.abspath(url_))
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   885
        url_ = 'file://' + urllib.pathname2url(path)
7284
ac81ffac0f35 url: fix file:// URL handling
Patrick Mezard <pmezard@gmail.com>
parents: 7270
diff changeset
   886
        authinfo = None
13818
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
   887
    return opener(ui, authinfo).open(url_, data)