Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(links): 外链圆标取图遇到不合法的重定向地址按取不到处理

longmeidaoe243b6ce1867
diff --git a/src/peach/routes_media.py b/src/peach/routes_media.py
index 351932c54..c8a1d3b00 100644
--- a/src/peach/routes_media.py
+++ b/src/peach/routes_media.py
@@ -1377,7 +1377,9 @@ def _site_mark_response(state, url: str, root: Path):
upstream = state.http_transport.client.get(
locate(target), headers={"User-Agent": USER_AGENT},
timeout=8, follow_redirects=True)
- except (OSError, httpx.HTTPError):
+ except (OSError, ValueError, httpx.HTTPError, httpx.InvalidURL):
+ # 重定向到主机名过不了 IDNA 的地址时传输层抛 ValueError,地址本身不合法时
+ # 抛 InvalidURL;两者都不是 HTTPError,同样算取不到。
return None
if upstream.status_code != 200 or not upstream.content:
return None
diff --git a/tests/test_fastapi_api.py b/tests/test_fastapi_api.py
index 081e1f8a0..c55df453b 100644
--- a/tests/test_fastapi_api.py
+++ b/tests/test_fastapi_api.py
@@ -1345,6 +1345,30 @@ class FastApiContractTests(unittest.IsolatedAsyncioTestCase):
self.assertGreater(len(asked), reached)
self.assertFalse(marker.exists())
+ async def test_a_site_redirecting_to_an_unusable_address_counts_as_unreachable(self):
+ """重定向目标的主机名过不了 IDNA 时传输层抛 ValueError,地址本身不合法时抛 InvalidURL。
+
+ 两者都不是 `httpx.HTTPError`,都按取不到处理:回 404 露出地球图标,失败窗口内不再出网。
+ """
+ self._archive_links(lambda url: "red")
+ for failure in (ValueError("encoding with 'idna' codec failed (UnicodeError: label empty or too long)"),
+ httpx.InvalidURL("Invalid port: 'abc'")):
+ with self.subTest(failure=type(failure).__name__):
+ for marker in (self.root / "generated" / "link-marks").glob("*.failed"):
+ marker.unlink()
+ asked = []
+
+ def broken(request, failure=failure):
+ asked.append(str(request.url))
+ raise failure
+ self._swap_http_client(broken)
+ answer = await self.client.get("/link-mark?t=secret&id=901")
+ self.assertEqual(answer.status_code, 404)
+ reached = len(asked)
+ self.assertTrue(reached)
+ self.assertEqual((await self.client.get("/link-mark?t=secret&id=901")).status_code, 404)
+ self.assertEqual(len(asked), reached, "失败窗口内不再出网")
+
async def test_unauthorized_keeps_three_shapes_grouped_by_route_class(self):
"""401 三种形态按路由类分组,收敛到 Depends 之后也不许并成一种。