2019-05-14 08:05:29 +00:00
|
|
|
use failure::*;
|
|
|
|
|
|
|
|
use proxmox_protocol::Chunker;
|
|
|
|
use futures::{Async, Poll};
|
|
|
|
use futures::stream::Stream;
|
|
|
|
|
2019-05-18 08:46:29 +00:00
|
|
|
/// Split input stream into dynamic sized chunks
|
2019-05-14 08:05:29 +00:00
|
|
|
pub struct ChunkStream<S: Stream<Item=Vec<u8>, Error=Error>> {
|
|
|
|
input: S,
|
|
|
|
chunker: Chunker,
|
|
|
|
buffer: Option<Vec<u8>>,
|
2019-05-19 09:05:56 +00:00
|
|
|
rest: Option<Vec<u8>>,
|
2019-05-14 08:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl <S: Stream<Item=Vec<u8>, Error=Error>> ChunkStream<S> {
|
|
|
|
|
|
|
|
pub fn new(input: S) -> Self {
|
2019-05-19 09:05:56 +00:00
|
|
|
Self { input, chunker: Chunker::new(4 * 1024 * 1024), buffer: None, rest: None }
|
2019-05-14 08:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <S: Stream<Item=Vec<u8>, Error=Error>> Stream for ChunkStream<S> {
|
|
|
|
|
|
|
|
type Item = Vec<u8>;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Vec<u8>>, Error> {
|
|
|
|
loop {
|
|
|
|
match self.input.poll() {
|
|
|
|
Err(err) => {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
return Ok(Async::NotReady);
|
|
|
|
}
|
|
|
|
Ok(Async::Ready(None)) => {
|
2019-05-19 09:05:56 +00:00
|
|
|
let mut data = self.buffer.take().or_else(|| Some(vec![])).unwrap();
|
|
|
|
if let Some(rest) = self.rest.take() { data.extend(rest); }
|
|
|
|
|
|
|
|
if data.len() > 0 {
|
|
|
|
return Ok(Async::Ready(Some(data)));
|
|
|
|
} else {
|
|
|
|
return Ok(Async::Ready(None));
|
|
|
|
}
|
2019-05-14 08:05:29 +00:00
|
|
|
}
|
2019-05-19 09:05:56 +00:00
|
|
|
Ok(Async::Ready(Some(mut data))) => {
|
|
|
|
|
|
|
|
if let Some(rest) = self.rest.take() { data.extend(rest); }
|
2019-05-18 08:46:29 +00:00
|
|
|
|
2019-05-14 08:05:29 +00:00
|
|
|
let buffer = self.buffer.get_or_insert_with(|| Vec::with_capacity(1024*1024));
|
|
|
|
let boundary = self.chunker.scan(&data);
|
|
|
|
|
|
|
|
if boundary == 0 {
|
|
|
|
buffer.extend(data);
|
|
|
|
// continue poll
|
|
|
|
} else if boundary == data.len() {
|
|
|
|
buffer.extend(data);
|
|
|
|
return Ok(Async::Ready(self.buffer.take()));
|
|
|
|
} else if boundary < data.len() {
|
|
|
|
let (left, right) = data.split_at(boundary);
|
|
|
|
buffer.extend(left);
|
|
|
|
|
2019-05-19 09:05:56 +00:00
|
|
|
let rest = self.rest.get_or_insert_with(|| Vec::with_capacity(right.len()));
|
|
|
|
rest.extend(right);
|
2019-05-14 08:05:29 +00:00
|
|
|
|
2019-05-19 09:05:56 +00:00
|
|
|
return Ok(Async::Ready(self.buffer.take()));
|
2019-05-14 08:05:29 +00:00
|
|
|
} else {
|
|
|
|
panic!("got unexpected chunk boundary from chunker");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 08:46:29 +00:00
|
|
|
|
|
|
|
/// Split input stream into fixed sized chunks
|
|
|
|
pub struct FixedChunkStream<S: Stream<Item=Vec<u8>, Error=Error>> {
|
|
|
|
input: S,
|
|
|
|
chunk_size: usize,
|
|
|
|
buffer: Option<Vec<u8>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <S: Stream<Item=Vec<u8>, Error=Error>> FixedChunkStream<S> {
|
|
|
|
|
|
|
|
pub fn new(input: S, chunk_size: usize) -> Self {
|
|
|
|
Self { input, chunk_size, buffer: None }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <S: Stream<Item=Vec<u8>, Error=Error>> Stream for FixedChunkStream<S> {
|
|
|
|
|
|
|
|
type Item = Vec<u8>;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Vec<u8>>, Error> {
|
|
|
|
loop {
|
|
|
|
match self.input.poll() {
|
|
|
|
Err(err) => {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
return Ok(Async::NotReady);
|
|
|
|
}
|
|
|
|
Ok(Async::Ready(None)) => {
|
|
|
|
// last chunk can have any size
|
|
|
|
return Ok(Async::Ready(self.buffer.take()));
|
|
|
|
}
|
|
|
|
Ok(Async::Ready(Some(data))) => {
|
|
|
|
let buffer = self.buffer.get_or_insert_with(|| Vec::with_capacity(1024*1024));
|
|
|
|
let need = self.chunk_size - buffer.len();
|
|
|
|
|
|
|
|
if need > data.len() {
|
|
|
|
buffer.extend(data);
|
|
|
|
// continue poll
|
|
|
|
} else if need == data.len() {
|
|
|
|
buffer.extend(data);
|
|
|
|
return Ok(Async::Ready(self.buffer.take()));
|
|
|
|
} else if need < data.len() {
|
|
|
|
let (left, right) = data.split_at(need);
|
|
|
|
buffer.extend(left);
|
|
|
|
|
|
|
|
let result = self.buffer.take();
|
|
|
|
|
|
|
|
self.buffer = Some(Vec::from(right));
|
|
|
|
|
|
|
|
return Ok(Async::Ready(result));
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|