Mercurial > hg-stable
annotate mercurial/url.py @ 10425:f8a9de664a1c
ui.progress: clarify termination requirement
author | Augie Fackler <durin42@gmail.com> |
---|---|
date | Sun, 07 Feb 2010 10:25:18 -0600 |
parents | 677f15da38c1 |
children | 95265afff99f |
rev | line source |
---|---|
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 | 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 |
8590
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
10 import urllib, urllib2, urlparse, httplib, os, re, socket, cStringIO |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
11 from i18n import _ |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
12 import keepalive, util |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
13 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
14 def hidepassword(url): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
15 '''hide user credential in a url string''' |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
16 scheme, netloc, path, params, query, fragment = urlparse.urlparse(url) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
17 netloc = re.sub('([^:]*):([^@]*)@(.*)', r'\1:***@\3', netloc) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
18 return urlparse.urlunparse((scheme, netloc, path, params, query, fragment)) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
19 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
20 def removeauth(url): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
21 '''remove all authentication information from a url string''' |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
22 scheme, netloc, path, params, query, fragment = urlparse.urlparse(url) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
23 netloc = netloc[netloc.find('@')+1:] |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
24 return urlparse.urlunparse((scheme, netloc, path, params, query, fragment)) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
25 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
26 def netlocsplit(netloc): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
27 '''split [user[:passwd]@]host[:port] into 4-tuple.''' |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
28 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
29 a = netloc.find('@') |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
30 if a == -1: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
31 user, passwd = None, None |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
32 else: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
33 userpass, netloc = netloc[:a], netloc[a + 1:] |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
34 c = userpass.find(':') |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
35 if c == -1: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
36 user, passwd = urllib.unquote(userpass), None |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
37 else: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
38 user = urllib.unquote(userpass[:c]) |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
39 passwd = urllib.unquote(userpass[c + 1:]) |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
40 c = netloc.find(':') |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
41 if c == -1: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
42 host, port = netloc, None |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
43 else: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
44 host, port = netloc[:c], netloc[c + 1:] |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
45 return host, port, user, passwd |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
46 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
47 def netlocunsplit(host, port, user=None, passwd=None): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
48 '''turn host, port, user, passwd into [user[:passwd]@]host[:port].''' |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
49 if port: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
50 hostport = host + ':' + port |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
51 else: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
52 hostport = host |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
53 if user: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
54 if passwd: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
55 userpass = urllib.quote(user) + ':' + urllib.quote(passwd) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
56 else: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
57 userpass = urllib.quote(user) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
58 return userpass + '@' + hostport |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
59 return hostport |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
60 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
61 _safe = ('abcdefghijklmnopqrstuvwxyz' |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
62 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
63 '0123456789' '_.-/') |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
64 _safeset = None |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
65 _hex = None |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
66 def quotepath(path): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
67 '''quote the path part of a URL |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
68 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
69 This is similar to urllib.quote, but it also tries to avoid |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
70 quoting things twice (inspired by wget): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
71 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
72 >>> quotepath('abc def') |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
73 'abc%20def' |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
74 >>> quotepath('abc%20def') |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
75 'abc%20def' |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
76 >>> quotepath('abc%20 def') |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
77 'abc%20%20def' |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
78 >>> quotepath('abc def%20') |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
79 'abc%20def%20' |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
80 >>> quotepath('abc def%2') |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
81 'abc%20def%252' |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
82 >>> quotepath('abc def%') |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
83 'abc%20def%25' |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
84 ''' |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
85 global _safeset, _hex |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
86 if _safeset is None: |
8150
bbc24c0753a0
util: use built-in set and frozenset
Martin Geisler <mg@lazybytes.net>
parents:
7285
diff
changeset
|
87 _safeset = set(_safe) |
bbc24c0753a0
util: use built-in set and frozenset
Martin Geisler <mg@lazybytes.net>
parents:
7285
diff
changeset
|
88 _hex = set('abcdefABCDEF0123456789') |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
89 l = list(path) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
90 for i in xrange(len(l)): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
91 c = l[i] |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
92 if (c == '%' and i + 2 < len(l) and |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
93 l[i + 1] in _hex and l[i + 2] in _hex): |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
94 pass |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
95 elif c not in _safeset: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
96 l[i] = '%%%02X' % ord(c) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
97 return ''.join(l) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
98 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
99 class passwordmgr(urllib2.HTTPPasswordMgrWithDefaultRealm): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
100 def __init__(self, ui): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
101 urllib2.HTTPPasswordMgrWithDefaultRealm.__init__(self) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
102 self.ui = ui |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
103 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
104 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
|
105 authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm.find_user_password( |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
106 self, realm, authuri) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
107 user, passwd = authinfo |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
108 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
|
109 self._writedebug(user, passwd) |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
110 return (user, passwd) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
111 |
8344
873429914ec5
url: fix bug in passwordmgr related to auth configuration
Sune Foldager <cryo@cyanite.org>
parents:
8333
diff
changeset
|
112 if not user: |
8847
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
113 auth = self.readauthtoken(authuri) |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
114 if auth: |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
115 user, passwd = auth.get('username'), auth.get('password') |
8333
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
116 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
|
117 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
|
118 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
|
119 |
8333
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
120 self.ui.write(_("http authorization required\n")) |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
121 self.ui.status(_("realm: %s\n") % realm) |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
122 if user: |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
123 self.ui.status(_("user: %s\n") % user) |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
124 else: |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
125 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
|
126 |
8333
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
127 if not passwd: |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
128 passwd = self.ui.getpass() |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
129 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
130 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
|
131 self._writedebug(user, passwd) |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
132 return (user, passwd) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
133 |
8333
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
134 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
|
135 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
|
136 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
|
137 |
8847
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
138 def readauthtoken(self, uri): |
8333
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
139 # Read configuration |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
140 config = dict() |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
141 for key, val in self.ui.configitems('auth'): |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
142 group, setting = key.split('.', 1) |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
143 gdict = config.setdefault(group, dict()) |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
144 gdict[setting] = val |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
145 |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
146 # Find the best match |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
147 scheme, hostpath = uri.split('://', 1) |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
148 bestlen = 0 |
8847
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
149 bestauth = None |
8333
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
150 for auth in config.itervalues(): |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
151 prefix = auth.get('prefix') |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
152 if not prefix: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
153 continue |
8333
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
154 p = prefix.split('://', 1) |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
155 if len(p) > 1: |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
156 schemes, prefix = [p[0]], p[1] |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
157 else: |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
158 schemes = (auth.get('schemes') or 'https').split() |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
159 if (prefix == '*' or hostpath.startswith(prefix)) and \ |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
160 len(prefix) > bestlen and scheme in schemes: |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
161 bestlen = len(prefix) |
8847
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
162 bestauth = auth |
8333
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
163 return bestauth |
89c80c3dc584
allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents:
8225
diff
changeset
|
164 |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
165 class proxyhandler(urllib2.ProxyHandler): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
166 def __init__(self, ui): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
167 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
|
168 # XXX proxyauthinfo = None |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
169 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
170 if proxyurl: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
171 # 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
|
172 if not (proxyurl.startswith('http:') or |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
173 proxyurl.startswith('https:')): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
174 proxyurl = 'http://' + proxyurl + '/' |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
175 snpqf = urlparse.urlsplit(proxyurl) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
176 proxyscheme, proxynetloc, proxypath, proxyquery, proxyfrag = snpqf |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
177 hpup = netlocsplit(proxynetloc) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
178 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
179 proxyhost, proxyport, proxyuser, proxypasswd = hpup |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
180 if not proxyuser: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
181 proxyuser = ui.config("http_proxy", "user") |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
182 proxypasswd = ui.config("http_proxy", "passwd") |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
183 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
184 # 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
|
185 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
|
186 no_list.extend([p.lower() for |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
187 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
|
188 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
|
189 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
|
190 if p.strip()]) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
191 # "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
|
192 if ui.configbool("http_proxy", "always"): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
193 self.no_list = [] |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
194 else: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
195 self.no_list = no_list |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
196 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
197 proxyurl = urlparse.urlunsplit(( |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
198 proxyscheme, netlocunsplit(proxyhost, proxyport, |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
199 proxyuser, proxypasswd or ''), |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
200 proxypath, proxyquery, proxyfrag)) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
201 proxies = {'http': proxyurl, 'https': proxyurl} |
9467
4c041f1ee1b4
do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents:
9347
diff
changeset
|
202 ui.debug('proxying through http://%s:%s\n' % |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
203 (proxyhost, proxyport)) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
204 else: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
205 proxies = {} |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
206 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
207 # 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
|
208 # 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
|
209 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
|
210 try: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
211 if env in os.environ: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
212 del os.environ[env] |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
213 except OSError: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
214 pass |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
215 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
216 urllib2.ProxyHandler.__init__(self, proxies) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
217 self.ui = ui |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
218 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
219 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
|
220 host = req.get_host().split(':')[0] |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
221 if host in self.no_list: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
222 return None |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
223 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
224 # 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
|
225 # (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
|
226 baseclass = req.__class__ |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
227 class _request(baseclass): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
228 def add_header(self, key, val): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
229 if key.lower() == 'proxy-authorization': |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
230 val = val.strip() |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
231 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
|
232 req.__class__ = _request |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
233 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
234 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
|
235 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
236 class httpsendfile(file): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
237 def __len__(self): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
238 return os.fstat(self.fileno()).st_size |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
239 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
240 def _gen_sendfile(connection): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
241 def _sendfile(self, data): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
242 # send a file |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
243 if isinstance(data, httpsendfile): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
244 # 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
|
245 data.seek(0) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
246 for chunk in util.filechunkiter(data): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
247 connection.send(self, chunk) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
248 else: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
249 connection.send(self, data) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
250 return _sendfile |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
251 |
8590
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
252 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
|
253 if has_https: |
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
254 try: |
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
255 # 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
|
256 import ssl |
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
257 _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
|
258 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
|
259 except ImportError: |
10409
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
260 CERT_REQUIRED = 2 |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
261 |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
262 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
|
263 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
|
264 if ca_certs: |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
265 raise util.Abort(_( |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
266 '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
|
267 |
8590
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
268 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
|
269 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
|
270 |
10409
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
271 _GLOBAL_DEFAULT_TIMEOUT = object() |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
272 |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
273 try: |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
274 _create_connection = socket.create_connection |
10411
af4c42ec19ed
ssl: fix compatibility with pre-2.6 Python
Matt Mackall <mpm@selenic.com>
parents:
10409
diff
changeset
|
275 except AttributeError: |
10409
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
276 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
|
277 source_address=None): |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
278 # 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
|
279 |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
280 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
|
281 host, port = address |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
282 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
|
283 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
|
284 sock = None |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
285 try: |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
286 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
|
287 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
|
288 sock.settimeout(timeout) |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
289 if source_address: |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
290 sock.bind(source_address) |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
291 sock.connect(sa) |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
292 return sock |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
293 |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
294 except socket.error, msg: |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
295 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
|
296 sock.close() |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
297 |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
298 raise socket.error, msg |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
299 |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
300 class httpconnection(keepalive.HTTPConnection): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
301 # must be able to send big bundle as stream. |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
302 send = _gen_sendfile(keepalive.HTTPConnection) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
303 |
8590
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
304 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
|
305 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
|
306 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
|
307 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
|
308 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
|
309 # 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
|
310 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
|
311 else: |
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
312 keepalive.HTTPConnection.connect(self) |
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
313 |
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
314 def getresponse(self): |
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
315 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
|
316 if proxyres: |
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
317 if proxyres.will_close: |
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
318 self.close() |
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
319 self.proxyres = None |
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
320 return proxyres |
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
321 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
|
322 |
9852
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
323 # 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
|
324 # 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
|
325 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
|
326 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
|
327 tunnel_host = req._tunnel_host |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
328 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
|
329 tunnel_host = 'https://' + tunnel_host |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
330 new_tunnel = True |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
331 else: |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
332 tunnel_host = req.get_selector() |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
333 new_tunnel = False |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
334 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
335 if new_tunnel or tunnel_host == req.get_full_url(): # has proxy |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
336 urlparts = urlparse.urlparse(tunnel_host) |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
337 if new_tunnel or urlparts[0] == 'https': # only use CONNECT for HTTPS |
10415
677f15da38c1
url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10411
diff
changeset
|
338 realhostport = urlparts[1] |
677f15da38c1
url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10411
diff
changeset
|
339 if realhostport[-1] == ']' or ':' not in realhostport: |
677f15da38c1
url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10411
diff
changeset
|
340 realhostport += ':443' |
9852
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
341 |
10415
677f15da38c1
url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10411
diff
changeset
|
342 h.realhostport = realhostport |
9852
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
343 h.headers = req.headers.copy() |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
344 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
|
345 return |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
346 |
10415
677f15da38c1
url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10411
diff
changeset
|
347 h.realhostport = None |
9852
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
348 h.headers = None |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
349 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
350 def _generic_proxytunnel(self): |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
351 proxyheaders = dict( |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
352 [(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
|
353 if x.lower().startswith('proxy-')]) |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
354 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
|
355 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
|
356 for header in proxyheaders.iteritems(): |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
357 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
|
358 self.send('\r\n') |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
359 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
360 # 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
|
361 # 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
|
362 # 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
|
363 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
|
364 strict=self.strict, |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
365 method=self._method) |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
366 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
367 while True: |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
368 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
|
369 if status != httplib.CONTINUE: |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
370 break |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
371 while True: |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
372 skip = res.fp.readline().strip() |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
373 if not skip: |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
374 break |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
375 res.status = status |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
376 res.reason = reason.strip() |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
377 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
378 if res.status == 200: |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
379 while True: |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
380 line = res.fp.readline() |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
381 if line == '\r\n': |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
382 break |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
383 return True |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
384 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
385 if version == 'HTTP/1.0': |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
386 res.version = 10 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
387 elif version.startswith('HTTP/1.'): |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
388 res.version = 11 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
389 elif version == 'HTTP/0.9': |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
390 res.version = 9 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
391 else: |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
392 raise httplib.UnknownProtocol(version) |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
393 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
394 if res.version == 9: |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
395 res.length = None |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
396 res.chunked = 0 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
397 res.will_close = 1 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
398 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
|
399 return False |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
400 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
401 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
|
402 res.msg.fp = None |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
403 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
404 # 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
|
405 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
|
406 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
|
407 res.chunked = 1 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
408 res.chunk_left = None |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
409 else: |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
410 res.chunked = 0 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
411 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
412 # 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
|
413 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
|
414 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
415 # 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
|
416 # 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
|
417 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
|
418 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
|
419 try: |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
420 res.length = int(length) |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
421 except ValueError: |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
422 res.length = None |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
423 else: |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
424 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
|
425 res.length = None |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
426 else: |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
427 res.length = None |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
428 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
429 # 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
|
430 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
|
431 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
|
432 res._method == 'HEAD'): |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
433 res.length = 0 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
434 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
435 # 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
|
436 # 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
|
437 # WILL close. |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
438 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
|
439 not res.chunked and |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
440 res.length is None): |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
441 res.will_close = 1 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
442 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
443 self.proxyres = res |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
444 |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
445 return False |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
446 |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
447 class httphandler(keepalive.HTTPHandler): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
448 def http_open(self, req): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
449 return self.do_open(httpconnection, req) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
450 |
8590
59acb9c7d90f
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents:
8344
diff
changeset
|
451 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
|
452 _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
|
453 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
|
454 |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
455 def __del__(self): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
456 self.close_all() |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
457 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
458 if has_https: |
9726
430e59ff3437
keepalive: handle broken pipes gracefully during large POSTs
Augie Fackler <durin42@gmail.com>
parents:
9467
diff
changeset
|
459 class BetterHTTPS(httplib.HTTPSConnection): |
430e59ff3437
keepalive: handle broken pipes gracefully during large POSTs
Augie Fackler <durin42@gmail.com>
parents:
9467
diff
changeset
|
460 send = keepalive.safesend |
430e59ff3437
keepalive: handle broken pipes gracefully during large POSTs
Augie Fackler <durin42@gmail.com>
parents:
9467
diff
changeset
|
461 |
10409
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
462 def connect(self): |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
463 if hasattr(self, 'ui'): |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
464 cacerts = self.ui.config('web', 'cacerts') |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
465 else: |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
466 cacerts = None |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
467 |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
468 if cacerts: |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
469 sock = _create_connection((self.host, self.port)) |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
470 self.sock = _ssl_wrap_socket(sock, self.key_file, |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
471 self.cert_file, cert_reqs=CERT_REQUIRED, |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
472 ca_certs=cacerts) |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
473 self.ui.debug(_('server identity verification succeeded\n')) |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
474 else: |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
475 httplib.HTTPSConnection.connect(self) |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
476 |
9726
430e59ff3437
keepalive: handle broken pipes gracefully during large POSTs
Augie Fackler <durin42@gmail.com>
parents:
9467
diff
changeset
|
477 class httpsconnection(BetterHTTPS): |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
478 response_class = keepalive.HTTPResponse |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
479 # must be able to send big bundle as stream. |
9726
430e59ff3437
keepalive: handle broken pipes gracefully during large POSTs
Augie Fackler <durin42@gmail.com>
parents:
9467
diff
changeset
|
480 send = _gen_sendfile(BetterHTTPS) |
430e59ff3437
keepalive: handle broken pipes gracefully during large POSTs
Augie Fackler <durin42@gmail.com>
parents:
9467
diff
changeset
|
481 getresponse = keepalive.wrapgetresponse(httplib.HTTPSConnection) |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
482 |
9852
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
483 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
|
484 if self.realhostport: # use CONNECT proxy |
9852
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
485 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
486 self.sock.connect((self.host, self.port)) |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
487 if _generic_proxytunnel(self): |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
488 self.sock = _ssl_wrap_socket(self.sock, self.cert_file, |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
489 self.key_file) |
9852
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
490 else: |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
491 BetterHTTPS.connect(self) |
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
492 |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
493 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
|
494 def __init__(self, ui): |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
495 keepalive.KeepAliveHandler.__init__(self) |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
496 urllib2.HTTPSHandler.__init__(self) |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
497 self.ui = ui |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
498 self.pwmgr = passwordmgr(self.ui) |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
499 |
9852
917cf6bb6d0c
url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents:
9726
diff
changeset
|
500 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
|
501 _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
|
502 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
|
503 |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
504 def https_open(self, req): |
8847
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
505 self.auth = self.pwmgr.readauthtoken(req.get_full_url()) |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
506 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
|
507 |
10408
50fb1fe143ff
url: httplib.HTTPSConnection already handles IPv6 and port parsing fine
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
508 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
|
509 keyfile = None |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
510 certfile = None |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
511 |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
512 if args: # key_file |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
513 keyfile = args.pop(0) |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
514 if args: # cert_file |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
515 certfile = args.pop(0) |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
516 |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
517 # 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
|
518 # hgrc, we prefer these |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
519 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
|
520 keyfile = self.auth['key'] |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
521 certfile = self.auth['cert'] |
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
522 |
10409
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
523 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
|
524 conn.ui = self.ui |
4c94a3df4b10
url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents:
10408
diff
changeset
|
525 return conn |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
526 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
527 # In python < 2.5 AbstractDigestAuthHandler raises a ValueError if |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
528 # it doesn't know about the auth type requested. This can happen if |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
529 # somebody is using BasicAuth and types a bad password. |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
530 class httpdigestauthhandler(urllib2.HTTPDigestAuthHandler): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
531 def http_error_auth_reqed(self, auth_header, host, req, headers): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
532 try: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
533 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
|
534 self, auth_header, host, req, headers) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
535 except ValueError, inst: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
536 arg = inst.args[0] |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
537 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
|
538 return |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
539 raise |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
540 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
541 def getauthinfo(path): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
542 scheme, netloc, urlpath, query, frag = urlparse.urlsplit(path) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
543 if not urlpath: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
544 urlpath = '/' |
7284
ac81ffac0f35
url: fix file:// URL handling
Patrick Mezard <pmezard@gmail.com>
parents:
7270
diff
changeset
|
545 if scheme != 'file': |
ac81ffac0f35
url: fix file:// URL handling
Patrick Mezard <pmezard@gmail.com>
parents:
7270
diff
changeset
|
546 # XXX: why are we quoting the path again with some smart |
ac81ffac0f35
url: fix file:// URL handling
Patrick Mezard <pmezard@gmail.com>
parents:
7270
diff
changeset
|
547 # heuristic here? Anyway, it cannot be done with file:// |
ac81ffac0f35
url: fix file:// URL handling
Patrick Mezard <pmezard@gmail.com>
parents:
7270
diff
changeset
|
548 # urls since path encoding is os/fs dependent (see |
ac81ffac0f35
url: fix file:// URL handling
Patrick Mezard <pmezard@gmail.com>
parents:
7270
diff
changeset
|
549 # urllib.pathname2url() for details). |
ac81ffac0f35
url: fix file:// URL handling
Patrick Mezard <pmezard@gmail.com>
parents:
7270
diff
changeset
|
550 urlpath = quotepath(urlpath) |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
551 host, port, user, passwd = netlocsplit(netloc) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
552 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
553 # urllib cannot handle URLs with embedded user or passwd |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
554 url = urlparse.urlunsplit((scheme, netlocunsplit(host, port), |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
555 urlpath, query, frag)) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
556 if user: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
557 netloc = host |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
558 if port: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
559 netloc += ':' + port |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
560 # Python < 2.4.3 uses only the netloc to search for a password |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
561 authinfo = (None, (url, netloc), user, passwd or '') |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
562 else: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
563 authinfo = None |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
564 return url, authinfo |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
565 |
9347
d0474b184347
url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents:
9122
diff
changeset
|
566 handlerfuncs = [] |
d0474b184347
url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents:
9122
diff
changeset
|
567 |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
568 def opener(ui, authinfo=None): |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
569 ''' |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
570 construct an opener suitable for urllib2 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
571 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
|
572 ''' |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
573 handlers = [httphandler()] |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
574 if has_https: |
8847
7951f385fcb7
url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents:
8590
diff
changeset
|
575 handlers.append(httpshandler(ui)) |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
576 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
577 handlers.append(proxyhandler(ui)) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
578 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
579 passmgr = passwordmgr(ui) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
580 if authinfo is not None: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
581 passmgr.add_password(*authinfo) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
582 user, passwd = authinfo[2:4] |
9467
4c041f1ee1b4
do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents:
9347
diff
changeset
|
583 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
|
584 (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
|
585 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
586 handlers.extend((urllib2.HTTPBasicAuthHandler(passmgr), |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
587 httpdigestauthhandler(passmgr))) |
9347
d0474b184347
url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents:
9122
diff
changeset
|
588 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
|
589 opener = urllib2.build_opener(*handlers) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
590 |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
591 # 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
|
592 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
|
593 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
|
594 return opener |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
595 |
7285
5ad99abfab79
url: detect scheme with a regexp instead of urlsplit()
Patrick Mezard <pmezard@gmail.com>
parents:
7284
diff
changeset
|
596 scheme_re = re.compile(r'^([a-zA-Z0-9+-.]+)://') |
5ad99abfab79
url: detect scheme with a regexp instead of urlsplit()
Patrick Mezard <pmezard@gmail.com>
parents:
7284
diff
changeset
|
597 |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
598 def open(ui, url, data=None): |
7285
5ad99abfab79
url: detect scheme with a regexp instead of urlsplit()
Patrick Mezard <pmezard@gmail.com>
parents:
7284
diff
changeset
|
599 scheme = None |
5ad99abfab79
url: detect scheme with a regexp instead of urlsplit()
Patrick Mezard <pmezard@gmail.com>
parents:
7284
diff
changeset
|
600 m = scheme_re.search(url) |
5ad99abfab79
url: detect scheme with a regexp instead of urlsplit()
Patrick Mezard <pmezard@gmail.com>
parents:
7284
diff
changeset
|
601 if m: |
5ad99abfab79
url: detect scheme with a regexp instead of urlsplit()
Patrick Mezard <pmezard@gmail.com>
parents:
7284
diff
changeset
|
602 scheme = m.group(1).lower() |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
603 if not scheme: |
7284
ac81ffac0f35
url: fix file:// URL handling
Patrick Mezard <pmezard@gmail.com>
parents:
7270
diff
changeset
|
604 path = util.normpath(os.path.abspath(url)) |
ac81ffac0f35
url: fix file:// URL handling
Patrick Mezard <pmezard@gmail.com>
parents:
7270
diff
changeset
|
605 url = 'file://' + urllib.pathname2url(path) |
ac81ffac0f35
url: fix file:// URL handling
Patrick Mezard <pmezard@gmail.com>
parents:
7270
diff
changeset
|
606 authinfo = None |
7270
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
607 else: |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
608 url, authinfo = getauthinfo(url) |
2db33c1a5654
factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
609 return opener(ui, authinfo).open(url, data) |