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.
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.
# File lib/phusion_passenger/utils/rewindable_input.rb, line 54 54: def close 55: if @rewindable_io 56: if @unlinked 57: @rewindable_io.close 58: else 59: @rewindable_io.close! 60: end 61: @rewindable_io = nil 62: end 63: end
# File lib/phusion_passenger/utils/rewindable_input.rb, line 34 34: def each(&block) 35: make_rewindable unless @rewindable_io 36: @rewindable_io.each(&block) 37: end
# File lib/phusion_passenger/utils/rewindable_input.rb, line 24 24: def gets 25: make_rewindable unless @rewindable_io 26: @rewindable_io.gets 27: end
# File lib/phusion_passenger/utils/rewindable_input.rb, line 29 29: def read(*args) 30: make_rewindable unless @rewindable_io 31: @rewindable_io.read(*args) 32: end
# File lib/phusion_passenger/utils/rewindable_input.rb, line 109 109: def filesystem_has_posix_semantics? 110: RUBY_PLATFORM !~ /(mswin|mingw|cygwin|java)/ 111: end
# File lib/phusion_passenger/utils/rewindable_input.rb, line 80 80: def make_rewindable 81: # Buffer all data into a tempfile. Since this tempfile is private to this 82: # RewindableInput object, we chmod it so that nobody else can read or write 83: # it. On POSIX filesystems we also unlink the file so that it doesn't 84: # even have a file entry on the filesystem anymore, though we can still 85: # access it because we have the file handle open. 86: @rewindable_io = Tempfile.new('RackRewindableInput') 87: @rewindable_io.chmod(0000) 88: @rewindable_io.set_encoding(Encoding::BINARY) if @rewindable_io.respond_to?(:set_encoding) 89: @rewindable_io.binmode 90: if filesystem_has_posix_semantics? && !tempfile_unlink_contains_bug? 91: @rewindable_io.unlink 92: @unlinked = true 93: end 94: 95: buffer = "" 96: while @io.read(1024 * 4, buffer) 97: entire_buffer_written_out = false 98: while !entire_buffer_written_out 99: written = @rewindable_io.write(buffer) 100: entire_buffer_written_out = written == buffer.size 101: if !entire_buffer_written_out 102: buffer.slice!(0 .. written - 1) 103: end 104: end 105: end 106: @rewindable_io.rewind 107: end
# File lib/phusion_passenger/utils/rewindable_input.rb, line 113 113: def tempfile_unlink_contains_bug? 114: # The tempfile library as included in Ruby 1.9.1-p152 and later 115: # contains a bug: unlinking an open Tempfile object also closes 116: # it, which breaks our expected POSIX semantics. This problem 117: # has been fixed in Ruby 1.9.2, but the Ruby team chose not to 118: # include the bug fix in later versions of the 1.9.1 series. 119: ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : "ruby" 120: ruby_engine == "ruby" && RUBY_VERSION == "1.9.1" && RUBY_PATCHLEVEL >= 152 121: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.