Class: Async::HTTP::Body::Hijack

Inherits:
Protocol::HTTP::Body::Readable
  • Object
show all
Defined in:
lib/async/http/body/hijack.rb

Overview

A body which is designed for hijacked server responses - a response which uses a block to read and write the request and response bodies respectively.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block, input = nil) ⇒ Hijack

Returns a new instance of Hijack.



24
25
26
27
28
29
30
31
# File 'lib/async/http/body/hijack.rb', line 24

def initialize(block, input = nil)
	@block = block
	@input = input
	
	@task = nil
	@stream = nil
	@output = nil
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



42
43
44
# File 'lib/async/http/body/hijack.rb', line 42

def input
  @input
end

Class Method Details

.response(request, status, headers, &block) ⇒ Object



16
17
18
# File 'lib/async/http/body/hijack.rb', line 16

def self.response(request, status, headers, &block)
	::Protocol::HTTP::Response[status, headers, self.wrap(request, &block)]
end

.wrap(request = nil, &block) ⇒ Object



20
21
22
# File 'lib/async/http/body/hijack.rb', line 20

def self.wrap(request = nil, &block)
	self.new(block, request&.body)
end

Instance Method Details

#call(stream) ⇒ Object



38
39
40
# File 'lib/async/http/body/hijack.rb', line 38

def call(stream)
	return @block.call(stream)
end

#empty?Boolean

Has the producer called #finish and has the reader consumed the nil token?

Returns:

  • (Boolean)


45
46
47
# File 'lib/async/http/body/hijack.rb', line 45

def empty?
	@output&.empty?
end

#inspectObject



69
70
71
# File 'lib/async/http/body/hijack.rb', line 69

def inspect
	"\#<#{self.class} #{@block.inspect}>"
end

#readObject

Read the next available chunk.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/async/http/body/hijack.rb', line 54

def read
	unless @output
		@output = Writable.new
		@stream = ::Protocol::HTTP::Body::Stream.new(@input, @output)
		
		@task = Task.current.async do |task|
			task.annotate "Streaming hijacked body."
			
			@block.call(@stream)
		end
	end
	
	return @output.read
end

#ready?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/async/http/body/hijack.rb', line 49

def ready?
	@output&.ready?
end

#stream?Boolean

We prefer streaming directly as it’s the lowest overhead.

Returns:

  • (Boolean)


34
35
36
# File 'lib/async/http/body/hijack.rb', line 34

def stream?
	true
end

#to_sObject



73
74
75
# File 'lib/async/http/body/hijack.rb', line 73

def to_s
	"<Hijack #{@block.class}>"
end