Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement multipart with multer #1033

Merged
merged 9 commits into from Apr 18, 2023

Conversation

jaysonsantos
Copy link
Contributor

@jaysonsantos jaysonsantos commented Mar 31, 2023

This is a continuation of #846

@seanmonstar I've managed to implement the change to use multer without resorting to stream::unfold but, for that I had to implement a change on their project to expose the poll next item function.

diff --git a/src/multipart.rs b/src/multipart.rs
index 7b69dbb..a04e141 100644
--- a/src/multipart.rs
+++ b/src/multipart.rs
@@ -219,21 +219,21 @@ impl<'r> Multipart<'r> {
     /// calling this method or [`Multipart::next_field_with_idx()`] again. See
     /// [field-exclusivity](#field-exclusivity) for details.
     pub async fn next_field(&mut self) -> Result<Option<Field<'r>>> {
+        future::poll_fn(|cx| self.poll_next_field(cx)).await
+    }
+
+    // CORRECTNESS: This method must only be called only when it is guaranteed
+    // that `self.state` is an exlusive `Arc`!
+    pub fn poll_next_field(&mut self, cx: &mut Context<'_>) -> Poll<Result<Option<Field<'r>>>> {
         // This is consistent as we have an `&mut` and `Field` is not `Clone`.
         // Here, we are guaranteeing that the returned `Field` will be the
         // _only_ field with access to the multipart parsing state. This ensure
         // that lock failure can never occur. This is effectively a dynamic
         // version of passing an `&mut` of `self` to the `Field`.
         if Arc::strong_count(&self.state) != 1 {
-            return Err(Error::LockFailure);
+            return Poll::Ready(Err(Error::LockFailure));
         }
 
-        future::poll_fn(|cx| self.poll_next_field(cx)).await
-    }
-
-    // CORRECTNESS: This method must only be called only when it is guaranteed
-    // that `self.state` is an exlusive `Arc`!
-    fn poll_next_field(&mut self, cx: &mut Context<'_>) -> Poll<Result<Option<Field<'r>>>> {
         debug_assert_eq!(Arc::strong_count(&self.state), 1);
         debug_assert!(self.state.try_lock().is_some(), "expected exlusive lock");
         let mut lock = match self.state.try_lock() {

So far, cargo test --features=multipart seems to work fine using that patch.
I will open a PR there and check if it makes sense.

Copy link
Owner

@seanmonstar seanmonstar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for writing this up so quickly! This is all set to go already?

@@ -131,17 +123,18 @@ impl Stream for FormData {
impl Part {
/// Get the name of this part.
pub fn name(&self) -> &str {
&self.headers.name
&self.part.name().unwrap_or("not-set")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multipart fields exist without a name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @seanmonstar, on the code there it points to the docs here https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition which says:

Only the value form-data, as well as the optional directive name and filename, can be used in the HTTP context.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subheading https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#as_a_header_for_a_multipart_body points out:

The first directive is always form-data, and the header must also include a name parameter to identify the relevant field.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True.
Your concern is that if the library is not following the RFC or tat I should put a default that makes more sense on that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way, I will open an issue there to try to clarify. I am not sure if it is optional because it is used in both to represent both the header of HTTP and the boundary

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry that I didn't explain more. I wasn't sure myself. So, I'm thinking that perhaps even if multer doesn't think so yet, we would treat a lack of a name as an error, and thus if the name does exist, we can just use self.part.name().expect("checked for name previously"). What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all good but, the panic is ok there?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, what I was thinking is that constructing a Part would require us to check for a name, and if it doesn't exist, then next_field() would return an Error. We wouldn't want an unchecked panic that a client could trigger, no.

@jaysonsantos
Copy link
Contributor Author

Hey @seanmonstar I guess so at least on my tests it was all good, any idea of edge cases to be tested there?

@jaysonsantos jaysonsantos force-pushed the use-multer-for-multipart branch 2 times, most recently from bd14cd3 to 426c94c Compare April 9, 2023 18:59
Copy link
Owner

@seanmonstar seanmonstar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work, thanks for taking this on!

@seanmonstar seanmonstar merged commit e2f4501 into seanmonstar:master Apr 18, 2023
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants