Skip to content

Commit

Permalink
send_json sets content-length header
Browse files Browse the repository at this point in the history
  • Loading branch information
algesten committed Feb 3, 2025
1 parent c7a9c9e commit 9a93c22
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

* send_json should set content-length header (#983)

# 3.0.4

* Manually unroll some macros to regular code (#978)
Expand Down
10 changes: 9 additions & 1 deletion src/send_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<'a> SendBody<'a> {
value: &impl serde::ser::Serialize,
) -> Result<SendBody<'static>, crate::Error> {
let json = serde_json::to_vec_pretty(value)?;
Ok(Self::from_owned_reader(io::Cursor::new(json)))
Ok(BodyInner::ByteVec(io::Cursor::new(json)).into())
}

pub(crate) fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
Expand All @@ -59,6 +59,8 @@ impl<'a> SendBody<'a> {

Ok(max)
}
#[cfg(feature = "json")]
BodyInner::ByteVec(v) => v.read(buf),
BodyInner::Reader(v) => v.read(buf),
BodyInner::OwnedReader(v) => v.read(buf),
BodyInner::Body(v) => v.read(buf),
Expand Down Expand Up @@ -182,6 +184,8 @@ impl<'a> AsSendBody for SendBody<'a> {
inner: match &mut self.inner {
BodyInner::None => BodyInner::None,
BodyInner::ByteSlice(v) => BodyInner::ByteSlice(v),
#[cfg(feature = "json")]
BodyInner::ByteVec(v) => BodyInner::ByteSlice(v.get_ref()),
BodyInner::Reader(v) => BodyInner::Reader(v),
BodyInner::Body(v) => BodyInner::Reader(v),
BodyInner::OwnedReader(v) => BodyInner::Reader(v),
Expand All @@ -194,6 +198,8 @@ impl<'a> AsSendBody for SendBody<'a> {
pub(crate) enum BodyInner<'a> {
None,
ByteSlice(&'a [u8]),
#[cfg(feature = "json")]
ByteVec(io::Cursor<Vec<u8>>),
Body(BodyReader<'a>),
Reader(&'a mut dyn Read),
OwnedReader(Box<dyn Read>),
Expand All @@ -204,6 +210,8 @@ impl<'a> BodyInner<'a> {
match self {
BodyInner::None => BodyMode::NoBody,
BodyInner::ByteSlice(v) => BodyMode::LengthDelimited(v.len() as u64),
#[cfg(feature = "json")]
BodyInner::ByteVec(v) => BodyMode::LengthDelimited(v.get_ref().len() as u64),
BodyInner::Body(v) => v.body_mode(),
BodyInner::Reader(_) => BodyMode::Chunked,
BodyInner::OwnedReader(_) => BodyMode::Chunked,
Expand Down

0 comments on commit 9a93c22

Please sign in to comment.