## Unreleased: mitmproxy next
+- fix: missing content-length header in curl export
+ ([#7810](https://github.com/mitmproxy/mitmproxy/pull/7810), @mheguy)
- fix: update log message with correct header name
([#7802](https://github.com/mitmproxy/mitmproxy/pull/7802), @kristof-mattei)
- Update deprecated `windows-2019` runner to `windows-2025`.
return request
-def pop_headers(request: http.Request) -> http.Request:
- # Remove some headers that are redundant for curl/httpie export
+def pop_headers(request: http.Request) -> None:
+ """Remove some headers that are redundant for curl/httpie export."""
request.headers.pop("content-length", None)
+
if request.headers.get("host", "") == request.host:
request.headers.pop("host")
if request.headers.get(":authority", "") == request.host:
request.headers.pop(":authority")
- return request
def cleanup_response(f: flow.Flow) -> http.Response:
def curl_command(f: flow.Flow) -> str:
request = cleanup_request(f)
- request = pop_headers(request)
+ pop_headers(request)
+
args = ["curl"]
server_addr = f.server_conn.peername[0] if f.server_conn.peername else None
args += ["-H", f"{k}: {v}"]
if request.method != "GET":
+ if not request.content:
+ # curl will not calculate content-length if there is no content
+ # some server/verb combinations require content-length headers
+ # (ex. nginx and POST)
+ args += ["-H", "content-length: 0"]
+
args += ["-X", request.method]
args.append(request.pretty_url)
def httpie_command(f: flow.Flow) -> str:
request = cleanup_request(f)
- request = pop_headers(request)
+ pop_headers(request)
# TODO: Once https://github.com/httpie/httpie/issues/414 is implemented, we
# should ensure we always connect to the IP address specified in the flow,
result = "curl -X POST http://address:22/path -d nobinarysupport"
assert export_curl(post_request) == result
+ def test_post_with_no_content_has_explicit_content_length_header(
+ self, export_curl, post_request
+ ):
+ post_request.request.content = None
+ result = "curl -H 'content-length: 0' -X POST http://address:22/path"
+ assert export_curl(post_request) == result
+
def test_fails_with_binary_data(self, export_curl, post_request):
# shlex.quote doesn't support a bytes object
# see https://github.com/python/cpython/pull/10871