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

Constant Summary collapse

NEWLINE =

The default line separator, used by Reader#gets.

"\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Reader

#each, #gets, #read, #read_nonblock, #read_partial, #read_until, #readpartial

Constructor Details

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

Initialize the stream with the given input and output.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/protocol/http/body/stream.rb', line 22

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
	@closed_read = false
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



36
37
38
# File 'lib/protocol/http/body/stream.rb', line 36

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



39
40
41
# File 'lib/protocol/http/body/stream.rb', line 39

def output
  @output
end

#The output stream.(outputstream.) ⇒ Object (readonly)



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

attr :output

Instance Method Details

#<<(buffer) ⇒ Object

Write data to the stream using #write.



321
322
323
# File 'lib/protocol/http/body/stream.rb', line 321

def << buffer
	write(buffer)
end

#close(error = nil) ⇒ Object

Close the input and output bodies.



378
379
380
381
382
383
384
385
# File 'lib/protocol/http/body/stream.rb', line 378

def close(error = nil)
	self.close_read(error)
	self.close_write(error)
	
	return nil
ensure
	@closed = true
end

#close_read(error = nil) ⇒ Object

Close the input body.

If, while processing the data that was read from this stream, an error is encountered, it should be passed to this method.



352
353
354
355
356
357
358
359
360
# File 'lib/protocol/http/body/stream.rb', line 352

def close_read(error = nil)
	if input = @input
		@input = nil
		@closed_read = true
		@buffer = nil
		
		input.close(error)
	end
end

#close_write(error = nil) ⇒ Object

Close the output body.

If, while generating the data that is written to this stream, an error is encountered, it should be passed to this method.



367
368
369
370
371
372
373
# File 'lib/protocol/http/body/stream.rb', line 367

def close_write(error = nil)
	if output = @output
		@output = nil
		
		output.close_write(error)
	end
end

#closed?Boolean

Returns:

  • (Boolean)


388
389
390
# File 'lib/protocol/http/body/stream.rb', line 388

def closed?
	@closed
end

#empty?Boolean

Returns:

  • (Boolean)


408
409
410
# File 'lib/protocol/http/body/stream.rb', line 408

def empty?
	@output.empty?
end

#flushObject

Flush the output stream.

This is currently a no-op.



344
345
# File 'lib/protocol/http/body/stream.rb', line 344

def flush
end

#inspectObject

Inspect the stream.



395
396
397
398
399
400
401
402
403
404
405
# File 'lib/protocol/http/body/stream.rb', line 395

def inspect
	buffer_info = @buffer ? "#{@buffer.bytesize} bytes buffered" : "no buffer"
	
	status = []
	status << "closed" if @closed
	status << "read-closed" if @closed_read
	
	status_info = status.empty? ? "open" : status.join(", ")
	
	return "#<#{self.class} #{buffer_info}, #{status_info}>"
end

#puts(*arguments, separator: NEWLINE) ⇒ Object

Write lines to the stream.

The current implementation buffers the lines and writes them in a single operation.



331
332
333
334
335
336
337
338
339
# File 'lib/protocol/http/body/stream.rb', line 331

def puts(*arguments, separator: NEWLINE)
	buffer = ::String.new
	
	arguments.each do |argument|
		buffer << argument << separator
	end
	
	write(buffer)
end

#The input stream.=(inputstream. = (value)) ⇒ Object



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

attr :input

#write(buffer) ⇒ Object

Write data to the underlying stream.



300
301
302
303
304
305
306
307
# File 'lib/protocol/http/body/stream.rb', line 300

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, exception: nil) ⇒ Object

Write data to the stream using #write.

Provided for compatibility with IO-like objects.



316
317
318
# File 'lib/protocol/http/body/stream.rb', line 316

def write_nonblock(buffer, exception: nil)
	write(buffer)
end