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/rack/rewindable_input.rb, line 45
45: def close
46: if @rewindable_io
47: if @unlinked
48: @rewindable_io.close
49: else
50: @rewindable_io.close!
51: end
52: @rewindable_io = nil
53: end
54: end
(Not documented)
# File lib/rack/rewindable_input.rb, line 30
30: def each(&block)
31: make_rewindable unless @rewindable_io
32: @rewindable_io.each(&block)
33: end
(Not documented)
# File lib/rack/rewindable_input.rb, line 20
20: def gets
21: make_rewindable unless @rewindable_io
22: @rewindable_io.gets
23: end
(Not documented)
# File lib/rack/rewindable_input.rb, line 96
96: def filesystem_has_posix_semantics?
97: RUBY_PLATFORM !~ /(mswin|mingw|cygwin|java)/
98: end
(Not documented)
# File lib/rack/rewindable_input.rb, line 67
67: def make_rewindable
68: # Buffer all data into a tempfile. Since this tempfile is private to this
69: # RewindableInput object, we chmod it so that nobody else can read or write
70: # it. On POSIX filesystems we also unlink the file so that it doesn't
71: # even have a file entry on the filesystem anymore, though we can still
72: # access it because we have the file handle open.
73: @rewindable_io = Tempfile.new('RackRewindableInput')
74: @rewindable_io.chmod(0000)
75: @rewindable_io.set_encoding(Encoding::BINARY) if @rewindable_io.respond_to?(:set_encoding)
76: @rewindable_io.binmode
77: if filesystem_has_posix_semantics?
78: @rewindable_io.unlink
79: @unlinked = true
80: end
81:
82: buffer = ""
83: while @io.read(1024 * 4, buffer)
84: entire_buffer_written_out = false
85: while !entire_buffer_written_out
86: written = @rewindable_io.write(buffer)
87: entire_buffer_written_out = written == buffer.size
88: if !entire_buffer_written_out
89: buffer.slice!(0 .. written - 1)
90: end
91: end
92: end
93: @rewindable_io.rewind
94: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.