Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(fetch): send zero content length for bodyless PUT requests

ldm0a4fd2b970116
diff --git a/moli-fetch/src/blocking/mod.rs b/moli-fetch/src/blocking/mod.rs
index 206d58b87..848bc6d32 100644
--- a/moli-fetch/src/blocking/mod.rs
+++ b/moli-fetch/src/blocking/mod.rs
@@ -627,8 +627,9 @@ pub(crate) fn configure_easy<H: Handler>(
method => {
easy.custom_request(method)
.with_context(|| anyhow!("failed to configure {method} request"))?;
- if let Some(ref body) = request.body {
- easy.post_fields_copy(body)
+ // Fetch requires Content-Length: 0 for a bodyless PUT, just as for POST.
+ if request.body.is_some() || method == "PUT" {
+ easy.post_fields_copy(request.body.as_deref().unwrap_or(&[]))
.context("failed to set custom request body")?;
}
}
@@ -685,13 +686,16 @@ pub(crate) fn configure_easy<H: Handler>(
has_headers = true;
}
}
- if request.method.eq_ignore_ascii_case("POST") && !has_content_type_header {
+ if (request.method.eq_ignore_ascii_case("POST")
+ || (request.method == "PUT" && request.body.is_none()))
+ && !has_content_type_header
+ {
// libcurl otherwise synthesizes `Content-Type: application/x-www-form-urlencoded`
- // for POST bodies. Browser fetch/sendBeacon only send Content-Type when
+ // for POST bodies and bodyless PUT requests. Browser requests only send Content-Type when
// BodyInit or caller headers produce one, so suppress curl's transport default.
headers
.append("Content-Type:")
- .context("failed to suppress curl default POST content-type")?;
+ .context("failed to suppress curl default content-type")?;
has_headers = true;
}
diff --git a/moli-fetch/src/tests/mod.rs b/moli-fetch/src/tests/mod.rs
index 43595438f..471b84541 100644
--- a/moli-fetch/src/tests/mod.rs
+++ b/moli-fetch/src/tests/mod.rs
@@ -1511,6 +1511,48 @@ async fn fetch_raw_stream_finishes_null_body_status_without_connection_close() -
Ok(())
}
+#[tokio::test]
+async fn fetch_redirects_recompute_bodyless_put_content_length() -> Result<()> {
+ for status in [301, 302, 303, 307, 308] {
+ let server = ScriptedHttpServer::spawn(vec![
+ ScriptedResponse::status(status, "Redirect").with_header("Location", "/final"),
+ ScriptedResponse::ok("done"),
+ ]);
+ let client = FetchClient::new(&FetchConfig::default(), new_shared_browser_cookie_store());
+ let response = client
+ .fetch_raw(Request::new(
+ "PUT",
+ &server.url_path("/start"),
+ None,
+ vec![],
+ )?)
+ .await?;
+ assert_eq!(response.status, 200);
+ assert!(response.redirected);
+ let requests = server.requests();
+ server.shutdown();
+ assert_eq!(requests.len(), 2, "{status}: {requests:?}");
+ let final_method = if status == 303 { "GET" } else { "PUT" };
+ for (request, method, path) in [
+ (&requests[0], "PUT", "/start"),
+ (&requests[1], final_method, "/final"),
+ ] {
+ assert!(request.starts_with(&format!("{method} {path} HTTP/1.1\r\n")));
+ assert_eq!(
+ request_head_header_value(request, "content-length"),
+ if method == "PUT" { Some("0") } else { None },
+ "{status}: {request}"
+ );
+ assert_eq!(request_head_header_value(request, "content-type"), None);
+ assert_eq!(
+ request_head_header_value(request, "transfer-encoding"),
+ None
+ );
+ }
+ }
+ Ok(())
+}
+
#[tokio::test]
async fn fetch_redirect_303_rewrites_post_to_get_and_drops_body_headers() -> Result<()> {
let server = ScriptedHttpServer::spawn(vec![