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

allow overriding the rack multipart parser tempfile class #639

Closed
wants to merge 2 commits into from

Conversation

codekitchen
Copy link
Contributor

This allows for more flexibility in how to buffer (or stream) multipart file uploads, rather than always using Tempfile and buffering to local TMPDIR.

Also allow a custom buffering size, to optimize large file reads.

Our initial use case for this is to use it in combination with Passenger's new PassengerBufferUpload feature in order to allow a Rails action to stream multipart file uploads to S3 as they are being uploaded by the client, with absolutely no local copy stored at all.

Our initial solution was to make a custom Request subclass that duplicated the Rack::Multipart::Parser code just to change this single Tempfile.new line, but the functionality seems generally useful so I decided to do a pull request.

Let me know if using these env variables is the best way to pass this information to the parser, it seemed like the most flexible and least invasive solution.

@evanphx
Copy link
Contributor

evanphx commented Apr 4, 2014

This seems fine. We should merge this @raggi .

@spastorino
Copy link
Contributor

Seems good, I will merge it. But let's wait first a little while for @raggi to review it since he was pinged :).

@@ -209,7 +214,7 @@ def get_data(filename, body, content_type, name, head)
# filename is blank which means no file has been selected
return
elsif filename
body.rewind
body.rewind if body.respond_to?(:rewind)
Copy link
Member

Choose a reason for hiding this comment

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

Does the spec currently allow body to not respond to rewind?

I've always had two minds about enforcing rewind in the spec as it adds hard requirements for file buffering or hard bounds from memory pressure - opening up a large class of issues for users. It's also a problem for integration in sandboxed environments, particularly those that would prefer to have read only filesystems and so on.

What is even more painful is that many FEs are already buffering to disk, and so we're pumping this data in and out of the kernel in manners all the time. I'd love for this to be better.

Anyway, discourse over - back to the point: can you check for me on the state of this with regard to SPEC, and if you want to propose a change there we should discuss.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Keep in mind that body here isn't the request body, it's the Tempfile (or with this change, the custom class) created above. So it's purely a stream internal to Rack::Multipart::Parser. As far as I can see, that's not touched on by the rack spec at all.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, yes, we're over here. Apologies, I was thinking of the request body rules and only glancing.

@raggi
Copy link
Member

raggi commented Apr 4, 2014

I like this in principle, want to check the SPEC details to be clear on what we're affecting, and then happy to merge.

@raggi
Copy link
Member

raggi commented Apr 5, 2014

Note to commiter: we need to add this to SPEC as an optional server feature.

@codekitchen
Copy link
Contributor Author

@raggi I took a stab at implementing the SPEC and lint changes just based on the surrounding code in those files. Let me know if anything needs tweaking, or if I'm barking up entirely the wrong tree.

@codekitchen
Copy link
Contributor Author

@raggi any thoughts on that last addition?

@raggi raggi added this to the Rack 1.6 milestone Jul 6, 2014
@raggi
Copy link
Member

raggi commented Aug 3, 2014

Would you mind rebasing against master?

This allows for more flexibility in how to buffer (or stream) multipart
file uploads, rather than always using Tempfile and buffering to local
TMPDIR.
@codekitchen
Copy link
Contributor Author

@raggi: rebased and the travis build is passing

spastorino added a commit that referenced this pull request Nov 27, 2014
@raggi
Copy link
Member

raggi commented Nov 27, 2014

Thank you all!

@codekitchen
Copy link
Contributor Author

oh sweet, thanks

@raggi
Copy link
Member

raggi commented Nov 28, 2014

no, no, thank you! ❤️

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

4 participants