Handle large proxy status responses

This commit is contained in:
Shujakuin
2026-07-20 14:02:08 +08:00
parent 1bc4120531
commit 8094aa7233
4 changed files with 29 additions and 4 deletions
+10 -1
View File
@@ -22,6 +22,7 @@ DEFAULT_TIMEOUT_SECONDS = 10
DEFAULT_TEST_URL = "https://www.gstatic.com/generate_204"
DEFAULT_TEST_TIMEOUT_MS = 5000
DEFAULT_DELAY_CONCURRENCY = 4
MAX_JSON_RESPONSE_BYTES = 8 * 1024 * 1024
class ApiError(RuntimeError):
@@ -154,7 +155,13 @@ class ProxyMonitorClient:
)
try:
with urllib.request.urlopen(request, timeout=self.timeout_seconds) as response:
raw = response.read(1_048_576)
try:
content_length = int(response.headers.get("Content-Length", "0"))
except ValueError:
content_length = 0
if content_length > MAX_JSON_RESPONSE_BYTES:
raise ApiError("response exceeds 8 MiB limit")
raw = response.read(MAX_JSON_RESPONSE_BYTES + 1)
except urllib.error.HTTPError as exc:
try:
detail = exc.read(1024).decode("utf-8", errors="replace").strip()
@@ -169,6 +176,8 @@ class ProxyMonitorClient:
if not expect_json:
return None
if len(raw) > MAX_JSON_RESPONSE_BYTES:
raise ApiError("response exceeds 8 MiB limit")
if not raw:
raise ApiError("response did not contain JSON")
try: