2017-11-20 04:51:14 +01:00
|
|
|
//! Multipart requests support
|
2017-10-19 08:43:50 +02:00
|
|
|
use std::{cmp, fmt};
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
use mime;
|
|
|
|
use httparse;
|
|
|
|
use bytes::Bytes;
|
|
|
|
use http::HttpTryFrom;
|
|
|
|
use http::header::{self, HeaderMap, HeaderName, HeaderValue};
|
2017-12-19 09:18:57 +01:00
|
|
|
use futures::{Async, Future, Stream, Poll};
|
2017-10-19 08:43:50 +02:00
|
|
|
use futures::task::{Task, current as current_task};
|
|
|
|
|
2017-11-16 07:06:28 +01:00
|
|
|
use error::{ParseError, PayloadError, MultipartError};
|
|
|
|
use payload::Payload;
|
2017-10-19 08:43:50 +02:00
|
|
|
|
|
|
|
const MAX_HEADERS: usize = 32;
|
|
|
|
|
|
|
|
/// The server-side implementation of `multipart/form-data` requests.
|
|
|
|
///
|
|
|
|
/// This will parse the incoming stream into `MultipartItem` instances via its
|
|
|
|
/// Stream implementation.
|
|
|
|
/// `MultipartItem::Field` contains multipart field. `MultipartItem::Multipart`
|
|
|
|
/// is used for nested multipart streams.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Multipart {
|
|
|
|
safety: Safety,
|
2017-10-20 01:22:21 +02:00
|
|
|
inner: Rc<RefCell<InnerMultipart>>,
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
|
2017-10-20 01:41:21 +02:00
|
|
|
///
|
2017-10-19 08:43:50 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum MultipartItem {
|
2017-10-20 01:22:21 +02:00
|
|
|
/// Multipart field
|
2017-10-19 08:43:50 +02:00
|
|
|
Field(Field),
|
2017-10-20 01:22:21 +02:00
|
|
|
/// Nested multipart stream
|
|
|
|
Nested(Multipart),
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum InnerMultipartItem {
|
|
|
|
None,
|
|
|
|
Field(Rc<RefCell<InnerField>>),
|
2017-10-20 01:22:21 +02:00
|
|
|
Multipart(Rc<RefCell<InnerMultipart>>),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
enum InnerState {
|
|
|
|
/// Stream eof
|
|
|
|
Eof,
|
|
|
|
/// Skip data until first boundary
|
|
|
|
FirstBoundary,
|
|
|
|
/// Reading boundary
|
|
|
|
Boundary,
|
|
|
|
/// Reading Headers,
|
|
|
|
Headers,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct InnerMultipart {
|
|
|
|
payload: PayloadRef,
|
|
|
|
boundary: String,
|
|
|
|
state: InnerState,
|
|
|
|
item: InnerMultipartItem,
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Multipart {
|
2017-10-22 18:30:05 +02:00
|
|
|
/// Create multipart instance for boundary.
|
2017-10-20 01:22:21 +02:00
|
|
|
pub fn new(boundary: String, payload: Payload) -> Multipart {
|
|
|
|
Multipart {
|
|
|
|
safety: Safety::new(),
|
|
|
|
inner: Rc::new(RefCell::new(
|
|
|
|
InnerMultipart {
|
|
|
|
payload: PayloadRef::new(payload),
|
|
|
|
boundary: boundary,
|
|
|
|
state: InnerState::FirstBoundary,
|
|
|
|
item: InnerMultipartItem::None,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-22 18:30:05 +02:00
|
|
|
/// Extract boundary info from headers.
|
|
|
|
pub fn boundary(headers: &HeaderMap) -> Result<String, MultipartError> {
|
|
|
|
if let Some(content_type) = headers.get(header::CONTENT_TYPE) {
|
2017-10-19 08:43:50 +02:00
|
|
|
if let Ok(content_type) = content_type.to_str() {
|
|
|
|
if let Ok(ct) = content_type.parse::<mime::Mime>() {
|
|
|
|
if let Some(boundary) = ct.get_param(mime::BOUNDARY) {
|
2017-10-20 01:22:21 +02:00
|
|
|
Ok(boundary.as_str().to_owned())
|
|
|
|
} else {
|
|
|
|
Err(MultipartError::Boundary)
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
2017-10-20 01:22:21 +02:00
|
|
|
} else {
|
|
|
|
Err(MultipartError::ParseContentType)
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
2017-10-20 01:22:21 +02:00
|
|
|
} else {
|
|
|
|
Err(MultipartError::ParseContentType)
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
2017-10-20 01:22:21 +02:00
|
|
|
} else {
|
|
|
|
Err(MultipartError::NoContentType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stream for Multipart {
|
|
|
|
type Item = MultipartItem;
|
|
|
|
type Error = MultipartError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
|
|
|
if self.safety.current() {
|
|
|
|
self.inner.borrow_mut().poll(&self.safety)
|
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-20 01:22:21 +02:00
|
|
|
}
|
2017-10-19 08:43:50 +02:00
|
|
|
|
2017-10-20 01:22:21 +02:00
|
|
|
impl InnerMultipart {
|
|
|
|
|
|
|
|
fn read_headers(payload: &mut Payload) -> Poll<HeaderMap, MultipartError>
|
2017-10-19 08:43:50 +02:00
|
|
|
{
|
2017-12-19 09:18:57 +01:00
|
|
|
match payload.readuntil(b"\r\n\r\n").poll()? {
|
2017-10-19 08:43:50 +02:00
|
|
|
Async::NotReady => Ok(Async::NotReady),
|
|
|
|
Async::Ready(bytes) => {
|
|
|
|
let mut hdrs = [httparse::EMPTY_HEADER; MAX_HEADERS];
|
|
|
|
match httparse::parse_headers(&bytes, &mut hdrs) {
|
|
|
|
Ok(httparse::Status::Complete((_, hdrs))) => {
|
|
|
|
// convert headers
|
|
|
|
let mut headers = HeaderMap::with_capacity(hdrs.len());
|
|
|
|
for h in hdrs {
|
|
|
|
if let Ok(name) = HeaderName::try_from(h.name) {
|
|
|
|
if let Ok(value) = HeaderValue::try_from(h.value) {
|
|
|
|
headers.append(name, value);
|
|
|
|
} else {
|
2017-10-20 01:22:21 +02:00
|
|
|
return Err(ParseError::Header.into())
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-10-20 01:22:21 +02:00
|
|
|
return Err(ParseError::Header.into())
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Async::Ready(headers))
|
|
|
|
}
|
2017-10-20 01:22:21 +02:00
|
|
|
Ok(httparse::Status::Partial) => Err(ParseError::Header.into()),
|
|
|
|
Err(err) => Err(ParseError::from(err).into()),
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 01:22:21 +02:00
|
|
|
fn read_boundary(payload: &mut Payload, boundary: &str) -> Poll<bool, MultipartError>
|
2017-10-19 08:43:50 +02:00
|
|
|
{
|
|
|
|
// TODO: need to read epilogue
|
2017-12-19 09:18:57 +01:00
|
|
|
match payload.readline().poll()? {
|
2017-10-19 08:43:50 +02:00
|
|
|
Async::NotReady => Ok(Async::NotReady),
|
|
|
|
Async::Ready(chunk) => {
|
|
|
|
if chunk.len() == boundary.len() + 4 &&
|
|
|
|
&chunk[..2] == b"--" &&
|
|
|
|
&chunk[2..boundary.len()+2] == boundary.as_bytes()
|
|
|
|
{
|
|
|
|
Ok(Async::Ready(false))
|
|
|
|
} else if chunk.len() == boundary.len() + 6 &&
|
|
|
|
&chunk[..2] == b"--" &&
|
|
|
|
&chunk[2..boundary.len()+2] == boundary.as_bytes() &&
|
|
|
|
&chunk[boundary.len()+2..boundary.len()+4] == b"--"
|
|
|
|
{
|
|
|
|
Ok(Async::Ready(true))
|
|
|
|
} else {
|
2017-10-20 01:22:21 +02:00
|
|
|
Err(MultipartError::Boundary)
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 01:22:21 +02:00
|
|
|
fn skip_until_boundary(payload: &mut Payload, boundary: &str) -> Poll<bool, MultipartError>
|
2017-10-19 08:43:50 +02:00
|
|
|
{
|
|
|
|
let mut eof = false;
|
|
|
|
loop {
|
2017-12-19 09:18:57 +01:00
|
|
|
if let Async::Ready(chunk) = payload.readline().poll()? {
|
2017-10-19 08:43:50 +02:00
|
|
|
if chunk.is_empty() {
|
|
|
|
//ValueError("Could not find starting boundary %r"
|
|
|
|
//% (self._boundary))
|
|
|
|
}
|
2017-10-23 07:23:38 +02:00
|
|
|
if chunk.len() < boundary.len() {
|
|
|
|
continue
|
|
|
|
}
|
2017-10-19 08:43:50 +02:00
|
|
|
if &chunk[..2] == b"--" && &chunk[2..chunk.len()-2] == boundary.as_bytes() {
|
|
|
|
break;
|
|
|
|
} else {
|
2017-10-23 07:23:38 +02:00
|
|
|
if chunk.len() < boundary.len() + 2{
|
|
|
|
continue
|
|
|
|
}
|
2017-10-19 08:43:50 +02:00
|
|
|
let b: &[u8] = boundary.as_ref();
|
2017-10-23 07:23:38 +02:00
|
|
|
if &chunk[..boundary.len()] == b &&
|
2017-10-19 08:43:50 +02:00
|
|
|
&chunk[boundary.len()..boundary.len()+2] == b"--" {
|
|
|
|
eof = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Async::Ready(eof))
|
|
|
|
}
|
|
|
|
|
2017-10-20 01:22:21 +02:00
|
|
|
fn poll(&mut self, safety: &Safety) -> Poll<Option<MultipartItem>, MultipartError> {
|
|
|
|
if self.state == InnerState::Eof {
|
2017-10-19 08:43:50 +02:00
|
|
|
Ok(Async::Ready(None))
|
|
|
|
} else {
|
|
|
|
// release field
|
|
|
|
loop {
|
2017-10-20 01:22:21 +02:00
|
|
|
// Nested multipart streams of fields has to be consumed
|
|
|
|
// before switching to next
|
|
|
|
if safety.current() {
|
|
|
|
let stop = match self.item {
|
|
|
|
InnerMultipartItem::Field(ref mut field) => {
|
|
|
|
match field.borrow_mut().poll(safety)? {
|
2017-10-23 06:40:41 +02:00
|
|
|
Async::NotReady => {
|
|
|
|
return Ok(Async::NotReady)
|
|
|
|
}
|
2017-10-20 01:22:21 +02:00
|
|
|
Async::Ready(Some(_)) =>
|
|
|
|
continue,
|
|
|
|
Async::Ready(None) =>
|
|
|
|
true,
|
|
|
|
}
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
2017-10-20 01:22:21 +02:00
|
|
|
InnerMultipartItem::Multipart(ref mut multipart) => {
|
|
|
|
match multipart.borrow_mut().poll(safety)? {
|
|
|
|
Async::NotReady =>
|
|
|
|
return Ok(Async::NotReady),
|
|
|
|
Async::Ready(Some(_)) =>
|
|
|
|
continue,
|
|
|
|
Async::Ready(None) =>
|
|
|
|
true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
if stop {
|
|
|
|
self.item = InnerMultipartItem::None;
|
|
|
|
}
|
|
|
|
if let InnerMultipartItem::None = self.item {
|
|
|
|
break;
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 01:22:21 +02:00
|
|
|
let headers = if let Some(payload) = self.payload.get_mut(safety) {
|
|
|
|
match self.state {
|
|
|
|
// read until first boundary
|
|
|
|
InnerState::FirstBoundary => {
|
|
|
|
if let Async::Ready(eof) =
|
|
|
|
InnerMultipart::skip_until_boundary(payload, &self.boundary)?
|
|
|
|
{
|
|
|
|
if eof {
|
|
|
|
self.state = InnerState::Eof;
|
|
|
|
return Ok(Async::Ready(None));
|
|
|
|
} else {
|
|
|
|
self.state = InnerState::Headers;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Ok(Async::NotReady)
|
|
|
|
}
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
// read boundary
|
2017-10-20 01:22:21 +02:00
|
|
|
InnerState::Boundary => {
|
|
|
|
match InnerMultipart::read_boundary(payload, &self.boundary)? {
|
2017-10-23 06:40:41 +02:00
|
|
|
Async::NotReady => {
|
|
|
|
return Ok(Async::NotReady)
|
|
|
|
}
|
2017-10-20 01:22:21 +02:00
|
|
|
Async::Ready(eof) => {
|
|
|
|
if eof {
|
|
|
|
self.state = InnerState::Eof;
|
|
|
|
return Ok(Async::Ready(None));
|
|
|
|
} else {
|
|
|
|
self.state = InnerState::Headers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
2017-10-20 01:22:21 +02:00
|
|
|
_ => (),
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
|
2017-10-20 01:22:21 +02:00
|
|
|
// read field headers for next field
|
|
|
|
if self.state == InnerState::Headers {
|
|
|
|
if let Async::Ready(headers) = InnerMultipart::read_headers(payload)? {
|
|
|
|
self.state = InnerState::Boundary;
|
|
|
|
headers
|
|
|
|
} else {
|
|
|
|
return Ok(Async::NotReady)
|
|
|
|
}
|
2017-10-19 08:43:50 +02:00
|
|
|
} else {
|
2017-10-20 01:22:21 +02:00
|
|
|
unreachable!()
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debug!("NotReady: field is in flight");
|
|
|
|
return Ok(Async::NotReady)
|
|
|
|
};
|
|
|
|
|
2017-10-20 01:22:21 +02:00
|
|
|
// content type
|
|
|
|
let mut mt = mime::APPLICATION_OCTET_STREAM;
|
|
|
|
if let Some(content_type) = headers.get(header::CONTENT_TYPE) {
|
|
|
|
if let Ok(content_type) = content_type.to_str() {
|
|
|
|
if let Ok(ct) = content_type.parse::<mime::Mime>() {
|
|
|
|
mt = ct;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 06:40:41 +02:00
|
|
|
self.state = InnerState::Boundary;
|
|
|
|
|
2017-10-20 01:22:21 +02:00
|
|
|
// nested multipart stream
|
|
|
|
if mt.type_() == mime::MULTIPART {
|
|
|
|
let inner = if let Some(boundary) = mt.get_param(mime::BOUNDARY) {
|
|
|
|
Rc::new(RefCell::new(
|
|
|
|
InnerMultipart {
|
|
|
|
payload: self.payload.clone(),
|
|
|
|
boundary: boundary.as_str().to_owned(),
|
|
|
|
state: InnerState::FirstBoundary,
|
|
|
|
item: InnerMultipartItem::None,
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
return Err(MultipartError::Boundary)
|
|
|
|
};
|
|
|
|
|
|
|
|
self.item = InnerMultipartItem::Multipart(Rc::clone(&inner));
|
|
|
|
|
|
|
|
Ok(Async::Ready(Some(
|
|
|
|
MultipartItem::Nested(
|
|
|
|
Multipart{safety: safety.clone(), inner: inner}))))
|
|
|
|
} else {
|
|
|
|
let field = Rc::new(RefCell::new(InnerField::new(
|
|
|
|
self.payload.clone(), self.boundary.clone(), &headers)?));
|
|
|
|
self.item = InnerMultipartItem::Field(Rc::clone(&field));
|
2017-10-19 08:43:50 +02:00
|
|
|
|
2017-10-20 01:22:21 +02:00
|
|
|
Ok(Async::Ready(Some(
|
|
|
|
MultipartItem::Field(
|
|
|
|
Field::new(safety.clone(), headers, mt, field)))))
|
|
|
|
}
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 01:22:21 +02:00
|
|
|
impl Drop for InnerMultipart {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// InnerMultipartItem::Field has to be dropped first because of Safety.
|
|
|
|
self.item = InnerMultipartItem::None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-19 08:43:50 +02:00
|
|
|
/// A single field in a multipart stream
|
|
|
|
pub struct Field {
|
|
|
|
ct: mime::Mime,
|
|
|
|
headers: HeaderMap,
|
|
|
|
inner: Rc<RefCell<InnerField>>,
|
|
|
|
safety: Safety,
|
|
|
|
}
|
|
|
|
|
2017-10-20 01:22:21 +02:00
|
|
|
/// A field's chunk
|
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
pub struct FieldChunk(pub Bytes);
|
|
|
|
|
2017-10-19 08:43:50 +02:00
|
|
|
impl Field {
|
|
|
|
|
2017-10-20 01:22:21 +02:00
|
|
|
fn new(safety: Safety, headers: HeaderMap,
|
|
|
|
ct: mime::Mime, inner: Rc<RefCell<InnerField>>) -> Self {
|
2017-10-19 08:43:50 +02:00
|
|
|
Field {
|
2017-10-20 01:22:21 +02:00
|
|
|
ct: ct,
|
2017-10-19 08:43:50 +02:00
|
|
|
headers: headers,
|
|
|
|
inner: inner,
|
|
|
|
safety: safety,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn headers(&self) -> &HeaderMap {
|
|
|
|
&self.headers
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn content_type(&self) -> &mime::Mime {
|
|
|
|
&self.ct
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stream for Field {
|
2017-10-20 01:22:21 +02:00
|
|
|
type Item = FieldChunk;
|
|
|
|
type Error = MultipartError;
|
2017-10-19 08:43:50 +02:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
|
|
|
if self.safety.current() {
|
|
|
|
self.inner.borrow_mut().poll(&self.safety)
|
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Field {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let res = write!(f, "\nMultipartField: {}\n", self.ct);
|
|
|
|
let _ = write!(f, " boundary: {}\n", self.inner.borrow().boundary);
|
|
|
|
let _ = write!(f, " headers:\n");
|
|
|
|
for key in self.headers.keys() {
|
|
|
|
let vals: Vec<_> = self.headers.get_all(key).iter().collect();
|
|
|
|
if vals.len() > 1 {
|
|
|
|
let _ = write!(f, " {:?}: {:?}\n", key, vals);
|
|
|
|
} else {
|
|
|
|
let _ = write!(f, " {:?}: {:?}\n", key, vals[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct InnerField {
|
|
|
|
payload: Option<PayloadRef>,
|
|
|
|
boundary: String,
|
|
|
|
eof: bool,
|
|
|
|
length: Option<u64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InnerField {
|
|
|
|
|
|
|
|
fn new(payload: PayloadRef, boundary: String, headers: &HeaderMap)
|
|
|
|
-> Result<InnerField, PayloadError>
|
|
|
|
{
|
|
|
|
let len = if let Some(len) = headers.get(header::CONTENT_LENGTH) {
|
|
|
|
if let Ok(s) = len.to_str() {
|
|
|
|
if let Ok(len) = s.parse::<u64>() {
|
|
|
|
Some(len)
|
|
|
|
} else {
|
|
|
|
return Err(PayloadError::Incomplete)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(PayloadError::Incomplete)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(InnerField {
|
|
|
|
payload: Some(payload),
|
|
|
|
boundary: boundary,
|
|
|
|
eof: false,
|
|
|
|
length: len })
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads body part content chunk of the specified size.
|
|
|
|
/// The body part must has `Content-Length` header with proper value.
|
2017-10-20 01:22:21 +02:00
|
|
|
fn read_len(payload: &mut Payload, size: &mut u64) -> Poll<Option<Bytes>, MultipartError>
|
2017-10-19 08:43:50 +02:00
|
|
|
{
|
|
|
|
if *size == 0 {
|
|
|
|
Ok(Async::Ready(None))
|
|
|
|
} else {
|
2017-12-19 09:18:57 +01:00
|
|
|
match payload.readany().poll() {
|
2017-10-27 08:14:33 +02:00
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
|
|
Ok(Async::Ready(None)) => Ok(Async::Ready(None)),
|
|
|
|
Ok(Async::Ready(Some(mut chunk))) => {
|
2017-12-19 09:18:57 +01:00
|
|
|
let len = cmp::min(chunk.len() as u64, *size);
|
2017-10-19 08:43:50 +02:00
|
|
|
*size -= len;
|
2017-12-19 09:18:57 +01:00
|
|
|
let ch = chunk.split_to(len as usize);
|
|
|
|
if !chunk.is_empty() {
|
|
|
|
payload.unread_data(chunk);
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
Ok(Async::Ready(Some(ch)))
|
|
|
|
},
|
2017-10-27 08:14:33 +02:00
|
|
|
Err(err) => Err(err.into())
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads content chunk of body part with unknown length.
|
|
|
|
/// The `Content-Length` header for body part is not necessary.
|
2017-10-20 01:22:21 +02:00
|
|
|
fn read_stream(payload: &mut Payload, boundary: &str) -> Poll<Option<Bytes>, MultipartError>
|
2017-10-19 08:43:50 +02:00
|
|
|
{
|
2017-12-19 09:18:57 +01:00
|
|
|
match payload.readuntil(b"\r").poll()? {
|
2017-10-19 08:43:50 +02:00
|
|
|
Async::NotReady => Ok(Async::NotReady),
|
|
|
|
Async::Ready(mut chunk) => {
|
|
|
|
if chunk.len() == 1 {
|
|
|
|
payload.unread_data(chunk);
|
2017-12-19 09:18:57 +01:00
|
|
|
match payload.readexactly(boundary.len() + 4).poll()? {
|
2017-10-19 08:43:50 +02:00
|
|
|
Async::NotReady => Ok(Async::NotReady),
|
|
|
|
Async::Ready(chunk) => {
|
|
|
|
if &chunk[..2] == b"\r\n" && &chunk[2..4] == b"--" &&
|
|
|
|
&chunk[4..] == boundary.as_bytes()
|
|
|
|
{
|
|
|
|
payload.unread_data(chunk);
|
|
|
|
Ok(Async::Ready(None))
|
|
|
|
} else {
|
|
|
|
Ok(Async::Ready(Some(chunk)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let to = chunk.len() - 1;
|
|
|
|
let ch = chunk.split_to(to);
|
|
|
|
payload.unread_data(chunk);
|
|
|
|
Ok(Async::Ready(Some(ch)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 01:22:21 +02:00
|
|
|
fn poll(&mut self, s: &Safety) -> Poll<Option<FieldChunk>, MultipartError> {
|
2017-10-19 08:43:50 +02:00
|
|
|
if self.payload.is_none() {
|
|
|
|
return Ok(Async::Ready(None))
|
|
|
|
}
|
|
|
|
if self.eof {
|
|
|
|
if let Some(payload) = self.payload.as_ref().unwrap().get_mut(s) {
|
2017-12-19 09:18:57 +01:00
|
|
|
match payload.readline().poll()? {
|
2017-10-19 08:43:50 +02:00
|
|
|
Async::NotReady =>
|
|
|
|
return Ok(Async::NotReady),
|
|
|
|
Async::Ready(chunk) => {
|
|
|
|
assert_eq!(
|
|
|
|
chunk.as_ref(), b"\r\n",
|
|
|
|
"reader did not read all the data or it is malformed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Ok(Async::NotReady);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.payload.take();
|
|
|
|
return Ok(Async::Ready(None))
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = if let Some(payload) = self.payload.as_ref().unwrap().get_mut(s) {
|
|
|
|
let res = if let Some(ref mut len) = self.length {
|
|
|
|
InnerField::read_len(payload, len)?
|
|
|
|
} else {
|
|
|
|
InnerField::read_stream(payload, &self.boundary)?
|
|
|
|
};
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Async::NotReady => Async::NotReady,
|
2017-10-20 01:22:21 +02:00
|
|
|
Async::Ready(Some(bytes)) => Async::Ready(Some(FieldChunk(bytes))),
|
2017-10-19 08:43:50 +02:00
|
|
|
Async::Ready(None) => {
|
|
|
|
self.eof = true;
|
2017-12-19 09:18:57 +01:00
|
|
|
match payload.readline().poll()? {
|
2017-10-19 08:43:50 +02:00
|
|
|
Async::NotReady => Async::NotReady,
|
|
|
|
Async::Ready(chunk) => {
|
|
|
|
assert_eq!(
|
|
|
|
chunk.as_ref(), b"\r\n",
|
|
|
|
"reader did not read all the data or it is malformed");
|
|
|
|
Async::Ready(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Async::NotReady
|
|
|
|
};
|
|
|
|
|
|
|
|
if Async::Ready(None) == result {
|
|
|
|
self.payload.take();
|
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct PayloadRef {
|
|
|
|
task: Option<Task>,
|
|
|
|
payload: Rc<Payload>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PayloadRef {
|
|
|
|
fn new(payload: Payload) -> PayloadRef {
|
|
|
|
PayloadRef {
|
|
|
|
task: None,
|
|
|
|
payload: Rc::new(payload),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_mut<'a, 'b>(&'a self, s: &'b Safety) -> Option<&'a mut Payload>
|
|
|
|
where 'a: 'b
|
|
|
|
{
|
|
|
|
if s.current() {
|
|
|
|
let payload: &mut Payload = unsafe {
|
|
|
|
&mut *(self.payload.as_ref() as *const _ as *mut _)};
|
|
|
|
Some(payload)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for PayloadRef {
|
|
|
|
fn clone(&self) -> PayloadRef {
|
|
|
|
PayloadRef {
|
|
|
|
task: Some(current_task()),
|
|
|
|
payload: Rc::clone(&self.payload),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Counter. It tracks of number of clones of payloads and give access to payload only
|
|
|
|
/// to top most task panics if Safety get destroyed and it not top most task.
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Safety {
|
|
|
|
task: Option<Task>,
|
|
|
|
level: usize,
|
|
|
|
payload: Rc<PhantomData<bool>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Safety {
|
|
|
|
fn new() -> Safety {
|
|
|
|
let payload = Rc::new(PhantomData);
|
|
|
|
Safety {
|
|
|
|
task: None,
|
|
|
|
level: Rc::strong_count(&payload),
|
|
|
|
payload: payload,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current(&self) -> bool {
|
|
|
|
Rc::strong_count(&self.payload) == self.level
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for Safety {
|
|
|
|
fn clone(&self) -> Safety {
|
|
|
|
let payload = Rc::clone(&self.payload);
|
|
|
|
Safety {
|
|
|
|
task: Some(current_task()),
|
|
|
|
level: Rc::strong_count(&payload),
|
|
|
|
payload: payload,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Safety {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// parent task is dead
|
|
|
|
if Rc::strong_count(&self.payload) != self.level {
|
|
|
|
panic!("Safety get dropped but it is not from top-most task");
|
|
|
|
}
|
|
|
|
if let Some(task) = self.task.take() {
|
|
|
|
task.notify()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-22 18:30:05 +02:00
|
|
|
|
2017-10-23 06:40:41 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use bytes::Bytes;
|
|
|
|
use futures::future::{lazy, result};
|
|
|
|
use tokio_core::reactor::Core;
|
2017-11-06 10:24:49 +01:00
|
|
|
use payload::{Payload, PayloadWriter};
|
2017-10-23 06:40:41 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_boundary() {
|
|
|
|
let headers = HeaderMap::new();
|
|
|
|
match Multipart::boundary(&headers) {
|
|
|
|
Err(MultipartError::NoContentType) => (),
|
|
|
|
_ => panic!("should not happen"),
|
|
|
|
}
|
2017-10-22 18:30:05 +02:00
|
|
|
|
2017-10-23 06:40:41 +02:00
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert(header::CONTENT_TYPE,
|
|
|
|
header::HeaderValue::from_static("test"));
|
2017-10-22 18:30:05 +02:00
|
|
|
|
2017-10-23 06:40:41 +02:00
|
|
|
match Multipart::boundary(&headers) {
|
|
|
|
Err(MultipartError::ParseContentType) => (),
|
|
|
|
_ => panic!("should not happen"),
|
|
|
|
}
|
2017-10-22 18:30:05 +02:00
|
|
|
|
2017-10-23 06:40:41 +02:00
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert(
|
|
|
|
header::CONTENT_TYPE,
|
|
|
|
header::HeaderValue::from_static("multipart/mixed"));
|
|
|
|
match Multipart::boundary(&headers) {
|
|
|
|
Err(MultipartError::Boundary) => (),
|
|
|
|
_ => panic!("should not happen"),
|
|
|
|
}
|
2017-10-22 18:45:53 +02:00
|
|
|
|
2017-10-23 06:40:41 +02:00
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert(
|
|
|
|
header::CONTENT_TYPE,
|
|
|
|
header::HeaderValue::from_static(
|
|
|
|
"multipart/mixed; boundary=\"5c02368e880e436dab70ed54e1c58209\""));
|
|
|
|
|
|
|
|
assert_eq!(Multipart::boundary(&headers).unwrap(),
|
|
|
|
"5c02368e880e436dab70ed54e1c58209");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multipart() {
|
|
|
|
Core::new().unwrap().run(lazy(|| {
|
|
|
|
let (mut sender, payload) = Payload::new(false);
|
|
|
|
|
|
|
|
let bytes = Bytes::from(
|
2017-10-23 07:23:38 +02:00
|
|
|
"testasdadsad\r\n\
|
|
|
|
--abbc761f78ff4d7cb7573b5a23f96ef0\r\n\
|
2017-10-23 06:40:41 +02:00
|
|
|
Content-Type: text/plain; charset=utf-8\r\nContent-Length: 4\r\n\r\n\
|
|
|
|
test\r\n\
|
2017-10-23 07:23:38 +02:00
|
|
|
--abbc761f78ff4d7cb7573b5a23f96ef0\r\n\
|
|
|
|
Content-Type: text/plain; charset=utf-8\r\nContent-Length: 4\r\n\r\n\
|
|
|
|
data\r\n\
|
2017-10-23 06:40:41 +02:00
|
|
|
--abbc761f78ff4d7cb7573b5a23f96ef0--\r\n");
|
|
|
|
sender.feed_data(bytes);
|
|
|
|
|
|
|
|
let mut multipart = Multipart::new(
|
|
|
|
"abbc761f78ff4d7cb7573b5a23f96ef0".to_owned(), payload);
|
|
|
|
match multipart.poll() {
|
|
|
|
Ok(Async::Ready(Some(item))) => {
|
|
|
|
match item {
|
|
|
|
MultipartItem::Field(mut field) => {
|
|
|
|
assert_eq!(field.content_type().type_(), mime::TEXT);
|
|
|
|
assert_eq!(field.content_type().subtype(), mime::PLAIN);
|
|
|
|
|
|
|
|
match field.poll() {
|
|
|
|
Ok(Async::Ready(Some(chunk))) =>
|
|
|
|
assert_eq!(chunk.0, "test"),
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
match field.poll() {
|
|
|
|
Ok(Async::Ready(None)) => (),
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
2017-10-23 07:23:38 +02:00
|
|
|
|
|
|
|
match multipart.poll() {
|
|
|
|
Ok(Async::Ready(Some(item))) => {
|
|
|
|
match item {
|
|
|
|
MultipartItem::Field(mut field) => {
|
|
|
|
assert_eq!(field.content_type().type_(), mime::TEXT);
|
|
|
|
assert_eq!(field.content_type().subtype(), mime::PLAIN);
|
|
|
|
|
|
|
|
match field.poll() {
|
|
|
|
Ok(Async::Ready(Some(chunk))) =>
|
|
|
|
assert_eq!(chunk.0, "data"),
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
match field.poll() {
|
|
|
|
Ok(Async::Ready(None)) => (),
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
|
2017-10-23 06:40:41 +02:00
|
|
|
match multipart.poll() {
|
|
|
|
Ok(Async::Ready(None)) => (),
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
2017-10-22 18:30:05 +02:00
|
|
|
|
2017-10-23 06:40:41 +02:00
|
|
|
let res: Result<(), ()> = Ok(());
|
|
|
|
result(res)
|
|
|
|
})).unwrap();
|
|
|
|
}
|
2017-10-22 18:30:05 +02:00
|
|
|
}
|