comparison mercurial/url.py @ 50925:13eab1a5db78

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.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Fri, 01 Sep 2023 16:35:05 +0200
parents ecaf00089461
children bc9c9ed0659d
comparison
equal deleted inserted replaced
50924:9bffc6c4e4c5 50925:13eab1a5db78
220 220
221 h.realhostport = None 221 h.realhostport = None
222 h.headers = None 222 h.headers = None
223 223
224 224
225 def _generic_proxytunnel(self: "httpsconnection"):
226 headers = self.headers # pytype: disable=attribute-error
227 proxyheaders = {
228 pycompat.bytestr(x): pycompat.bytestr(headers[x])
229 for x in headers
230 if x.lower().startswith('proxy-')
231 }
232 realhostport = self.realhostport # pytype: disable=attribute-error
233 self.send(b'CONNECT %s HTTP/1.0\r\n' % realhostport)
234
235 for header in proxyheaders.items():
236 self.send(b'%s: %s\r\n' % header)
237 self.send(b'\r\n')
238
239 # majority of the following code is duplicated from
240 # httplib.HTTPConnection as there are no adequate places to
241 # override functions to provide the needed functionality.
242
243 # pytype: disable=attribute-error
244 res = self.response_class(self.sock, method=self._method)
245 # pytype: enable=attribute-error
246
247 while True:
248 # pytype: disable=attribute-error
249 version, status, reason = res._read_status()
250 # pytype: enable=attribute-error
251 if status != httplib.CONTINUE:
252 break
253 # skip lines that are all whitespace
254 list(iter(lambda: res.fp.readline().strip(), b''))
255
256 if status == 200:
257 # skip lines until we find a blank line
258 list(iter(res.fp.readline, b'\r\n'))
259 else:
260 self.close()
261 raise socket.error(
262 "Tunnel connection failed: %d %s" % (status, reason.strip())
263 )
264
265
266 class httphandler(keepalive.HTTPHandler): 225 class httphandler(keepalive.HTTPHandler):
267 def http_open(self, req): 226 def http_open(self, req):
268 return self.do_open(httpconnection, req) 227 return self.do_open(httpconnection, req)
269 228
270 def _start_transaction(self, h, req): 229 def _start_transaction(self, h, req):
303 262
304 return logginghttpconnection(createconnection, *args, **kwargs) 263 return logginghttpconnection(createconnection, *args, **kwargs)
305 264
306 265
307 if has_https: 266 if has_https:
267
268 def _generic_proxytunnel(self: "httpsconnection"):
269 headers = self.headers # pytype: disable=attribute-error
270 proxyheaders = {
271 pycompat.bytestr(x): pycompat.bytestr(headers[x])
272 for x in headers
273 if x.lower().startswith('proxy-')
274 }
275 realhostport = self.realhostport # pytype: disable=attribute-error
276 self.send(b'CONNECT %s HTTP/1.0\r\n' % realhostport)
277
278 for header in proxyheaders.items():
279 self.send(b'%s: %s\r\n' % header)
280 self.send(b'\r\n')
281
282 # majority of the following code is duplicated from
283 # httplib.HTTPConnection as there are no adequate places to
284 # override functions to provide the needed functionality.
285
286 # pytype: disable=attribute-error
287 res = self.response_class(self.sock, method=self._method)
288 # pytype: enable=attribute-error
289
290 while True:
291 # pytype: disable=attribute-error
292 version, status, reason = res._read_status()
293 # pytype: enable=attribute-error
294 if status != httplib.CONTINUE:
295 break
296 # skip lines that are all whitespace
297 list(iter(lambda: res.fp.readline().strip(), b''))
298
299 if status == 200:
300 # skip lines until we find a blank line
301 list(iter(res.fp.readline, b'\r\n'))
302 else:
303 self.close()
304 raise socket.error(
305 "Tunnel connection failed: %d %s" % (status, reason.strip())
306 )
308 307
309 class httpsconnection(keepalive.HTTPConnection): 308 class httpsconnection(keepalive.HTTPConnection):
310 response_class = keepalive.HTTPResponse 309 response_class = keepalive.HTTPResponse
311 default_port = httplib.HTTPS_PORT 310 default_port = httplib.HTTPS_PORT
312 # must be able to send big bundle as stream. 311 # must be able to send big bundle as stream.