std::io::copy instead of own

This commit is contained in:
Martin Algesten
2018-06-30 14:55:58 +02:00
parent 317f75f8bf
commit 924393b811

View File

@@ -1,5 +1,5 @@
use chunked_transfer; use chunked_transfer;
use std::io::{empty, Cursor, Read, Result as IoResult, Write}; use std::io::{copy, empty, Cursor, Read, Result as IoResult};
use stream::Stream; use stream::Stream;
#[cfg(feature = "charset")] #[cfg(feature = "charset")]
@@ -14,8 +14,6 @@ use super::SerdeValue;
#[cfg(feature = "json")] #[cfg(feature = "json")]
use serde_json; use serde_json;
const CHUNK_SIZE: usize = 1024 * 1024;
pub enum Payload { pub enum Payload {
Empty, Empty,
Text(String, String), Text(String, String),
@@ -71,28 +69,13 @@ impl Payload {
} }
} }
pub fn send_body(body: SizedReader, do_chunk: bool, stream: &mut Stream) -> IoResult<()> { pub fn send_body(mut body: SizedReader, do_chunk: bool, stream: &mut Stream) -> IoResult<()> {
if do_chunk { if do_chunk {
pipe(body.reader, chunked_transfer::Encoder::new(stream))?; let mut chunker = chunked_transfer::Encoder::new(stream);
copy(&mut body.reader, &mut chunker)?;
} else { } else {
pipe(body.reader, stream)?; copy(&mut body.reader, stream)?;
} }
Ok(()) Ok(())
} }
fn pipe<R, W>(mut reader: R, mut writer: W) -> IoResult<()>
where
R: Read,
W: Write,
{
let mut buf = [0_u8; CHUNK_SIZE];
loop {
let len = reader.read(&mut buf)?;
if len == 0 {
break;
}
writer.write_all(&buf[0..len])?;
}
Ok(())
}