Class: Protocol::HTTP::Body::Stream

Inherits:
Object
  • Object
show all
Includes:
Reader
Defined in:
lib/protocol/http/body/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

#read, #read_nonblock, #read_partial

Constructor Details

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

Returns a new instance of Stream.

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
# File 'lib/protocol/http/body/stream.rb', line 30

def initialize(input, 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.



41
42
43
# File 'lib/protocol/http/body/stream.rb', line 41

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



42
43
44
# File 'lib/protocol/http/body/stream.rb', line 42

def output
  @output
end

Instance Method Details

#<<(buffer) ⇒ Object



158
159
160
# File 'lib/protocol/http/body/stream.rb', line 158

def <<(buffer)
	write(buffer)
end

#close(error = nil) ⇒ Object

Close the input and output bodies.



177
178
179
180
181
182
183
184
# File 'lib/protocol/http/body/stream.rb', line 177

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

	return nil
ensure
	@closed = true
end

#close_readObject



165
166
167
168
# File 'lib/protocol/http/body/stream.rb', line 165

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

#close_writeObject

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



171
172
173
174
# File 'lib/protocol/http/body/stream.rb', line 171

def close_write
	@output&.close
	@output = nil
end

#closed?Boolean

Whether the stream has been closed.

Returns:

  • (Boolean)


187
188
189
# File 'lib/protocol/http/body/stream.rb', line 187

def closed?
	@closed
end

#empty?Boolean

Whether there are any output chunks remaining?

Returns:

  • (Boolean)


192
193
194
# File 'lib/protocol/http/body/stream.rb', line 192

def empty?
	@output.empty?
end

#flushObject



162
163
# File 'lib/protocol/http/body/stream.rb', line 162

def flush
end

#write(buffer) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/protocol/http/body/stream.rb', line 145

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



154
155
156
# File 'lib/protocol/http/body/stream.rb', line 154

def write_nonblock(buffer)
	write(buffer)
end