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.



324
325
326
# File 'lib/protocol/http/body/stream.rb', line 324

def << buffer
	write(buffer)
end

#close(error = nil) ⇒ Object

Close the input and output bodies.



381
382
383
384
385
386
387
388
# File 'lib/protocol/http/body/stream.rb', line 381

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.



355
356
357
358
359
360
361
362
363
# File 'lib/protocol/http/body/stream.rb', line 355

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.



370
371
372
373
374
375
376
# File 'lib/protocol/http/body/stream.rb', line 370

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

#closed?Boolean

Returns:

  • (Boolean)


391
392
393
# File 'lib/protocol/http/body/stream.rb', line 391

def closed?
	@closed
end

#empty?Boolean

Returns:

  • (Boolean)


411
412
413
# File 'lib/protocol/http/body/stream.rb', line 411

def empty?
	@output.empty?
end

#flushObject

Flush the output stream.

This is currently a no-op.



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

def flush
end

#inspectObject

Inspect the stream.



398
399
400
401
402
403
404
405
406
407
408
# File 'lib/protocol/http/body/stream.rb', line 398

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.



334
335
336
337
338
339
340
341
342
# File 'lib/protocol/http/body/stream.rb', line 334

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.



303
304
305
306
307
308
309
310
# File 'lib/protocol/http/body/stream.rb', line 303

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.



319
320
321
# File 'lib/protocol/http/body/stream.rb', line 319

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