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 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

(Not documented)

    # File lib/rack/rewindable_input.rb, line 14
14:     def initialize(io)
15:       @io = io
16:       @rewindable_io = nil
17:       @unlinked = false
18:     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 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
each(&block) click to toggle source

(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
gets() click to toggle source

(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
read(*args) click to toggle source

(Not documented)

    # File lib/rack/rewindable_input.rb, line 25
25:     def read(*args)
26:       make_rewindable unless @rewindable_io
27:       @rewindable_io.read(*args)
28:     end
rewind() click to toggle source

(Not documented)

    # File lib/rack/rewindable_input.rb, line 35
35:     def rewind
36:       make_rewindable unless @rewindable_io
37:       @rewindable_io.rewind
38:     end

Private Instance Methods

filesystem_has_posix_semantics?() click to toggle source

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

(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.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.