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

Avoid create multiple large copies of uploaded file data in memory #286

Merged
merged 3 commits into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ Metrics/BlockLength:
- spec/**/*
- rack-test.gemspec

# Rationale: Enforcing maximum module length makes code worse, without exception
Metrics/ModuleLength:
Enabled: false

# Rationale: allow Weirich-style blocks, but do not enforce them.
Style/BlockDelimiters:
Enabled: false
Expand Down
15 changes: 15 additions & 0 deletions lib/rack/test/uploaded_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ def method_missing(method_name, *args, &block) #:nodoc:
tempfile.public_send(method_name, *args, &block)
end

# Append to given buffer in 64K chunks to avoid multiple large
# copies of file data in memory. Rewind tempfile before and
# after to make sure all data in tempfile is appended to the
# buffer.
def append_to(buffer)
tempfile.rewind

buf = String.new
buffer << tempfile.readpartial(65_536, buf) until tempfile.eof?

tempfile.rewind

nil
end

def respond_to_missing?(method_name, include_private = false) #:nodoc:
tempfile.respond_to?(method_name, include_private) || super
end
Expand Down
6 changes: 4 additions & 2 deletions lib/rack/test/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,16 @@ def build_primitive_part(parameter_name, value)

def build_file_part(parameter_name, uploaded_file)
uploaded_file.set_encoding(Encoding::BINARY) if uploaded_file.respond_to?(:set_encoding)
<<-EOF
buffer = String.new
buffer << (<<-EOF)
--#{MULTIPART_BOUNDARY}\r
Content-Disposition: form-data; name="#{parameter_name}"; filename="#{escape_path(uploaded_file.original_filename)}"\r
Content-Type: #{uploaded_file.content_type}\r
Content-Length: #{uploaded_file.size}\r
\r
#{uploaded_file.read}\r
EOF
uploaded_file.append_to(buffer)
buffer << "\r\n"
end
module_function :build_file_part
end
Expand Down
3 changes: 2 additions & 1 deletion spec/rack/test/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
params = Rack::Multipart.parse_multipart(env)
check expect(params['submit-name']).to eq('Larry')
check expect(params['files'][:filename]).to eq('foo.txt')
expect(params['files'][:tempfile].read).to eq("bar\n")
expect(files.pos).to eq(0)
expect(params['files'][:tempfile].read).to eq(files.read)
end

it 'builds multipart bodies from array of files' do
Expand Down