|
2 | 2 |
|
3 | 3 | import aiohttp |
4 | 4 | import backoff |
| 5 | +from urllib.parse import urlparse, urlunparse |
5 | 6 |
|
6 | 7 | from .base import BaseDownloader, DownloadResult |
7 | 8 |
|
@@ -149,6 +150,19 @@ def __init__( |
149 | 150 | self.proxy = proxy |
150 | 151 | self.proxy_auth = proxy_auth |
151 | 152 | self.headers_ready_callback = headers_ready_callback |
| 153 | + |
| 154 | + # Workaround to prevent credentials in the proxy url |
| 155 | + if self.proxy: |
| 156 | + parsed_url = urlparse(self.proxy) |
| 157 | + netloc = parsed_url.netloc |
| 158 | + if "@" in netloc: |
| 159 | + if self.proxy_auth is not None: |
| 160 | + raise RuntimeError("Proxy credentials were specified in two places.") |
| 161 | + auth, url = netloc.rsplit("@", maxsplit=1) |
| 162 | + proxy_username, proxy_password = auth.split(":", maxsplit=1) |
| 163 | + self.proxy = urlunparse(parsed_url._replace(netloc=url)) |
| 164 | + self.proxy_auth = aiohttp.BasicAuth(login=proxy_username, password=proxy_password) |
| 165 | + |
152 | 166 | super().__init__(url, **kwargs) |
153 | 167 |
|
154 | 168 | def raise_for_status(self, response): |
@@ -206,7 +220,9 @@ async def _run(self, extra_data=None): |
206 | 220 | Args: |
207 | 221 | extra_data (dict): Extra data passed by the downloader. |
208 | 222 | """ |
209 | | - async with self.session.get(self.url, proxy=self.proxy, auth=self.auth) as response: |
| 223 | + async with self.session.get( |
| 224 | + self.url, auth=self.auth, proxy=self.proxy, proxy_auth=self.proxy_auth |
| 225 | + ) as response: |
210 | 226 | self.raise_for_status(response) |
211 | 227 | to_return = await self._handle_response(response) |
212 | 228 | await response.release() |
|
0 commit comments