std::io::copy instead of own
This commit is contained in:
27
src/body.rs
27
src/body.rs
@@ -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(())
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user