debian-mirror-gitlab/config/initializers/puma_client_tempfile_patch.rb

102 lines
2.7 KiB
Ruby
Raw Normal View History

2021-04-29 21:17:54 +05:30
# frozen_string_literal: true
if Gitlab::Runtime.puma?
2021-06-08 01:23:25 +05:30
# This patch represents https://github.com/puma/puma/pull/2613. If
# this PR is accepted in the next Puma release, we can remove this
# entire file.
#
# The patch itself is quite large because the tempfile creation in
# Puma is inside these fairly long methods. The actual changes are
# just two lines, commented with 'GitLab' to make them easier to find.
2021-04-29 21:17:54 +05:30
raise "Remove this monkey patch: #{__FILE__}" unless Puma::Const::VERSION == '5.1.1'
2021-06-08 01:23:25 +05:30
module Puma
class Client
private
def setup_body
@body_read_start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
if @env[HTTP_EXPECT] == CONTINUE
# TODO allow a hook here to check the headers before
# going forward
@io << HTTP_11_100
@io.flush
end
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
@read_header = false
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
body = @parser.body
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
te = @env[TRANSFER_ENCODING2]
if te
if te.include?(",")
te.split(",").each do |part|
if CHUNKED.casecmp(part.strip) == 0 # rubocop:disable Metrics/BlockNesting
return setup_chunked_body(body)
2021-04-29 21:17:54 +05:30
end
end
2021-06-08 01:23:25 +05:30
elsif CHUNKED.casecmp(te) == 0
return setup_chunked_body(body)
2021-04-29 21:17:54 +05:30
end
2021-06-08 01:23:25 +05:30
end
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
@chunked_body = false
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
cl = @env[CONTENT_LENGTH]
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
unless cl
@buffer = body.empty? ? nil : body
@body = EmptyBody
set_ready
return true
end
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
remain = cl.to_i - body.bytesize
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
if remain <= 0
@body = StringIO.new(body)
@buffer = nil
set_ready
return true
end
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
if remain > MAX_BODY
@body = Tempfile.new(Const::PUMA_TMP_BASE)
@body.binmode
@body.unlink # GitLab: this is the changed part
@tempfile = @body
else
# The body[0,0] trick is to get an empty string in the same
# encoding as body.
@body = StringIO.new body[0,0] # rubocop:disable Layout/SpaceAfterComma
end
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
@body.write body
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
@body_remain = remain
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
return false # rubocop:disable Style/RedundantReturn
end
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
def setup_chunked_body(body)
@chunked_body = true
@partial_part_left = 0
@prev_chunk = ""
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
@body = Tempfile.new(Const::PUMA_TMP_BASE)
@body.binmode
@body.unlink # GitLab: this is the changed part
@tempfile = @body
@chunked_content_length = 0
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
if decode_chunk(body)
@env[CONTENT_LENGTH] = @chunked_content_length
return true # rubocop:disable Style/RedundantReturn
2021-04-29 21:17:54 +05:30
end
end
end
end
end