# HG changeset patch # User Pierre-Yves David # Date 1693578905 -7200 # Node ID 13eab1a5db7827a59239e771249605367eb311e7 # Parent 9bffc6c4e4c57743a2c10a19fb06972adc7dbaf2 url: move the _generic_proxytunnel in the `has_https` block It is only used when https exists. If you stay out of the block, pytype complains that the type of its argument are not declared. diff -r 9bffc6c4e4c5 -r 13eab1a5db78 mercurial/url.py --- a/mercurial/url.py Thu Dec 08 15:57:42 2022 +0100 +++ b/mercurial/url.py Fri Sep 01 16:35:05 2023 +0200 @@ -222,47 +222,6 @@ h.headers = None -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()) - ) - - class httphandler(keepalive.HTTPHandler): def http_open(self, req): return self.do_open(httpconnection, req) @@ -306,6 +265,46 @@ if has_https: + 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()) + ) + class httpsconnection(keepalive.HTTPConnection): response_class = keepalive.HTTPResponse default_port = httplib.HTTPS_PORT