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

fix sendfile #1463

Closed
wants to merge 1 commit into from
Closed

fix sendfile #1463

wants to merge 1 commit into from

Conversation

ahorek
Copy link
Contributor

@ahorek ahorek commented Jan 10, 2020

body.respond_to?(:to_ary) was returning false, but after this change, it returns true 72959eb

now the body is wrapped to a proxy here
https://github.com/rack/rack/blob/master/lib/rack/content_length.rb#L23

the wrapped body doesn't respond_to :to_path anymore
https://github.com/rack/rack/blob/master/lib/rack/sendfile.rb#L114

and the final response doesn't contain 'X-Sendfile' header as before
https://github.com/rack/rack/blob/master/lib/rack/sendfile.rb#L129

this change fixes the problem, but I've absolutely no idea if it's correct or not :)
I have to investigate more what's happening, is it a regression or is it intentional?

@ioquatix
Copy link
Member

The first thing we need to do here is make a failing spec, can you have a go at implementing that?

@ahorek
Copy link
Contributor Author

ahorek commented Jan 11, 2020

it turns out it's a middleware ordering problem

this line caused the failing behavior

config.middleware.insert_after Rack::Sendfile, Rack::ContentLength

Rack::ContentLength middleware discards the original body, so using Rack::Sendfile after that have no effect

unfortunately, there're no tests for combination of multiple middlewares in rack, but there's one in rails
https://github.com/rails/rails/blob/b2eb1d1c55a59fee1e6c4cba7030d8ceb524267c/railties/test/application/middleware/sendfile_test.rb#L36

If I read the documentation correctly Rack::Sendfile should be called at the top of the stack. So it's an application problem.

@ahorek ahorek closed this Jan 11, 2020
@ahorek
Copy link
Contributor Author

ahorek commented Jan 11, 2020

this change fixes the problem

config.middleware.insert_before Rack::Sendfile, Rack::ContentLength

@ioquatix could you confirm, that the order of Rack::Sendfile and Rack::ContentLength should matter?

@ioquatix
Copy link
Member

It matters.

But I also think the ContentLength middleware is junk and should not be needed.

Maybe we can modify ContentLength to not clobber SendFile or fix SendFile to include file size (if it doesn’t already) so as to not require buffering to compute content length.

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