summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Hawthorn <john@hawthorn.email>2023-03-12 23:11:30 -0700
committerGitHub <noreply@github.com>2023-03-13 19:11:30 +1300
commit0b45107cb717c6e1c13f01f908f5c7f2b095fe57 (patch)
treef533bb4e54bb97faa275bd428b1c420e83b8ff79
parent59d9ba903fdb50cf8db708c8263a7b2a79de83fb (diff)
downloadrack-0b45107cb717c6e1c13f01f908f5c7f2b095fe57.tar.gz
Avoid rebuilding regex (#2042)
Previously we would rebuild this regex once per-part. We can instead compile it once per-request.
-rw-r--r--lib/rack/multipart/parser.rb3
1 files changed, 2 insertions, 1 deletions
diff --git a/lib/rack/multipart/parser.rb b/lib/rack/multipart/parser.rb
index 36a68f29..c856b68b 100644
--- a/lib/rack/multipart/parser.rb
+++ b/lib/rack/multipart/parser.rb
@@ -225,6 +225,7 @@ module Rack
@sbuf = StringScanner.new("".dup)
@body_regex = /(?:#{EOL}|\A)--#{Regexp.quote(boundary)}(?:#{EOL}|--)/m
+ @body_regex_at_end = /#{@body_regex}\z/m
@rx_max_size = boundary.bytesize + 6 # (\r\n-- at start, either \r\n or -- at finish)
@head_regex = /(.*?#{EOL})#{EOL}/m
end
@@ -334,7 +335,7 @@ module Rack
def handle_mime_body
if (body_with_boundary = @sbuf.check_until(@body_regex)) # check but do not advance the pointer yet
- body = body_with_boundary.sub(/#{@body_regex}\z/m, '') # remove the boundary from the string
+ body = body_with_boundary.sub(@body_regex_at_end, '') # remove the boundary from the string
@collector.on_mime_body @mime_index, body
@sbuf.pos += body.length + 2 # skip \r\n after the content
@state = :CONSUME_TOKEN