fix(proxy): set Content-Length: 0 on empty-body replacement

build_raw_request only auto-adds Content-Length for a non-empty body, so replacing the body with an empty string produced a request with no Content-Length. Set it to 0 explicitly in apply_modifications (scoped to the body-replacement path, so body-less requests like GET are unaffected). Adds a test.
This commit is contained in:
Osamaali313 2026-06-29 22:19:13 +03:00
parent 587dc8b275
commit cd415e7d4d
2 changed files with 20 additions and 0 deletions

View file

@ -269,6 +269,10 @@ def apply_modifications(
if "Content-Length" not in explicit_cl:
for key in [k for k in headers if k.title() == "Content-Length"]:
del headers[key]
# build_raw_request only auto-adds Content-Length for a non-empty
# body, so an empty replacement body needs it set explicitly.
if body == "":
headers["Content-Length"] = "0"
if "cookies" in modifications:
cookies: dict[str, str] = {}
if headers.get("Cookie"):

View file

@ -57,3 +57,19 @@ def test_no_body_modification_keeps_content_length() -> None:
}
result = apply_modifications(components, {"headers": {"X-Test": "1"}}, "http://x.test/")
assert result["headers"].get("Content-Length") == "3"
def test_empty_body_replacement_sets_content_length_zero() -> None:
components = {
"method": "POST",
"headers": {"Host": "x.test", "Content-Length": "3"},
"body": "old",
}
result = apply_modifications(components, {"body": ""}, "http://x.test/")
_conn, raw = build_raw_request(
method=result["method"],
url=result["url"],
headers=result["headers"],
body=result["body"],
)
assert _content_length(raw) == "0"