|
6 | 6 | import argparse |
7 | 7 | import ipaddress |
8 | 8 | import json |
| 9 | +import math |
9 | 10 | import socket |
10 | 11 | import sys |
11 | 12 | import threading |
@@ -321,38 +322,39 @@ def check_one(self, link: CatalogLink) -> LinkResult: |
321 | 322 | self.guard.resolve_url(link.url) |
322 | 323 | session = self.sessions.get() |
323 | 324 | head = self._request(session, "HEAD", link.url) |
324 | | - if head.status_code < 400: |
325 | | - history = _history(head) |
326 | | - result = LinkResult( |
| 325 | + try: |
| 326 | + if head.status_code < 400: |
| 327 | + history = _history(head) |
| 328 | + return LinkResult( |
| 329 | + link.id, |
| 330 | + link.path, |
| 331 | + link.title, |
| 332 | + link.url, |
| 333 | + classify_status(head.status_code, redirected=bool(history)), |
| 334 | + status_code=head.status_code, |
| 335 | + method="HEAD", |
| 336 | + final_url=head.url, |
| 337 | + history=history, |
| 338 | + ) |
| 339 | + finally: |
| 340 | + head.close() |
| 341 | + |
| 342 | + response = self._request(session, "GET", link.url) |
| 343 | + try: |
| 344 | + history = _history(response) |
| 345 | + return LinkResult( |
327 | 346 | link.id, |
328 | 347 | link.path, |
329 | 348 | link.title, |
330 | 349 | link.url, |
331 | | - classify_status(head.status_code, redirected=bool(history)), |
332 | | - status_code=head.status_code, |
333 | | - method="HEAD", |
334 | | - final_url=head.url, |
| 350 | + classify_status(response.status_code, redirected=bool(history)), |
| 351 | + status_code=response.status_code, |
| 352 | + method="GET", |
| 353 | + final_url=response.url, |
335 | 354 | history=history, |
336 | 355 | ) |
337 | | - head.close() |
338 | | - return result |
339 | | - head.close() |
340 | | - |
341 | | - response = self._request(session, "GET", link.url) |
342 | | - history = _history(response) |
343 | | - result = LinkResult( |
344 | | - link.id, |
345 | | - link.path, |
346 | | - link.title, |
347 | | - link.url, |
348 | | - classify_status(response.status_code, redirected=bool(history)), |
349 | | - status_code=response.status_code, |
350 | | - method="GET", |
351 | | - final_url=response.url, |
352 | | - history=history, |
353 | | - ) |
354 | | - response.close() |
355 | | - return result |
| 356 | + finally: |
| 357 | + response.close() |
356 | 358 | except UnsafeTarget as exc: |
357 | 359 | return LinkResult( |
358 | 360 | link.id, link.path, link.title, link.url, "blocked", error=str(exc) |
@@ -451,15 +453,15 @@ def exit_code_for_report(report: Mapping[str, Any]) -> int: |
451 | 453 |
|
452 | 454 | def _positive_float(value: str) -> float: |
453 | 455 | parsed = float(value) |
454 | | - if parsed <= 0: |
455 | | - raise argparse.ArgumentTypeError("must be greater than zero") |
| 456 | + if not math.isfinite(parsed) or parsed <= 0: |
| 457 | + raise argparse.ArgumentTypeError("must be a finite number greater than zero") |
456 | 458 | return parsed |
457 | 459 |
|
458 | 460 |
|
459 | 461 | def _non_negative_float(value: str) -> float: |
460 | 462 | parsed = float(value) |
461 | | - if parsed < 0: |
462 | | - raise argparse.ArgumentTypeError("must be zero or greater") |
| 463 | + if not math.isfinite(parsed) or parsed < 0: |
| 464 | + raise argparse.ArgumentTypeError("must be a finite number zero or greater") |
463 | 465 | return parsed |
464 | 466 |
|
465 | 467 |
|
|
0 commit comments