Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
shelld3v committed Nov 18, 2024
1 parent 9137c86 commit 365e4ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
42 changes: 19 additions & 23 deletions lib/connection/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,6 @@ def set_url(self, url: str) -> None:
def set_header(self, key: str, value: str) -> None:
self.headers[key] = value.lstrip()

def set_proxy(self, proxy: str) -> None:
if not proxy:
return

if not proxy.startswith(PROXY_SCHEMES):
proxy = f"http://{proxy}"

if self.proxy_cred and "@" not in proxy:
# socks5://localhost:9050 => socks5://[credential]@localhost:9050
proxy = proxy.replace("://", f"://{self.proxy_cred}@", 1)

self.session.proxies = {"https": proxy}
if not proxy.startswith("https://"):
self.session.proxies["http"] = proxy

def is_rate_exceeded(self) -> bool:
return self._rate >= options["max_rate"] > 0

Expand Down Expand Up @@ -193,9 +178,19 @@ def request(self, path: str, proxy: str | None = None) -> Response:
# Why using a loop instead of max_retries argument? Check issue #1009
for _ in range(options["max_retries"] + 1):
try:
proxies = {}
try:
proxy = proxy or random.choice(options["proxies"])
self.set_proxy(proxy)
proxy_url = proxy or random.choice(options["proxies"])
if not proxy_url.startswith(PROXY_SCHEMES):
proxy_url = f"http://{proxy_url}"

if self.proxy_cred and "@" not in proxy_url:
# socks5://localhost:9050 => socks5://[credential]@localhost:9050
proxy_url = proxy_url.replace("://", f"://{self.proxy_cred}@", 1)

proxies["https"] = proxy_url
if not proxy_url.startswith("https://"):
proxies["http"] = proxy_url
except IndexError:
pass

Expand All @@ -210,13 +205,14 @@ def request(self, path: str, proxy: str | None = None) -> Response:
headers=self.headers,
data=options["data"],
)
prepped = self.session.prepare_request(request)
prepped.url = url
prep = self.session.prepare_request(request)
prep.url = url

origin_response = self.session.send(
prepped,
prep,
allow_redirects=options["follow_redirects"],
timeout=options["timeout"],
proxies=proxies,
stream=True,
)
response = Response(url, origin_response)
Expand Down Expand Up @@ -357,11 +353,11 @@ async def replay_request(self, path: str, proxy: str) -> AsyncResponse:
mounts={"all://": transport},
timeout=httpx.Timeout(options["timeout"]),
)
return await self.request(path, self.replay_session)
return await self.request(path, self.replay_session, replay=True)

# :path: is expected not to start with "/"
async def request(
self, path: str, session: httpx.AsyncClient | None = None
self, path: str, session: httpx.AsyncClient | None = None, replay: bool = False
) -> AsyncResponse:
while self.is_rate_exceeded():
await asyncio.sleep(0.1)
Expand All @@ -383,7 +379,7 @@ async def request(
url,
headers=self.headers,
data=options["data"],
extensions={"target": f"/{safequote(path)}".encode()},
extensions={"target": (url if replay else f"/{safequote(path)}").encode()},
)

xresponse = await session.send(
Expand Down
4 changes: 2 additions & 2 deletions lib/connection/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def __eq__(self, other: Any) -> bool:


class Response(BaseResponse):
def __init__(self, response: requests.Response) -> None:
super().__init__(response)
def __init__(self, url, response: requests.Response) -> None:
super().__init__(url, response)

for chunk in response.iter_content(chunk_size=ITER_CHUNK_SIZE):
self.body += chunk
Expand Down

0 comments on commit 365e4ba

Please sign in to comment.