annotate mercurial/url.py @ 43076:2372284d9457

formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:45:02 -0400
parents 2a3c0106ded9
children 687b865b95ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9852
diff changeset
8 # GNU General Public License version 2 or any later version.
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
9
25990
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
10 from __future__ import absolute_import
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
11
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
12 import base64
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
13 import os
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
14 import socket
37100
9e6d3465f17e url: make logginghttphandler compatible with Python 2.7.6
Yuya Nishihara <yuya@tcha.org>
parents: 37084
diff changeset
15 import sys
25990
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
16
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
17 from .i18n import _
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
18 from . import (
30820
6a70cf94d1b5 py3: replace pycompat.getenv with encoding.environ.get
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30763
diff changeset
19 encoding,
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25990
diff changeset
20 error,
25990
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
21 httpconnection as httpconnectionmod,
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
22 keepalive,
34428
0ee9cf8d054a url: use native strings for header values
Augie Fackler <augie@google.com>
parents: 33499
diff changeset
23 pycompat,
25990
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
24 sslutil,
34466
1232f7fa00c3 cleanup: use urllibcompat for renamed methods on urllib request objects
Augie Fackler <augie@google.com>
parents: 34428
diff changeset
25 urllibcompat,
25990
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
26 util,
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
27 )
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
28 from .utils import stringutil
29455
0c741fd6158a py3: conditionalize httplib import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29379
diff changeset
29
0c741fd6158a py3: conditionalize httplib import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29379
diff changeset
30 httplib = util.httplib
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 26806
diff changeset
31 stringio = util.stringio
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
32 urlerr = util.urlerr
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
33 urlreq = util.urlreq
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
34
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
35
34694
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
36 def escape(s, quote=None):
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
37 '''Replace special characters "&", "<" and ">" to HTML-safe sequences.
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
38 If the optional flag quote is true, the quotation mark character (")
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
39 is also translated.
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
40
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
41 This is the same as cgi.escape in Python, but always operates on
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
42 bytes, whereas cgi.escape in Python 3 only works on unicodes.
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
43 '''
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
44 s = s.replace(b"&", b"&amp;")
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
45 s = s.replace(b"<", b"&lt;")
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
46 s = s.replace(b">", b"&gt;")
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
47 if quote:
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
48 s = s.replace(b'"', b"&quot;")
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
49 return s
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
50
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
51
29377
2c019aac6b99 url: extract password database from password manager
liscju <piotr.listkiewicz@gmail.com>
parents: 29252
diff changeset
52 class passwordmgr(object):
2c019aac6b99 url: extract password database from password manager
liscju <piotr.listkiewicz@gmail.com>
parents: 29252
diff changeset
53 def __init__(self, ui, passwddb):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
54 self.ui = ui
29377
2c019aac6b99 url: extract password database from password manager
liscju <piotr.listkiewicz@gmail.com>
parents: 29252
diff changeset
55 self.passwddb = passwddb
2c019aac6b99 url: extract password database from password manager
liscju <piotr.listkiewicz@gmail.com>
parents: 29252
diff changeset
56
2c019aac6b99 url: extract password database from password manager
liscju <piotr.listkiewicz@gmail.com>
parents: 29252
diff changeset
57 def add_password(self, realm, uri, user, passwd):
2c019aac6b99 url: extract password database from password manager
liscju <piotr.listkiewicz@gmail.com>
parents: 29252
diff changeset
58 return self.passwddb.add_password(realm, uri, user, passwd)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
59
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
60 def find_user_password(self, realm, authuri):
41449
bc776c31c093 url: add some defensive asserts on expected incoming types
Augie Fackler <augie@google.com>
parents: 40043
diff changeset
61 assert isinstance(realm, (type(None), str))
bc776c31c093 url: add some defensive asserts on expected incoming types
Augie Fackler <augie@google.com>
parents: 40043
diff changeset
62 assert isinstance(authuri, str)
29377
2c019aac6b99 url: extract password database from password manager
liscju <piotr.listkiewicz@gmail.com>
parents: 29252
diff changeset
63 authinfo = self.passwddb.find_user_password(realm, authuri)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
64 user, passwd = authinfo
41450
d437d1e2a711 url: convert some variables back to bytes
Augie Fackler <augie@google.com>
parents: 41449
diff changeset
65 user, passwd = pycompat.bytesurl(user), pycompat.bytesurl(passwd)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
66 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
67 self._writedebug(user, passwd)
41590
349c8879becd py3: ensure the HTTP password manager returns strings, not bytes
Matt Harbison <matt_harbison@yahoo.com>
parents: 41450
diff changeset
68 return (pycompat.strurl(user), pycompat.strurl(passwd))
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
69
15005
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 14244
diff changeset
70 if not user or not passwd:
15025
0593e8f81c71 http: pass user to readauthforuri() (fix 4a43e23b8c55)
Patrick Mezard <pmezard@gmail.com>
parents: 15005
diff changeset
71 res = httpconnectionmod.readauthforuri(self.ui, authuri, user)
13372
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
72 if res:
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
73 group, auth = res
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
74 user, passwd = auth.get('username'), auth.get('password')
13372
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
75 self.ui.debug("using auth.%s.* for authentication\n" % group)
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
76 if not user or not passwd:
36652
8381126bf43c url: more bytes/unicodes fussing in url.py around auth handling
Augie Fackler <augie@google.com>
parents: 36454
diff changeset
77 u = util.url(pycompat.bytesurl(authuri))
20291
7d589d923b8a url: added authuri when login information is requested (issue3209)
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20174
diff changeset
78 u.query = None
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
79 if not self.ui.interactive():
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
80 raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
81 _('http authorization required for %s')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
82 % util.hidepassword(bytes(u))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
83 )
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
84
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
85 self.ui.write(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
86 _("http authorization required for %s\n")
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
87 % util.hidepassword(bytes(u))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
88 )
36652
8381126bf43c url: more bytes/unicodes fussing in url.py around auth handling
Augie Fackler <augie@google.com>
parents: 36454
diff changeset
89 self.ui.write(_("realm: %s\n") % pycompat.bytesurl(realm))
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
90 if user:
12862
9d6adddc8eea url: show realm/user when asking for username/password
timeless <timeless@gmail.com>
parents: 12770
diff changeset
91 self.ui.write(_("user: %s\n") % user)
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
92 else:
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
93 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
94
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
95 if not passwd:
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
96 passwd = self.ui.getpass()
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
97
29377
2c019aac6b99 url: extract password database from password manager
liscju <piotr.listkiewicz@gmail.com>
parents: 29252
diff changeset
98 self.passwddb.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
99 self._writedebug(user, passwd)
41590
349c8879becd py3: ensure the HTTP password manager returns strings, not bytes
Matt Harbison <matt_harbison@yahoo.com>
parents: 41450
diff changeset
100 return (pycompat.strurl(user), pycompat.strurl(passwd))
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
101
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
102 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
103 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
104 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
105
15025
0593e8f81c71 http: pass user to readauthforuri() (fix 4a43e23b8c55)
Patrick Mezard <pmezard@gmail.com>
parents: 15005
diff changeset
106 def find_stored_password(self, authuri):
29377
2c019aac6b99 url: extract password database from password manager
liscju <piotr.listkiewicz@gmail.com>
parents: 29252
diff changeset
107 return self.passwddb.find_user_password(None, authuri)
15025
0593e8f81c71 http: pass user to readauthforuri() (fix 4a43e23b8c55)
Patrick Mezard <pmezard@gmail.com>
parents: 15005
diff changeset
108
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
109
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
110 class proxyhandler(urlreq.proxyhandler):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
111 def __init__(self, ui):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
112 proxyurl = ui.config("http_proxy", "host") or encoding.environ.get(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
113 'http_proxy'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
114 )
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
115 # XXX proxyauthinfo = None
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
116
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
117 if proxyurl:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
118 # proxy can be proper url or host[:port]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
119 if not (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
120 proxyurl.startswith('http:') or proxyurl.startswith('https:')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
121 ):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
122 proxyurl = 'http://' + proxyurl + '/'
14076
924c82157d46 url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents: 14071
diff changeset
123 proxy = util.url(proxyurl)
13820
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
124 if not proxy.user:
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
125 proxy.user = ui.config("http_proxy", "user")
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
126 proxy.passwd = ui.config("http_proxy", "passwd")
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
127
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
128 # 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
129 no_list = ["localhost", "127.0.0.1"]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
130 no_list.extend(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
131 [p.lower() for p in ui.configlist("http_proxy", "no")]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
132 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
133 no_list.extend(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
134 [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
135 p.strip().lower()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
136 for p in encoding.environ.get("no_proxy", '').split(',')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
137 if p.strip()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
138 ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
139 )
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
140 # "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
141 if ui.configbool("http_proxy", "always"):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
142 self.no_list = []
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
143 else:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
144 self.no_list = no_list
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
145
41710
4028897dfa05 url: always use str for proxy configuration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41708
diff changeset
146 # Keys and values need to be str because the standard library
4028897dfa05 url: always use str for proxy configuration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41708
diff changeset
147 # expects them to be.
4028897dfa05 url: always use str for proxy configuration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41708
diff changeset
148 proxyurl = str(proxy)
4028897dfa05 url: always use str for proxy configuration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41708
diff changeset
149 proxies = {r'http': proxyurl, r'https': proxyurl}
4028897dfa05 url: always use str for proxy configuration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41708
diff changeset
150 ui.debug('proxying through %s\n' % util.hidepassword(bytes(proxy)))
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
151 else:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
152 proxies = {}
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
153
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
154 urlreq.proxyhandler.__init__(self, proxies)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
155 self.ui = ui
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
156
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
157 def proxy_open(self, req, proxy, type_):
41710
4028897dfa05 url: always use str for proxy configuration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41708
diff changeset
158 host = pycompat.bytesurl(urllibcompat.gethost(req)).split(':')[0]
19535
df2155ebf502 proxy: allow wildcards in the no proxy list (issue1821)
Matt Mackall <mpm@selenic.com>
parents: 18176
diff changeset
159 for e in self.no_list:
df2155ebf502 proxy: allow wildcards in the no proxy list (issue1821)
Matt Mackall <mpm@selenic.com>
parents: 18176
diff changeset
160 if host == e:
df2155ebf502 proxy: allow wildcards in the no proxy list (issue1821)
Matt Mackall <mpm@selenic.com>
parents: 18176
diff changeset
161 return None
df2155ebf502 proxy: allow wildcards in the no proxy list (issue1821)
Matt Mackall <mpm@selenic.com>
parents: 18176
diff changeset
162 if e.startswith('*.') and host.endswith(e[2:]):
df2155ebf502 proxy: allow wildcards in the no proxy list (issue1821)
Matt Mackall <mpm@selenic.com>
parents: 18176
diff changeset
163 return None
df2155ebf502 proxy: allow wildcards in the no proxy list (issue1821)
Matt Mackall <mpm@selenic.com>
parents: 18176
diff changeset
164 if e.startswith('.') and host.endswith(e[1:]):
df2155ebf502 proxy: allow wildcards in the no proxy list (issue1821)
Matt Mackall <mpm@selenic.com>
parents: 18176
diff changeset
165 return None
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
166
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
167 return urlreq.proxyhandler.proxy_open(self, req, proxy, type_)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
168
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
169
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
170 def _gen_sendfile(orgsend):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
171 def _sendfile(self, data):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
172 # send a file
14244
e7525a555a64 url: use new http support if requested by the user
Augie Fackler <durin42@gmail.com>
parents: 14204
diff changeset
173 if isinstance(data, httpconnectionmod.httpsendfile):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
174 # 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
175 data.seek(0)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
176 for chunk in util.filechunkiter(data):
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
177 orgsend(self, chunk)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
178 else:
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
179 orgsend(self, data)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
180
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
181 return _sendfile
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
182
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
183
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
184 has_https = util.safehasattr(urlreq, 'httpshandler')
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
185
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
186
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
187 class httpconnection(keepalive.HTTPConnection):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
188 # must be able to send big bundle as stream.
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
189 send = _gen_sendfile(keepalive.HTTPConnection.send)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
190
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
191 def getresponse(self):
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
192 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
193 if proxyres:
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
194 if proxyres.will_close:
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
195 self.close()
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
196 self.proxyres = None
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
197 return proxyres
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
198 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
199
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
200
41708
d20f1594ff4a url: always access req._tunnel_host
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41590
diff changeset
201 # Large parts of this function have their origin from before Python 2.6
d20f1594ff4a url: always access req._tunnel_host
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41590
diff changeset
202 # and could potentially be removed.
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
203 def _generic_start_transaction(handler, h, req):
41708
d20f1594ff4a url: always access req._tunnel_host
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41590
diff changeset
204 tunnel_host = req._tunnel_host
14964
376c32a5ccdc url: replace uses of hasattr with safehasattr or getattr
Augie Fackler <durin42@gmail.com>
parents: 14244
diff changeset
205 if tunnel_host:
41710
4028897dfa05 url: always use str for proxy configuration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41708
diff changeset
206 if tunnel_host[:7] not in [r'http://', r'https:/']:
4028897dfa05 url: always use str for proxy configuration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41708
diff changeset
207 tunnel_host = r'https://' + tunnel_host
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
208 new_tunnel = True
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
209 else:
34466
1232f7fa00c3 cleanup: use urllibcompat for renamed methods on urllib request objects
Augie Fackler <augie@google.com>
parents: 34428
diff changeset
210 tunnel_host = urllibcompat.getselector(req)
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
211 new_tunnel = False
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
212
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
213 if new_tunnel or tunnel_host == urllibcompat.getfullurl(req): # has proxy
41710
4028897dfa05 url: always use str for proxy configuration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41708
diff changeset
214 u = util.url(pycompat.bytesurl(tunnel_host))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
215 if new_tunnel or u.scheme == 'https': # only use CONNECT for HTTPS
13820
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
216 h.realhostport = ':'.join([u.host, (u.port or '443')])
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
217 h.headers = req.headers.copy()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
218 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
219 return
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
220
10415
677f15da38c1 url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10411
diff changeset
221 h.realhostport = None
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
222 h.headers = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
223
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
224
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
225 def _generic_proxytunnel(self):
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
226 proxyheaders = dict(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
227 [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
228 (x, self.headers[x])
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
229 for x in self.headers
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
230 if x.lower().startswith(r'proxy-')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
231 ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
232 )
10415
677f15da38c1 url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10411
diff changeset
233 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
234 for header in proxyheaders.iteritems():
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
235 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
236 self.send('\r\n')
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
237
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
238 # 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
239 # 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
240 # override functions to provide the needed functionality
41711
544035846830 url: don't pass strict argument on Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41710
diff changeset
241 # strict was removed in Python 3.4.
544035846830 url: don't pass strict argument on Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41710
diff changeset
242 kwargs = {}
544035846830 url: don't pass strict argument on Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41710
diff changeset
243 if not pycompat.ispy3:
544035846830 url: don't pass strict argument on Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41710
diff changeset
244 kwargs['strict'] = self.strict
544035846830 url: don't pass strict argument on Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41710
diff changeset
245
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
246 res = self.response_class(self.sock, method=self._method, **kwargs)
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
247
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
248 while True:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
249 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
250 if status != httplib.CONTINUE:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
251 break
29729
44ea12756fef url: use `iter(callable, sentinel)` instead of while True
Augie Fackler <augie@google.com>
parents: 29662
diff changeset
252 # skip lines that are all whitespace
44ea12756fef url: use `iter(callable, sentinel)` instead of while True
Augie Fackler <augie@google.com>
parents: 29662
diff changeset
253 list(iter(lambda: res.fp.readline().strip(), ''))
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
254 res.status = status
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
255 res.reason = reason.strip()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
256
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
257 if res.status == 200:
29729
44ea12756fef url: use `iter(callable, sentinel)` instead of while True
Augie Fackler <augie@google.com>
parents: 29662
diff changeset
258 # skip lines until we find a blank line
44ea12756fef url: use `iter(callable, sentinel)` instead of while True
Augie Fackler <augie@google.com>
parents: 29662
diff changeset
259 list(iter(res.fp.readline, '\r\n'))
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
260 return True
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
261
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
262 if version == 'HTTP/1.0':
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
263 res.version = 10
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
264 elif version.startswith('HTTP/1.'):
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
265 res.version = 11
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
266 elif version == 'HTTP/0.9':
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
267 res.version = 9
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
268 else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
269 raise httplib.UnknownProtocol(version)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
270
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
271 if res.version == 9:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
272 res.length = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
273 res.chunked = 0
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
274 res.will_close = 1
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 26806
diff changeset
275 res.msg = httplib.HTTPMessage(stringio())
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
276 return False
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
277
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
278 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
279 res.msg.fp = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
280
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
281 # 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
282 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
283 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
284 res.chunked = 1
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
285 res.chunk_left = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
286 else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
287 res.chunked = 0
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
288
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
289 # 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
290 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
291
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
292 # do we have a Content-Length?
17428
72803c8edaa4 avoid using abbreviations that look like spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents: 17424
diff changeset
293 # NOTE: RFC 2616, section 4.4, #3 says we ignore this if
72803c8edaa4 avoid using abbreviations that look like spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents: 17424
diff changeset
294 # transfer-encoding is "chunked"
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
295 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
296 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
297 try:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
298 res.length = int(length)
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
299 except ValueError:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
300 res.length = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
301 else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
302 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
303 res.length = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
304 else:
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
305 res.length = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
306
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
307 # does the body have a fixed length? (of zero)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
308 if (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
309 status == httplib.NO_CONTENT
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
310 or status == httplib.NOT_MODIFIED
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
311 or 100 <= status < 200
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
312 or res._method == 'HEAD' # 1xx codes
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
313 ):
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
314 res.length = 0
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
315
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
316 # 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
317 # 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
318 # WILL close.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
319 if not res.will_close and not res.chunked and res.length is None:
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
320 res.will_close = 1
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
321
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
322 self.proxyres = res
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
323
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
324 return False
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
325
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
326
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
327 class httphandler(keepalive.HTTPHandler):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
328 def http_open(self, req):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
329 return self.do_open(httpconnection, req)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
330
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
331 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
332 _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
333 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
334
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
335
37011
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
336 class logginghttpconnection(keepalive.HTTPConnection):
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
337 def __init__(self, createconn, *args, **kwargs):
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
338 keepalive.HTTPConnection.__init__(self, *args, **kwargs)
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
339 self._create_connection = createconn
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
340
37100
9e6d3465f17e url: make logginghttphandler compatible with Python 2.7.6
Yuya Nishihara <yuya@tcha.org>
parents: 37084
diff changeset
341 if sys.version_info < (2, 7, 7):
9e6d3465f17e url: make logginghttphandler compatible with Python 2.7.6
Yuya Nishihara <yuya@tcha.org>
parents: 37084
diff changeset
342 # copied from 2.7.14, since old implementations directly call
9e6d3465f17e url: make logginghttphandler compatible with Python 2.7.6
Yuya Nishihara <yuya@tcha.org>
parents: 37084
diff changeset
343 # socket.create_connection()
9e6d3465f17e url: make logginghttphandler compatible with Python 2.7.6
Yuya Nishihara <yuya@tcha.org>
parents: 37084
diff changeset
344 def connect(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
345 self.sock = self._create_connection(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
346 (self.host, self.port), self.timeout, self.source_address
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
347 )
37100
9e6d3465f17e url: make logginghttphandler compatible with Python 2.7.6
Yuya Nishihara <yuya@tcha.org>
parents: 37084
diff changeset
348 if self._tunnel_host:
9e6d3465f17e url: make logginghttphandler compatible with Python 2.7.6
Yuya Nishihara <yuya@tcha.org>
parents: 37084
diff changeset
349 self._tunnel()
9e6d3465f17e url: make logginghttphandler compatible with Python 2.7.6
Yuya Nishihara <yuya@tcha.org>
parents: 37084
diff changeset
350
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
351
37011
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
352 class logginghttphandler(httphandler):
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
353 """HTTP handler that logs socket I/O."""
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
354
40043
6509fcec830c url: allow to configure timeout on http connection
Cédric Krier <ced@b2ck.com>
parents: 40034
diff changeset
355 def __init__(self, logfh, name, observeropts, timeout=None):
6509fcec830c url: allow to configure timeout on http connection
Cédric Krier <ced@b2ck.com>
parents: 40034
diff changeset
356 super(logginghttphandler, self).__init__(timeout=timeout)
37011
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
357
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
358 self._logfh = logfh
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
359 self._logname = name
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
360 self._observeropts = observeropts
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
361
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
362 # do_open() calls the passed class to instantiate an HTTPConnection. We
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
363 # pass in a callable method that creates a custom HTTPConnection instance
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
364 # whose callback to create the socket knows how to proxy the socket.
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
365 def http_open(self, req):
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
366 return self.do_open(self._makeconnection, req)
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
367
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
368 def _makeconnection(self, *args, **kwargs):
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
369 def createconnection(*args, **kwargs):
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
370 sock = socket.create_connection(*args, **kwargs)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
371 return util.makeloggingsocket(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
372 self._logfh, sock, self._logname, **self._observeropts
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
373 )
37011
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
374
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
375 return logginghttpconnection(createconnection, *args, **kwargs)
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
376
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
377
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
378 if has_https:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
379
40031
f2dffa1359c6 url: have httpsconnection inherit from our custom HTTPConnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40018
diff changeset
380 class httpsconnection(keepalive.HTTPConnection):
13424
08f9c587141f url: merge BetterHTTPS with httpsconnection to get some proxy https validation
Mads Kiilerich <mads@kiilerich.com>
parents: 13422
diff changeset
381 response_class = keepalive.HTTPResponse
25414
f7ccbc2776b7 https: do not inherit httplib.HTTPSConnection that creates unused SSLContext
Yuya Nishihara <yuya@tcha.org>
parents: 25207
diff changeset
382 default_port = httplib.HTTPS_PORT
13424
08f9c587141f url: merge BetterHTTPS with httpsconnection to get some proxy https validation
Mads Kiilerich <mads@kiilerich.com>
parents: 13422
diff changeset
383 # must be able to send big bundle as stream.
08f9c587141f url: merge BetterHTTPS with httpsconnection to get some proxy https validation
Mads Kiilerich <mads@kiilerich.com>
parents: 13422
diff changeset
384 send = _gen_sendfile(keepalive.safesend)
25414
f7ccbc2776b7 https: do not inherit httplib.HTTPSConnection that creates unused SSLContext
Yuya Nishihara <yuya@tcha.org>
parents: 25207
diff changeset
385 getresponse = keepalive.wrapgetresponse(httplib.HTTPConnection)
f7ccbc2776b7 https: do not inherit httplib.HTTPSConnection that creates unused SSLContext
Yuya Nishihara <yuya@tcha.org>
parents: 25207
diff changeset
386
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
387 def __init__(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
388 self,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
389 host,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
390 port=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
391 key_file=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
392 cert_file=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
393 *args,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
394 **kwargs
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
395 ):
40031
f2dffa1359c6 url: have httpsconnection inherit from our custom HTTPConnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40018
diff changeset
396 keepalive.HTTPConnection.__init__(self, host, port, *args, **kwargs)
25414
f7ccbc2776b7 https: do not inherit httplib.HTTPSConnection that creates unused SSLContext
Yuya Nishihara <yuya@tcha.org>
parents: 25207
diff changeset
397 self.key_file = key_file
f7ccbc2776b7 https: do not inherit httplib.HTTPSConnection that creates unused SSLContext
Yuya Nishihara <yuya@tcha.org>
parents: 25207
diff changeset
398 self.cert_file = cert_file
9726
430e59ff3437 keepalive: handle broken pipes gracefully during large POSTs
Augie Fackler <durin42@gmail.com>
parents: 9467
diff changeset
399
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
400 def connect(self):
29662
b76ea1384bf2 url: drop compatibility wrapper of socket.create_connection()
Yuya Nishihara <yuya@tcha.org>
parents: 29639
diff changeset
401 self.sock = socket.create_connection((self.host, self.port))
13422
ebce5196b9db url: always create BetterHTTPS connections the same way
Mads Kiilerich <mads@kiilerich.com>
parents: 13421
diff changeset
402
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
403 host = self.host
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
404 if self.realhostport: # use CONNECT proxy
14064
e4bfb9c337f3 remove unused imports and variables
Alexander Solovyov <alexander@solovyov.net>
parents: 13902
diff changeset
405 _generic_proxytunnel(self)
13424
08f9c587141f url: merge BetterHTTPS with httpsconnection to get some proxy https validation
Mads Kiilerich <mads@kiilerich.com>
parents: 13422
diff changeset
406 host = self.realhostport.rsplit(':', 1)[0]
25429
9d1c61715939 ssl: rename ssl_wrap_socket() to conform to our naming convention
Yuya Nishihara <yuya@tcha.org>
parents: 25415
diff changeset
407 self.sock = sslutil.wrapsocket(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
408 self.sock,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
409 self.key_file,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
410 self.cert_file,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
411 ui=self.ui,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
412 serverhostname=host,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
413 )
29227
dffe78d80a6c sslutil: convert socket validation from a class to a function (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28883
diff changeset
414 sslutil.validatesocket(self.sock)
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
415
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
416 class httpshandler(keepalive.KeepAliveHandler, urlreq.httpshandler):
40043
6509fcec830c url: allow to configure timeout on http connection
Cédric Krier <ced@b2ck.com>
parents: 40034
diff changeset
417 def __init__(self, ui, timeout=None):
6509fcec830c url: allow to configure timeout on http connection
Cédric Krier <ced@b2ck.com>
parents: 40034
diff changeset
418 keepalive.KeepAliveHandler.__init__(self, timeout=timeout)
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
419 urlreq.httpshandler.__init__(self)
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
420 self.ui = ui
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
421 self.pwmgr = passwordmgr(self.ui, self.ui.httppasswordmgrdb)
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
422
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
423 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
424 _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
425 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
426
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
427 def https_open(self, req):
34466
1232f7fa00c3 cleanup: use urllibcompat for renamed methods on urllib request objects
Augie Fackler <augie@google.com>
parents: 34428
diff changeset
428 # urllibcompat.getfullurl() does not contain credentials
1232f7fa00c3 cleanup: use urllibcompat for renamed methods on urllib request objects
Augie Fackler <augie@google.com>
parents: 34428
diff changeset
429 # and we may need them to match the certificates.
1232f7fa00c3 cleanup: use urllibcompat for renamed methods on urllib request objects
Augie Fackler <augie@google.com>
parents: 34428
diff changeset
430 url = urllibcompat.getfullurl(req)
15025
0593e8f81c71 http: pass user to readauthforuri() (fix 4a43e23b8c55)
Patrick Mezard <pmezard@gmail.com>
parents: 15005
diff changeset
431 user, password = self.pwmgr.find_stored_password(url)
0593e8f81c71 http: pass user to readauthforuri() (fix 4a43e23b8c55)
Patrick Mezard <pmezard@gmail.com>
parents: 15005
diff changeset
432 res = httpconnectionmod.readauthforuri(self.ui, url, user)
13372
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
433 if res:
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
434 group, auth = res
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
435 self.auth = auth
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
436 self.ui.debug("using auth.%s.* for authentication\n" % group)
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
437 else:
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
438 self.auth = None
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
439 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
440
10408
50fb1fe143ff url: httplib.HTTPSConnection already handles IPv6 and port parsing fine
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
441 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
442 keyfile = None
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
443 certfile = None
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
444
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
445 if len(args) >= 1: # key_file
10511
6f61c480f51c url: *args argument is a tuple, not a list (found by pylint)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10484
diff changeset
446 keyfile = args[0]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
447 if len(args) >= 2: # cert_file
10511
6f61c480f51c url: *args argument is a tuple, not a list (found by pylint)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10484
diff changeset
448 certfile = args[1]
6f61c480f51c url: *args argument is a tuple, not a list (found by pylint)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10484
diff changeset
449 args = args[2:]
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
450
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
451 # 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
452 # hgrc, we prefer these
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
453 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
454 keyfile = self.auth['key']
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
455 certfile = self.auth['cert']
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
456
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
457 conn = httpsconnection(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
458 host, port, keyfile, certfile, *args, **kwargs
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
459 )
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
460 conn.ui = self.ui
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
461 return conn
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
462
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
463
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
464 class httpdigestauthhandler(urlreq.httpdigestauthhandler):
11457
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
465 def __init__(self, *args, **kwargs):
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
466 urlreq.httpdigestauthhandler.__init__(self, *args, **kwargs)
11457
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
467 self.retried_req = None
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
468
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
469 def reset_retry_count(self):
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
470 # Python 2.6.5 will call this on 401 or 407 errors and thus loop
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
471 # forever. We disable reset_retry_count completely and reset in
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
472 # http_error_auth_reqed instead.
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
473 pass
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
474
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
475 def http_error_auth_reqed(self, auth_header, host, req, headers):
11457
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
476 # Reset the retry counter once for each request.
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
477 if req is not self.retried_req:
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
478 self.retried_req = req
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
479 self.retried = 0
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
480 return urlreq.httpdigestauthhandler.http_error_auth_reqed(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
481 self, auth_header, host, req, headers
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
482 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
483
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
484
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
485 class httpbasicauthhandler(urlreq.httpbasicauthhandler):
11844
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
486 def __init__(self, *args, **kwargs):
20964
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
487 self.auth = None
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
488 urlreq.httpbasicauthhandler.__init__(self, *args, **kwargs)
11844
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
489 self.retried_req = None
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
490
20964
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
491 def http_request(self, request):
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
492 if self.auth:
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
493 request.add_unredirected_header(self.auth_header, self.auth)
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
494
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
495 return request
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
496
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
497 def https_request(self, request):
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
498 if self.auth:
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
499 request.add_unredirected_header(self.auth_header, self.auth)
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
500
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
501 return request
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
502
11844
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
503 def reset_retry_count(self):
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
504 # Python 2.6.5 will call this on 401 or 407 errors and thus loop
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
505 # forever. We disable reset_retry_count completely and reset in
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
506 # http_error_auth_reqed instead.
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
507 pass
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
508
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
509 def http_error_auth_reqed(self, auth_header, host, req, headers):
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
510 # Reset the retry counter once for each request.
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
511 if req is not self.retried_req:
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
512 self.retried_req = req
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
513 self.retried = 0
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
514 return urlreq.httpbasicauthhandler.http_error_auth_reqed(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
515 self, auth_header, host, req, headers
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
516 )
11844
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
517
20964
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
518 def retry_http_basic_auth(self, host, req, realm):
34466
1232f7fa00c3 cleanup: use urllibcompat for renamed methods on urllib request objects
Augie Fackler <augie@google.com>
parents: 34428
diff changeset
519 user, pw = self.passwd.find_user_password(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
520 realm, urllibcompat.getfullurl(req)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
521 )
20964
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
522 if pw is not None:
36652
8381126bf43c url: more bytes/unicodes fussing in url.py around auth handling
Augie Fackler <augie@google.com>
parents: 36454
diff changeset
523 raw = "%s:%s" % (pycompat.bytesurl(user), pycompat.bytesurl(pw))
8381126bf43c url: more bytes/unicodes fussing in url.py around auth handling
Augie Fackler <augie@google.com>
parents: 36454
diff changeset
524 auth = r'Basic %s' % pycompat.strurl(base64.b64encode(raw).strip())
29639
6fd751fa58d3 url: avoid re-issuing incorrect password (issue3210)
Kim Randell <Kim.Randell@vicon.com>
parents: 29599
diff changeset
525 if req.get_header(self.auth_header, None) == auth:
20964
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
526 return None
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
527 self.auth = auth
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
528 req.add_unredirected_header(self.auth_header, auth)
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
529 return self.parent.open(req)
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
530 else:
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
531 return None
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
532
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
533
31936
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
534 class cookiehandler(urlreq.basehandler):
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
535 def __init__(self, ui):
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
536 self.cookiejar = None
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
537
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
538 cookiefile = ui.config('auth', 'cookiefile')
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
539 if not cookiefile:
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
540 return
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
541
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
542 cookiefile = util.expandpath(cookiefile)
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
543 try:
37735
126998dcfb08 url: some bytes/str cleanup where we interface with stdlib funcs
Augie Fackler <augie@google.com>
parents: 37100
diff changeset
544 cookiejar = util.cookielib.MozillaCookieJar(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
545 pycompat.fsdecode(cookiefile)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
546 )
31936
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
547 cookiejar.load()
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
548 self.cookiejar = cookiejar
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
549 except util.cookielib.LoadError as e:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
550 ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
551 _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
552 '(error loading cookie file %s: %s; continuing without '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
553 'cookies)\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
554 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
555 % (cookiefile, stringutil.forcebytestr(e))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
556 )
31936
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
557
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
558 def http_request(self, request):
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
559 if self.cookiejar:
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
560 self.cookiejar.add_cookie_header(request)
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
561
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
562 return request
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
563
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
564 def https_request(self, request):
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
565 if self.cookiejar:
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
566 self.cookiejar.add_cookie_header(request)
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
567
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
568 return request
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
569
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
570
9347
d0474b184347 url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents: 9122
diff changeset
571 handlerfuncs = []
d0474b184347 url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents: 9122
diff changeset
572
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
573
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
574 def opener(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
575 ui,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
576 authinfo=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
577 useragent=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
578 loggingfh=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
579 loggingname=b's',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
580 loggingopts=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
581 sendaccept=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
582 ):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
583 '''
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
584 construct an opener suitable for urllib2
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
585 authinfo will be added to the password manager
37011
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
586
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
587 The opener can be configured to log socket events if the various
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
588 ``logging*`` arguments are specified.
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
589
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
590 ``loggingfh`` denotes a file object to log events to.
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
591 ``loggingname`` denotes the name of the to print when logging.
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
592 ``loggingopts`` is a dict of keyword arguments to pass to the constructed
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
593 ``util.socketobserver`` instance.
37045
a708e1e4d7a8 url: support suppressing Accept header
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37011
diff changeset
594
a708e1e4d7a8 url: support suppressing Accept header
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37011
diff changeset
595 ``sendaccept`` allows controlling whether the ``Accept`` request header
a708e1e4d7a8 url: support suppressing Accept header
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37011
diff changeset
596 is sent. The header is sent by default.
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
597 '''
40043
6509fcec830c url: allow to configure timeout on http connection
Cédric Krier <ced@b2ck.com>
parents: 40034
diff changeset
598 timeout = ui.configwith(float, 'http', 'timeout')
37011
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
599 handlers = []
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
600
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
601 if loggingfh:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
602 handlers.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
603 logginghttphandler(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
604 loggingfh, loggingname, loggingopts or {}, timeout=timeout
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
605 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
606 )
37011
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
607 # We don't yet support HTTPS when logging I/O. If we attempt to open
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
608 # an HTTPS URL, we'll likely fail due to unknown protocol.
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
609
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
610 else:
40043
6509fcec830c url: allow to configure timeout on http connection
Cédric Krier <ced@b2ck.com>
parents: 40034
diff changeset
611 handlers.append(httphandler(timeout=timeout))
37011
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
612 if has_https:
40043
6509fcec830c url: allow to configure timeout on http connection
Cédric Krier <ced@b2ck.com>
parents: 40034
diff changeset
613 handlers.append(httpshandler(ui, timeout=timeout))
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
614
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
615 handlers.append(proxyhandler(ui))
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
616
29378
fea71f66ebff url: remember http password database in ui object
liscju <piotr.listkiewicz@gmail.com>
parents: 29377
diff changeset
617 passmgr = passwordmgr(ui, ui.httppasswordmgrdb)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
618 if authinfo is not None:
29379
fc777c855d66 largefiles: make cloning not ask two times about password (issue4883)
liscju <piotr.listkiewicz@gmail.com>
parents: 29378
diff changeset
619 realm, uris, user, passwd = authinfo
fc777c855d66 largefiles: make cloning not ask two times about password (issue4883)
liscju <piotr.listkiewicz@gmail.com>
parents: 29378
diff changeset
620 saveduser, savedpass = passmgr.find_stored_password(uris[0])
fc777c855d66 largefiles: make cloning not ask two times about password (issue4883)
liscju <piotr.listkiewicz@gmail.com>
parents: 29378
diff changeset
621 if user != saveduser or passwd:
fc777c855d66 largefiles: make cloning not ask two times about password (issue4883)
liscju <piotr.listkiewicz@gmail.com>
parents: 29378
diff changeset
622 passmgr.add_password(realm, uris, user, passwd)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
623 ui.debug(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
624 'http auth: user %s, password %s\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
625 % (user, passwd and '*' * len(passwd) or 'not set')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
626 )
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
627
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
628 handlers.extend(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
629 (httpbasicauthhandler(passmgr), httpdigestauthhandler(passmgr))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
630 )
9347
d0474b184347 url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents: 9122
diff changeset
631 handlers.extend([h(ui, passmgr) for h in handlerfuncs])
31936
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
632 handlers.append(cookiehandler(ui))
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
633 opener = urlreq.buildopener(*handlers)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
634
40034
393e44324037 httppeer: report http statistics
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40031
diff changeset
635 # keepalive.py's handlers will populate these attributes if they exist.
393e44324037 httppeer: report http statistics
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40031
diff changeset
636 opener.requestscount = 0
393e44324037 httppeer: report http statistics
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40031
diff changeset
637 opener.sentbytescount = 0
393e44324037 httppeer: report http statistics
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40031
diff changeset
638 opener.receivedbytescount = 0
393e44324037 httppeer: report http statistics
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40031
diff changeset
639
29589
486de14eb394 url: add distribution and version to user-agent request header (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29455
diff changeset
640 # The user agent should should *NOT* be used by servers for e.g.
486de14eb394 url: add distribution and version to user-agent request header (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29455
diff changeset
641 # protocol detection or feature negotiation: there are other
486de14eb394 url: add distribution and version to user-agent request header (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29455
diff changeset
642 # facilities for that.
486de14eb394 url: add distribution and version to user-agent request header (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29455
diff changeset
643 #
486de14eb394 url: add distribution and version to user-agent request header (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29455
diff changeset
644 # "mercurial/proto-1.0" was the original user agent string and
486de14eb394 url: add distribution and version to user-agent request header (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29455
diff changeset
645 # exists for backwards compatibility reasons.
486de14eb394 url: add distribution and version to user-agent request header (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29455
diff changeset
646 #
486de14eb394 url: add distribution and version to user-agent request header (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29455
diff changeset
647 # The "(Mercurial %s)" string contains the distribution
486de14eb394 url: add distribution and version to user-agent request header (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29455
diff changeset
648 # name and version. Other client implementations should choose their
486de14eb394 url: add distribution and version to user-agent request header (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29455
diff changeset
649 # own distribution name. Since servers should not be using the user
486de14eb394 url: add distribution and version to user-agent request header (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29455
diff changeset
650 # agent string for anything, clients should be able to define whatever
486de14eb394 url: add distribution and version to user-agent request header (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29455
diff changeset
651 # user agent they deem appropriate.
35439
e7bb5fc4570c lfs: add git to the User-Agent header for blob transfers
Matt Harbison <matt_harbison@yahoo.com>
parents: 34694
diff changeset
652 #
e7bb5fc4570c lfs: add git to the User-Agent header for blob transfers
Matt Harbison <matt_harbison@yahoo.com>
parents: 34694
diff changeset
653 # The custom user agent is for lfs, because unfortunately some servers
e7bb5fc4570c lfs: add git to the User-Agent header for blob transfers
Matt Harbison <matt_harbison@yahoo.com>
parents: 34694
diff changeset
654 # do look at this value.
e7bb5fc4570c lfs: add git to the User-Agent header for blob transfers
Matt Harbison <matt_harbison@yahoo.com>
parents: 34694
diff changeset
655 if not useragent:
e7bb5fc4570c lfs: add git to the User-Agent header for blob transfers
Matt Harbison <matt_harbison@yahoo.com>
parents: 34694
diff changeset
656 agent = 'mercurial/proto-1.0 (Mercurial %s)' % util.version()
e7bb5fc4570c lfs: add git to the User-Agent header for blob transfers
Matt Harbison <matt_harbison@yahoo.com>
parents: 34694
diff changeset
657 opener.addheaders = [(r'User-agent', pycompat.sysstr(agent))]
e7bb5fc4570c lfs: add git to the User-Agent header for blob transfers
Matt Harbison <matt_harbison@yahoo.com>
parents: 34694
diff changeset
658 else:
e7bb5fc4570c lfs: add git to the User-Agent header for blob transfers
Matt Harbison <matt_harbison@yahoo.com>
parents: 34694
diff changeset
659 opener.addheaders = [(r'User-agent', pycompat.sysstr(useragent))]
30763
a520aefb96f1 httppeer: advertise and support application/mercurial-0.2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30664
diff changeset
660
a520aefb96f1 httppeer: advertise and support application/mercurial-0.2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30664
diff changeset
661 # This header should only be needed by wire protocol requests. But it has
a520aefb96f1 httppeer: advertise and support application/mercurial-0.2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30664
diff changeset
662 # been sent on all requests since forever. We keep sending it for backwards
a520aefb96f1 httppeer: advertise and support application/mercurial-0.2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30664
diff changeset
663 # compatibility reasons. Modern versions of the wire protocol use
a520aefb96f1 httppeer: advertise and support application/mercurial-0.2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30664
diff changeset
664 # X-HgProto-<N> for advertising client support.
37045
a708e1e4d7a8 url: support suppressing Accept header
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37011
diff changeset
665 if sendaccept:
a708e1e4d7a8 url: support suppressing Accept header
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37011
diff changeset
666 opener.addheaders.append((r'Accept', r'application/mercurial-0.1'))
a708e1e4d7a8 url: support suppressing Accept header
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37011
diff changeset
667
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
668 return opener
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
669
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
670
42109
2a3c0106ded9 import: suppress accept: header
timeless <timeless@mozdev.org>
parents: 41711
diff changeset
671 def open(ui, url_, data=None, sendaccept=True):
14076
924c82157d46 url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents: 14071
diff changeset
672 u = util.url(url_)
13818
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
673 if u.scheme:
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
674 u.scheme = u.scheme.lower()
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
675 url_, authinfo = u.authinfo()
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
676 else:
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
677 path = util.normpath(os.path.abspath(url_))
37735
126998dcfb08 url: some bytes/str cleanup where we interface with stdlib funcs
Augie Fackler <augie@google.com>
parents: 37100
diff changeset
678 url_ = 'file://' + pycompat.bytesurl(urlreq.pathname2url(path))
7284
ac81ffac0f35 url: fix file:// URL handling
Patrick Mezard <pmezard@gmail.com>
parents: 7270
diff changeset
679 authinfo = None
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
680 return opener(ui, authinfo, sendaccept=sendaccept).open(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
681 pycompat.strurl(url_), data
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
682 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
683
40018
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
684
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
685 def wrapresponse(resp):
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
686 """Wrap a response object with common error handlers.
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
687
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
688 This ensures that any I/O from any consumer raises the appropriate
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
689 error and messaging.
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
690 """
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
691 origread = resp.read
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
692
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
693 class readerproxy(resp.__class__):
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
694 def read(self, size=None):
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
695 try:
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
696 return origread(size)
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
697 except httplib.IncompleteRead as e:
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
698 # e.expected is an integer if length known or None otherwise.
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
699 if e.expected:
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
700 got = len(e.partial)
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
701 total = e.expected + got
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
702 msg = _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
703 'HTTP request error (incomplete response; '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
704 'expected %d bytes got %d)'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
705 ) % (total, got)
40018
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
706 else:
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
707 msg = _('HTTP request error (incomplete response)')
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
708
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
709 raise error.PeerTransportError(
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
710 msg,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
711 hint=_(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
712 'this may be an intermittent network failure; '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
713 'if the error persists, consider contacting the '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
714 'network or server operator'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
715 ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
716 )
40018
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
717 except httplib.HTTPException as e:
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
718 raise error.PeerTransportError(
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
719 _('HTTP request error (%s)') % e,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
720 hint=_(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
721 'this may be an intermittent network failure; '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
722 'if the error persists, consider contacting the '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
723 'network or server operator'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
724 ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
725 )
40018
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
726
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
727 resp.__class__ = readerproxy