Parent

Class Index [+]

Quicksearch

Rack::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 # 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/rack/rewindable_input.rb, line 16
16:     def initialize(io)
17:       @io = io
18:       @rewindable_io = nil
19:       @unlinked = false
20:     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/rack/rewindable_input.rb, line 47
47:     def close
48:       if @rewindable_io
49:         if @unlinked
50:           @rewindable_io.close
51:         else
52:           @rewindable_io.close!
53:         end
54:         @rewindable_io = nil
55:       end
56:     end
each(&block) click to toggle source
    # File lib/rack/rewindable_input.rb, line 32
32:     def each(&block)
33:       make_rewindable unless @rewindable_io
34:       @rewindable_io.each(&block)
35:     end
gets() click to toggle source
    # File lib/rack/rewindable_input.rb, line 22
22:     def gets
23:       make_rewindable unless @rewindable_io
24:       @rewindable_io.gets
25:     end
read(*args) click to toggle source
    # File lib/rack/rewindable_input.rb, line 27
27:     def read(*args)
28:       make_rewindable unless @rewindable_io
29:       @rewindable_io.read(*args)
30:     end
rewind() click to toggle source
    # File lib/rack/rewindable_input.rb, line 37
37:     def rewind
38:       make_rewindable unless @rewindable_io
39:       @rewindable_io.rewind
40:     end

Private Instance Methods

filesystem_has_posix_semantics?() click to toggle source
     # File lib/rack/rewindable_input.rb, line 99
 99:     def filesystem_has_posix_semantics?
100:       RUBY_PLATFORM !~ /(mswin|mingw|cygwin|java)/
101:     end
make_rewindable() click to toggle source
    # File lib/rack/rewindable_input.rb, line 69
69:     def make_rewindable
70:       # Buffer all data into a tempfile. Since this tempfile is private to this
71:       # RewindableInput object, we chmod it so that nobody else can read or write
72:       # it. On POSIX filesystems we also unlink the file so that it doesn't
73:       # even have a file entry on the filesystem anymore, though we can still
74:       # access it because we have the file handle open.
75:       @rewindable_io = Tempfile.new('RackRewindableInput')
76:       @rewindable_io.chmod(0000)
77:       @rewindable_io.set_encoding(Encoding::BINARY) if @rewindable_io.respond_to?(:set_encoding)
78:       @rewindable_io.binmode
79:       if filesystem_has_posix_semantics?
80:         @rewindable_io.unlink
81:         raise 'Unlink failed. IO closed.' if @rewindable_io.closed?
82:         @unlinked = true
83:       end
84: 
85:       buffer = ""
86:       while @io.read(1024 * 4, buffer)
87:         entire_buffer_written_out = false
88:         while !entire_buffer_written_out
89:           written = @rewindable_io.write(buffer)
90:           entire_buffer_written_out = written == Rack::Utils.bytesize(buffer)
91:           if !entire_buffer_written_out
92:             buffer.slice!(0 .. written - 1)
93:           end
94:         end
95:       end
96:       @rewindable_io.rewind
97:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.