Skip to content

Commit 9c0a437

Browse files
authored
Merge pull request #38 from simple-repository/feature/follow-metadata-redirects
Follow redirects when retrieving metadata
2 parents b0e6174 + 2408036 commit 9c0a437

3 files changed

Lines changed: 16 additions & 15 deletions

File tree

simple_repository_browser/_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def create_app(self) -> fastapi.FastAPI:
5353

5454
async def lifespan(app: fastapi.FastAPI):
5555
async with (
56-
httpx.AsyncClient(timeout=30) as http_client,
56+
httpx.AsyncClient(timeout=30, follow_redirects=True) as http_client,
5757
aiosqlite.connect(self.db_path, timeout=5) as db,
5858
):
5959
_controller = self.create_controller(

simple_repository_browser/crawler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ async def fetch_pkg_info(
174174
)
175175

176176
info_file, pkg_info = await package_info(
177-
releases[version].files, self._source, prj.name
177+
releases[version].files, self._source, prj.name, self._http_client
178178
)
179179

180180
self._cache[key] = info_file, releases[version].files, pkg_info

simple_repository_browser/fetch_description.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,16 @@ class PackageInfo:
104104
files_info: dict[str, FileInfo] = dataclasses.field(default_factory=dict)
105105

106106

107-
async def fetch_file(url, dest):
108-
async with httpx.AsyncClient(verify=False) as http_client:
109-
async with http_client.stream("GET", url) as r:
110-
try:
111-
r.raise_for_status()
112-
except httpx.HTTPError as err:
113-
raise IOError(f"Unable to fetch file (reason: {str(err)})")
114-
chunk_size = 1024 * 100
115-
with open(dest, "wb") as fd:
116-
async for chunk in r.aiter_bytes(chunk_size):
117-
fd.write(chunk)
107+
async def fetch_file(url, dest, http_client: httpx.AsyncClient):
108+
async with http_client.stream("GET", url) as r:
109+
try:
110+
r.raise_for_status()
111+
except httpx.HTTPError as err:
112+
raise IOError(f"Unable to fetch file (reason: {str(err)})")
113+
chunk_size = 1024 * 100
114+
with open(dest, "wb") as fd:
115+
async for chunk in r.aiter_bytes(chunk_size):
116+
fd.write(chunk)
118117

119118

120119
class PkgInfoFromFile(pkginfo.Distribution):
@@ -167,6 +166,7 @@ async def _fetch_metadata_resource(
167166
project_name: str,
168167
file: model.File,
169168
tmp_file_path: str,
169+
http_client: httpx.AsyncClient,
170170
) -> tuple[model.File, pkginfo.Distribution]:
171171
"""Fetch metadata resource and return updated file and package info."""
172172
if not file.dist_info_metadata:
@@ -193,7 +193,7 @@ async def _fetch_metadata_resource(
193193
with open(tmp_file_path, "wb") as tmp:
194194
tmp.write(resource.text.encode())
195195
elif isinstance(resource, model.HttpResource):
196-
await fetch_file(resource.url, tmp_file_path)
196+
await fetch_file(resource.url, tmp_file_path, http_client)
197197
else:
198198
raise ValueError(f"Unhandled resource type ({type(resource)})")
199199

@@ -255,6 +255,7 @@ async def package_info(
255255
release_files: tuple[model.File, ...],
256256
repository: SimpleRepository,
257257
project_name: str,
258+
http_client: httpx.AsyncClient,
258259
) -> tuple[model.File, PackageInfo]:
259260
files_info = _create_files_info_mapping(release_files)
260261
file = _select_best_file(release_files)
@@ -263,7 +264,7 @@ async def package_info(
263264
suffix=os.path.splitext(file.filename)[1],
264265
) as tmp:
265266
file, info = await _fetch_metadata_resource(
266-
repository, project_name, file, tmp.name
267+
repository, project_name, file, tmp.name, http_client
267268
)
268269

269270
description = generate_safe_description_html(info)

0 commit comments

Comments
 (0)