diff --git a/strix/tools/proxy/caido_api.py b/strix/tools/proxy/caido_api.py index 525be944..c56db672 100644 --- a/strix/tools/proxy/caido_api.py +++ b/strix/tools/proxy/caido_api.py @@ -157,7 +157,7 @@ def build_raw_request( final_headers = {**headers} final_headers.setdefault("Host", parsed.netloc) final_headers.setdefault("User-Agent", "strix") - if body and "Content-Length" not in {k.title() for k in final_headers}: + if body is not None and "Content-Length" not in {k.title() for k in final_headers}: final_headers["Content-Length"] = str(len(body.encode("utf-8"))) lines = [f"{method.upper()} {path} HTTP/1.1"] diff --git a/tests/test_caido_api.py b/tests/test_caido_api.py index e287f6de..ba577ea7 100644 --- a/tests/test_caido_api.py +++ b/tests/test_caido_api.py @@ -60,3 +60,20 @@ def test_build_raw_request_recomputes_content_length_after_replacement() -> None ) head = raw.decode("utf-8").split("\r\n\r\n", 1)[0] assert f"Content-Length: {len(new_body.encode('utf-8'))}" in head + + +def test_build_raw_request_sets_content_length_zero_for_empty_body() -> None: + components = _components(body="old") + modified = apply_modifications( + components, + {"body": ""}, + "http://example.com/submit", + ) + _, raw = build_raw_request( + method=modified["method"], + url=modified["url"], + headers=modified["headers"], + body=modified["body"], + ) + head = raw.decode("utf-8").split("\r\n\r\n", 1)[0] + assert "Content-Length: 0" in head