| Class | PhusionPassenger::Utils::RewindableInput |
| In: |
lib/phusion_passenger/utils/rewindable_input.rb
|
| Parent: | Object |
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.
# 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
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