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)


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

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.



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

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



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

def output
  @output
end

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



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

attr :output

Instance Method Details

#<<(buffer) ⇒ Object

Write data to the stream using #write.



313
314
315
# File 'lib/protocol/http/body/stream.rb', line 313

def <<(buffer)
	write(buffer)
end

#close(error = nil) ⇒ Object

Close the input and output bodies.



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

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.



344
345
346
347
348
349
350
351
352
# File 'lib/protocol/http/body/stream.rb', line 344

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.



359
360
361
362
363
364
365
# File 'lib/protocol/http/body/stream.rb', line 359

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

#closed?Boolean

Returns:

  • (Boolean)


380
381
382
# File 'lib/protocol/http/body/stream.rb', line 380

def closed?
	@closed
end

#empty?Boolean

Returns:

  • (Boolean)


385
386
387
# File 'lib/protocol/http/body/stream.rb', line 385

def empty?
	@output.empty?
end

#flushObject

Flush the output stream.

This is currently a no-op.



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

def flush
end

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

Write lines to the stream.

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



323
324
325
326
327
328
329
330
331
# File 'lib/protocol/http/body/stream.rb', line 323

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



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

attr :input

#write(buffer) ⇒ Object

Write data to the underlying stream.



292
293
294
295
296
297
298
299
# File 'lib/protocol/http/body/stream.rb', line 292

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.



308
309
310
# File 'lib/protocol/http/body/stream.rb', line 308

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