fix(net/crimp): correctly set content length for PUT requests

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>
This commit is contained in:
sterni 2024-03-11 17:17:13 +01:00 committed by clbot
parent 54609e8c17
commit 438db1be30

View file

@ -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")?;
}