Parse ``Content-Length`` and surface malformed values as PluginAPIError. ``int(response.headers.get("Content-Length", 0))`` raises a raw ``TypeError``/``ValueError`` on a malformed header — those don't match the ``except requests.RequestException`` arms in the download methods and l
(response: "requests.Response", *, what: str)
| 45 | |
| 46 | |
| 47 | def _parse_content_length(response: "requests.Response", *, what: str) -> int: |
| 48 | """Parse ``Content-Length`` and surface malformed values as PluginAPIError. |
| 49 | |
| 50 | ``int(response.headers.get("Content-Length", 0))`` raises a raw |
| 51 | ``TypeError``/``ValueError`` on a malformed header — those don't match |
| 52 | the ``except requests.RequestException`` arms in the download methods |
| 53 | and leak past the typed-error contract that callers depend on. |
| 54 | """ |
| 55 | # Missing header defaults to "0" and passes the size cap silently, |
| 56 | # because the streaming reader (``_iter_with_size_cap``) enforces the |
| 57 | # real bound while bytes flow in. Do not "harden" this default to |
| 58 | # raise — well-behaved upstreams that omit the header on small |
| 59 | # responses would then fail for no reason. |
| 60 | raw = response.headers.get("Content-Length", "0") |
| 61 | try: |
| 62 | return int(raw) |
| 63 | except (TypeError, ValueError) as exc: |
| 64 | raise PluginAPIError( |
| 65 | f"Malformed Content-Length header for {what}: {raw!r}" |
| 66 | ) from exc |
| 67 | |
| 68 | |
| 69 | _DEFAULT_PORTS = {"http": 80, "https": 443} |
no test coverage detected