1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-09 02:19:49 +00:00

encode returns result

This commit is contained in:
Nikolay Kim 2018-01-04 09:32:15 -08:00
parent fdf7726831
commit afeffe4b19

View file

@ -638,7 +638,7 @@ impl ContentEncoder {
} }
} }
ContentEncoder::Identity(ref mut encoder) => { ContentEncoder::Identity(ref mut encoder) => {
encoder.encode(data); encoder.encode(data)?;
Ok(()) Ok(())
} }
} }
@ -705,36 +705,37 @@ impl TransferEncoding {
/// Encode message. Return `EOF` state of encoder /// Encode message. Return `EOF` state of encoder
#[inline] #[inline]
pub fn encode(&mut self, msg: &[u8]) -> bool { pub fn encode(&mut self, msg: &[u8]) -> io::Result<bool> {
match self.kind { match self.kind {
TransferEncodingKind::Eof => { TransferEncodingKind::Eof => {
self.buffer.get_mut().extend_from_slice(msg); self.buffer.get_mut().extend_from_slice(msg);
msg.is_empty() Ok(msg.is_empty())
}, },
TransferEncodingKind::Chunked(ref mut eof) => { TransferEncodingKind::Chunked(ref mut eof) => {
if *eof { if *eof {
return true; return Ok(true);
} }
if msg.is_empty() { if msg.is_empty() {
*eof = true; *eof = true;
self.buffer.get_mut().extend_from_slice(b"0\r\n\r\n"); self.buffer.get_mut().extend_from_slice(b"0\r\n\r\n");
} else { } else {
write!(self.buffer.get_mut(), "{:X}\r\n", msg.len()).unwrap(); write!(self.buffer.get_mut(), "{:X}\r\n", msg.len())
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
self.buffer.get_mut().extend_from_slice(msg); self.buffer.get_mut().extend_from_slice(msg);
self.buffer.get_mut().extend_from_slice(b"\r\n"); self.buffer.get_mut().extend_from_slice(b"\r\n");
} }
*eof Ok(*eof)
}, },
TransferEncodingKind::Length(ref mut remaining) => { TransferEncodingKind::Length(ref mut remaining) => {
if msg.is_empty() { if msg.is_empty() {
return *remaining == 0 return Ok(*remaining == 0)
} }
let max = cmp::min(*remaining, msg.len() as u64); let max = cmp::min(*remaining, msg.len() as u64);
self.buffer.get_mut().extend_from_slice(msg[..max as usize].as_ref()); self.buffer.get_mut().extend_from_slice(msg[..max as usize].as_ref());
*remaining -= max as u64; *remaining -= max as u64;
*remaining == 0 Ok(*remaining == 0)
}, },
} }
} }
@ -758,7 +759,7 @@ impl io::Write for TransferEncoding {
#[inline] #[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.encode(buf); self.encode(buf)?;
Ok(buf.len()) Ok(buf.len())
} }
@ -834,3 +835,19 @@ impl AcceptEncoding {
ContentEncoding::Identity ContentEncoding::Identity
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_chunked_te() {
let bytes = SharedBytes::default();
let mut enc = TransferEncoding::chunked(bytes.clone());
assert!(!enc.encode(b"test").ok().unwrap());
assert!(enc.encode(b"").ok().unwrap());
assert_eq!(bytes.get_mut().take().freeze(),
Bytes::from_static(b"4\r\ntest\r\n0\r\n\r\n"));
}
}