Comments (2)
This is an option that passes the current tests because they don't seem to cover edge cases:
diff --git a/src/buffer.rs b/src/buffer.rs
index 23f863d..23b0962 100644
--- a/src/buffer.rs
+++ b/src/buffer.rs
@@ -105,56 +105,64 @@ impl<'r> StreamBuffer<'r> {
return Ok(None);
}
- let boundary_deriv = format!("{}{}{}", constants::CRLF, constants::BOUNDARY_EXT, boundary);
- let b_len = boundary_deriv.len();
+ let mut consumed = 0;
+ let mut position = None;
+
+ for needle in [
+ constants::CRLF.as_bytes(),
+ constants::BOUNDARY_EXT.as_bytes(),
+ boundary.as_bytes(),
+ ] {
+ let window = &self.buf[consumed..];
+ consumed += if let Some(pos) = memchr::memmem::find(window, needle) {
+ position = Some(pos);
+ pos
+ } else {
+ position = None;
+ break;
+ };
+ }
- match memchr::memmem::find(&self.buf, boundary_deriv.as_bytes()) {
- Some(idx) => {
- log::trace!("new field found at {}", idx);
- let bytes = self.buf.split_to(idx).freeze();
+ match position {
+ Some(_) => {
+ log::trace!("new field found at {}", consumed);
+ let bytes = self.buf.split_to(consumed - 4).freeze();
// discard \r\n.
self.buf.advance(constants::CRLF.len());
- Ok(Some((true, bytes)))
+ return Ok(Some((true, bytes)));
}
None if self.eof => {
log::trace!("no new field found: EOF. terminating");
- Err(crate::Error::IncompleteFieldData {
+ return Err(crate::Error::IncompleteFieldData {
field_name: field_name.map(|s| s.to_owned()),
- })
- }
- None => {
- let buf_len = self.buf.len();
- let rem_boundary_part_max_len = b_len - 1;
- let rem_boundary_part_idx = if buf_len >= rem_boundary_part_max_len {
- buf_len - rem_boundary_part_max_len
- } else {
- 0
- };
-
- log::trace!("no new field found, not EOF, checking close");
- let bytes = &self.buf[rem_boundary_part_idx..];
- match memchr::memmem::rfind(bytes, constants::CR.as_bytes()) {
- Some(rel_idx) => {
- let idx = rel_idx + rem_boundary_part_idx;
-
- match memchr::memmem::find(boundary_deriv.as_bytes(), &self.buf[idx..]) {
- Some(_) => {
- let bytes = self.buf.split_to(idx).freeze();
-
- match bytes.is_empty() {
- true => Ok(None),
- false => Ok(Some((false, bytes))),
- }
- }
- None => Ok(Some((false, self.read_full_buf()))),
- }
- }
- None => Ok(Some((false, self.read_full_buf()))),
- }
+ });
}
- }
+ _ => {}
+ };
+
+ log::trace!("no new field found, not EOF, checking close");
+ Ok(Some((false, self.read_full_buf())))
+ // These are not covered by tests
+ // match memchr::memmem::rfind(&self.buf, constants::CR.as_bytes()) {
+ // Some(rel_idx) => {
+ // let idx = rel_idx + rem_boundary_part_idx;
+ //
+ // match memchr::memmem::find(boundary_deriv.as_bytes(),
+ // &self.buf[idx..]) { Some(_) => {
+ // let bytes = self.buf.split_to(idx).freeze();
+ //
+ // match bytes.is_empty() {
+ // true => Ok(None),
+ // false => Ok(Some((false, bytes))),
+ // }
+ // }
+ // None => Ok(Some((false, self.read_full_buf()))),
+ // }
+ // }
+ // None => Ok(Some((false, self.read_full_buf()))),
+ // }
}
pub fn read_full_buf(&mut self) -> Bytes {
from multer-rs.
@jaysonsantos Did you see #47 (comment)?
from multer-rs.
Related Issues (20)
- The size limit should be `u64` rather than `usize` type.
- Use std::u64::MAX instead of u64::MAX as u64::MAX depends on rustc 1.43.0 HOT 1
- Unable to support utf8 file name HOT 8
- Support for async-std HOT 2
- "Multipart stream is incomplete" when form data has leading newlines. HOT 7
- Couldn't work with bytes 1.0 HOT 2
- Does this crate work with multipart/mixed ? HOT 1
- Consider allowing whitespaces after the boundary and before the CRLF HOT 1
- New release v2.0.0 ? HOT 6
- Doesn't support 'name=' without double quotes
- Release 2.0.3 HOT 3
- Supporting unquoted name and filename broke semicolons in filenames HOT 1
- Clarify Field.name in relation to the RFC HOT 1
- support `filename*` HOT 3
- Not extracting `file_name` correctly if filename contains double quotes
- reading from stdin? HOT 4
- No release entry for v3.0.0 HOT 2
- StreamBuffer can use unlimied memory if the underlying stream is always ready HOT 3
- Provide accurate error information. HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from multer-rs.