Skip to content

Commit

Permalink
Handle cancellation of uploads actix#834 actix#736
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed May 12, 2019
1 parent 36d017d commit 2350a2d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
6 changes: 6 additions & 0 deletions actix-multipart/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changes

## [0.1.0-beta.4] - 2019-05-12

* Handle cancellation of uploads #834 #736

* Upgrade to actix-web 1.0.0-beta.4

## [0.1.0-beta.1] - 2019-04-21

* Do not support nested multipart
Expand Down
4 changes: 2 additions & 2 deletions actix-multipart/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "actix-multipart"
version = "0.1.0-beta.1"
version = "0.1.0-beta.4"
authors = ["Nikolay Kim <[email protected]>"]
description = "Multipart support for actix web framework."
readme = "README.md"
Expand All @@ -18,7 +18,7 @@ name = "actix_multipart"
path = "src/lib.rs"

[dependencies]
actix-web = "1.0.0-beta.1"
actix-web = "1.0.0-beta.4"
actix-service = "0.4.0"
bytes = "0.4"
derive_more = "0.14"
Expand Down
3 changes: 3 additions & 0 deletions actix-multipart/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub enum MultipartError {
/// Payload error
#[display(fmt = "{}", _0)]
Payload(PayloadError),
/// Not consumed
#[display(fmt = "Multipart stream is not consumed")]
NotConsumed,
}

/// Return `BadRequest` for `MultipartError`
Expand Down
17 changes: 14 additions & 3 deletions actix-multipart/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Multipart payload support
use std::cell::{RefCell, UnsafeCell};
use std::cell::{Cell, RefCell, UnsafeCell};
use std::marker::PhantomData;
use std::rc::Rc;
use std::{cmp, fmt};
Expand Down Expand Up @@ -116,6 +116,8 @@ impl Stream for Multipart {
payload.poll_stream()?;
}
inner.poll(&self.safety)
} else if !self.safety.is_clean() {
Err(MultipartError::NotConsumed)
} else {
Ok(Async::NotReady)
}
Expand Down Expand Up @@ -415,6 +417,8 @@ impl Stream for Field {
}

inner.poll(&self.safety)
} else if !self.safety.is_clean() {
return Err(MultipartError::NotConsumed);
} else {
Ok(Async::NotReady)
}
Expand Down Expand Up @@ -655,6 +659,7 @@ struct Safety {
task: Option<Task>,
level: usize,
payload: Rc<PhantomData<bool>>,
clean: Rc<Cell<bool>>,
}

impl Safety {
Expand All @@ -663,12 +668,17 @@ impl Safety {
Safety {
task: None,
level: Rc::strong_count(&payload),
clean: Rc::new(Cell::new(true)),
payload,
}
}

fn current(&self) -> bool {
Rc::strong_count(&self.payload) == self.level
Rc::strong_count(&self.payload) == self.level && self.clean.get()
}

fn is_clean(&self) -> bool {
self.clean.get()
}
}

Expand All @@ -678,6 +688,7 @@ impl Clone for Safety {
Safety {
task: Some(current_task()),
level: Rc::strong_count(&payload),
clean: self.clean.clone(),
payload,
}
}
Expand All @@ -687,7 +698,7 @@ 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");
self.clean.set(true);
}
if let Some(task) = self.task.take() {
task.notify()
Expand Down

0 comments on commit 2350a2d

Please sign in to comment.