GithubHelp home page GithubHelp logo

Comments (2)

jaysonsantos avatar jaysonsantos commented on June 26, 2024

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.

SergioBenitez avatar SergioBenitez commented on June 26, 2024

@jaysonsantos Did you see #47 (comment)?

from multer-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.