Parent

Class Index [+]

Quicksearch

PhusionPassenger::Utils::RewindableInput

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.

Public Class Methods

new(io) click to toggle source
    # File lib/phusion_passenger/utils/rewindable_input.rb, line 18
18:     def initialize(io)
19:       @io = io
20:       @rewindable_io = nil
21:       @unlinked = false
22:     end

Public Instance Methods

close() click to toggle source

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
each(&block) click to toggle source
    # 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
gets() click to toggle source
    # 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
read(*args) click to toggle source
    # 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
rewind() click to toggle source
    # File lib/phusion_passenger/utils/rewindable_input.rb, line 39
39:     def rewind
40:       make_rewindable unless @rewindable_io
41:       @rewindable_io.rewind
42:     end
size() click to toggle source
    # File lib/phusion_passenger/utils/rewindable_input.rb, line 44
44:     def size
45:       make_rewindable unless @rewindable_io
46:       @rewindable_io.size
47:     end

Private Instance Methods

filesystem_has_posix_semantics?() click to toggle source
     # 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
make_rewindable() click to toggle source
     # 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

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.