diff --git a/src/mitm/proxy.rs b/src/mitm/proxy.rs index a2f3b80..796d8b9 100644 --- a/src/mitm/proxy.rs +++ b/src/mitm/proxy.rs @@ -578,9 +578,13 @@ async fn handle_http_over_tls( }; if let Some(modified_body) = super::modify::modify_request(&raw_body, tool_ctx.as_ref()) { - // Rebuild request_buf: original headers + rechunked modified body + // Rebuild request_buf: headers (with updated Content-Length) + rechunked modified body let new_chunked = super::modify::rechunk(&modified_body); - let mut new_buf = request_buf[..headers_end].to_vec(); + + // Fix Content-Length header to match new body size + let header_str = String::from_utf8_lossy(&request_buf[..headers_end]); + let updated_headers = update_content_length(&header_str, new_chunked.len()); + let mut new_buf = updated_headers.into_bytes(); new_buf.extend_from_slice(&new_chunked); request_buf = new_buf; } @@ -1030,3 +1034,15 @@ fn parse_http_request_meta(buf: &[u8]) -> (usize, usize, bool) { (headers_end, content_length, is_streaming) } +/// Rewrite the Content-Length header in raw HTTP headers to match a new body size. +/// If no Content-Length header is found, returns the headers unchanged. +fn update_content_length(headers: &str, new_body_len: usize) -> String { + use regex::Regex; + let re = Regex::new(r"(?im)^content-length:\s*\d+").unwrap(); + if re.is_match(headers) { + re.replace(headers, format!("Content-Length: {new_body_len}")) + .to_string() + } else { + headers.to_string() + } +}