Class: Protocol::HTTP::Executor::Body::Output

Inherits:
Body::Readable
  • Object
show all
Defined in:
lib/protocol/http/executor/body/output.rb

Overview

A response body read from the worker output channel.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(execution, headers, metadata) ⇒ Output

Initialize a remote response body.



19
20
21
22
23
24
25
26
# File 'lib/protocol/http/executor/body/output.rb', line 19

def initialize(execution, headers, )
	@execution = execution
	@headers = headers
	@length = [:length]
	@stream = [:stream]
	@mode = nil
	@finished = false
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



29
30
31
# File 'lib/protocol/http/executor/body/output.rb', line 29

def length
  @length
end

#The remote response body length.(remoteresponsebodylength.) ⇒ Object (readonly)



29
# File 'lib/protocol/http/executor/body/output.rb', line 29

attr :length

Instance Method Details

#call(stream) ⇒ Object

Stream the remote body directly through the given duplex stream.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/protocol/http/executor/body/output.rb', line 50

def call(stream)
	unless stream?
		return super(stream)
	end
	
	start(:stream)
	input_task = @execution.stream_input(stream)
	error = nil
	
	begin
		while chunk = read_output
			stream.write(chunk)
			stream.flush
		end
	rescue => error
		@execution.cancel(error)
		raise
	ensure
		input_task&.cancel
		stream.close(error)
		@execution.finish unless error
	end
end

#close(error = nil) ⇒ Object

Close the response body and cancel unfinished execution.



75
76
77
78
79
80
# File 'lib/protocol/http/executor/body/output.rb', line 75

def close(error = nil)
	unless @finished
		@finished = true
		@execution.cancel(error)
	end
end

#empty?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/protocol/http/executor/body/output.rb', line 37

def empty?
	@finished
end

#readObject

Read the next response body chunk.



42
43
44
45
46
47
# File 'lib/protocol/http/executor/body/output.rb', line 42

def read
	return nil if @finished
	
	start(:read)
	return read_output
end

#stream?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/protocol/http/executor/body/output.rb', line 32

def stream?
	@stream
end