##// END OF EJS Templates
interfaces: convert `imanifestrevisionstored` from zope `Attribute` attrs...
interfaces: convert `imanifestrevisionstored` from zope `Attribute` attrs This is the same transformation as b455dfddfed0 did for dirstate.

File last commit:

r52885:71658f79 default
r53370:a1dd484b default
Show More
url.py
742 lines | 25.4 KiB | text/x-python | PythonLexer
Benoit Boissinot
factor out the url handling from httprepo...
r7270 # url.py - HTTP handling for mercurial
#
Raphaël Gomès
contributor: change mentions of mpm to olivia...
r47575 # Copyright 2005, 2006, 2007, 2008 Olivia Mackall <olivia@selenic.com>
Benoit Boissinot
factor out the url handling from httprepo...
r7270 # Copyright 2006, 2007 Alexis S. L. Carvalho <alexis@cecm.usp.br>
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
#
Martin Geisler
updated license to be explicit about GPL version 2
r8225 # This software may be used and distributed according to the terms of the
Matt Mackall
Update license to GPLv2+
r10263 # GNU General Public License version 2 or any later version.
Benoit Boissinot
factor out the url handling from httprepo...
r7270
Matt Harbison
typing: add `from __future__ import annotations` to most files...
r52756 from __future__ import annotations
Gregory Szorc
url: use absolute_import
r25990
import base64
Joerg Sonnenberger
clonebundles: allow manifest to specify sha256 digest of bundles
r52875 import hashlib
Gregory Szorc
url: use absolute_import
r25990 import socket
from .i18n import _
Joerg Sonnenberger
clonebundles: allow manifest to specify sha256 digest of bundles
r52875 from .node import hex
Gregory Szorc
url: use absolute_import
r25990 from . import (
Pulkit Goyal
py3: replace pycompat.getenv with encoding.environ.get...
r30820 encoding,
Pierre-Yves David
error: get Abort from 'error' instead of 'util'...
r26587 error,
Gregory Szorc
url: use absolute_import
r25990 httpconnection as httpconnectionmod,
keepalive,
Augie Fackler
url: use native strings for header values...
r34429 pycompat,
Gregory Szorc
url: use absolute_import
r25990 sslutil,
Augie Fackler
cleanup: use urllibcompat for renamed methods on urllib request objects...
r34467 urllibcompat,
Gregory Szorc
url: use absolute_import
r25990 util,
)
urlutil: extract `url` related code from `util` into the new module...
r47669 from .utils import (
stringutil,
urlutil,
)
Pulkit Goyal
py3: conditionalize httplib import...
r29455
httplib = util.httplib
timeless
pycompat: switch to util.stringio for py3 compat
r28861 stringio = util.stringio
timeless
pycompat: switch to util.urlreq/util.urlerr for py3 compat
r28883 urlerr = util.urlerr
urlreq = util.urlreq
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
url: add cgi.escape equivalent for bytestrings...
r34695 def escape(s, quote=None):
Augie Fackler
formating: upgrade to black 20.8b1...
r46554 """Replace special characters "&", "<" and ">" to HTML-safe sequences.
Augie Fackler
url: add cgi.escape equivalent for bytestrings...
r34695 If the optional flag quote is true, the quotation mark character (")
is also translated.
This is the same as cgi.escape in Python, but always operates on
bytes, whereas cgi.escape in Python 3 only works on unicodes.
Augie Fackler
formating: upgrade to black 20.8b1...
r46554 """
Augie Fackler
url: add cgi.escape equivalent for bytestrings...
r34695 s = s.replace(b"&", b"&amp;")
s = s.replace(b"<", b"&lt;")
s = s.replace(b">", b"&gt;")
if quote:
s = s.replace(b'"', b"&quot;")
return s
Augie Fackler
formatting: blacken the codebase...
r43346
Gregory Szorc
py3: use class X: instead of class X(object):...
r49801 class passwordmgr:
liscju
url: extract password database from password manager...
r29377 def __init__(self, ui, passwddb):
Benoit Boissinot
factor out the url handling from httprepo...
r7270 self.ui = ui
liscju
url: extract password database from password manager...
r29377 self.passwddb = passwddb
def add_password(self, realm, uri, user, passwd):
return self.passwddb.add_password(realm, uri, user, passwd)
Benoit Boissinot
factor out the url handling from httprepo...
r7270
def find_user_password(self, realm, authuri):
Augie Fackler
url: add some defensive asserts on expected incoming types...
r41492 assert isinstance(realm, (type(None), str))
assert isinstance(authuri, str)
liscju
url: extract password database from password manager...
r29377 authinfo = self.passwddb.find_user_password(realm, authuri)
Benoit Boissinot
factor out the url handling from httprepo...
r7270 user, passwd = authinfo
Augie Fackler
url: convert some variables back to bytes...
r41493 user, passwd = pycompat.bytesurl(user), pycompat.bytesurl(passwd)
Benoit Boissinot
factor out the url handling from httprepo...
r7270 if user and passwd:
Sune Foldager
allow http authentication information to be specified in the configuration
r8333 self._writedebug(user, passwd)
Matt Harbison
py3: ensure the HTTP password manager returns strings, not bytes...
r41730 return (pycompat.strurl(user), pycompat.strurl(passwd))
Benoit Boissinot
factor out the url handling from httprepo...
r7270
Patrick Mezard
hgweb: do not ignore [auth] if url has a username (issue2822)...
r15005 if not user or not passwd:
Patrick Mezard
http: pass user to readauthforuri() (fix 4a43e23b8c55)...
r15025 res = httpconnectionmod.readauthforuri(self.ui, authuri, user)
Steve Borho
url: return the matched authentication group name from readauthforuri()...
r13372 if res:
group, auth = res
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 user, passwd = auth.get(b'username'), auth.get(b'password')
self.ui.debug(b"using auth.%s.* for authentication\n" % group)
Sune Foldager
allow http authentication information to be specified in the configuration
r8333 if not user or not passwd:
urlutil: extract `url` related code from `util` into the new module...
r47669 u = urlutil.url(pycompat.bytesurl(authuri))
Lucas Moscovicz
url: added authuri when login information is requested (issue3209)...
r20291 u.query = None
Sune Foldager
allow http authentication information to be specified in the configuration
r8333 if not self.ui.interactive():
Augie Fackler
formatting: blacken the codebase...
r43346 raise error.Abort(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _(b'http authorization required for %s')
urlutil: extract `url` related code from `util` into the new module...
r47669 % urlutil.hidepassword(bytes(u))
Augie Fackler
formatting: blacken the codebase...
r43346 )
Benoit Boissinot
factor out the url handling from httprepo...
r7270
Augie Fackler
formatting: blacken the codebase...
r43346 self.ui.write(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _(b"http authorization required for %s\n")
urlutil: extract `url` related code from `util` into the new module...
r47669 % urlutil.hidepassword(bytes(u))
Augie Fackler
formatting: blacken the codebase...
r43346 )
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 self.ui.write(_(b"realm: %s\n") % pycompat.bytesurl(realm))
Sune Foldager
allow http authentication information to be specified in the configuration
r8333 if user:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 self.ui.write(_(b"user: %s\n") % user)
Sune Foldager
allow http authentication information to be specified in the configuration
r8333 else:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 user = self.ui.prompt(_(b"user:"), default=None)
Benoit Boissinot
factor out the url handling from httprepo...
r7270
Sune Foldager
allow http authentication information to be specified in the configuration
r8333 if not passwd:
passwd = self.ui.getpass()
Benoit Boissinot
factor out the url handling from httprepo...
r7270
Yuya Nishihara
url: do not continue HTTP authentication with user=None (issue6425)...
r46381 # As of Python 3.8, the default implementation of
# AbstractBasicAuthHandler.retry_http_basic_auth() assumes the user
# is set if pw is not None. This means (None, str) is not a valid
# return type of find_user_password().
if user is None:
return None, None
liscju
url: extract password database from password manager...
r29377 self.passwddb.add_password(realm, authuri, user, passwd)
Sune Foldager
allow http authentication information to be specified in the configuration
r8333 self._writedebug(user, passwd)
Matt Harbison
py3: ensure the HTTP password manager returns strings, not bytes...
r41730 return (pycompat.strurl(user), pycompat.strurl(passwd))
Benoit Boissinot
factor out the url handling from httprepo...
r7270
Sune Foldager
allow http authentication information to be specified in the configuration
r8333 def _writedebug(self, user, passwd):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 msg = _(b'http auth: user %s, password %s\n')
self.ui.debug(msg % (user, passwd and b'*' * len(passwd) or b'not set'))
Sune Foldager
allow http authentication information to be specified in the configuration
r8333
Patrick Mezard
http: pass user to readauthforuri() (fix 4a43e23b8c55)...
r15025 def find_stored_password(self, authuri):
liscju
url: extract password database from password manager...
r29377 return self.passwddb.find_user_password(None, authuri)
Patrick Mezard
http: pass user to readauthforuri() (fix 4a43e23b8c55)...
r15025
Augie Fackler
formatting: blacken the codebase...
r43346
timeless
pycompat: switch to util.urlreq/util.urlerr for py3 compat
r28883 class proxyhandler(urlreq.proxyhandler):
Benoit Boissinot
factor out the url handling from httprepo...
r7270 def __init__(self, ui):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 proxyurl = ui.config(b"http_proxy", b"host") or encoding.environ.get(
b'http_proxy'
Augie Fackler
formatting: blacken the codebase...
r43346 )
Benoit Boissinot
factor out the url handling from httprepo...
r7270 # XXX proxyauthinfo = None
if proxyurl:
# proxy can be proper url or host[:port]
Augie Fackler
formatting: blacken the codebase...
r43346 if not (
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 proxyurl.startswith(b'http:') or proxyurl.startswith(b'https:')
Augie Fackler
formatting: blacken the codebase...
r43346 ):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 proxyurl = b'http://' + proxyurl + b'/'
urlutil: extract `url` related code from `util` into the new module...
r47669 proxy = urlutil.url(proxyurl)
Brodie Rao
url: use url.url in proxyhandler
r13820 if not proxy.user:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 proxy.user = ui.config(b"http_proxy", b"user")
proxy.passwd = ui.config(b"http_proxy", b"passwd")
Benoit Boissinot
factor out the url handling from httprepo...
r7270
# see if we should use a proxy for this url
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 no_list = [b"localhost", b"127.0.0.1"]
Augie Fackler
formatting: blacken the codebase...
r43346 no_list.extend(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 [p.lower() for p in ui.configlist(b"http_proxy", b"no")]
Augie Fackler
formatting: blacken the codebase...
r43346 )
no_list.extend(
[
p.strip().lower()
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 for p in encoding.environ.get(b"no_proxy", b'').split(b',')
Augie Fackler
formatting: blacken the codebase...
r43346 if p.strip()
]
)
Benoit Boissinot
factor out the url handling from httprepo...
r7270 # "http_proxy.always" config is for running tests on localhost
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if ui.configbool(b"http_proxy", b"always"):
Benoit Boissinot
factor out the url handling from httprepo...
r7270 self.no_list = []
else:
self.no_list = no_list
Gregory Szorc
url: always use str for proxy configuration...
r41859 # Keys and values need to be str because the standard library
# expects them to be.
proxyurl = str(proxy)
Augie Fackler
cleanup: remove pointless r-prefixes on single-quoted strings...
r43906 proxies = {'http': proxyurl, 'https': proxyurl}
urlutil: extract `url` related code from `util` into the new module...
r47669 ui.debug(
b'proxying through %s\n' % urlutil.hidepassword(bytes(proxy))
)
Benoit Boissinot
factor out the url handling from httprepo...
r7270 else:
proxies = {}
timeless
pycompat: switch to util.urlreq/util.urlerr for py3 compat
r28883 urlreq.proxyhandler.__init__(self, proxies)
Benoit Boissinot
factor out the url handling from httprepo...
r7270 self.ui = ui
def proxy_open(self, req, proxy, type_):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 host = pycompat.bytesurl(urllibcompat.gethost(req)).split(b':')[0]
Matt Mackall
proxy: allow wildcards in the no proxy list (issue1821)
r19535 for e in self.no_list:
if host == e:
return None
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if e.startswith(b'*.') and host.endswith(e[2:]):
Matt Mackall
proxy: allow wildcards in the no proxy list (issue1821)
r19535 return None
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if e.startswith(b'.') and host.endswith(e[1:]):
Matt Mackall
proxy: allow wildcards in the no proxy list (issue1821)
r19535 return None
Benoit Boissinot
factor out the url handling from httprepo...
r7270
timeless
pycompat: switch to util.urlreq/util.urlerr for py3 compat
r28883 return urlreq.proxyhandler.proxy_open(self, req, proxy, type_)
Benoit Boissinot
factor out the url handling from httprepo...
r7270
Augie Fackler
formatting: blacken the codebase...
r43346
Mads Kiilerich
url: refactor _gen_sendfile
r13420 def _gen_sendfile(orgsend):
Benoit Boissinot
factor out the url handling from httprepo...
r7270 def _sendfile(self, data):
# send a file
Augie Fackler
url: use new http support if requested by the user...
r14244 if isinstance(data, httpconnectionmod.httpsendfile):
Benoit Boissinot
factor out the url handling from httprepo...
r7270 # if auth required, some data sent twice, so rewind here
data.seek(0)
for chunk in util.filechunkiter(data):
Mads Kiilerich
url: refactor _gen_sendfile
r13420 orgsend(self, chunk)
Benoit Boissinot
factor out the url handling from httprepo...
r7270 else:
Mads Kiilerich
url: refactor _gen_sendfile
r13420 orgsend(self, data)
Augie Fackler
formatting: blacken the codebase...
r43346
Benoit Boissinot
factor out the url handling from httprepo...
r7270 return _sendfile
Augie Fackler
formatting: blacken the codebase...
r43346
safehasattr: drop usage in favor of hasattr...
r51821 has_https = hasattr(urlreq, 'httpshandler')
Henrik Stuart
url: SSL server certificate verification using web.cacerts file (issue1174)
r10409
Augie Fackler
formatting: blacken the codebase...
r43346
Benoit Boissinot
factor out the url handling from httprepo...
r7270 class httpconnection(keepalive.HTTPConnection):
# must be able to send big bundle as stream.
Mads Kiilerich
url: refactor _gen_sendfile
r13420 send = _gen_sendfile(keepalive.HTTPConnection.send)
Benoit Boissinot
factor out the url handling from httprepo...
r7270
Augie Fackler
formatting: blacken the codebase...
r43346
Gregory Szorc
url: always access req._tunnel_host...
r41857 # Large parts of this function have their origin from before Python 2.6
# and could potentially be removed.
Henrik Stuart
url: generalise HTTPS proxy handling to accomodate Python changes...
r9852 def _generic_start_transaction(handler, h, req):
Gregory Szorc
url: always access req._tunnel_host...
r41857 tunnel_host = req._tunnel_host
Augie Fackler
url: replace uses of hasattr with safehasattr or getattr
r14964 if tunnel_host:
Augie Fackler
cleanup: remove pointless r-prefixes on single-quoted strings...
r43906 if tunnel_host[:7] not in ['http://', 'https:/']:
tunnel_host = 'https://' + tunnel_host
Henrik Stuart
url: generalise HTTPS proxy handling to accomodate Python changes...
r9852 new_tunnel = True
else:
Augie Fackler
cleanup: use urllibcompat for renamed methods on urllib request objects...
r34467 tunnel_host = urllibcompat.getselector(req)
Henrik Stuart
url: generalise HTTPS proxy handling to accomodate Python changes...
r9852 new_tunnel = False
Augie Fackler
formatting: blacken the codebase...
r43346 if new_tunnel or tunnel_host == urllibcompat.getfullurl(req): # has proxy
urlutil: extract `url` related code from `util` into the new module...
r47669 u = urlutil.url(pycompat.bytesurl(tunnel_host))
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if new_tunnel or u.scheme == b'https': # only use CONNECT for HTTPS
h.realhostport = b':'.join([u.host, (u.port or b'443')])
Henrik Stuart
url: generalise HTTPS proxy handling to accomodate Python changes...
r9852 h.headers = req.headers.copy()
h.headers.update(handler.parent.addheaders)
return
Benoit Boissinot
url: proxy handling, simplify and correctly deal with IPv6...
r10415 h.realhostport = None
Henrik Stuart
url: generalise HTTPS proxy handling to accomodate Python changes...
r9852 h.headers = None
Augie Fackler
formatting: blacken the codebase...
r43346
Benoit Boissinot
factor out the url handling from httprepo...
r7270 class httphandler(keepalive.HTTPHandler):
def http_open(self, req):
return self.do_open(httpconnection, req)
Henrik Stuart
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)...
r8590 def _start_transaction(self, h, req):
Henrik Stuart
url: generalise HTTPS proxy handling to accomodate Python changes...
r9852 _generic_start_transaction(self, h, req)
Henrik Stuart
url: use CONNECT for HTTPS connections through HTTP proxy (issue967)...
r8590 return keepalive.HTTPHandler._start_transaction(self, h, req)
Augie Fackler
formatting: blacken the codebase...
r43346
Joerg Sonnenberger
debugwireproto: redo logging to also work for https
r52721 class logginghttphandler(httphandler):
"""HTTP(S) handler that logs socket I/O."""
Augie Fackler
formatting: blacken the codebase...
r43346
Joerg Sonnenberger
debugwireproto: redo logging to also work for https
r52721 def __init__(self, logfh, name, observeropts, *args, **kwargs):
super().__init__(*args, **kwargs)
Gregory Szorc
url: add HTTP handler that uses a proxied socket...
r37029
self._logfh = logfh
self._logname = name
self._observeropts = observeropts
Joerg Sonnenberger
debugwireproto: redo logging to also work for https
r52721 def do_open(self, http_class, *args, **kwargs):
_logfh = self._logfh
_logname = self._logname
_observeropts = self._observeropts
Gregory Szorc
url: add HTTP handler that uses a proxied socket...
r37029
Joerg Sonnenberger
debugwireproto: redo logging to also work for https
r52721 class logginghttpconnection(http_class):
def connect(self):
super().connect()
self.sock = util.makeloggingsocket(
_logfh, self.sock, _logname, **_observeropts
)
Gregory Szorc
url: add HTTP handler that uses a proxied socket...
r37029
Joerg Sonnenberger
debugwireproto: redo logging to also work for https
r52721 return super().do_open(logginghttpconnection, *args, **kwargs)
Gregory Szorc
url: add HTTP handler that uses a proxied socket...
r37029
Augie Fackler
formatting: blacken the codebase...
r43346
Benoit Boissinot
factor out the url handling from httprepo...
r7270 if has_https:
Augie Fackler
formatting: blacken the codebase...
r43346
url: move the _generic_proxytunnel in the `has_https` block...
r51818 def _generic_proxytunnel(self: "httpsconnection"):
headers = self.headers # pytype: disable=attribute-error
proxyheaders = {
pycompat.bytestr(x): pycompat.bytestr(headers[x])
for x in headers
if x.lower().startswith('proxy-')
}
realhostport = self.realhostport # pytype: disable=attribute-error
self.send(b'CONNECT %s HTTP/1.0\r\n' % realhostport)
for header in proxyheaders.items():
self.send(b'%s: %s\r\n' % header)
self.send(b'\r\n')
# majority of the following code is duplicated from
# httplib.HTTPConnection as there are no adequate places to
# override functions to provide the needed functionality.
# pytype: disable=attribute-error
res = self.response_class(self.sock, method=self._method)
# pytype: enable=attribute-error
while True:
# pytype: disable=attribute-error
version, status, reason = res._read_status()
# pytype: enable=attribute-error
if status != httplib.CONTINUE:
break
# skip lines that are all whitespace
list(iter(lambda: res.fp.readline().strip(), b''))
if status == 200:
# skip lines until we find a blank line
list(iter(res.fp.readline, b'\r\n'))
else:
self.close()
raise socket.error(
"Tunnel connection failed: %d %s" % (status, reason.strip())
)
Gregory Szorc
url: have httpsconnection inherit from our custom HTTPConnection...
r40067 class httpsconnection(keepalive.HTTPConnection):
Mads Kiilerich
url: merge BetterHTTPS with httpsconnection to get some proxy https validation
r13424 response_class = keepalive.HTTPResponse
Yuya Nishihara
https: do not inherit httplib.HTTPSConnection that creates unused SSLContext...
r25414 default_port = httplib.HTTPS_PORT
Mads Kiilerich
url: merge BetterHTTPS with httpsconnection to get some proxy https validation
r13424 # must be able to send big bundle as stream.
send = _gen_sendfile(keepalive.safesend)
Yuya Nishihara
https: do not inherit httplib.HTTPSConnection that creates unused SSLContext...
r25414 getresponse = keepalive.wrapgetresponse(httplib.HTTPConnection)
Augie Fackler
formatting: blacken the codebase...
r43346 def __init__(
self,
host,
port=None,
key_file=None,
cert_file=None,
*args,
Matt Harbison
format: add many "missing" comma...
r52755 **kwargs,
Augie Fackler
formatting: blacken the codebase...
r43346 ):
Gregory Szorc
url: have httpsconnection inherit from our custom HTTPConnection...
r40067 keepalive.HTTPConnection.__init__(self, host, port, *args, **kwargs)
Yuya Nishihara
https: do not inherit httplib.HTTPSConnection that creates unused SSLContext...
r25414 self.key_file = key_file
self.cert_file = cert_file
Augie Fackler
keepalive: handle broken pipes gracefully during large POSTs
r9726
Henrik Stuart
url: SSL server certificate verification using web.cacerts file (issue1174)
r10409 def connect(self):
Julien Cristau
url: don't ignore timeout for https connections...
r51244 self.sock = socket.create_connection(
(self.host, self.port), self.timeout
)
Mads Kiilerich
url: always create BetterHTTPS connections the same way
r13422
Mads Kiilerich
url: refactor BetterHTTPS.connect
r13421 host = self.host
Matt Harbison
typing: suppress a few attribute errors in url.py...
r50285 realhostport = self.realhostport # pytype: disable=attribute-error
if realhostport: # use CONNECT proxy
Alexander Solovyov
remove unused imports and variables
r14064 _generic_proxytunnel(self)
Matt Harbison
typing: suppress a few attribute errors in url.py...
r50285 host = realhostport.rsplit(b':', 1)[0]
Yuya Nishihara
ssl: rename ssl_wrap_socket() to conform to our naming convention...
r25429 self.sock = sslutil.wrapsocket(
Augie Fackler
formatting: blacken the codebase...
r43346 self.sock,
self.key_file,
self.cert_file,
Matt Harbison
typing: suppress a few attribute errors in url.py...
r50285 ui=self.ui, # pytype: disable=attribute-error
Augie Fackler
formatting: blacken the codebase...
r43346 serverhostname=host,
)
Gregory Szorc
sslutil: convert socket validation from a class to a function (API)...
r29227 sslutil.validatesocket(self.sock)
Henrik Stuart
url: SSL server certificate verification using web.cacerts file (issue1174)
r10409
timeless
pycompat: switch to util.urlreq/util.urlerr for py3 compat
r28883 class httpshandler(keepalive.KeepAliveHandler, urlreq.httpshandler):
Cédric Krier
url: allow to configure timeout on http connection...
r40079 def __init__(self, ui, timeout=None):
keepalive.KeepAliveHandler.__init__(self, timeout=timeout)
timeless
pycompat: switch to util.urlreq/util.urlerr for py3 compat
r28883 urlreq.httpshandler.__init__(self)
Henrik Stuart
url: support client certificate files over HTTPS (issue643)...
r8847 self.ui = ui
Augie Fackler
formatting: blacken the codebase...
r43346 self.pwmgr = passwordmgr(self.ui, self.ui.httppasswordmgrdb)
Henrik Stuart
url: support client certificate files over HTTPS (issue643)...
r8847
Henrik Stuart
url: generalise HTTPS proxy handling to accomodate Python changes...
r9852 def _start_transaction(self, h, req):
_generic_start_transaction(self, h, req)
return keepalive.KeepAliveHandler._start_transaction(self, h, req)
Benoit Boissinot
factor out the url handling from httprepo...
r7270 def https_open(self, req):
Augie Fackler
cleanup: use urllibcompat for renamed methods on urllib request objects...
r34467 # urllibcompat.getfullurl() does not contain credentials
# and we may need them to match the certificates.
url = urllibcompat.getfullurl(req)
Patrick Mezard
http: pass user to readauthforuri() (fix 4a43e23b8c55)...
r15025 user, password = self.pwmgr.find_stored_password(url)
res = httpconnectionmod.readauthforuri(self.ui, url, user)
Steve Borho
url: return the matched authentication group name from readauthforuri()...
r13372 if res:
group, auth = res
self.auth = auth
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 self.ui.debug(b"using auth.%s.* for authentication\n" % group)
Steve Borho
url: return the matched authentication group name from readauthforuri()...
r13372 else:
self.auth = None
Henrik Stuart
url: support client certificate files over HTTPS (issue643)...
r8847 return self.do_open(self._makeconnection, req)
Benoit Boissinot
url: httplib.HTTPSConnection already handles IPv6 and port parsing fine
r10408 def _makeconnection(self, host, port=None, *args, **kwargs):
Henrik Stuart
url: support client certificate files over HTTPS (issue643)...
r8847 keyfile = None
certfile = None
Augie Fackler
formatting: blacken the codebase...
r43346 if len(args) >= 1: # key_file
Benoit Boissinot
url: *args argument is a tuple, not a list (found by pylint)...
r10511 keyfile = args[0]
Augie Fackler
formatting: blacken the codebase...
r43346 if len(args) >= 2: # cert_file
Benoit Boissinot
url: *args argument is a tuple, not a list (found by pylint)...
r10511 certfile = args[1]
args = args[2:]
Henrik Stuart
url: support client certificate files over HTTPS (issue643)...
r8847
# if the user has specified different key/cert files in
# hgrc, we prefer these
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if self.auth and b'key' in self.auth and b'cert' in self.auth:
keyfile = self.auth[b'key']
certfile = self.auth[b'cert']
Henrik Stuart
url: support client certificate files over HTTPS (issue643)...
r8847
Augie Fackler
formatting: blacken the codebase...
r43346 conn = httpsconnection(
host, port, keyfile, certfile, *args, **kwargs
)
Henrik Stuart
url: SSL server certificate verification using web.cacerts file (issue1174)
r10409 conn.ui = self.ui
return conn
Benoit Boissinot
factor out the url handling from httprepo...
r7270
Augie Fackler
formatting: blacken the codebase...
r43346
timeless
pycompat: switch to util.urlreq/util.urlerr for py3 compat
r28883 class httpdigestauthhandler(urlreq.httpdigestauthhandler):
Mads Kiilerich
http digest auth: reset redirect counter on new requests (issue2255)...
r11457 def __init__(self, *args, **kwargs):
timeless
pycompat: switch to util.urlreq/util.urlerr for py3 compat
r28883 urlreq.httpdigestauthhandler.__init__(self, *args, **kwargs)
Mads Kiilerich
http digest auth: reset redirect counter on new requests (issue2255)...
r11457 self.retried_req = None
def reset_retry_count(self):
# Python 2.6.5 will call this on 401 or 407 errors and thus loop
# forever. We disable reset_retry_count completely and reset in
# http_error_auth_reqed instead.
pass
Benoit Boissinot
factor out the url handling from httprepo...
r7270 def http_error_auth_reqed(self, auth_header, host, req, headers):
Mads Kiilerich
http digest auth: reset redirect counter on new requests (issue2255)...
r11457 # Reset the retry counter once for each request.
if req is not self.retried_req:
self.retried_req = req
self.retried = 0
timeless
pycompat: switch to util.urlreq/util.urlerr for py3 compat
r28883 return urlreq.httpdigestauthhandler.http_error_auth_reqed(
Augie Fackler
formatting: blacken the codebase...
r43346 self, auth_header, host, req, headers
)
Benoit Boissinot
factor out the url handling from httprepo...
r7270
timeless
pycompat: switch to util.urlreq/util.urlerr for py3 compat
r28883 class httpbasicauthhandler(urlreq.httpbasicauthhandler):
Wagner Bruna
http basic auth: reset redirect counter on new requests (issue2255)...
r11844 def __init__(self, *args, **kwargs):
Stéphane Klein
http: reuse authentication info after the first failed request (issue3567)...
r20964 self.auth = None
timeless
pycompat: switch to util.urlreq/util.urlerr for py3 compat
r28883 urlreq.httpbasicauthhandler.__init__(self, *args, **kwargs)
Wagner Bruna
http basic auth: reset redirect counter on new requests (issue2255)...
r11844 self.retried_req = None
Stéphane Klein
http: reuse authentication info after the first failed request (issue3567)...
r20964 def http_request(self, request):
if self.auth:
request.add_unredirected_header(self.auth_header, self.auth)
return request
def https_request(self, request):
if self.auth:
request.add_unredirected_header(self.auth_header, self.auth)
return request
Wagner Bruna
http basic auth: reset redirect counter on new requests (issue2255)...
r11844 def reset_retry_count(self):
# Python 2.6.5 will call this on 401 or 407 errors and thus loop
# forever. We disable reset_retry_count completely and reset in
# http_error_auth_reqed instead.
pass
def http_error_auth_reqed(self, auth_header, host, req, headers):
# Reset the retry counter once for each request.
if req is not self.retried_req:
self.retried_req = req
self.retried = 0
timeless
pycompat: switch to util.urlreq/util.urlerr for py3 compat
r28883 return urlreq.httpbasicauthhandler.http_error_auth_reqed(
Augie Fackler
formatting: blacken the codebase...
r43346 self, auth_header, host, req, headers
)
Wagner Bruna
http basic auth: reset redirect counter on new requests (issue2255)...
r11844
Stéphane Klein
http: reuse authentication info after the first failed request (issue3567)...
r20964 def retry_http_basic_auth(self, host, req, realm):
Augie Fackler
cleanup: use urllibcompat for renamed methods on urllib request objects...
r34467 user, pw = self.passwd.find_user_password(
Augie Fackler
formatting: blacken the codebase...
r43346 realm, urllibcompat.getfullurl(req)
)
Stéphane Klein
http: reuse authentication info after the first failed request (issue3567)...
r20964 if pw is not None:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raw = b"%s:%s" % (pycompat.bytesurl(user), pycompat.bytesurl(pw))
Augie Fackler
cleanup: remove pointless r-prefixes on single-quoted strings...
r43906 auth = 'Basic %s' % pycompat.strurl(base64.b64encode(raw).strip())
Kim Randell
url: avoid re-issuing incorrect password (issue3210)...
r29639 if req.get_header(self.auth_header, None) == auth:
Stéphane Klein
http: reuse authentication info after the first failed request (issue3567)...
r20964 return None
self.auth = auth
req.add_unredirected_header(self.auth_header, auth)
return self.parent.open(req)
else:
return None
Augie Fackler
formatting: blacken the codebase...
r43346
Joerg Sonnenberger
http: use urllib's cookie handler...
r52723 def load_cookiejar(ui):
cookiefile = ui.config(b'auth', b'cookiefile')
if not cookiefile:
return
cookiefile = util.expandpath(cookiefile)
try:
cookiejar = util.cookielib.MozillaCookieJar(
pycompat.fsdecode(cookiefile)
)
cookiejar.load()
return cookiejar
except util.cookielib.LoadError as e:
ui.warn(
_(
b'(error loading cookie file %s: %s; continuing without '
b'cookies)\n'
Augie Fackler
formatting: blacken the codebase...
r43346 )
Joerg Sonnenberger
http: use urllib's cookie handler...
r52723 % (cookiefile, stringutil.forcebytestr(e))
)
Gregory Szorc
url: support auth.cookiesfile for adding cookies to HTTP requests...
r31936
Augie Fackler
formatting: blacken the codebase...
r43346
Joerg Sonnenberger
urllib2: redo response.readlines addition via class patching
r52720 class readlinehandler(urlreq.basehandler):
def http_response(self, request, response):
class readlineresponse(response.__class__):
def readlines(self, sizehint=0):
total = 0
list = []
while True:
line = self.readline()
if not line:
break
list.append(line)
total += len(line)
if sizehint and total >= sizehint:
break
return list
response.__class__ = readlineresponse
return response
https_response = http_response
Joerg Sonnenberger
clonebundles: allow manifest to specify sha256 digest of bundles
r52875 class digesthandler(urlreq.basehandler):
# exchange.py assumes the algorithms are listed in order of preference,
# earlier entries are prefered.
digest_algorithms = {
b'sha256': hashlib.sha256,
b'sha512': hashlib.sha512,
}
def __init__(self, digest):
if b':' not in digest:
raise error.Abort(_(b'invalid digest specification'))
algo, checksum = digest.split(b':')
if algo not in self.digest_algorithms:
raise error.Abort(_(b'unsupported digest algorithm: %s') % algo)
self._digest = checksum
self._hasher = self.digest_algorithms[algo]()
def http_response(self, request, response):
class digestresponse(response.__class__):
def _digest_input(self, data):
self._hasher.update(data)
self._digest_consumed += len(data)
if self._digest_finished:
digest = hex(self._hasher.digest())
if digest != self._digest:
raise error.SecurityError(
_(
b'file with digest %s expected, but %s found for %d bytes'
)
% (
pycompat.bytestr(self._digest),
pycompat.bytestr(digest),
self._digest_consumed,
)
)
def read(self, amt=None):
Joerg Sonnenberger
clonebundle-digest: add recursion guards for Python 3.8...
r52885 self._digest_recursion_level += 1
Joerg Sonnenberger
clonebundles: allow manifest to specify sha256 digest of bundles
r52875 data = super().read(amt)
Joerg Sonnenberger
clonebundle-digest: add recursion guards for Python 3.8...
r52885 self._digest_recursion_level -= 1
if self._digest_recursion_level == 0:
self._digest_input(data)
Joerg Sonnenberger
clonebundles: allow manifest to specify sha256 digest of bundles
r52875 return data
def readline(self):
Joerg Sonnenberger
clonebundle-digest: add recursion guards for Python 3.8...
r52885 self._digest_recursion_level += 1
Joerg Sonnenberger
clonebundles: allow manifest to specify sha256 digest of bundles
r52875 data = super().readline()
Joerg Sonnenberger
clonebundle-digest: add recursion guards for Python 3.8...
r52885 self._digest_recursion_level -= 1
if self._digest_recursion_level == 0:
self._digest_input(data)
Joerg Sonnenberger
clonebundles: allow manifest to specify sha256 digest of bundles
r52875 return data
def readinto(self, dest):
Joerg Sonnenberger
clonebundle-digest: add recursion guards for Python 3.8...
r52885 self._digest_recursion_level += 1
Joerg Sonnenberger
clonebundles: allow manifest to specify sha256 digest of bundles
r52875 got = super().readinto(dest)
Joerg Sonnenberger
clonebundle-digest: add recursion guards for Python 3.8...
r52885 self._digest_recursion_level -= 1
if self._digest_recursion_level == 0:
self._digest_input(dest[:got])
Joerg Sonnenberger
clonebundles: allow manifest to specify sha256 digest of bundles
r52875 return got
def _close_conn(self):
self._digest_finished = True
return super().close()
response.__class__ = digestresponse
response._digest = self._digest
response._digest_consumed = 0
response._hasher = self._hasher.copy()
Joerg Sonnenberger
clonebundle-digest: add recursion guards for Python 3.8...
r52885 # Python 3.8 / 3.9 recurses internally between read/readinto.
response._digest_recursion_level = 0
Joerg Sonnenberger
clonebundles: allow manifest to specify sha256 digest of bundles
r52875 response._digest_finished = False
return response
https_response = http_response
Henrik Stuart
url: add support for custom handlers in extensions
r9347 handlerfuncs = []
Augie Fackler
formatting: blacken the codebase...
r43346
def opener(
ui,
authinfo=None,
useragent=None,
loggingfh=None,
loggingname=b's',
loggingopts=None,
sendaccept=True,
Joerg Sonnenberger
clonebundles: allow manifest to specify sha256 digest of bundles
r52875 digest=None,
Augie Fackler
formatting: blacken the codebase...
r43346 ):
Augie Fackler
formating: upgrade to black 20.8b1...
r46554 """
Benoit Boissinot
factor out the url handling from httprepo...
r7270 construct an opener suitable for urllib2
authinfo will be added to the password manager
Gregory Szorc
url: add HTTP handler that uses a proxied socket...
r37029
The opener can be configured to log socket events if the various
``logging*`` arguments are specified.
``loggingfh`` denotes a file object to log events to.
``loggingname`` denotes the name of the to print when logging.
``loggingopts`` is a dict of keyword arguments to pass to the constructed
``util.socketobserver`` instance.
Gregory Szorc
url: support suppressing Accept header...
r37063
``sendaccept`` allows controlling whether the ``Accept`` request header
is sent. The header is sent by default.
Augie Fackler
formating: upgrade to black 20.8b1...
r46554 """
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 timeout = ui.configwith(float, b'http', b'timeout')
Gregory Szorc
url: add HTTP handler that uses a proxied socket...
r37029 handlers = []
if loggingfh:
Augie Fackler
formatting: blacken the codebase...
r43346 handlers.append(
logginghttphandler(
loggingfh, loggingname, loggingopts or {}, timeout=timeout
)
)
Gregory Szorc
url: add HTTP handler that uses a proxied socket...
r37029 else:
Cédric Krier
url: allow to configure timeout on http connection...
r40079 handlers.append(httphandler(timeout=timeout))
Joerg Sonnenberger
debugwireproto: redo logging to also work for https
r52721 if has_https:
# pytype get confused about the conditional existence for httpshandler here.
handlers.append(
httpshandler(ui, timeout=timeout) # pytype: disable=name-error
)
Benoit Boissinot
factor out the url handling from httprepo...
r7270
handlers.append(proxyhandler(ui))
liscju
url: remember http password database in ui object...
r29378 passmgr = passwordmgr(ui, ui.httppasswordmgrdb)
Benoit Boissinot
factor out the url handling from httprepo...
r7270 if authinfo is not None:
liscju
largefiles: make cloning not ask two times about password (issue4883)...
r29379 realm, uris, user, passwd = authinfo
saveduser, savedpass = passmgr.find_stored_password(uris[0])
if user != saveduser or passwd:
passmgr.add_password(realm, uris, user, passwd)
Augie Fackler
formatting: blacken the codebase...
r43346 ui.debug(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'http auth: user %s, password %s\n'
% (user, passwd and b'*' * len(passwd) or b'not set')
Augie Fackler
formatting: blacken the codebase...
r43346 )
Benoit Boissinot
factor out the url handling from httprepo...
r7270
Augie Fackler
formatting: blacken the codebase...
r43346 handlers.extend(
(httpbasicauthhandler(passmgr), httpdigestauthhandler(passmgr))
)
Henrik Stuart
url: add support for custom handlers in extensions
r9347 handlers.extend([h(ui, passmgr) for h in handlerfuncs])
Joerg Sonnenberger
http: use urllib's cookie handler...
r52723 handlers.append(urlreq.httpcookieprocessor(cookiejar=load_cookiejar(ui)))
Joerg Sonnenberger
urllib2: redo response.readlines addition via class patching
r52720 handlers.append(readlinehandler())
Joerg Sonnenberger
clonebundles: allow manifest to specify sha256 digest of bundles
r52875 if digest:
handlers.append(digesthandler(digest))
timeless
pycompat: switch to util.urlreq/util.urlerr for py3 compat
r28883 opener = urlreq.buildopener(*handlers)
Benoit Boissinot
factor out the url handling from httprepo...
r7270
Gregory Szorc
httppeer: report http statistics...
r40070 # keepalive.py's handlers will populate these attributes if they exist.
opener.requestscount = 0
opener.sentbytescount = 0
opener.receivedbytescount = 0
Gregory Szorc
url: add distribution and version to user-agent request header (BC)...
r29589 # The user agent should should *NOT* be used by servers for e.g.
# protocol detection or feature negotiation: there are other
# facilities for that.
#
# "mercurial/proto-1.0" was the original user agent string and
# exists for backwards compatibility reasons.
#
# The "(Mercurial %s)" string contains the distribution
# name and version. Other client implementations should choose their
# own distribution name. Since servers should not be using the user
# agent string for anything, clients should be able to define whatever
# user agent they deem appropriate.
Matt Harbison
lfs: add git to the User-Agent header for blob transfers...
r35455 #
# The custom user agent is for lfs, because unfortunately some servers
# do look at this value.
if not useragent:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 agent = b'mercurial/proto-1.0 (Mercurial %s)' % util.version()
Augie Fackler
cleanup: remove pointless r-prefixes on single-quoted strings...
r43906 opener.addheaders = [('User-agent', pycompat.sysstr(agent))]
Matt Harbison
lfs: add git to the User-Agent header for blob transfers...
r35455 else:
Augie Fackler
cleanup: remove pointless r-prefixes on single-quoted strings...
r43906 opener.addheaders = [('User-agent', pycompat.sysstr(useragent))]
Gregory Szorc
httppeer: advertise and support application/mercurial-0.2...
r30763
# This header should only be needed by wire protocol requests. But it has
# been sent on all requests since forever. We keep sending it for backwards
# compatibility reasons. Modern versions of the wire protocol use
# X-HgProto-<N> for advertising client support.
Gregory Szorc
url: support suppressing Accept header...
r37063 if sendaccept:
Augie Fackler
cleanup: remove pointless r-prefixes on single-quoted strings...
r43906 opener.addheaders.append(('Accept', 'application/mercurial-0.1'))
Gregory Szorc
url: support suppressing Accept header...
r37063
Benoit Boissinot
factor out the url handling from httprepo...
r7270 return opener
Augie Fackler
formatting: blacken the codebase...
r43346
Joerg Sonnenberger
clonebundles: allow manifest to specify sha256 digest of bundles
r52875 def open(ui, url_, data=None, sendaccept=True, digest=None):
urlutil: extract `url` related code from `util` into the new module...
r47669 u = urlutil.url(url_)
Brodie Rao
url: use url.url in url.open()
r13818 if u.scheme:
u.scheme = u.scheme.lower()
url_, authinfo = u.authinfo()
else:
windows: use abspath in url...
r48430 path = util.normpath(util.abspath(url_))
Gregory Szorc
url: pass str to pathname2url...
r45131 url_ = b'file://' + pycompat.bytesurl(
urlreq.pathname2url(pycompat.fsdecode(path))
)
Patrick Mezard
url: fix file:// URL handling
r7284 authinfo = None
Joerg Sonnenberger
clonebundles: allow manifest to specify sha256 digest of bundles
r52875 return opener(ui, authinfo, sendaccept=sendaccept, digest=digest).open(
Augie Fackler
formatting: blacken the codebase...
r43346 pycompat.strurl(url_), data
)
Gregory Szorc
url: move _wraphttpresponse() from httpeer...
r40054
def wrapresponse(resp):
"""Wrap a response object with common error handlers.
This ensures that any I/O from any consumer raises the appropriate
error and messaging.
"""
origread = resp.read
class readerproxy(resp.__class__):
def read(self, size=None):
try:
return origread(size)
except httplib.IncompleteRead as e:
# e.expected is an integer if length known or None otherwise.
if e.expected:
got = len(e.partial)
total = e.expected + got
Augie Fackler
formatting: blacken the codebase...
r43346 msg = _(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'HTTP request error (incomplete response; '
b'expected %d bytes got %d)'
Augie Fackler
formatting: blacken the codebase...
r43346 ) % (total, got)
Gregory Szorc
url: move _wraphttpresponse() from httpeer...
r40054 else:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 msg = _(b'HTTP request error (incomplete response)')
Gregory Szorc
url: move _wraphttpresponse() from httpeer...
r40054
raise error.PeerTransportError(
msg,
Augie Fackler
formatting: blacken the codebase...
r43346 hint=_(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'this may be an intermittent network failure; '
b'if the error persists, consider contacting the '
b'network or server operator'
Augie Fackler
formatting: blacken the codebase...
r43346 ),
)
Gregory Szorc
url: move _wraphttpresponse() from httpeer...
r40054 except httplib.HTTPException as e:
raise error.PeerTransportError(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _(b'HTTP request error (%s)') % e,
Augie Fackler
formatting: blacken the codebase...
r43346 hint=_(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'this may be an intermittent network failure; '
b'if the error persists, consider contacting the '
b'network or server operator'
Augie Fackler
formatting: blacken the codebase...
r43346 ),
)
Gregory Szorc
url: move _wraphttpresponse() from httpeer...
r40054
resp.__class__ = readerproxy