From 438db1be3065b451ebb232e8b3308e69a9e28fa4 Mon Sep 17 00:00:00 2001
From: sterni <sternenseemann@systemli.org>
Date: Mon, 11 Mar 2024 17:17:13 +0100
Subject: [PATCH] fix(net/crimp): correctly set content length for PUT requests
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Since https://github.com/curl/curl/commit/9c845be2797e20475
(presumably), libcurl will overwrite our previously set request
method to POST if we set .post_field_size(…). The fix is to use
the proper option for PUT/upload, .in_filesize(…). While we're
at it, switch to using .upload(…) instead of the deprecated
.put(…) which should be the same for HTTP.

Change-Id: I393c1a02c70d5b99dff5901cd6e9d9434f68c15b
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11132
Tested-by: BuildkiteCI
Autosubmit: sterni <sternenseemann@systemli.org>
Reviewed-by: tazjin <tazjin@tvl.su>
---
 net/crimp/src/lib.rs | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/net/crimp/src/lib.rs b/net/crimp/src/lib.rs
index 4dd4d6c31..7dfd261ee 100644
--- a/net/crimp/src/lib.rs
+++ b/net/crimp/src/lib.rs
@@ -365,7 +365,7 @@ impl<'a> Request<'a> {
         match self.method {
             Method::Get => self.handle.get(true)?,
             Method::Post => self.handle.post(true)?,
-            Method::Put => self.handle.put(true)?,
+            Method::Put => self.handle.upload(true)?,
             Method::Patch => self.handle.custom_request("PATCH")?,
             Method::Delete => self.handle.custom_request("DELETE")?,
         }
@@ -386,14 +386,23 @@ impl<'a> Request<'a> {
         // and configure the expected body size (or form payload).
         match self.body {
             Body::Bytes { content_type, data } => {
-                self.handle.post_field_size(data.len() as u64)?;
+                match self.method {
+                    Method::Put => self.handle.in_filesize(data.len() as u64)?,
+                    // TODO(sterni): this may still be wrong for some request types?
+                    _ => self.handle.post_field_size(data.len() as u64)?,
+                };
+
                 self.headers
                     .append(&format!("Content-Type: {}", content_type))?;
             }
 
             #[cfg(feature = "json")]
             Body::Json(ref data) => {
-                self.handle.post_field_size(data.len() as u64)?;
+                match self.method {
+                    Method::Put => self.handle.in_filesize(data.len() as u64)?,
+                    // TODO(sterni): this may still be wrong for some request types?
+                    _ => self.handle.post_field_size(data.len() as u64)?,
+                };
                 self.headers.append("Content-Type: application/json")?;
             }