annotate mercurial/url.py @ 51873:0afd58c72175 default tip

tests: enable pytype checking on `mercurial/bundlerepo.py`
author Matt Harbison <matt_harbison@yahoo.com>
date Wed, 18 Sep 2024 21:00:20 -0400
parents f4733654f144
children
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 #
46819
d4ba4d51f85f contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents: 45942
diff changeset
3 # Copyright 2005, 2006, 2007, 2008 Olivia Mackall <olivia@selenic.com>
7270
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
51863
f4733654f144 typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents: 51862
diff changeset
10 from __future__ import annotations
25990
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 socket
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
14
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
15 from .i18n import _
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
16 from . import (
30820
6a70cf94d1b5 py3: replace pycompat.getenv with encoding.environ.get
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30763
diff changeset
17 encoding,
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25990
diff changeset
18 error,
25990
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
19 httpconnection as httpconnectionmod,
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
20 keepalive,
34428
0ee9cf8d054a url: use native strings for header values
Augie Fackler <augie@google.com>
parents: 33499
diff changeset
21 pycompat,
25990
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
22 sslutil,
34466
1232f7fa00c3 cleanup: use urllibcompat for renamed methods on urllib request objects
Augie Fackler <augie@google.com>
parents: 34428
diff changeset
23 urllibcompat,
25990
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
24 util,
c3efcdba08ea url: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25837
diff changeset
25 )
46907
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
26 from .utils import (
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
27 stringutil,
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
28 urlutil,
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
29 )
29455
0c741fd6158a py3: conditionalize httplib import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29379
diff changeset
30
0c741fd6158a py3: conditionalize httplib import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29379
diff changeset
31 httplib = util.httplib
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 26806
diff changeset
32 stringio = util.stringio
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
33 urlerr = util.urlerr
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
34 urlreq = util.urlreq
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
35
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
36
34694
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
37 def escape(s, quote=None):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45759
diff changeset
38 """Replace special characters "&", "<" and ">" to HTML-safe sequences.
34694
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
39 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
40 is also translated.
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
41
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
42 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
43 bytes, whereas cgi.escape in Python 3 only works on unicodes.
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45759
diff changeset
44 """
34694
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
45 s = s.replace(b"&", b"&amp;")
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
46 s = s.replace(b"<", b"&lt;")
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
47 s = s.replace(b">", b"&gt;")
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
48 if quote:
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
49 s = s.replace(b'"', b"&quot;")
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
50 return s
2976cf87a60a url: add cgi.escape equivalent for bytestrings
Augie Fackler <augie@google.com>
parents: 34466
diff changeset
51
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
52
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48936
diff changeset
53 class passwordmgr:
29377
2c019aac6b99 url: extract password database from password manager
liscju <piotr.listkiewicz@gmail.com>
parents: 29252
diff changeset
54 def __init__(self, ui, passwddb):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
55 self.ui = ui
29377
2c019aac6b99 url: extract password database from password manager
liscju <piotr.listkiewicz@gmail.com>
parents: 29252
diff changeset
56 self.passwddb = passwddb
2c019aac6b99 url: extract password database from password manager
liscju <piotr.listkiewicz@gmail.com>
parents: 29252
diff changeset
57
2c019aac6b99 url: extract password database from password manager
liscju <piotr.listkiewicz@gmail.com>
parents: 29252
diff changeset
58 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
59 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
60
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
61 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
62 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
63 assert isinstance(authuri, str)
29377
2c019aac6b99 url: extract password database from password manager
liscju <piotr.listkiewicz@gmail.com>
parents: 29252
diff changeset
64 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
65 user, passwd = authinfo
41450
d437d1e2a711 url: convert some variables back to bytes
Augie Fackler <augie@google.com>
parents: 41449
diff changeset
66 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
67 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
68 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
69 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
70
15005
4a43e23b8c55 hgweb: do not ignore [auth] if url has a username (issue2822)
Patrick Mezard <pmezard@gmail.com>
parents: 14244
diff changeset
71 if not user or not passwd:
15025
0593e8f81c71 http: pass user to readauthforuri() (fix 4a43e23b8c55)
Patrick Mezard <pmezard@gmail.com>
parents: 15005
diff changeset
72 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
73 if res:
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
74 group, auth = res
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
75 user, passwd = auth.get(b'username'), auth.get(b'password')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
76 self.ui.debug(b"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
77 if not user or not passwd:
46907
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
78 u = urlutil.url(pycompat.bytesurl(authuri))
20291
7d589d923b8a url: added authuri when login information is requested (issue3209)
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20174
diff changeset
79 u.query = None
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
80 if not self.ui.interactive():
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
81 raise error.Abort(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
82 _(b'http authorization required for %s')
46907
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
83 % urlutil.hidepassword(bytes(u))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
84 )
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
85
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
86 self.ui.write(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
87 _(b"http authorization required for %s\n")
46907
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
88 % urlutil.hidepassword(bytes(u))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
89 )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
90 self.ui.write(_(b"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
91 if user:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
92 self.ui.write(_(b"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
93 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
94 user = self.ui.prompt(_(b"user:"), default=None)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
95
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
96 if not passwd:
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
97 passwd = self.ui.getpass()
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
98
45759
ff48eea4a926 url: do not continue HTTP authentication with user=None (issue6425)
Yuya Nishihara <yuya@tcha.org>
parents: 44783
diff changeset
99 # As of Python 3.8, the default implementation of
ff48eea4a926 url: do not continue HTTP authentication with user=None (issue6425)
Yuya Nishihara <yuya@tcha.org>
parents: 44783
diff changeset
100 # AbstractBasicAuthHandler.retry_http_basic_auth() assumes the user
ff48eea4a926 url: do not continue HTTP authentication with user=None (issue6425)
Yuya Nishihara <yuya@tcha.org>
parents: 44783
diff changeset
101 # is set if pw is not None. This means (None, str) is not a valid
ff48eea4a926 url: do not continue HTTP authentication with user=None (issue6425)
Yuya Nishihara <yuya@tcha.org>
parents: 44783
diff changeset
102 # return type of find_user_password().
ff48eea4a926 url: do not continue HTTP authentication with user=None (issue6425)
Yuya Nishihara <yuya@tcha.org>
parents: 44783
diff changeset
103 if user is None:
ff48eea4a926 url: do not continue HTTP authentication with user=None (issue6425)
Yuya Nishihara <yuya@tcha.org>
parents: 44783
diff changeset
104 return None, None
ff48eea4a926 url: do not continue HTTP authentication with user=None (issue6425)
Yuya Nishihara <yuya@tcha.org>
parents: 44783
diff changeset
105
29377
2c019aac6b99 url: extract password database from password manager
liscju <piotr.listkiewicz@gmail.com>
parents: 29252
diff changeset
106 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
107 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
108 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
109
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
110 def _writedebug(self, user, passwd):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
111 msg = _(b'http auth: user %s, password %s\n')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
112 self.ui.debug(msg % (user, passwd and b'*' * len(passwd) or b'not set'))
8333
89c80c3dc584 allow http authentication information to be specified in the configuration
Sune Foldager <cryo@cyanite.org>
parents: 8225
diff changeset
113
15025
0593e8f81c71 http: pass user to readauthforuri() (fix 4a43e23b8c55)
Patrick Mezard <pmezard@gmail.com>
parents: 15005
diff changeset
114 def find_stored_password(self, authuri):
29377
2c019aac6b99 url: extract password database from password manager
liscju <piotr.listkiewicz@gmail.com>
parents: 29252
diff changeset
115 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
116
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
117
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
118 class proxyhandler(urlreq.proxyhandler):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
119 def __init__(self, ui):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
120 proxyurl = ui.config(b"http_proxy", b"host") or encoding.environ.get(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
121 b'http_proxy'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
122 )
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
123 # XXX proxyauthinfo = None
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
124
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
125 if proxyurl:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
126 # proxy can be proper url or host[:port]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
127 if not (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
128 proxyurl.startswith(b'http:') or proxyurl.startswith(b'https:')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
129 ):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
130 proxyurl = b'http://' + proxyurl + b'/'
46907
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
131 proxy = urlutil.url(proxyurl)
13820
65b89e80f892 url: use url.url in proxyhandler
Brodie Rao <brodie@bitheap.org>
parents: 13819
diff changeset
132 if not proxy.user:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
133 proxy.user = ui.config(b"http_proxy", b"user")
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
134 proxy.passwd = ui.config(b"http_proxy", b"passwd")
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
135
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
136 # see if we should use a proxy for this url
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
137 no_list = [b"localhost", b"127.0.0.1"]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
138 no_list.extend(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
139 [p.lower() for p in ui.configlist(b"http_proxy", b"no")]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
140 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
141 no_list.extend(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
142 [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
143 p.strip().lower()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
144 for p in encoding.environ.get(b"no_proxy", b'').split(b',')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
145 if p.strip()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
146 ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
147 )
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
148 # "http_proxy.always" config is for running tests on localhost
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
149 if ui.configbool(b"http_proxy", b"always"):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
150 self.no_list = []
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 self.no_list = no_list
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
153
41710
4028897dfa05 url: always use str for proxy configuration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41708
diff changeset
154 # 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
155 # expects them to be.
4028897dfa05 url: always use str for proxy configuration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41708
diff changeset
156 proxyurl = str(proxy)
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43106
diff changeset
157 proxies = {'http': proxyurl, 'https': proxyurl}
46907
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
158 ui.debug(
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
159 b'proxying through %s\n' % urlutil.hidepassword(bytes(proxy))
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
160 )
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
161 else:
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
162 proxies = {}
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
163
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
164 urlreq.proxyhandler.__init__(self, proxies)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
165 self.ui = ui
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
166
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
167 def proxy_open(self, req, proxy, type_):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
168 host = pycompat.bytesurl(urllibcompat.gethost(req)).split(b':')[0]
19535
df2155ebf502 proxy: allow wildcards in the no proxy list (issue1821)
Matt Mackall <mpm@selenic.com>
parents: 18176
diff changeset
169 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
170 if host == e:
df2155ebf502 proxy: allow wildcards in the no proxy list (issue1821)
Matt Mackall <mpm@selenic.com>
parents: 18176
diff changeset
171 return None
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
172 if e.startswith(b'*.') and host.endswith(e[2:]):
19535
df2155ebf502 proxy: allow wildcards in the no proxy list (issue1821)
Matt Mackall <mpm@selenic.com>
parents: 18176
diff changeset
173 return None
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
174 if e.startswith(b'.') and host.endswith(e[1:]):
19535
df2155ebf502 proxy: allow wildcards in the no proxy list (issue1821)
Matt Mackall <mpm@selenic.com>
parents: 18176
diff changeset
175 return None
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
176
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
177 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
178
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
179
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
180 def _gen_sendfile(orgsend):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
181 def _sendfile(self, data):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
182 # send a file
14244
e7525a555a64 url: use new http support if requested by the user
Augie Fackler <durin42@gmail.com>
parents: 14204
diff changeset
183 if isinstance(data, httpconnectionmod.httpsendfile):
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
184 # 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
185 data.seek(0)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
186 for chunk in util.filechunkiter(data):
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
187 orgsend(self, chunk)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
188 else:
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
189 orgsend(self, data)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
190
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
191 return _sendfile
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
192
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
193
50928
d718eddf01d9 safehasattr: drop usage in favor of hasattr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50926
diff changeset
194 has_https = hasattr(urlreq, 'httpshandler')
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
195
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
196
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
197 class httpconnection(keepalive.HTTPConnection):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
198 # must be able to send big bundle as stream.
13420
051f498628f7 url: refactor _gen_sendfile
Mads Kiilerich <mads@kiilerich.com>
parents: 13419
diff changeset
199 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
200
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
201
41708
d20f1594ff4a url: always access req._tunnel_host
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41590
diff changeset
202 # 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
203 # 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
204 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
205 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
206 if tunnel_host:
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43106
diff changeset
207 if tunnel_host[:7] not in ['http://', 'https:/']:
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43106
diff changeset
208 tunnel_host = 'https://' + tunnel_host
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
209 new_tunnel = True
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
210 else:
34466
1232f7fa00c3 cleanup: use urllibcompat for renamed methods on urllib request objects
Augie Fackler <augie@google.com>
parents: 34428
diff changeset
211 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
212 new_tunnel = False
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
213
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
214 if new_tunnel or tunnel_host == urllibcompat.getfullurl(req): # has proxy
46907
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
215 u = urlutil.url(pycompat.bytesurl(tunnel_host))
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
216 if new_tunnel or u.scheme == b'https': # only use CONNECT for HTTPS
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
217 h.realhostport = b':'.join([u.host, (u.port or b'443')])
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
218 h.headers = req.headers.copy()
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
219 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
220 return
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
221
10415
677f15da38c1 url: proxy handling, simplify and correctly deal with IPv6
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10411
diff changeset
222 h.realhostport = None
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
223 h.headers = None
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
224
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
225
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
226 class httphandler(keepalive.HTTPHandler):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
227 def http_open(self, req):
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
228 return self.do_open(httpconnection, req)
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
229
8590
59acb9c7d90f url: use CONNECT for HTTPS connections through HTTP proxy (issue967)
Henrik Stuart <hg@hstuart.dk>
parents: 8344
diff changeset
230 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
231 _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
232 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
233
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
234
51828
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
235 class logginghttphandler(httphandler):
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
236 """HTTP(S) handler that logs socket I/O."""
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
237
51828
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
238 def __init__(self, logfh, name, observeropts, *args, **kwargs):
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
239 super().__init__(*args, **kwargs)
37011
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
240
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
241 self._logfh = logfh
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
242 self._logname = name
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
243 self._observeropts = observeropts
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
244
51828
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
245 def do_open(self, http_class, *args, **kwargs):
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
246 _logfh = self._logfh
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
247 _logname = self._logname
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
248 _observeropts = self._observeropts
37011
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
249
51828
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
250 class logginghttpconnection(http_class):
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
251 def connect(self):
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
252 super().connect()
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
253 self.sock = util.makeloggingsocket(
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
254 _logfh, self.sock, _logname, **_observeropts
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
255 )
37011
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
256
51828
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
257 return super().do_open(logginghttpconnection, *args, **kwargs)
37011
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
258
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
259
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
260 if has_https:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
261
50925
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
262 def _generic_proxytunnel(self: "httpsconnection"):
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
263 headers = self.headers # pytype: disable=attribute-error
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
264 proxyheaders = {
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
265 pycompat.bytestr(x): pycompat.bytestr(headers[x])
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
266 for x in headers
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
267 if x.lower().startswith('proxy-')
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
268 }
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
269 realhostport = self.realhostport # pytype: disable=attribute-error
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
270 self.send(b'CONNECT %s HTTP/1.0\r\n' % realhostport)
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
271
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
272 for header in proxyheaders.items():
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
273 self.send(b'%s: %s\r\n' % header)
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
274 self.send(b'\r\n')
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
275
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
276 # majority of the following code is duplicated from
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
277 # httplib.HTTPConnection as there are no adequate places to
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
278 # override functions to provide the needed functionality.
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
279
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
280 # pytype: disable=attribute-error
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
281 res = self.response_class(self.sock, method=self._method)
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
282 # pytype: enable=attribute-error
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
283
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
284 while True:
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
285 # pytype: disable=attribute-error
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
286 version, status, reason = res._read_status()
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
287 # pytype: enable=attribute-error
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
288 if status != httplib.CONTINUE:
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
289 break
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
290 # skip lines that are all whitespace
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
291 list(iter(lambda: res.fp.readline().strip(), b''))
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
292
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
293 if status == 200:
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
294 # skip lines until we find a blank line
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
295 list(iter(res.fp.readline, b'\r\n'))
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
296 else:
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
297 self.close()
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
298 raise socket.error(
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
299 "Tunnel connection failed: %d %s" % (status, reason.strip())
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
300 )
13eab1a5db78 url: move the _generic_proxytunnel in the `has_https` block
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50614
diff changeset
301
40031
f2dffa1359c6 url: have httpsconnection inherit from our custom HTTPConnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40018
diff changeset
302 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
303 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
304 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
305 # 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
306 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
307 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
308
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
309 def __init__(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
310 self,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
311 host,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
312 port=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
313 key_file=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
314 cert_file=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
315 *args,
51862
607e94e01851 format: add many "missing" comma
Matt Harbison <matt_harbison@yahoo.com>
parents: 51830
diff changeset
316 **kwargs,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
317 ):
40031
f2dffa1359c6 url: have httpsconnection inherit from our custom HTTPConnection
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40018
diff changeset
318 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
319 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
320 self.cert_file = cert_file
9726
430e59ff3437 keepalive: handle broken pipes gracefully during large POSTs
Augie Fackler <durin42@gmail.com>
parents: 9467
diff changeset
321
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
322 def connect(self):
50318
3bb7c56e8fe6 url: don't ignore timeout for https connections
Julien Cristau <jcristau@mozilla.com>
parents: 49390
diff changeset
323 self.sock = socket.create_connection(
3bb7c56e8fe6 url: don't ignore timeout for https connections
Julien Cristau <jcristau@mozilla.com>
parents: 49390
diff changeset
324 (self.host, self.port), self.timeout
3bb7c56e8fe6 url: don't ignore timeout for https connections
Julien Cristau <jcristau@mozilla.com>
parents: 49390
diff changeset
325 )
13422
ebce5196b9db url: always create BetterHTTPS connections the same way
Mads Kiilerich <mads@kiilerich.com>
parents: 13421
diff changeset
326
13421
bd8bfa85d5a5 url: refactor BetterHTTPS.connect
Mads Kiilerich <mads@kiilerich.com>
parents: 13420
diff changeset
327 host = self.host
49390
9f3edb305261 typing: suppress a few attribute errors in url.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49279
diff changeset
328 realhostport = self.realhostport # pytype: disable=attribute-error
9f3edb305261 typing: suppress a few attribute errors in url.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49279
diff changeset
329 if realhostport: # use CONNECT proxy
14064
e4bfb9c337f3 remove unused imports and variables
Alexander Solovyov <alexander@solovyov.net>
parents: 13902
diff changeset
330 _generic_proxytunnel(self)
49390
9f3edb305261 typing: suppress a few attribute errors in url.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49279
diff changeset
331 host = realhostport.rsplit(b':', 1)[0]
25429
9d1c61715939 ssl: rename ssl_wrap_socket() to conform to our naming convention
Yuya Nishihara <yuya@tcha.org>
parents: 25415
diff changeset
332 self.sock = sslutil.wrapsocket(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
333 self.sock,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
334 self.key_file,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
335 self.cert_file,
49390
9f3edb305261 typing: suppress a few attribute errors in url.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49279
diff changeset
336 ui=self.ui, # pytype: disable=attribute-error
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
337 serverhostname=host,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
338 )
29227
dffe78d80a6c sslutil: convert socket validation from a class to a function (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28883
diff changeset
339 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
340
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
341 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
342 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
343 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
344 urlreq.httpshandler.__init__(self)
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
345 self.ui = ui
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
346 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
347
9852
917cf6bb6d0c url: generalise HTTPS proxy handling to accomodate Python changes
Henrik Stuart <hg@hstuart.dk>
parents: 9726
diff changeset
348 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
349 _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
350 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
351
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
352 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
353 # 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
354 # 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
355 url = urllibcompat.getfullurl(req)
15025
0593e8f81c71 http: pass user to readauthforuri() (fix 4a43e23b8c55)
Patrick Mezard <pmezard@gmail.com>
parents: 15005
diff changeset
356 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
357 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
358 if res:
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
359 group, auth = res
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
360 self.auth = auth
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
361 self.ui.debug(b"using auth.%s.* for authentication\n" % group)
13372
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
362 else:
5bced0d28a39 url: return the matched authentication group name from readauthforuri()
Steve Borho <steve@borho.org>
parents: 13371
diff changeset
363 self.auth = None
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
364 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
365
10408
50fb1fe143ff url: httplib.HTTPSConnection already handles IPv6 and port parsing fine
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
366 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
367 keyfile = None
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
368 certfile = None
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
369
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
370 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
371 keyfile = args[0]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
372 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
373 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
374 args = args[2:]
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
375
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
376 # 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
377 # hgrc, we prefer these
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
378 if self.auth and b'key' in self.auth and b'cert' in self.auth:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
379 keyfile = self.auth[b'key']
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
380 certfile = self.auth[b'cert']
8847
7951f385fcb7 url: support client certificate files over HTTPS (issue643)
Henrik Stuart <hg@hstuart.dk>
parents: 8590
diff changeset
381
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
382 conn = httpsconnection(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
383 host, port, keyfile, certfile, *args, **kwargs
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
384 )
10409
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
385 conn.ui = self.ui
4c94a3df4b10 url: SSL server certificate verification using web.cacerts file (issue1174)
Henrik Stuart <hg@hstuart.dk>
parents: 10408
diff changeset
386 return conn
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
387
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
388
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
389 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
390 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
391 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
392 self.retried_req = None
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
393
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
394 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
395 # 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
396 # 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
397 # 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
398 pass
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
399
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
400 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
401 # 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
402 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
403 self.retried_req = req
2ec346160783 http digest auth: reset redirect counter on new requests (issue2255)
Mads Kiilerich <mads@kiilerich.com>
parents: 11415
diff changeset
404 self.retried = 0
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
405 return urlreq.httpdigestauthhandler.http_error_auth_reqed(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
406 self, auth_header, host, req, headers
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
407 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
408
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
409
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
410 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
411 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
412 self.auth = None
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
413 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
414 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
415
20964
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
416 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
417 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
418 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
419
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
420 return request
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
421
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
422 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
423 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
424 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
425
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
426 return request
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
427
11844
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
428 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
429 # 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
430 # 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
431 # 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
432 pass
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
433
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
434 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
435 # 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
436 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
437 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
438 self.retried = 0
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
439 return urlreq.httpbasicauthhandler.http_error_auth_reqed(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
440 self, auth_header, host, req, headers
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
441 )
11844
6c51a5056020 http basic auth: reset redirect counter on new requests (issue2255)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11457
diff changeset
442
20964
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
443 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
444 user, pw = self.passwd.find_user_password(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
445 realm, urllibcompat.getfullurl(req)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
446 )
20964
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
447 if pw is not None:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
448 raw = b"%s:%s" % (pycompat.bytesurl(user), pycompat.bytesurl(pw))
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43106
diff changeset
449 auth = '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
450 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
451 return None
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
452 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
453 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
454 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
455 else:
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
456 return None
a939eeb94833 http: reuse authentication info after the first failed request (issue3567)
Stéphane Klein <contact@stephane-klein.info>
parents: 20291
diff changeset
457
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
458
51830
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
459 def load_cookiejar(ui):
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
460 cookiefile = ui.config(b'auth', b'cookiefile')
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
461 if not cookiefile:
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
462 return
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
463 cookiefile = util.expandpath(cookiefile)
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
464 try:
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
465 cookiejar = util.cookielib.MozillaCookieJar(
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
466 pycompat.fsdecode(cookiefile)
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
467 )
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
468 cookiejar.load()
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
469 return cookiejar
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
470 except util.cookielib.LoadError as e:
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
471 ui.warn(
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
472 _(
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
473 b'(error loading cookie file %s: %s; continuing without '
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
474 b'cookies)\n'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
475 )
51830
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
476 % (cookiefile, stringutil.forcebytestr(e))
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
477 )
31936
806f9a883b4f url: support auth.cookiesfile for adding cookies to HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30820
diff changeset
478
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
479
51827
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
480 class readlinehandler(urlreq.basehandler):
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
481 def http_response(self, request, response):
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
482 class readlineresponse(response.__class__):
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
483 def readlines(self, sizehint=0):
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
484 total = 0
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
485 list = []
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
486 while True:
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
487 line = self.readline()
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
488 if not line:
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
489 break
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
490 list.append(line)
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
491 total += len(line)
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
492 if sizehint and total >= sizehint:
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
493 break
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
494 return list
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
495
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
496 response.__class__ = readlineresponse
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
497 return response
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
498
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
499 https_response = http_response
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
500
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
501
9347
d0474b184347 url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents: 9122
diff changeset
502 handlerfuncs = []
d0474b184347 url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents: 9122
diff changeset
503
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
504
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
505 def opener(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
506 ui,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
507 authinfo=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
508 useragent=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
509 loggingfh=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
510 loggingname=b's',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
511 loggingopts=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
512 sendaccept=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
513 ):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45759
diff changeset
514 """
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
515 construct an opener suitable for urllib2
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
516 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
517
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
518 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
519 ``logging*`` arguments are specified.
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
520
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
521 ``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
522 ``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
523 ``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
524 ``util.socketobserver`` instance.
37045
a708e1e4d7a8 url: support suppressing Accept header
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37011
diff changeset
525
a708e1e4d7a8 url: support suppressing Accept header
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37011
diff changeset
526 ``sendaccept`` allows controlling whether the ``Accept`` request header
a708e1e4d7a8 url: support suppressing Accept header
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37011
diff changeset
527 is sent. The header is sent by default.
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45759
diff changeset
528 """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
529 timeout = ui.configwith(float, b'http', b'timeout')
37011
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
530 handlers = []
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
531
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
532 if loggingfh:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
533 handlers.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
534 logginghttphandler(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
535 loggingfh, loggingname, loggingopts or {}, timeout=timeout
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
536 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
537 )
37011
02221d6fb041 url: add HTTP handler that uses a proxied socket
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36652
diff changeset
538 else:
40043
6509fcec830c url: allow to configure timeout on http connection
Cédric Krier <ced@b2ck.com>
parents: 40034
diff changeset
539 handlers.append(httphandler(timeout=timeout))
51828
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
540 if has_https:
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
541 # pytype get confused about the conditional existence for httpshandler here.
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
542 handlers.append(
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
543 httpshandler(ui, timeout=timeout) # pytype: disable=name-error
b08de326bee4 debugwireproto: redo logging to also work for https
Joerg Sonnenberger <joerg@bec.de>
parents: 51827
diff changeset
544 )
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
545
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
546 handlers.append(proxyhandler(ui))
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
547
29378
fea71f66ebff url: remember http password database in ui object
liscju <piotr.listkiewicz@gmail.com>
parents: 29377
diff changeset
548 passmgr = passwordmgr(ui, ui.httppasswordmgrdb)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
549 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
550 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
551 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
552 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
553 passmgr.add_password(realm, uris, user, passwd)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
554 ui.debug(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
555 b'http auth: user %s, password %s\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
556 % (user, passwd and b'*' * len(passwd) or b'not set')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
557 )
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
558
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
559 handlers.extend(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
560 (httpbasicauthhandler(passmgr), httpdigestauthhandler(passmgr))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
561 )
9347
d0474b184347 url: add support for custom handlers in extensions
Henrik Stuart <hg@hstuart.dk>
parents: 9122
diff changeset
562 handlers.extend([h(ui, passmgr) for h in handlerfuncs])
51830
208698117124 http: use urllib's cookie handler
Joerg Sonnenberger <joerg@bec.de>
parents: 51828
diff changeset
563 handlers.append(urlreq.httpcookieprocessor(cookiejar=load_cookiejar(ui)))
51827
ace0da86edd0 urllib2: redo response.readlines addition via class patching
Joerg Sonnenberger <joerg@bec.de>
parents: 50928
diff changeset
564 handlers.append(readlinehandler())
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28861
diff changeset
565 opener = urlreq.buildopener(*handlers)
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
566
40034
393e44324037 httppeer: report http statistics
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40031
diff changeset
567 # 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
568 opener.requestscount = 0
393e44324037 httppeer: report http statistics
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40031
diff changeset
569 opener.sentbytescount = 0
393e44324037 httppeer: report http statistics
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40031
diff changeset
570 opener.receivedbytescount = 0
393e44324037 httppeer: report http statistics
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40031
diff changeset
571
29589
486de14eb394 url: add distribution and version to user-agent request header (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29455
diff changeset
572 # 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
573 # 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
574 # 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
575 #
486de14eb394 url: add distribution and version to user-agent request header (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29455
diff changeset
576 # "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
577 # 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
578 #
486de14eb394 url: add distribution and version to user-agent request header (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29455
diff changeset
579 # 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
580 # 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
581 # 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
582 # 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
583 # 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
584 #
e7bb5fc4570c lfs: add git to the User-Agent header for blob transfers
Matt Harbison <matt_harbison@yahoo.com>
parents: 34694
diff changeset
585 # 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
586 # 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
587 if not useragent:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
588 agent = b'mercurial/proto-1.0 (Mercurial %s)' % util.version()
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43106
diff changeset
589 opener.addheaders = [('User-agent', pycompat.sysstr(agent))]
35439
e7bb5fc4570c lfs: add git to the User-Agent header for blob transfers
Matt Harbison <matt_harbison@yahoo.com>
parents: 34694
diff changeset
590 else:
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43106
diff changeset
591 opener.addheaders = [('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
592
a520aefb96f1 httppeer: advertise and support application/mercurial-0.2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30664
diff changeset
593 # 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
594 # 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
595 # 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
596 # X-HgProto-<N> for advertising client support.
37045
a708e1e4d7a8 url: support suppressing Accept header
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37011
diff changeset
597 if sendaccept:
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43106
diff changeset
598 opener.addheaders.append(('Accept', 'application/mercurial-0.1'))
37045
a708e1e4d7a8 url: support suppressing Accept header
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37011
diff changeset
599
7270
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
600 return opener
2db33c1a5654 factor out the url handling from httprepo
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
601
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
602
42109
2a3c0106ded9 import: suppress accept: header
timeless <timeless@mozdev.org>
parents: 41711
diff changeset
603 def open(ui, url_, data=None, sendaccept=True):
46907
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
604 u = urlutil.url(url_)
13818
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
605 if u.scheme:
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
606 u.scheme = u.scheme.lower()
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
607 url_, authinfo = u.authinfo()
bf6156bab41b url: use url.url in url.open()
Brodie Rao <brodie@bitheap.org>
parents: 13817
diff changeset
608 else:
47630
8e5192e41e0b windows: use abspath in url
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46907
diff changeset
609 path = util.normpath(util.abspath(url_))
44641
e74af49aa3c9 url: pass str to pathname2url
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44452
diff changeset
610 url_ = b'file://' + pycompat.bytesurl(
e74af49aa3c9 url: pass str to pathname2url
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44452
diff changeset
611 urlreq.pathname2url(pycompat.fsdecode(path))
e74af49aa3c9 url: pass str to pathname2url
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44452
diff changeset
612 )
7284
ac81ffac0f35 url: fix file:// URL handling
Patrick Mezard <pmezard@gmail.com>
parents: 7270
diff changeset
613 authinfo = None
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
614 return opener(ui, authinfo, sendaccept=sendaccept).open(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
615 pycompat.strurl(url_), data
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
616 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
617
40018
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
618
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
619 def wrapresponse(resp):
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
620 """Wrap a response object with common error handlers.
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
621
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
622 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
623 error and messaging.
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
624 """
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
625 origread = resp.read
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
626
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
627 class readerproxy(resp.__class__):
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
628 def read(self, size=None):
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
629 try:
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
630 return origread(size)
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
631 except httplib.IncompleteRead as e:
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
632 # 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
633 if e.expected:
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
634 got = len(e.partial)
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
635 total = e.expected + got
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
636 msg = _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
637 b'HTTP request error (incomplete response; '
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
638 b'expected %d bytes got %d)'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
639 ) % (total, got)
40018
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
640 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
641 msg = _(b'HTTP request error (incomplete response)')
40018
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
642
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
643 raise error.PeerTransportError(
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
644 msg,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
645 hint=_(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
646 b'this may be an intermittent network failure; '
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
647 b'if the error persists, consider contacting the '
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
648 b'network or server operator'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
649 ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
650 )
40018
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
651 except httplib.HTTPException as e:
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
652 raise error.PeerTransportError(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
653 _(b'HTTP request error (%s)') % e,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
654 hint=_(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
655 b'this may be an intermittent network failure; '
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
656 b'if the error persists, consider contacting the '
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
657 b'network or server operator'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
658 ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42109
diff changeset
659 )
40018
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
660
f80db6adabbe url: move _wraphttpresponse() from httpeer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37735
diff changeset
661 resp.__class__ = readerproxy