#[derive(Debug, PartialEq, Clone)] enum Kind { /// An Encoder for when Transfer-Encoding includes `chunked`.
Chunked, /// An Encoder for when Content-Length is set. /// /// Enforces that the body is not longer than the Content-Length header.
Length(u64), /// An Encoder for when neither Content-Length nor Chunked encoding is set. /// /// This is mostly only used with HTTP/1.0 with a length. This kind requires /// the connection to be closed when the body is finished. #[cfg(feature = "server")]
CloseDelimited,
}
pub(crate) fn encode<B>(&mutself, msg: B) -> EncodedBuf<B> where
B: Buf,
{ let len = msg.remaining();
debug_assert!(len > 0, "encode() called with empty buf");
let kind = matchself.kind {
Kind::Chunked => {
trace!("encoding chunked {}B", len); let buf = ChunkSize::new(len)
.chain(msg)
.chain(b"\r\n"as &'static [u8]);
BufKind::Chunked(buf)
}
Kind::Length(refmut remaining) => {
trace!("sized write, len = {}", len); if len as u64 > *remaining { let limit = *remaining as usize;
*remaining = 0;
BufKind::Limited(msg.take(limit))
} else {
*remaining -= len as u64;
BufKind::Exact(msg)
}
} #[cfg(feature = "server")]
Kind::CloseDelimited => {
trace!("close delimited write {}B", len);
BufKind::Exact(msg)
}
};
EncodedBuf { kind }
}
pub(super) fn encode_and_end<B>(&self, msg: B, dst: &mut WriteBuf<EncodedBuf<B>>) -> bool where
B: Buf,
{ let len = msg.remaining();
debug_assert!(len > 0, "encode() called with empty buf");
/// Encodes the full body, without verifying the remaining length matches. /// /// This is used in conjunction with HttpBody::__hyper_full_data(), which /// means we can trust that the buf has the correct size (the buf itself /// was checked to make the headers). pub(super) fn danger_full_buf<B>(self, msg: B, dst: &mut WriteBuf<EncodedBuf<B>>) where
B: Buf,
{
debug_assert!(msg.remaining() > 0, "encode() called with empty buf");
debug_assert!( matchself.kind {
Kind::Length(len) => len == msg.remaining() as u64,
_ => true,
}, "danger_full_buf length mismatches"
);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.