Skip to content

Commit

Permalink
Merge branch 'release/v1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rousan committed May 20, 2020
2 parents 77485b2 + 0c48547 commit 0d7157a
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "multer"
version = "1.1.1"
version = "1.2.0"
description = "An async parser for `multipart/form-data` content-type in Rust."
homepage = "https://github.com/rousan/multer-rs"
repository = "https://github.com/rousan/multer-rs"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
multer = "1.1"
multer = "1.2"
```

# Basic Example
Expand Down
8 changes: 4 additions & 4 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ pub(crate) struct StreamBuffer {
pub(crate) eof: bool,
pub(crate) buf: BytesMut,
pub(crate) stream: Pin<Box<dyn Stream<Item = Result<Bytes, crate::Error>> + Send>>,
pub(crate) whole_stream_size_limit: usize,
pub(crate) stream_size_counter: usize,
pub(crate) whole_stream_size_limit: u64,
pub(crate) stream_size_counter: u64,
}

impl StreamBuffer {
pub fn new<S>(stream: S, whole_stream_size_limit: usize) -> Self
pub fn new<S>(stream: S, whole_stream_size_limit: u64) -> Self
where
S: Stream<Item = Result<Bytes, crate::Error>> + Send + 'static,
{
Expand All @@ -34,7 +34,7 @@ impl StreamBuffer {
loop {
match self.stream.as_mut().poll_next(cx) {
Poll::Ready(Some(Ok(data))) => {
self.stream_size_counter += data.len();
self.stream_size_counter += data.len() as u64;

if self.stream_size_counter > self.whole_stream_size_limit {
return Err(crate::Error::StreamSizeExceeded {
Expand Down
4 changes: 2 additions & 2 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use lazy_static::lazy_static;
use regex::Regex;

pub(crate) const DEFAULT_WHOLE_STREAM_SIZE_LIMIT: usize = usize::MAX;
pub(crate) const DEFAULT_PER_FIELD_SIZE_LIMIT: usize = usize::MAX;
pub(crate) const DEFAULT_WHOLE_STREAM_SIZE_LIMIT: u64 = u64::MAX;
pub(crate) const DEFAULT_PER_FIELD_SIZE_LIMIT: u64 = u64::MAX;

pub(crate) const MAX_HEADERS: usize = 32;
pub(crate) const BOUNDARY_EXT: &'static str = "--";
Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ pub enum Error {
limit,
"field_name.as_deref().unwrap_or(\"<unknown>\")"
)]
FieldSizeExceeded { limit: usize, field_name: Option<String> },
FieldSizeExceeded { limit: u64, field_name: Option<String> },

/// The incoming stream size exceeded the maximum limit.
#[display(fmt = "Stream size exceeded the maximum limit: {} bytes", limit)]
StreamSizeExceeded { limit: usize },
StreamSizeExceeded { limit: u64 },

/// Stream read failed.
#[display(fmt = "Stream read failed: {}", _0)]
Expand Down
2 changes: 1 addition & 1 deletion src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl Stream for Field {

match stream_buffer.read_field_data(state.boundary.as_str(), state.curr_field_name.as_deref()) {
Ok(Some((done, bytes))) => {
state.curr_field_size_counter += bytes.len();
state.curr_field_size_counter += bytes.len() as u64;

if state.curr_field_size_counter > state.curr_field_size_limit {
return Poll::Ready(Some(Err(crate::Error::FieldSizeExceeded {
Expand Down
2 changes: 1 addition & 1 deletion src/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ impl Stream for Multipart {
if state.stage == StreamingStage::CleaningPrevFieldData {
match stream_buffer.read_field_data(state.boundary.as_str(), state.curr_field_name.as_deref()) {
Ok(Some((done, bytes))) => {
state.curr_field_size_counter += bytes.len();
state.curr_field_size_counter += bytes.len() as u64;

if state.curr_field_size_counter > state.curr_field_size_limit {
return Poll::Ready(Some(Err(crate::Error::FieldSizeExceeded {
Expand Down
16 changes: 8 additions & 8 deletions src/size_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ use std::collections::HashMap;
///
/// Please refer [`Constraints`](./struct.Constraints.html) for more info.
pub struct SizeLimit {
pub(crate) whole_stream: usize,
pub(crate) per_field: usize,
pub(crate) field_map: HashMap<String, usize>,
pub(crate) whole_stream: u64,
pub(crate) per_field: u64,
pub(crate) field_map: HashMap<String, u64>,
}

impl SizeLimit {
/// Creates a default size limit which is [`usize::MAX`](https://doc.rust-lang.org/stable/std/primitive.usize.html#associatedconstant.MAX) for the whole stream
/// Creates a default size limit which is [`u64::MAX`](https://doc.rust-lang.org/stable/std/primitive.u64.html#associatedconstant.MAX) for the whole stream
/// and for each field.
pub fn new() -> SizeLimit {
SizeLimit::default()
}

/// Sets size limit for the whole stream.
pub fn whole_stream(mut self, limit: usize) -> SizeLimit {
pub fn whole_stream(mut self, limit: u64) -> SizeLimit {
self.whole_stream = limit;
self
}

/// Sets size limit for each field.
pub fn per_field(mut self, limit: usize) -> SizeLimit {
pub fn per_field(mut self, limit: u64) -> SizeLimit {
self.per_field = limit;
self
}
Expand All @@ -33,12 +33,12 @@ impl SizeLimit {
///
/// It is useful when you want to set a size limit on a textual field which will be stored in memory
/// to avoid potential `DDoS attack` from attackers running the server out of memory.
pub fn for_field<N: Into<String>>(mut self, field_name: N, limit: usize) -> SizeLimit {
pub fn for_field<N: Into<String>>(mut self, field_name: N, limit: u64) -> SizeLimit {
self.field_map.insert(field_name.into(), limit);
self
}

pub(crate) fn extract_size_limit_for(&self, field: Option<&str>) -> usize {
pub(crate) fn extract_size_limit_for(&self, field: Option<&str>) -> u64 {
field
.and_then(|field| self.field_map.get(&field.to_owned()))
.copied()
Expand Down
4 changes: 2 additions & 2 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub(crate) struct MultipartState {
pub(crate) next_field_waker: Option<Waker>,
pub(crate) next_field_idx: usize,
pub(crate) curr_field_name: Option<String>,
pub(crate) curr_field_size_limit: usize,
pub(crate) curr_field_size_counter: usize,
pub(crate) curr_field_size_limit: u64,
pub(crate) curr_field_size_counter: u64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down

0 comments on commit 0d7157a

Please sign in to comment.