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.



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

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

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



56
57
58
# File 'lib/async/http/body/hijack.rb', line 56

def input
  @input
end

Class Method Details

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



31
32
33
# File 'lib/async/http/body/hijack.rb', line 31

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

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



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

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

Instance Method Details

#call(stream) ⇒ Object



52
53
54
# File 'lib/async/http/body/hijack.rb', line 52

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)


59
60
61
62
63
64
65
# File 'lib/async/http/body/hijack.rb', line 59

def empty?
	if @stream
		@stream.empty?
	else
		false
	end
end

#inspectObject



88
89
90
# File 'lib/async/http/body/hijack.rb', line 88

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

#readObject

Read the next available chunk.



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/async/http/body/hijack.rb', line 74

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

#ready?Boolean

Returns:

  • (Boolean)


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

def ready?
	if @stream
		@stream.output.ready?
	end
end

#stream?Boolean

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

Returns:

  • (Boolean)


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

def stream?
	true
end

#to_sObject



92
93
94
# File 'lib/async/http/body/hijack.rb', line 92

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