diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-02-03 04:19:28 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-02-03 04:19:28 +0300 |
commit | 30f229be4c0c3b8458244da44c37cc0485a875b2 (patch) | |
tree | 44c47024b98f9bd959ef221d30fe1000cc67e154 /lib/gitlab/buffered_io.rb | |
parent | 33bbd0b39bda02f24468184e5e3d8f6d257246d4 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/buffered_io.rb')
-rw-r--r-- | lib/gitlab/buffered_io.rb | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/gitlab/buffered_io.rb b/lib/gitlab/buffered_io.rb new file mode 100644 index 00000000000..a2f1cf42b04 --- /dev/null +++ b/lib/gitlab/buffered_io.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +module Gitlab + # Net::BufferedIO is overwritten by webmock but in order to test this class, it needs to inherit from the original BufferedIO. + # https://github.com/bblimke/webmock/blob/867f4b290fd133658aa9530cba4ba8b8c52c0d35/lib/webmock/http_lib_adapters/net_http.rb#L266 + parent_class = if const_defined?('WebMock::HttpLibAdapters::NetHttpAdapter::OriginalNetBufferedIO') && Rails.env.test? + WebMock::HttpLibAdapters::NetHttpAdapter::OriginalNetBufferedIO + else + Net::BufferedIO + end + + class BufferedIo < parent_class + extend ::Gitlab::Utils::Override + + HEADER_READ_TIMEOUT = 20 + + # rubocop: disable Style/RedundantReturn + # rubocop: disable Cop/LineBreakAfterGuardClauses + # rubocop: disable Layout/EmptyLineAfterGuardClause + + # Original method: + # https://github.com/ruby/ruby/blob/cdb7d699d0641e8f081d590d06d07887ac09961f/lib/net/protocol.rb#L190-L200 + override :readuntil + def readuntil(terminator, ignore_eof = false) + start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) + check_timeout = Feature.enabled?(:header_read_timeout_buffered_io) + + begin + until idx = @rbuf.index(terminator) + if check_timeout && (elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) > HEADER_READ_TIMEOUT + raise Gitlab::HTTP::HeaderReadTimeout, "Request timed out after reading headers for #{elapsed} seconds" + end + + rbuf_fill + end + + return rbuf_consume(idx + terminator.size) + rescue EOFError + raise unless ignore_eof + return rbuf_consume(@rbuf.size) + end + end + # rubocop: enable Style/RedundantReturn + # rubocop: enable Cop/LineBreakAfterGuardClauses + # rubocop: enable Layout/EmptyLineAfterGuardClause + end +end |