PHP 8.2.31
Preview: rewindable_input.rb Size: 3.19 KB
//proc/thread-self/root/opt/alt/ruby24/lib64/ruby/gems/2.4.0/gems/rack-1.6.4/lib/rack/rewindable_input.rb

# -*- encoding: binary -*-
require 'tempfile'
require 'rack/utils'

module Rack
  # Class which can make any IO object rewindable, including non-rewindable ones. It does
  # this by buffering the data into a tempfile, which is rewindable.
  #
  # rack.input is required to be rewindable, so if your input stream IO is non-rewindable
  # by nature (e.g. a pipe or a socket) then you can wrap it in an object of this class
  # to easily make it rewindable.
  #
  # Don't forget to call #close when you're done. This frees up temporary resources that
  # RewindableInput uses, though it does *not* close the original IO object.
  class RewindableInput
    def initialize(io)
      @io = io
      @rewindable_io = nil
      @unlinked = false
    end

    def gets
      make_rewindable unless @rewindable_io
      @rewindable_io.gets
    end

    def read(*args)
      make_rewindable unless @rewindable_io
      @rewindable_io.read(*args)
    end

    def each(&block)
      make_rewindable unless @rewindable_io
      @rewindable_io.each(&block)
    end

    def rewind
      make_rewindable unless @rewindable_io
      @rewindable_io.rewind
    end

    # Closes this RewindableInput object without closing the originally
    # wrapped IO oject. Cleans up any temporary resources that this RewindableInput
    # has created.
    #
    # This method may be called multiple times. It does nothing on subsequent calls.
    def close
      if @rewindable_io
        if @unlinked
          @rewindable_io.close
        else
          @rewindable_io.close!
        end
        @rewindable_io = nil
      end
    end

    private

    # Ruby's Tempfile class has a bug. Subclass it and fix it.
    class Tempfile < ::Tempfile
      def _close
        @tmpfile.close if @tmpfile
        @data[1] = nil if @data
        @tmpfile = nil
      end
    end

    def make_rewindable
      # Buffer all data into a tempfile. Since this tempfile is private to this
      # RewindableInput object, we chmod it so that nobody else can read or write
      # it. On POSIX filesystems we also unlink the file so that it doesn't
      # even have a file entry on the filesystem anymore, though we can still
      # access it because we have the file handle open.
      @rewindable_io = Tempfile.new('RackRewindableInput')
      @rewindable_io.chmod(0000)
      @rewindable_io.set_encoding(Encoding::BINARY) if @rewindable_io.respond_to?(:set_encoding)
      @rewindable_io.binmode
      if filesystem_has_posix_semantics?
        # Use ::File.unlink as 1.9.1 Tempfile has a bug where unlink closes the file!
        ::File.unlink @rewindable_io.path
        raise 'Unlink failed. IO closed.' if @rewindable_io.closed?
        @unlinked = true
      end

      buffer = ""
      while @io.read(1024 * 4, buffer)
        entire_buffer_written_out = false
        while !entire_buffer_written_out
          written = @rewindable_io.write(buffer)
          entire_buffer_written_out = written == Rack::Utils.bytesize(buffer)
          if !entire_buffer_written_out
            buffer.slice!(0 .. written - 1)
          end
        end
      end
      @rewindable_io.rewind
    end

    def filesystem_has_posix_semantics?
      RUBY_PLATFORM !~ /(mswin|mingw|cygwin|java)/
    end
  end
end

Directory Contents

Dirs: 6 × Files: 38

Name Size Perms Modified Actions
auth DIR
- drwxr-xr-x 2024-03-03 22:50:39
Edit Download
backports DIR
- drwxr-xr-x 2024-03-03 22:50:39
Edit Download
handler DIR
- drwxr-xr-x 2024-03-03 22:50:39
Edit Download
multipart DIR
- drwxr-xr-x 2024-03-03 22:50:39
Edit Download
session DIR
- drwxr-xr-x 2024-03-03 22:50:39
Edit Download
utils DIR
- drwxr-xr-x 2024-03-03 22:50:39
Edit Download
958 B lrw-r--r-- 2019-12-06 13:32:45
Edit Download
4.27 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
1.34 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
1.58 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
2.28 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
2.50 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
379 B lrw-r--r-- 2019-12-06 13:32:45
Edit Download
840 B lrw-r--r-- 2019-12-06 13:32:45
Edit Download
670 B lrw-r--r-- 2019-12-06 13:32:45
Edit Download
4.57 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
4.20 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
2.11 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
4.09 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
3.25 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
484 B lrw-r--r-- 2019-12-06 13:32:45
Edit Download
29.26 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
1.97 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
601 B lrw-r--r-- 2019-12-06 13:32:45
Edit Download
357 B lrw-r--r-- 2019-12-06 13:32:45
Edit Download
1.01 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
30.94 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
5.68 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
1.14 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
951 B lrw-r--r-- 2019-12-06 13:32:45
Edit Download
1.73 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
2.97 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
12.59 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
4.33 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
3.19 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
961 B lrw-r--r-- 2019-12-06 13:32:45
Edit Download
5.18 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
10.80 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
11.70 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
3.46 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
4.88 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
670 B lrw-r--r-- 2019-12-06 13:32:45
Edit Download
2.46 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download
20.86 KB lrw-r--r-- 2019-12-06 13:32:45
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).