summary refs log tree commit
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2017-07-24 21:07:54 -0400
committerGitHub <noreply@github.com>2017-07-24 21:07:54 -0400
commitc356589d945af46516a9a4fd3c435d0889c99c07 (patch)
tree3b6c21388767555433d08dcea71d296f1483f7c2
parent9899d3ad3ccb9365186d969141a606eae015dd9f (diff)
parente8057a5b9ff08176fda0447eb59cbb38e2bbd45f (diff)
downloadrack-c356589d945af46516a9a4fd3c435d0889c99c07.tar.gz
Merge pull request #1193 from tompng/multipart_less_memory
Reduce memory usage when uploading large file
-rw-r--r--lib/rack/multipart/parser.rb12
1 files changed, 6 insertions, 6 deletions
diff --git a/lib/rack/multipart/parser.rb b/lib/rack/multipart/parser.rb
index cd64d444..c02e26f6 100644
--- a/lib/rack/multipart/parser.rb
+++ b/lib/rack/multipart/parser.rb
@@ -166,16 +166,14 @@ module Rack
 
       def initialize(boundary, tempfile, bufsize, query_parser)
         @buf            = String.new
-        @buf_rx_start   = 0
 
         @query_parser   = query_parser
         @params         = query_parser.make_params
         @boundary       = "--#{boundary}"
-        @boundary_size  = @boundary.bytesize + EOL.size
         @bufsize        = bufsize
 
         @rx = /(?:#{EOL})?#{Regexp.quote(@boundary)}(#{EOL}|--)/n
-        @rx_max_size = EOL.size + @boundary.size + [EOL.size, '--'.size].max
+        @rx_max_size = EOL.size + @boundary.bytesize + [EOL.size, '--'.size].max
         @full_boundary = @boundary
         @end_boundary = @boundary + '--'
         @state = :FAST_FORWARD
@@ -265,15 +263,17 @@ module Rack
       end
 
       def handle_mime_body
-        if i = @buf.index(rx, @buf_rx_start)
+        if i = @buf.index(rx)
           # Save the rest.
           @collector.on_mime_body @mime_index, @buf.slice!(0, i)
           @buf.slice!(0, 2) # Remove \r\n after the content
-          @buf_rx_start = 0 # Reset rx search position
           @state = :CONSUME_TOKEN
           @mime_index += 1
         else
-          @buf_rx_start = [@buf_rx_start, @buf.size - @rx_max_size].max
+          # Save the read body part.
+          if @rx_max_size < @buf.size
+            @collector.on_mime_body @mime_index, @buf.slice!(0, @buf.size - @rx_max_size)
+          end
           :want_read
         end
       end