Commit 65e617d1 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Implement https:// support for redirect()

parent 94805fac
Loading
Loading
Loading
Loading
+13 −6
Original line number Diff line number Diff line
@@ -31,9 +31,7 @@ def redirect(session: requests.Session, prefix: str, address: ipaddress.IPv4Addr
	"prefix" is formated as either "{hostname}[:{port}]" or "{schema}://{hostname}[:{port}]"
	where "schema" defaults to (and currently only supports) "http".
	"""
	if prefix.startswith("https://"):
		raise ValueError("https:// prefixes not currently supported")
	if not prefix.startswith("http://"):
	if not prefix.startswith("http://") or prefix.startswith("https://"):
		prefix = f"http://{prefix}"
	session.mount(prefix, _DirectedAdapter(address))

@@ -53,8 +51,11 @@ class _DirectedAdapter(requests.adapters.HTTPAdapter):
		super().__init__()
		self.destination = destination

	def get_connection(self, url: str, proxies: Mapping[str, str]|None = None) -> _HTTPConnectionPool:
	def get_connection(self, url: str, proxies: Mapping[str, str]|None = None) -> connectionpool.HTTPConnectionPool:
		parts = urlparse(url)
		if parts.scheme == "https":
			return _HTTPSConnectionPool(parts.hostname, parts.port, address=self.destination)
		else:
			return _HTTPConnectionPool(parts.hostname, parts.port, address=self.destination)


@@ -66,5 +67,11 @@ class _HTTPConnectionPool(connectionpool.HTTPConnectionPool):
		host = ""

		def __init__(self, /, address: ipaddress.IPv4Address, **kwargs: Any):
			connection.HTTPConnection.__init__(self, **kwargs)
			super().__init__(**kwargs)
			self._dns_host = str(address)


class _HTTPSConnectionPool(connectionpool.HTTPSConnectionPool):

	class ConnectionCls(connection.HTTPSConnection, _HTTPConnectionPool.ConnectionCls):
		...