2017-10-09 05:16:48 +02:00
|
|
|
use std::rc::{Rc, Weak};
|
|
|
|
use std::cell::RefCell;
|
2017-10-14 01:33:23 +02:00
|
|
|
use std::convert::From;
|
2017-10-09 05:16:48 +02:00
|
|
|
use std::collections::VecDeque;
|
2017-10-14 01:33:23 +02:00
|
|
|
use std::io::Error as IoError;
|
2017-10-14 09:11:12 +02:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2017-10-09 05:16:48 +02:00
|
|
|
use futures::{Async, Poll, Stream};
|
|
|
|
use futures::task::{Task, current as current_task};
|
|
|
|
|
|
|
|
const MAX_PAYLOAD_SIZE: usize = 65_536; // max buffer size 64k
|
|
|
|
|
2017-10-14 01:33:23 +02:00
|
|
|
/// Just Bytes object
|
|
|
|
pub type PayloadItem = Result<Bytes, PayloadError>;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
/// A set of error that can occur during payload parsing.
|
|
|
|
pub enum PayloadError {
|
|
|
|
/// A payload reached EOF, but is not complete.
|
|
|
|
Incomplete,
|
|
|
|
/// Parse error
|
|
|
|
ParseError(IoError),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<IoError> for PayloadError {
|
|
|
|
fn from(err: IoError) -> PayloadError {
|
|
|
|
PayloadError::ParseError(err)
|
|
|
|
}
|
|
|
|
}
|
2017-10-09 05:16:48 +02:00
|
|
|
|
|
|
|
/// Stream of byte chunks
|
|
|
|
///
|
|
|
|
/// Payload stores chunks in vector. First chunk can be received with `.readany()` method.
|
|
|
|
pub struct Payload {
|
|
|
|
inner: Rc<RefCell<Inner>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Payload {
|
|
|
|
|
|
|
|
pub(crate) fn new(eof: bool) -> (PayloadSender, Payload) {
|
|
|
|
let shared = Rc::new(RefCell::new(Inner::new(eof)));
|
|
|
|
|
|
|
|
(PayloadSender{inner: Rc::downgrade(&shared)},
|
|
|
|
Payload{inner: shared})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates paused state of the payload. If payload data is not consumed
|
|
|
|
/// it get paused. Max size of not consumed data is 64k
|
|
|
|
pub fn paused(&self) -> bool {
|
|
|
|
self.inner.borrow().paused()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates EOF of payload
|
|
|
|
pub fn eof(&self) -> bool {
|
|
|
|
self.inner.borrow().eof()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Length of the data in this payload
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.inner.borrow().len()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Is payload empty
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.inner.borrow().len() == 0
|
|
|
|
}
|
|
|
|
|
2017-10-09 05:55:44 +02:00
|
|
|
/// Get first available chunk of data.
|
|
|
|
/// Chunk get returned as Some(PayloadItem), `None` indicates eof.
|
2017-10-09 05:16:48 +02:00
|
|
|
pub fn readany(&mut self) -> Async<Option<PayloadItem>> {
|
|
|
|
self.inner.borrow_mut().readany()
|
|
|
|
}
|
|
|
|
|
2017-10-14 09:11:12 +02:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn readall(&mut self) -> Option<Bytes> {
|
|
|
|
self.inner.borrow_mut().readall()
|
|
|
|
}
|
|
|
|
|
2017-10-09 05:16:48 +02:00
|
|
|
/// Put unused data back to payload
|
2017-10-14 01:33:23 +02:00
|
|
|
pub fn unread_data(&mut self, data: Bytes) {
|
2017-10-09 05:16:48 +02:00
|
|
|
self.inner.borrow_mut().unread_data(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Stream for Payload {
|
|
|
|
type Item = PayloadItem;
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<PayloadItem>, ()> {
|
|
|
|
Ok(self.readany())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct PayloadSender {
|
|
|
|
inner: Weak<RefCell<Inner>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PayloadSender {
|
2017-10-14 01:33:23 +02:00
|
|
|
pub(crate) fn set_error(&mut self, err: PayloadError) {
|
|
|
|
if let Some(shared) = self.inner.upgrade() {
|
|
|
|
shared.borrow_mut().set_error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 05:16:48 +02:00
|
|
|
pub(crate) fn feed_eof(&mut self) {
|
|
|
|
if let Some(shared) = self.inner.upgrade() {
|
|
|
|
shared.borrow_mut().feed_eof()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn feed_data(&mut self, data: Bytes) {
|
|
|
|
if let Some(shared) = self.inner.upgrade() {
|
|
|
|
shared.borrow_mut().feed_data(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn maybe_paused(&self) -> bool {
|
|
|
|
match self.inner.upgrade() {
|
|
|
|
Some(shared) => {
|
|
|
|
let inner = shared.borrow();
|
|
|
|
if inner.paused() && inner.len() < MAX_PAYLOAD_SIZE {
|
|
|
|
drop(inner);
|
|
|
|
shared.borrow_mut().resume();
|
|
|
|
false
|
|
|
|
} else if !inner.paused() && inner.len() > MAX_PAYLOAD_SIZE {
|
|
|
|
drop(inner);
|
|
|
|
shared.borrow_mut().pause();
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
inner.paused()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Inner {
|
|
|
|
len: usize,
|
|
|
|
eof: bool,
|
|
|
|
paused: bool,
|
2017-10-14 01:33:23 +02:00
|
|
|
err: Option<PayloadError>,
|
2017-10-09 05:16:48 +02:00
|
|
|
task: Option<Task>,
|
|
|
|
items: VecDeque<Bytes>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Inner {
|
|
|
|
|
|
|
|
fn new(eof: bool) -> Self {
|
|
|
|
Inner {
|
|
|
|
len: 0,
|
|
|
|
eof: eof,
|
|
|
|
paused: false,
|
2017-10-14 01:33:23 +02:00
|
|
|
err: None,
|
2017-10-09 05:16:48 +02:00
|
|
|
task: None,
|
|
|
|
items: VecDeque::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn paused(&self) -> bool {
|
|
|
|
self.paused
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pause(&mut self) {
|
|
|
|
self.paused = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resume(&mut self) {
|
|
|
|
self.paused = false;
|
|
|
|
}
|
|
|
|
|
2017-10-14 01:33:23 +02:00
|
|
|
fn set_error(&mut self, err: PayloadError) {
|
|
|
|
self.err = Some(err);
|
|
|
|
if let Some(task) = self.task.take() {
|
|
|
|
task.notify()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 05:16:48 +02:00
|
|
|
fn feed_eof(&mut self) {
|
|
|
|
self.eof = true;
|
|
|
|
if let Some(task) = self.task.take() {
|
|
|
|
task.notify()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn feed_data(&mut self, data: Bytes) {
|
|
|
|
self.len += data.len();
|
|
|
|
self.items.push_back(data);
|
|
|
|
if let Some(task) = self.task.take() {
|
|
|
|
task.notify()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eof(&self) -> bool {
|
2017-10-14 09:11:12 +02:00
|
|
|
self.items.is_empty() && self.eof
|
2017-10-09 05:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
self.len
|
|
|
|
}
|
|
|
|
|
2017-10-14 01:33:23 +02:00
|
|
|
fn readany(&mut self) -> Async<Option<PayloadItem>> {
|
2017-10-09 05:16:48 +02:00
|
|
|
if let Some(data) = self.items.pop_front() {
|
|
|
|
self.len -= data.len();
|
2017-10-14 01:33:23 +02:00
|
|
|
Async::Ready(Some(Ok(data)))
|
2017-10-09 05:16:48 +02:00
|
|
|
} else if self.eof {
|
|
|
|
Async::Ready(None)
|
2017-10-14 01:33:23 +02:00
|
|
|
} else if let Some(err) = self.err.take() {
|
|
|
|
Async::Ready(Some(Err(err)))
|
2017-10-09 05:16:48 +02:00
|
|
|
} else {
|
|
|
|
self.task = Some(current_task());
|
|
|
|
Async::NotReady
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-14 09:11:12 +02:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn readall(&mut self) -> Option<Bytes> {
|
|
|
|
let len = self.items.iter().fold(0, |cur, item| cur + item.len());
|
|
|
|
if len > 0 {
|
|
|
|
let mut buf = BytesMut::with_capacity(len);
|
|
|
|
for item in &self.items {
|
|
|
|
buf.extend(item);
|
|
|
|
}
|
|
|
|
self.items = VecDeque::new();
|
|
|
|
Some(buf.take().freeze())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 05:16:48 +02:00
|
|
|
pub fn unread_data(&mut self, data: Bytes) {
|
|
|
|
self.len += data.len();
|
|
|
|
self.items.push_front(data)
|
|
|
|
}
|
|
|
|
}
|