Class: Rackup::Stream

Inherits:
Object
  • Object
show all
Includes:
Reader
Defined in:
lib/rackup/stream.rb

Overview

The input stream is an IO-like object which contains the raw HTTP POST data. When applicable, its external encoding must be “ASCII-8BIT” and it must be opened in binary mode, for Ruby 1.9 compatibility. The input stream must respond to gets, each, read and rewind.

Defined Under Namespace

Modules: Reader

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Reader

#each, #gets, #read, #read_nonblock, #read_partial

Constructor Details

#initialize(input = nil, output = Buffered.new) ⇒ Stream

Returns a new instance of Stream.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
# File 'lib/rackup/stream.rb', line 9

def initialize(input = nil, output = Buffered.new)
  @input = input
  @output = output

  raise ArgumentError, "Non-writable output!" unless output.respond_to?(:write)

  # Will hold remaining data in `#read`.
  @buffer = nil
  @closed = false
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



20
21
22
# File 'lib/rackup/stream.rb', line 20

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



21
22
23
# File 'lib/rackup/stream.rb', line 21

def output
  @output
end

Instance Method Details

#<<(buffer) ⇒ Object



147
148
149
# File 'lib/rackup/stream.rb', line 147

def <<(buffer)
  write(buffer)
end

#close(error = nil) ⇒ Object

Close the input and output bodies.



169
170
171
172
173
174
175
176
# File 'lib/rackup/stream.rb', line 169

def close(error = nil)
  self.close_read
  self.close_write

  return nil
ensure
  @closed = true
end

#close_readObject



154
155
156
157
# File 'lib/rackup/stream.rb', line 154

def close_read
  @input&.close
  @input = nil
end

#close_writeObject

close must never be called on the input stream. huh?



160
161
162
163
164
165
166
# File 'lib/rackup/stream.rb', line 160

def close_write
  if @output.respond_to?(:close)
    @output&.close
  end

  @output = nil
end

#closed?Boolean

Whether the stream has been closed.

Returns:

  • (Boolean)


179
180
181
# File 'lib/rackup/stream.rb', line 179

def closed?
  @closed
end

#empty?Boolean

Whether there are any output chunks remaining?

Returns:

  • (Boolean)


184
185
186
# File 'lib/rackup/stream.rb', line 184

def empty?
  @output.empty?
end

#flushObject



151
152
# File 'lib/rackup/stream.rb', line 151

def flush
end

#write(buffer) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/rackup/stream.rb', line 134

def write(buffer)
  if @output
    @output.write(buffer)
    return buffer.bytesize
  else
    raise IOError, "Stream is not writable, output has been closed!"
  end
end

#write_nonblock(buffer) ⇒ Object



143
144
145
# File 'lib/rackup/stream.rb', line 143

def write_nonblock(buffer)
  write(buffer)
end