MCPcopy
hub / github.com/GitGuardian/ggshield / _parse_content_length

Function _parse_content_length

ggshield/core/plugin/client.py:47–66  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

45
46
47def _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}

Callers 2

download_pluginMethod · 0.85

Calls 2

PluginAPIErrorClass · 0.85
getMethod · 0.80

Tested by

no test coverage detected