2019-05-14 08:05:29 +00:00
|
|
|
use failure::*;
|
|
|
|
|
|
|
|
use proxmox_protocol::Chunker;
|
|
|
|
use futures::{Async, Poll};
|
|
|
|
use futures::stream::Stream;
|
|
|
|
|
2019-05-22 07:39:02 +00:00
|
|
|
use bytes::BytesMut;
|
|
|
|
|
2019-05-18 08:46:29 +00:00
|
|
|
/// Split input stream into dynamic sized chunks
|
2019-05-22 07:18:05 +00:00
|
|
|
pub struct ChunkStream<S> {
|
2019-05-14 08:05:29 +00:00
|
|
|
input: S,
|
|
|
|
chunker: Chunker,
|
2019-05-22 07:39:02 +00:00
|
|
|
buffer: BytesMut,
|
|
|
|
scan_pos: usize,
|
2019-05-14 08:05:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 07:18:05 +00:00
|
|
|
impl <S> ChunkStream<S> {
|
2019-05-14 08:05:29 +00:00
|
|
|
pub fn new(input: S) -> Self {
|
2019-05-22 07:39:02 +00:00
|
|
|
Self { input, chunker: Chunker::new(4 * 1024 * 1024), buffer: BytesMut::new(), scan_pos: 0}
|
2019-05-14 08:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 07:18:05 +00:00
|
|
|
impl <S> Stream for ChunkStream<S>
|
|
|
|
where S: Stream,
|
|
|
|
S::Item: AsRef<[u8]>,
|
|
|
|
S::Error: Into<Error>,
|
|
|
|
{
|
2019-05-14 08:05:29 +00:00
|
|
|
|
2019-05-22 07:39:02 +00:00
|
|
|
type Item = BytesMut;
|
2019-05-14 08:05:29 +00:00
|
|
|
type Error = Error;
|
|
|
|
|
2019-05-22 07:39:02 +00:00
|
|
|
fn poll(&mut self) -> Poll<Option<BytesMut>, Error> {
|
2019-05-14 08:05:29 +00:00
|
|
|
loop {
|
2019-05-22 06:05:27 +00:00
|
|
|
|
2019-05-22 07:39:02 +00:00
|
|
|
if self.scan_pos < self.buffer.len() {
|
|
|
|
let boundary = self.chunker.scan(&self.buffer[self.scan_pos..]);
|
|
|
|
|
|
|
|
let chunk_size = self.scan_pos + boundary;
|
2019-05-22 06:05:27 +00:00
|
|
|
|
|
|
|
if boundary == 0 {
|
2019-05-22 07:39:02 +00:00
|
|
|
self.scan_pos = self.buffer.len();
|
2019-05-22 06:05:27 +00:00
|
|
|
// continue poll
|
2019-05-22 07:39:02 +00:00
|
|
|
} else if chunk_size <= self.buffer.len() {
|
|
|
|
let result = self.buffer.split_to(chunk_size);
|
|
|
|
self.scan_pos = 0;
|
|
|
|
return Ok(Async::Ready(Some(result)));
|
2019-05-22 06:05:27 +00:00
|
|
|
} else {
|
|
|
|
panic!("got unexpected chunk boundary from chunker");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 08:05:29 +00:00
|
|
|
match self.input.poll() {
|
|
|
|
Err(err) => {
|
2019-05-22 07:18:05 +00:00
|
|
|
return Err(err.into());
|
2019-05-14 08:05:29 +00:00
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
return Ok(Async::NotReady);
|
|
|
|
}
|
|
|
|
Ok(Async::Ready(None)) => {
|
2019-05-22 07:39:02 +00:00
|
|
|
self.scan_pos = 0;
|
|
|
|
if self.buffer.len() > 0 {
|
|
|
|
return Ok(Async::Ready(Some(self.buffer.take())));
|
2019-05-19 09:05:56 +00:00
|
|
|
} else {
|
|
|
|
return Ok(Async::Ready(None));
|
|
|
|
}
|
2019-05-14 08:05:29 +00:00
|
|
|
}
|
2019-05-22 05:44:51 +00:00
|
|
|
Ok(Async::Ready(Some(data))) => {
|
2019-05-22 07:39:02 +00:00
|
|
|
self.buffer.extend_from_slice(data.as_ref());
|
|
|
|
}
|
2019-05-14 08:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 08:46:29 +00:00
|
|
|
|
|
|
|
/// Split input stream into fixed sized chunks
|
2019-05-22 07:05:35 +00:00
|
|
|
pub struct FixedChunkStream<S> {
|
2019-05-18 08:46:29 +00:00
|
|
|
input: S,
|
|
|
|
chunk_size: usize,
|
2019-05-22 07:05:35 +00:00
|
|
|
buffer: BytesMut,
|
2019-05-18 08:46:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 07:05:35 +00:00
|
|
|
impl <S> FixedChunkStream<S> {
|
2019-05-18 08:46:29 +00:00
|
|
|
|
|
|
|
pub fn new(input: S, chunk_size: usize) -> Self {
|
2019-05-22 07:05:35 +00:00
|
|
|
Self { input, chunk_size, buffer: BytesMut::new() }
|
2019-05-18 08:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 07:05:35 +00:00
|
|
|
impl <S> Stream for FixedChunkStream<S>
|
|
|
|
where S: Stream,
|
|
|
|
S::Item: AsRef<[u8]>,
|
|
|
|
{
|
2019-05-18 08:46:29 +00:00
|
|
|
|
2019-05-22 07:05:35 +00:00
|
|
|
type Item = BytesMut;
|
|
|
|
type Error = S::Error;
|
2019-05-18 08:46:29 +00:00
|
|
|
|
2019-05-22 07:05:35 +00:00
|
|
|
fn poll(&mut self) -> Poll<Option<BytesMut>, S::Error> {
|
2019-05-18 08:46:29 +00:00
|
|
|
loop {
|
2019-05-22 07:05:35 +00:00
|
|
|
|
|
|
|
if self.buffer.len() == self.chunk_size {
|
|
|
|
return Ok(Async::Ready(Some(self.buffer.take())));
|
|
|
|
} else if self.buffer.len() > self.chunk_size {
|
|
|
|
let result = self.buffer.split_to(self.chunk_size);
|
|
|
|
return Ok(Async::Ready(Some(result)));
|
|
|
|
}
|
|
|
|
|
2019-05-18 08:46:29 +00:00
|
|
|
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
|
2019-05-22 07:05:35 +00:00
|
|
|
if self.buffer.len() > 0 {
|
|
|
|
return Ok(Async::Ready(Some(self.buffer.take())));
|
2019-05-18 08:46:29 +00:00
|
|
|
} else {
|
2019-05-22 07:05:35 +00:00
|
|
|
return Ok(Async::Ready(None));
|
2019-05-18 08:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-22 07:05:35 +00:00
|
|
|
Ok(Async::Ready(Some(data))) => {
|
|
|
|
self.buffer.extend_from_slice(data.as_ref());
|
|
|
|
}
|
2019-05-18 08:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|